gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / bookmarkshistory / DefaultHistoryController.java @ 1989
History | View | Annotate | Download (5.79 KB)
1 |
package org.gvsig.tools.swing.impl.bookmarkshistory; |
---|---|
2 |
|
3 |
import java.awt.Point; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.net.URL; |
7 |
import java.util.function.Predicate; |
8 |
import javax.swing.ImageIcon; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JMenu; |
11 |
import javax.swing.JMenuItem; |
12 |
import javax.swing.JPopupMenu; |
13 |
import org.apache.commons.lang3.StringUtils; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.bookmarksandhistory.History; |
16 |
import org.gvsig.tools.i18n.I18nManager; |
17 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
18 |
import org.gvsig.tools.swing.api.bookmarkshistory.HistoryController; |
19 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
20 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
21 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
22 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
23 |
import org.gvsig.tools.swing.icontheme.IconThemeManager; |
24 |
import org.gvsig.tools.swing.impl.DefaultActionListenerSupport; |
25 |
import org.gvsig.tools.swing.impl.pickercontroller.DatePickerControllerImpl; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author jjdelcerro
|
30 |
*/
|
31 |
public class DefaultHistoryController<T> extends DefaultActionListenerSupport implements HistoryController<T> { |
32 |
public class HistoryEventImpl<T> extends ActionEvent implements HistoryEvent<T> { |
33 |
|
34 |
private T currentValue;
|
35 |
|
36 |
public HistoryEventImpl(Object source, int id, String command) { |
37 |
super(source, id, command);
|
38 |
} |
39 |
|
40 |
@Override
|
41 |
public void setCurrentValue(T value) { |
42 |
this.currentValue = value;
|
43 |
} |
44 |
|
45 |
public T getCurrentValue() {
|
46 |
return this.currentValue; |
47 |
} |
48 |
|
49 |
} |
50 |
|
51 |
|
52 |
public static void selfRegister() { |
53 |
URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/bookmarkandhistory/history-clock.png"); |
54 |
if (imageResource != null) { |
55 |
IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
56 |
IconTheme theme = iconThemeManager.getCurrent(); |
57 |
ImageIcon icon = new ImageIcon(imageResource); |
58 |
theme.registerDefault("tools", "bookmarkandhistory", "history-clock", icon, imageResource); |
59 |
}} |
60 |
|
61 |
private final JButton button; |
62 |
private final History<T> history; |
63 |
private Predicate filter; |
64 |
private Boolean allowAddBookmarks; |
65 |
private T newValue;
|
66 |
|
67 |
public DefaultHistoryController(History<T> history, JButton button) { |
68 |
this.history = history;
|
69 |
this.button = button;
|
70 |
if (this.button != null) { |
71 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
72 |
this.button.setIcon(iconTheme.get("history-clock")); |
73 |
this.button.addActionListener(new ActionListener() { |
74 |
@Override
|
75 |
public void actionPerformed(ActionEvent e) { |
76 |
doHistory(); |
77 |
} |
78 |
}); |
79 |
} |
80 |
} |
81 |
|
82 |
private void doHistory() { |
83 |
JPopupMenu popup = createMenu().getPopupMenu();
|
84 |
Point p = this.button.getLocationOnScreen(); |
85 |
popup.show(this.button, 0, this.button.getHeight()); |
86 |
} |
87 |
|
88 |
public JMenu createMenu() { |
89 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
90 |
JMenu menu = new JMenu(i18n.getTranslation("_History")); |
91 |
if (!history.isEmpty()) {
|
92 |
for (T value : history.toList()) {
|
93 |
if (value != null) { |
94 |
menu.add(createHistoryMenuItem(value)); |
95 |
} |
96 |
} |
97 |
menu.addSeparator(); |
98 |
} |
99 |
JMenuItem showAllHistory = new JMenuItem(i18n.getTranslation("_Show_all_history")); |
100 |
showAllHistory.addActionListener(new ActionListener() { |
101 |
@Override
|
102 |
public void actionPerformed(ActionEvent e) { |
103 |
doShowHistoryDialog(); |
104 |
} |
105 |
}); |
106 |
menu.add(showAllHistory); |
107 |
return menu;
|
108 |
} |
109 |
|
110 |
private JMenuItem createHistoryMenuItem(final T value) { |
111 |
JMenuItem item = new JMenuItem(StringUtils.abbreviate(value.toString(), 45)); |
112 |
item.addActionListener(new ActionListener() { |
113 |
@Override
|
114 |
public void actionPerformed(ActionEvent e) { |
115 |
HistoryEventImpl event = new HistoryEventImpl(this, HistoryController.ID_SETVALUE, "set-value"); |
116 |
event.setCurrentValue(value); |
117 |
DefaultHistoryController.this.fireActionEvent(event); |
118 |
} |
119 |
}); |
120 |
return item;
|
121 |
} |
122 |
|
123 |
private void doShowHistoryDialog() { |
124 |
WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
125 |
final DefaultHistoryPanel<T> panel = new DefaultHistoryPanel<T>(history); |
126 |
final Dialog dialog = winManager.createDialog( |
127 |
panel, |
128 |
"History",
|
129 |
null,
|
130 |
WindowManager_v2.BUTTONS_OK_CANCEL |
131 |
); |
132 |
dialog.addActionListener(new ActionListener() { |
133 |
@Override
|
134 |
public void actionPerformed(ActionEvent e) { |
135 |
if (dialog.getAction() != WindowManager_v2.BUTTON_OK) {
|
136 |
return;
|
137 |
} |
138 |
T currentValue = panel.getSelectedValue(); |
139 |
if (currentValue != null) { |
140 |
HistoryEventImpl event = new HistoryEventImpl(this, HistoryController.ID_SETVALUE, "set-value"); |
141 |
event.setCurrentValue(currentValue); |
142 |
DefaultHistoryController.this.fireActionEvent(event); |
143 |
} |
144 |
} |
145 |
}); |
146 |
dialog.show(WindowManager.MODE.DIALOG); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void setFilter(Predicate filter) { |
151 |
this.filter = filter;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void setNewValueWithOutHistory(T newValue) { |
156 |
this.newValue = newValue;
|
157 |
} |
158 |
|
159 |
@Override
|
160 |
public History<T> get() {
|
161 |
return this.history; |
162 |
} |
163 |
} |