1   /*  CollectionSelectionDialog.java
2    *
3    *  Copyright (c) 1998-2005, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Cristian URSU,  05/Oct/2001
11   *
12   *  $Id: CollectionSelectionDialog.java,v 1.8 2005/01/11 13:51:34 ian Exp $
13   *
14   */
15  
16  package gate.gui;
17  
18  import java.awt.*;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  import java.util.*;
22  
23  import javax.swing.*;
24  
25  
26  /** This class visually selects some items from a collection and returns
27    * a collection with the items selected by the user.
28    */
29  public class CollectionSelectionDialog extends JDialog {
30  
31    // Local data
32    ////////////////////////////
33    /** This is the model for the list that the user will choose from*/
34    DefaultListModel sourceListModel = null;
35    /** This is the model for the list that the user chosed*/
36    DefaultListModel targetListModel = null;
37    /** A value indicating which button has been pressed (Ok or Cancel)*/
38    int buttonPressed = JFileChooser.CANCEL_OPTION;
39    // Gui Components
40    /////////////////////////
41    /** The button that removes items from the target list*/
42    JButton removeButton = null;
43    /** The button that adds items to the target list*/
44    JButton addButton = null;
45    /** The source list which contains the items that the user will select from*/
46    JList   sourceList = null;
47    /** The source list which contains the items that the user choosed*/
48    JList   targetList = null;
49    /** The Ok button*/
50    JButton okButton = null;
51    /** The Cancel button*/
52    JButton cancelButton = null;
53    /** A label for the source list*/
54    JLabel sourceLabel = null;
55    /** A label for the target list*/
56    JLabel targetLabel = null;
57    /** The parent frame for this dialog*/
58    Frame mainFrame = null;
59  
60    /** Constructs an ColectionSelectionDialog
61      * @param aFrame the parent frame of this dialog
62      * @param aModal (wheter or not this dialog is modal)
63      */
64    public CollectionSelectionDialog(Frame aFrame, boolean aModal){
65      super(aFrame,aModal);
66      this.setLocationRelativeTo(aFrame);
67      mainFrame = aFrame;
68    }//CollectionSelectionDialog
69  
70    /** Constructs an ColectionSelectionDialog using <b>null<b> as a frame
71      *   and <b>true
72      *  </b> as modal value for dialog
73      */
74    public CollectionSelectionDialog(){
75      this(null, true);
76    }// CollectionSelectionDialog
77  
78    /** Init local data from a sorce collection
79      * @param aSourceData is the collection from what the user will choose
80      */
81    protected void initLocalData(Collection aSourceData){
82      targetListModel = new DefaultListModel();
83      sourceListModel = new DefaultListModel();
84      if (aSourceData == null) return;
85      ArrayList source = new ArrayList(aSourceData);
86      Collections.sort(source);
87      Iterator iter = source.iterator();
88      while(iter.hasNext()){
89        sourceListModel.addElement(iter.next());
90      }// End while
91    }// initLocalData();
92  
93    /** This method creates the GUI components and paces them into the layout*/
94    protected void initGuiComponents(){
95      this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
96                                                    BoxLayout.Y_AXIS));
97      // Create source label
98      sourceLabel = new JLabel("Source");
99      sourceLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
100     // Create source list
101     sourceList = new JList(sourceListModel);
102     sourceList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
103     sourceList.setVisibleRowCount(10);
104     sourceList.setAlignmentX(Component.LEFT_ALIGNMENT);
105 
106     // Create target label
107     targetLabel = new JLabel("Target");
108     targetLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
109     // Create the target list
110     targetList = new JList(targetListModel);
111     targetList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
112     targetList.setVisibleRowCount(10);
113     targetList.setAlignmentX(Component.LEFT_ALIGNMENT);
114     targetList.setPreferredSize(sourceList.getPreferredSize());
115     // Create Add >>  button
116     addButton = new JButton(">>>");
117     // Create Remove <<  button
118     removeButton = new JButton("<<<");
119     // Create ok button
120     okButton = new JButton("Ok");
121     // Create cancel button
122     cancelButton = new JButton("Cancel");
123     ///////////////////////////////////////
124     // Arange components
125     //////////////////////////////////////
126 
127     // Create the main box
128     Box componentsBox = Box.createVerticalBox();
129     componentsBox.add(Box.createRigidArea(new Dimension(0,5)));
130 
131     Box firstLevelBox = Box.createHorizontalBox();
132     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
133     // Add the Source list
134     Box currentBox = Box.createVerticalBox();
135     currentBox.add(sourceLabel);
136     currentBox.add(new JScrollPane(sourceList));
137 
138     // Add the current box to the firstLevelBox
139     firstLevelBox.add(currentBox);
140     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
141 
142     // Add the add and remove buttons
143     currentBox = Box.createVerticalBox();
144     currentBox.add(addButton);
145     currentBox.add(Box.createRigidArea(new Dimension(0,10)));
146     currentBox.add(removeButton);
147 
148     // Add the remove buttons to the firstLevelBox
149     firstLevelBox.add(currentBox);
150     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
151 
152     // Add the target list
153     currentBox = Box.createVerticalBox();
154     currentBox.add(targetLabel);
155     currentBox.add(new JScrollPane(targetList));
156 
157     // Add target list to the firstLevelBox
158     firstLevelBox.add(currentBox);
159     firstLevelBox.add(Box.createRigidArea(new Dimension(20,0)));
160 
161     // Add ok and cancel buttons to the currentBox
162     currentBox = Box.createHorizontalBox();
163     currentBox.add(Box.createHorizontalGlue());
164     currentBox.add(okButton);
165     currentBox.add(Box.createRigidArea(new Dimension(25,0)));
166     currentBox.add(cancelButton);
167     currentBox.add(Box.createHorizontalGlue());
168 
169     // Add all components to the components box
170     componentsBox.add(firstLevelBox);
171     componentsBox.add(Box.createRigidArea(new Dimension(0,10)));
172     componentsBox.add(currentBox);
173     componentsBox.add(Box.createRigidArea(new Dimension(0,5)));
174     // Add the components box to the dialog
175     this.getContentPane().add(componentsBox);
176     this.pack();
177 }//initGuiComponents();
178 
179   /** Init all the listeners*/
180   protected void initListeners(){
181     okButton.addActionListener(new ActionListener() {
182       public void actionPerformed(ActionEvent e) {
183         doOk();
184       }// actionPerformed();
185     });// addActionListener();
186     cancelButton.addActionListener(new ActionListener() {
187       public void actionPerformed(ActionEvent e) {
188         doCancel();
189       }// actionPerformed();
190     });// addActionListener();
191     addButton.addActionListener(new ActionListener() {
192       public void actionPerformed(ActionEvent e) {
193         doAdd();
194       }// actionPerformed();
195     });// addActionListener();
196     removeButton.addActionListener(new ActionListener() {
197       public void actionPerformed(ActionEvent e) {
198         doRemove();
199       }// actionPerformed();
200     });// addActionListener();
201   }//initListeners()
202 
203   /** This method is called when the user press the OK button*/
204   private void doOk(){
205     buttonPressed = JFileChooser.APPROVE_OPTION;
206     this.setVisible(false);
207   }//doOk();
208 
209   /** This method is called when the user press the CANCEL button*/
210   private void doCancel(){
211     buttonPressed = JFileChooser.CANCEL_OPTION;
212     this.setVisible(false);
213   }//doCancel();
214   /** Called when user press remove button*/
215   private void doRemove(){
216     Object[] selectedItems = targetList.getSelectedValues();
217     for (int i = 0 ; i < selectedItems.length; i ++){
218       sourceListModel.addElement(selectedItems[i]);
219       targetListModel.removeElement(selectedItems[i]);
220     }// end for
221   }// doRemove();
222   /** Called when user press add button*/
223   private void doAdd(){
224     Object[] selectedItems = sourceList.getSelectedValues();
225     for (int i = 0 ; i < selectedItems.length; i ++){
226       targetListModel.addElement(selectedItems[i]);
227       sourceListModel.removeElement(selectedItems[i]);
228     }// end for
229   }// doAdd();
230   /** Returns the target collection*/
231   public Collection getSelectedCollection(){
232     ArrayList resultsList = new ArrayList();
233     for (int i=0; i<targetListModel.getSize(); i++){
234       resultsList.add(targetListModel.getElementAt(i));
235     }// End for
236     return (Collection) resultsList;
237   }// getSelectedCollection()
238 
239   /** This method displays the CollectionSelectionDialog*/
240   public int show(String aTitle,Collection aSourceData){
241     if (aTitle == null){
242       JOptionPane.showMessageDialog(mainFrame,
243       "Feature selection dialog coud not been created because title was null!",
244       "GATE", JOptionPane.ERROR_MESSAGE);
245       return buttonPressed;
246     }// End if
247     if (aSourceData == null){
248      JOptionPane.showMessageDialog(mainFrame,
249      "Feature selection dialog coud not been created because data source null!",
250      "GATE", JOptionPane.ERROR_MESSAGE);
251      return buttonPressed;
252     }// End if
253     this.setTitle(aTitle);
254     initLocalData(aSourceData);
255     initGuiComponents();
256     initListeners();
257     super.setVisible(true);
258     return buttonPressed;
259   }// show()
260 }//CollectionSelectionDialog class