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