View Javadoc

1   /*
2   
3       dsh-piccolo-eventlist-view  Piccolo2D views for event lists.
4       Copyright (c) 2010-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.piccolo.eventlist.view;
25  
26  import ca.odell.glazedlists.EventList;
27  import ca.odell.glazedlists.ListSelection;
28  
29  import ca.odell.glazedlists.event.ListEvent;
30  import ca.odell.glazedlists.event.ListEventListener;
31  
32  import java.awt.event.ActionEvent;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import javax.swing.AbstractAction;
38  
39  import org.dishevelled.eventlist.view.EventListView;
40  import org.dishevelled.eventlist.view.EventListViewSupport;
41  
42  import org.dishevelled.identify.IdentifiableAction;
43  
44  import static org.dishevelled.iconbundle.tango.TangoProject.*;
45  
46  import org.piccolo2d.PNode;
47  
48  /**
49   * Abstract event list node.
50   *
51   * @param <E> model element type
52   * @author  Michael Heuer
53   * @version $Revision$ $Date$
54   */
55  public abstract class AbstractEventListNode<E>
56      extends PNode
57      implements EventListView<E>
58  {
59      /** Event list view support. */
60      private final EventListViewSupport<E> eventListViewSupport;
61  
62      /** Select all action. */
63      private final IdentifiableAction selectAllAction = new IdentifiableAction("Select all", EDIT_SELECT_ALL)
64          {
65                  @Override
66                  public void actionPerformed(final ActionEvent event)
67                  {
68                      selectAll();
69                  }
70          };
71  
72      /** Clear selection action. */
73      private final AbstractAction clearSelectionAction = new AbstractAction("Clear selection")
74          {
75                  @Override
76                  public void actionPerformed(final ActionEvent event)
77                  {
78                      clearSelection();
79                  }
80          };
81  
82      /** Invert selection action. */
83      private final AbstractAction invertSelectionAction = new AbstractAction("Invert selection")
84          {
85                  @Override
86                  public void actionPerformed(final ActionEvent event)
87                  {
88                      invertSelection();
89                  }
90          };
91  
92      /** Cut action. */
93      private final IdentifiableAction cutAction = new IdentifiableAction("Cut", EDIT_CUT)
94          {
95                  @Override
96                  public void actionPerformed(final ActionEvent event)
97                  {
98                      cut();
99                  }
100         };
101 
102     /** Copy action. */
103     private final IdentifiableAction copyAction = new IdentifiableAction("Copy", EDIT_COPY)
104         {
105                 @Override
106                 public void actionPerformed(final ActionEvent event)
107                 {
108                     copy();
109                 }
110         };
111 
112     /** Paste action. */
113     private final IdentifiableAction pasteAction = new IdentifiableAction("Paste", EDIT_PASTE)
114         {
115                 @Override
116                 public void actionPerformed(final ActionEvent event)
117                 {
118                     paste();
119                 }
120         };
121 
122     /** Add action. */
123     private final IdentifiableAction addAction = new IdentifiableAction("Add", LIST_ADD)
124         {
125                 @Override
126                 public void actionPerformed(final ActionEvent event)
127                 {
128                     add();
129                 }
130         };
131 
132     /** Remove action. */
133     private final IdentifiableAction removeAction = new IdentifiableAction("Remove", LIST_REMOVE)
134         {
135                 @Override
136                 public void actionPerformed(final ActionEvent event)
137                 {
138                     remove();
139                 }
140         };
141 
142     /** Remove all action. */
143     private final AbstractAction removeAllAction = new AbstractAction("Remove all")
144         {
145                 @Override
146                 public void actionPerformed(final ActionEvent event)
147                 {
148                     removeAll();
149                 }
150         };
151 
152     /** Listener. */
153     private ListEventListener<E> listener = new ListEventListener<E>()
154         {
155             @Override
156             public void listChanged(final ListEvent<E> event)
157             {
158                 updateListActions();
159             }
160         };
161 
162     /** Selection listener. */
163     private ListSelection.Listener selectionListener = new ListSelection.Listener()
164         {
165             @Override
166             public void selectionChanged(final int index0, final int index1)
167             {
168                 updateSelectionActions();
169             }
170         };
171 
172 
173     /**
174      * Create a new abstract event list node with the specified model.
175      *
176      * @param model model, must not be null
177      */
178     protected AbstractEventListNode(final EventList<E> model)
179     {
180         super();
181         eventListViewSupport = new EventListViewSupport<E>(model);
182         getModel().addListEventListener(listener);
183         getSelectionModel().addSelectionListener(selectionListener);
184         updateListActions();
185         updateSelectionActions();
186     }
187 
188 
189     /**
190      * Update list actions.
191      */
192     private void updateListActions()
193     {
194         selectAllAction.setEnabled(!isEmpty());
195         invertSelectionAction.setEnabled(!isEmpty());
196         removeAllAction.setEnabled(!isEmpty());
197     }
198 
199     /**
200      * Update selection actions.
201      */
202     private void updateSelectionActions()
203     {
204         copyAction.setEnabled(!isSelectionEmpty());
205         cutAction.setEnabled(!isSelectionEmpty());
206         removeAction.setEnabled(!isSelectionEmpty());
207         clearSelectionAction.setEnabled(!isSelectionEmpty());
208     }
209 
210     /**
211      * Return true if the model is empty.
212      *
213      * @return true if the model is empty.
214      */
215     public final boolean isEmpty()
216     {
217         return getModel().isEmpty();
218     }
219 
220     /**
221      * Return true if the selection is empty.
222      *
223      * @return true if the selection is empty.
224      */
225     public final boolean isSelectionEmpty()
226     {
227         return getSelectionModel().getSelected().isEmpty();
228     }
229 
230     /**
231      * Select all.
232      */
233     public final void selectAll()
234     {
235         getSelectionModel().selectAll();
236     }
237 
238     /**
239      * Clear selection.
240      */
241     public final void clearSelection()
242     {
243         getSelectionModel().deselectAll();
244     }
245 
246     /**
247      * Invert selection.
248      */
249     public final void invertSelection()
250     {
251         getSelectionModel().invertSelection();
252     }
253 
254     /**
255      * Cut.
256      */
257     public final void cut()
258     {
259         if (!isSelectionEmpty())
260         {
261             cut(new ArrayList<E>(getSelectionModel().getSelected()));
262         }
263     }
264 
265     /**
266      * Copy.
267      */
268     public final void copy()
269     {
270         if (!isSelectionEmpty())
271         {
272             copy(new ArrayList<E>(getSelectionModel().getSelected()));
273         }
274     }
275 
276     /**
277      * Cut the specific list of elements to the clipboard.
278      *
279      * @param toCut list of elements to cut, must not be null
280      */
281     protected abstract void cut(List<E> toCut);
282 
283     /**
284      * Copy the specific list of elements to the clipboard.
285      *
286      * @param toCopy list of elements to copy, must not be null
287      */
288     protected abstract void copy(List<E> toCopy);
289 
290     /**
291      * Add.
292      */
293     public abstract void add();
294 
295     /**
296      * Paste.
297      */
298     public abstract void paste();
299 
300     /**
301      * Remove.
302      */
303     public final void remove()
304     {
305         getSelectionModel().getSelected().clear();
306     }
307 
308     /**
309      * Remove all.
310      */
311     public final void removeAll()
312     {
313         getModel().clear();
314     }
315 
316     /**
317      * Return the select all action.
318      *
319      * @return the select all action
320      */
321     protected final IdentifiableAction getSelectAllAction()
322     {
323         return selectAllAction;
324     }
325 
326     /**
327      * Return the clear selection action.
328      *
329      * @return the clear selection action
330      */
331     protected final AbstractAction getClearSelectionAction()
332     {
333         return clearSelectionAction;
334     }
335 
336     /**
337      * Return the invert selection action.
338      *
339      * @return the invert selection action
340      */
341     protected final AbstractAction getInvertSelectionAction()
342     {
343         return invertSelectionAction;
344     }
345 
346     /**
347      * Return the cut action.
348      *
349      * @return the cut action
350      */
351     protected final IdentifiableAction getCutAction()
352     {
353         return cutAction;
354     }
355 
356     /**
357      * Return the copy action.
358      *
359      * @return the copy action
360      */
361     protected final IdentifiableAction getCopyAction()
362     {
363         return copyAction;
364     }
365 
366     /**
367      * Return the paste action.
368      *
369      * @return the paste action
370      */
371     protected final IdentifiableAction getPasteAction()
372     {
373         return pasteAction;
374     }
375 
376     /**
377      * Return the add action.
378      *
379      * @return the add action
380      */
381     protected final IdentifiableAction getAddAction()
382     {
383         return addAction;
384     }
385 
386     /**
387      * Return the remove action.
388      *
389      * @return the remove action
390      */
391     protected final IdentifiableAction getRemoveAction()
392     {
393         return removeAction;
394     }
395 
396     /**
397      * Return the remove all action.
398      *
399      * @return the remove all action
400      */
401     protected final AbstractAction getRemoveAllAction()
402     {
403         return removeAllAction;
404     }
405 
406     @Override
407     public final EventList<E> getModel()
408     {
409         return eventListViewSupport.getModel();
410     }
411 
412     @Override
413     public final ListSelection<E> getSelectionModel()
414     {
415         return eventListViewSupport.getSelectionModel();
416     }
417 
418     /**
419      * Release the resources consumed by this abstract event list view
420      * so that it may eventually be garbage collected.  Subclasses that override
421      * this method should call <code>super.dispose()</code>.
422      */
423     public void dispose()
424     {
425         getModel().removeListEventListener(listener);
426         getSelectionModel().removeSelectionListener(selectionListener);
427     }
428 }