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 | 1986 | omartinez | 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 | 1990 | jjdelcerro | import static org.gvsig.tools.swing.api.bookmarkshistory.ActionEventWithCurrentValue.ID_SETVALUE; |
19 | 1986 | omartinez | 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 | 1990 | jjdelcerro | import org.gvsig.tools.util.LabeledValue; |
28 | 1986 | omartinez | |
29 | /**
|
||
30 | *
|
||
31 | * @author jjdelcerro
|
||
32 | */
|
||
33 | public class DefaultHistoryController<T> extends DefaultActionListenerSupport implements HistoryController<T> { |
||
34 | 1987 | omartinez | public class HistoryEventImpl<T> extends ActionEvent implements HistoryEvent<T> { |
35 | 1986 | omartinez | |
36 | 1987 | omartinez | 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 | 1990 | jjdelcerro | @Override
|
48 | 1987 | omartinez | public T getCurrentValue() {
|
49 | return this.currentValue; |
||
50 | } |
||
51 | |||
52 | } |
||
53 | |||
54 | |||
55 | 1986 | omartinez | public static void selfRegister() { |
56 | 1987 | omartinez | URL imageResource = DatePickerControllerImpl.class.getClassLoader().getResource("org/gvsig/tools/swing/bookmarkandhistory/history-clock.png"); |
57 | 1986 | omartinez | if (imageResource != null) { |
58 | IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager(); |
||
59 | IconTheme theme = iconThemeManager.getCurrent(); |
||
60 | ImageIcon icon = new ImageIcon(imageResource); |
||
61 | 1987 | omartinez | theme.registerDefault("tools", "bookmarkandhistory", "history-clock", icon, imageResource); |
62 | 1986 | omartinez | }} |
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 | 1987 | omartinez | this.button.setIcon(iconTheme.get("history-clock")); |
74 | 1986 | omartinez | 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 | 1990 | jjdelcerro | boolean needSeparator = false; |
93 | 1986 | omartinez | if (!history.isEmpty()) {
|
94 | for (T value : history.toList()) {
|
||
95 | if (value != null) { |
||
96 | 1990 | jjdelcerro | JMenuItem item = createHistoryMenuItem(value);
|
97 | if( filter!=null && !filter.test(value) ) { |
||
98 | item.setEnabled(false);
|
||
99 | } |
||
100 | menu.add(item); |
||
101 | needSeparator = true;
|
||
102 | 1986 | omartinez | } |
103 | } |
||
104 | 1990 | jjdelcerro | |
105 | } |
||
106 | if( needSeparator ) {
|
||
107 | 1986 | omartinez | menu.addSeparator(); |
108 | 1990 | jjdelcerro | needSeparator = false;
|
109 | 1986 | omartinez | } |
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 | 1990 | jjdelcerro | needSeparator = true;
|
119 | 1986 | omartinez | return menu;
|
120 | } |
||
121 | |||
122 | private JMenuItem createHistoryMenuItem(final T value) { |
||
123 | 1990 | jjdelcerro | 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 | 1986 | omartinez | item.addActionListener(new ActionListener() { |
132 | @Override
|
||
133 | public void actionPerformed(ActionEvent e) { |
||
134 | 1990 | jjdelcerro | HistoryEventImpl event = new HistoryEventImpl(this, ID_SETVALUE, "set-value"); |
135 | 1987 | omartinez | event.setCurrentValue(value); |
136 | 1990 | jjdelcerro | fireActionEvent(event); |
137 | 1986 | omartinez | } |
138 | }); |
||
139 | return item;
|
||
140 | } |
||
141 | |||
142 | private void doShowHistoryDialog() { |
||
143 | WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
||
144 | 1990 | jjdelcerro | final DefaultHistoryPanel<T> panel = new DefaultHistoryPanel<>(history); |
145 | 1986 | omartinez | 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 | 1990 | jjdelcerro | HistoryEventImpl event = new HistoryEventImpl(this, ID_SETVALUE, "set-value"); |
160 | 1987 | omartinez | event.setCurrentValue(currentValue); |
161 | 1986 | omartinez | 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 | } |