1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.view; |
11 | |
|
12 | |
import java.awt.Component; |
13 | |
import java.beans.PropertyChangeEvent; |
14 | |
import java.beans.PropertyChangeListener; |
15 | |
import java.io.File; |
16 | |
import java.util.List; |
17 | |
|
18 | |
import javax.swing.DefaultListCellRenderer; |
19 | |
import javax.swing.Icon; |
20 | |
import javax.swing.JComponent; |
21 | |
import javax.swing.JList; |
22 | |
import javax.swing.JOptionPane; |
23 | |
import javax.swing.JScrollPane; |
24 | |
|
25 | |
import org.jdesktop.application.Action; |
26 | |
import org.jdesktop.application.ApplicationContext; |
27 | |
import org.jdesktop.beansbinding.BeanProperty; |
28 | |
import org.jdesktop.beansbinding.Binding; |
29 | |
import org.jdesktop.beansbinding.BindingGroup; |
30 | |
import org.jdesktop.beansbinding.Bindings; |
31 | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; |
32 | |
import org.jdesktop.observablecollections.ObservableList; |
33 | |
import org.jdesktop.observablecollections.ObservableListListener; |
34 | |
import org.jdesktop.swingbinding.JListBinding; |
35 | |
import org.jdesktop.swingbinding.SwingBindings; |
36 | |
import org.slf4j.Logger; |
37 | |
import org.slf4j.LoggerFactory; |
38 | |
|
39 | |
import bibliothek.gui.dock.common.CLocation; |
40 | |
|
41 | |
import org.openpermis.editor.policy.adapter.AdapterTrader; |
42 | |
import org.openpermis.editor.policy.gui.DoubleClickForwarder; |
43 | |
import org.openpermis.editor.policy.presenter.PolicyContext; |
44 | |
import org.openpermis.editor.policy.presenter.RecentFilesPresenter; |
45 | |
import org.openpermis.policy.bean.PolicyBean; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class RecentFilesTool |
52 | |
extends AbstractToolView<RecentFilesPresenter> |
53 | |
implements PropertyChangeListener, ObservableListListener |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | private static final Logger LOGGER = |
63 | |
LoggerFactory.getLogger(RecentFilesTool.class); |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 0 | private static final CLocation LOCATION = AbstractToolView.DEFAULT_LOCATION.south(0.2); |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private final PolicyLoader loader; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private JList list; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public RecentFilesTool ( |
95 | |
ApplicationContext context, |
96 | |
AdapterTrader trader, |
97 | |
PolicyLoader loader, |
98 | |
RecentFilesPresenter presenter |
99 | |
) { |
100 | 0 | super(context, trader); |
101 | 0 | this.loader = loader; |
102 | 0 | setPresenter(presenter); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
@Action |
112 | |
public void openOtherFile () { |
113 | 0 | LOGGER.debug("openOtherFile"); |
114 | 0 | this.loader.loadPolicy(); |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@Action |
122 | |
public void deleteSelectedFile () { |
123 | 0 | LOGGER.debug("deleteSelectedFile"); |
124 | 0 | final String active = getPresenter().getActive(); |
125 | 0 | if (active != null) { |
126 | 0 | getPresenter().removeActive(); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
@Action |
135 | |
public void openSelectedFile () { |
136 | 0 | LOGGER.debug("openSelectedFile"); |
137 | 0 | final String active = getPresenter().getActive(); |
138 | 0 | if (active != null) { |
139 | 0 | final File file = new File(active); |
140 | 0 | if (!file.isFile()) { |
141 | 0 | JOptionPane.showMessageDialog( |
142 | |
getContentPane(), |
143 | |
"File [" + file.getAbsolutePath() + "] not found.\n" + |
144 | |
"Policy cannot be loaded.", |
145 | |
"File Not Found", |
146 | |
JOptionPane.ERROR_MESSAGE |
147 | |
); |
148 | 0 | getPresenter().removeActive(); |
149 | |
} else { |
150 | 0 | this.loader.loadPolicy(file); |
151 | 0 | getPresenter().activeToFront(); |
152 | |
} |
153 | |
} |
154 | 0 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
@Action |
161 | |
public void clearFiles () { |
162 | 0 | LOGGER.debug("clearFiles"); |
163 | 0 | getPresenter().clearRecentFiles(); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
protected void updateActions () { |
172 | 0 | super.updateActions(); |
173 | 0 | getActionMap().get("openSelectedFile").setEnabled(getPresenter().getActive() != null); |
174 | 0 | getActionMap().get("deleteSelectedFile").setEnabled(getPresenter().getActive() != null); |
175 | 0 | getActionMap().get("clearFiles").setEnabled(getPresenter().getRecentFiles().size() > 0); |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
@Override |
182 | |
protected void attachPresenter (RecentFilesPresenter presenter, BindingGroup bindings) { |
183 | 0 | super.attachPresenter(presenter, bindings); |
184 | 0 | presenter.addPropertyChangeListener(this); |
185 | 0 | presenter.getRecentFiles().addObservableListListener(this); |
186 | 0 | final JListBinding<String, List<String>, JList> listBinding = |
187 | |
SwingBindings.createJListBinding( |
188 | |
UpdateStrategy.READ, getPresenter().getRecentFiles(), this.list |
189 | |
); |
190 | 0 | bindings.addBinding(listBinding); |
191 | 0 | final Binding<?, ?, ?, ?> activeBinding = Bindings.createAutoBinding( |
192 | |
UpdateStrategy.READ_WRITE, |
193 | |
getPresenter(), |
194 | |
BeanProperty.create("active"), |
195 | |
this.list, |
196 | |
BeanProperty.create("selectedElement") |
197 | |
); |
198 | 0 | bindings.addBinding(activeBinding); |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
@Override |
205 | |
protected void detachPresenter (RecentFilesPresenter presenter) { |
206 | 0 | super.detachPresenter(presenter); |
207 | 0 | presenter.removePropertyChangeListener(this); |
208 | 0 | presenter.getRecentFiles().removeObservableListListener(this); |
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public void refresh (PolicyBean policy, PolicyContext context) { |
217 | |
|
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
@Override |
224 | |
public CLocation getDefaultLocation () { |
225 | 0 | return LOCATION; |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
@Override |
234 | |
public JComponent createContentPane () { |
235 | 0 | this.list = new JList(new Object[0]); |
236 | 0 | this.list.setCellRenderer(new Renderer()); |
237 | 0 | DoubleClickForwarder.register(this.list, getActionMap().get("openSelectedFile")); |
238 | 0 | return new JScrollPane(this.list); |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public void propertyChange (PropertyChangeEvent event) { |
247 | 0 | updateActions(); |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
@SuppressWarnings("unchecked") |
256 | |
public void listElementPropertyChanged (ObservableList fileList, int index) { |
257 | 0 | updateActions(); |
258 | 0 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
@SuppressWarnings("unchecked") |
264 | |
public void listElementReplaced (ObservableList fileList, int index, Object oldElement) { |
265 | 0 | updateActions(); |
266 | 0 | } |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
@SuppressWarnings("unchecked") |
272 | |
public void listElementsAdded (ObservableList fileList, int index, int length) { |
273 | 0 | updateActions(); |
274 | 0 | } |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
@SuppressWarnings("unchecked") |
280 | |
public void listElementsRemoved (ObservableList fileList, int index, List oldElements) { |
281 | 0 | updateActions(); |
282 | 0 | } |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
@SuppressWarnings("serial") |
291 | |
private final class Renderer |
292 | |
extends DefaultListCellRenderer |
293 | |
{ |
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
private final Icon icon; |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | 0 | public Renderer () { |
308 | 0 | this.icon = RecentFilesTool.this.getResourceMap().getIcon("fileIcon"); |
309 | 0 | } |
310 | |
|
311 | |
|
312 | |
|
313 | |
@Override |
314 | |
public Component getListCellRendererComponent ( |
315 | |
JList source, Object value, int index, boolean selected, boolean focussed |
316 | |
) { |
317 | 0 | final String path = String.valueOf(value); |
318 | 0 | final int fileIndex = path.lastIndexOf(File.separatorChar); |
319 | 0 | final String file = fileIndex == -1 ? path : path.substring(fileIndex + 1); |
320 | 0 | setToolTipText(path); |
321 | 0 | super.getListCellRendererComponent(source, file, index, selected, focussed); |
322 | 0 | setIcon(this.icon); |
323 | 0 | return this; |
324 | |
} |
325 | |
|
326 | |
} |
327 | |
|
328 | |
|
329 | |
} |