View Javadoc

1   /*
2   
3       dsh-curate  Interfaces for curating collections quickly.
4       Copyright (c) 2007-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.curate.impl;
25  
26  import java.awt.BorderLayout;
27  import java.awt.event.MouseAdapter;
28  import java.awt.event.MouseEvent;
29  
30  import java.util.Collection;
31  import java.util.Iterator;
32  
33  import javax.swing.JCheckBoxMenuItem;
34  import javax.swing.JPanel;
35  import javax.swing.JPopupMenu;
36  
37  import javax.swing.border.EmptyBorder;
38  
39  import org.dishevelled.curate.CullView;
40  
41  import org.dishevelled.iconbundle.tango.TangoProject;
42  
43  import org.dishevelled.identify.IdToolBar;
44  
45  /**
46   * Cull dialog.
47   *
48   * @param <E> input and result collections element type
49   * @author  Michael Heuer
50   * @version $Revision$ $Date$
51   */
52  public final class CullDialog<E>
53      extends AbstractCurateDialog
54      implements CullView<E>
55  {
56      /** Cull panel. */
57      private final CullPanel<E> cullPanel;
58  
59      /** Toolbar. */
60      private final IdToolBar toolBar;
61  
62  
63      /**
64       * Create a new cull dialog.
65       */
66      public CullDialog()
67      {
68          cullPanel = new CullPanel<E>();
69          toolBar = new IdToolBar();
70          toolBar.add(cullPanel.getRemoveAction());
71          toolBar.add(cullPanel.getRemoveAllAction());
72          toolBar.addSeparator();
73          toolBar.add(cullPanel.getStartAssistAction());
74          toolBar.add(cullPanel.getStopAssistAction());
75          toolBar.displayIcons();
76          toolBar.setFloatable(false);
77          toolBar.setIconSize(TangoProject.SMALL);
78          // todo:  would be nice if actions displayed checkbox in menu
79          final JPopupMenu toolBarContextMenu = new JPopupMenu();
80          for (Iterator<?> displayMenuItems = toolBar.getDisplayMenuItems().iterator(); displayMenuItems.hasNext(); )
81          {
82              JCheckBoxMenuItem displayMenuItem = (JCheckBoxMenuItem) displayMenuItems.next();
83              toolBarContextMenu.add(displayMenuItem);
84          }
85          toolBarContextMenu.addSeparator();
86          for (Iterator<?> iconSizeMenuItems = toolBar.getDefaultIconSizeMenuItems().iterator(); iconSizeMenuItems.hasNext(); )
87          {
88              JCheckBoxMenuItem iconSizeMenuItem = (JCheckBoxMenuItem) iconSizeMenuItems.next();
89              toolBarContextMenu.add(iconSizeMenuItem);
90          }
91          toolBar.addMouseListener(new MouseAdapter()
92          {
93              /** {@inheritDoc} */
94              public void mousePressed(final MouseEvent event)
95              {
96                  showContextMenu(event);
97              }
98  
99              /** {@inheritDoc} */
100             public void mouseReleased(final MouseEvent event)
101             {
102                 showContextMenu(event);
103             }
104 
105             /**
106              * Show the context menu if the specified mouse event is a popup trigger.
107              *
108              * @param event event
109              */
110             private void showContextMenu(final MouseEvent event)
111             {
112                 if (event.isPopupTrigger())
113                 {
114                     toolBarContextMenu.show(event.getComponent(), event.getX(), event.getY());
115                 }
116             }
117         });
118 
119         JPanel contentPane = (JPanel) getContentPane();
120         contentPane.setLayout(new BorderLayout());
121         cullPanel.setBorder(new EmptyBorder(20, 10, 20, 10));
122         JPanel buttonPanel = createButtonPanel();
123         buttonPanel.setBorder(new EmptyBorder(0, 10, 10, 10));
124 
125         contentPane.add("North", toolBar);
126         contentPane.add("Center", cullPanel);
127         contentPane.add("South", buttonPanel);
128         contentPane.requestFocus();
129     }
130 
131 
132     /** {@inheritDoc} */
133     public void setInput(final Collection<E> input)
134     {
135         cullPanel.setInput(input);
136     }
137 
138     /** {@inheritDoc} */
139     public Collection<E> getRemaining()
140     {
141         return cullPanel.getRemaining();
142     }
143 
144     /** {@inheritDoc} */
145     public Collection<E> getRemoved()
146     {
147         return cullPanel.getRemoved();
148     }
149 
150     /** {@inheritDoc} */
151     protected void cancel()
152     {
153         // remaining == input, removed == empty
154         setVisible(false);
155     }
156 
157     /** {@inheritDoc} */
158     protected void help()
159     {
160         // empty
161     }
162 
163     /** {@inheritDoc} */
164     protected void ok()
165     {
166         // notify clients that remaining and removed are ready to be read
167         setVisible(false);
168     }
169 }