CollectionSelectionDialog.java
001 /*  CollectionSelectionDialog.java
002  *
003  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
004  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005  *
006  *  This file is part of GATE (see http://gate.ac.uk/), and is free
007  *  software, licenced under the GNU Library General Public License,
008  *  Version 2, June 1991 (in the distribution as file licence.html,
009  *  and also available at http://gate.ac.uk/gate/licence.html).
010  *
011  *  Cristian URSU,  05/Oct/2001
012  *
013  *  $Id: CollectionSelectionDialog.java 12006 2009-12-01 17:24:28Z thomas_heitz $
014  *
015  */
016 
017 package gate.gui;
018 
019 import java.awt.*;
020 import java.awt.event.ActionEvent;
021 import java.awt.event.ActionListener;
022 import java.util.*;
023 
024 import javax.swing.*;
025 
026 
027 /** This class visually selects some items from a collection and returns
028   * a collection with the items selected by the user.
029   */
030 public class CollectionSelectionDialog extends JDialog {
031 
032   // Local data
033   ////////////////////////////
034   /** This is the model for the list that the user will choose from*/
035   DefaultListModel sourceListModel = null;
036   /** This is the model for the list that the user chosed*/
037   DefaultListModel targetListModel = null;
038   /** A value indicating which button has been pressed (Ok or Cancel)*/
039   int buttonPressed = JFileChooser.CANCEL_OPTION;
040   // Gui Components
041   /////////////////////////
042   /** The button that removes items from the target list*/
043   JButton removeButton = null;
044   /** The button that adds items to the target list*/
045   JButton addButton = null;
046   /** The source list which contains the items that the user will select from*/
047   JList   sourceList = null;
048   /** The source list which contains the items that the user choosed*/
049   JList   targetList = null;
050   /** The Ok button*/
051   JButton okButton = null;
052   /** The Cancel button*/
053   JButton cancelButton = null;
054   /** A label for the source list*/
055   JLabel sourceLabel = null;
056   /** A label for the target list*/
057   JLabel targetLabel = null;
058   /** The parent frame for this dialog*/
059   Frame mainFrame = null;
060 
061   /** Constructs an ColectionSelectionDialog
062     @param aFrame the parent frame of this dialog
063     @param aModal (wheter or not this dialog is modal)
064     */
065   public CollectionSelectionDialog(Frame aFrame, boolean aModal){
066     super(aFrame,aModal);
067     this.setLocationRelativeTo(aFrame);
068     mainFrame = aFrame;
069   }//CollectionSelectionDialog
070 
071   /** Constructs an ColectionSelectionDialog using <b>null<b> as a frame
072     *   and <b>true
073     *  </b> as modal value for dialog
074     */
075   public CollectionSelectionDialog(){
076     this(null, true);
077   }// CollectionSelectionDialog
078 
079   /** Init local data from a sorce collection
080     @param aSourceData is the collection from what the user will choose
081     */
082   protected void initLocalData(Collection aSourceData){
083     targetListModel = new DefaultListModel();
084     sourceListModel = new DefaultListModel();
085     if (aSourceData == nullreturn;
086     ArrayList source = new ArrayList(aSourceData);
087     Collections.sort(source);
088     Iterator iter = source.iterator();
089     while(iter.hasNext()){
090       sourceListModel.addElement(iter.next());
091     }// End while
092   }// initLocalData();
093 
094   /** This method creates the GUI components and paces them into the layout*/
095   protected void initGuiComponents(){
096     this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
097                                                   BoxLayout.Y_AXIS));
098     // Create source label
099     sourceLabel = new JLabel("Source");
100     sourceLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
101     // Create source list
102     sourceList = new JList(sourceListModel);
103     sourceList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
104     sourceList.setVisibleRowCount(10);
105     sourceList.setAlignmentX(Component.LEFT_ALIGNMENT);
106 
107     // Create target label
108     targetLabel = new JLabel("Target");
109     targetLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
110     // Create the target list
111     targetList = new JList(targetListModel);
112     targetList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
113     targetList.setVisibleRowCount(10);
114     targetList.setAlignmentX(Component.LEFT_ALIGNMENT);
115     targetList.setPreferredSize(sourceList.getPreferredSize());
116     // Create Add >>  button
117     addButton = new JButton(">>>");
118     // Create Remove <<  button
119     removeButton = new JButton("<<<");
120     // Create ok button
121     okButton = new JButton("Ok");
122     // Create cancel button
123     cancelButton = new JButton("Cancel");
124     ///////////////////////////////////////
125     // Arange components
126     //////////////////////////////////////
127 
128     // Create the main box
129     Box componentsBox = Box.createVerticalBox();
130     componentsBox.add(Box.createRigidArea(new Dimension(0,5)));
131 
132     Box firstLevelBox = Box.createHorizontalBox();
133     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
134     // Add the Source list
135     Box currentBox = Box.createVerticalBox();
136     currentBox.add(sourceLabel);
137     currentBox.add(new JScrollPane(sourceList));
138 
139     // Add the current box to the firstLevelBox
140     firstLevelBox.add(currentBox);
141     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
142 
143     // Add the add and remove buttons
144     currentBox = Box.createVerticalBox();
145     currentBox.add(addButton);
146     currentBox.add(Box.createRigidArea(new Dimension(0,10)));
147     currentBox.add(removeButton);
148 
149     // Add the remove buttons to the firstLevelBox
150     firstLevelBox.add(currentBox);
151     firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
152 
153     // Add the target list
154     currentBox = Box.createVerticalBox();
155     currentBox.add(targetLabel);
156     currentBox.add(new JScrollPane(targetList));
157 
158     // Add target list to the firstLevelBox
159     firstLevelBox.add(currentBox);
160     firstLevelBox.add(Box.createRigidArea(new Dimension(20,0)));
161 
162     // Add ok and cancel buttons to the currentBox
163     currentBox = Box.createHorizontalBox();
164     currentBox.add(Box.createHorizontalGlue());
165     currentBox.add(okButton);
166     currentBox.add(Box.createRigidArea(new Dimension(25,0)));
167     currentBox.add(cancelButton);
168     currentBox.add(Box.createHorizontalGlue());
169 
170     // Add all components to the components box
171     componentsBox.add(firstLevelBox);
172     componentsBox.add(Box.createRigidArea(new Dimension(0,10)));
173     componentsBox.add(currentBox);
174     componentsBox.add(Box.createRigidArea(new Dimension(0,5)));
175     // Add the components box to the dialog
176     this.getContentPane().add(componentsBox);
177     this.pack();
178 }//initGuiComponents();
179 
180   /** Init all the listeners*/
181   protected void initListeners(){
182     okButton.addActionListener(new ActionListener() {
183       public void actionPerformed(ActionEvent e) {
184         doOk();
185       }// actionPerformed();
186     });// addActionListener();
187     cancelButton.addActionListener(new ActionListener() {
188       public void actionPerformed(ActionEvent e) {
189         doCancel();
190       }// actionPerformed();
191     });// addActionListener();
192     addButton.addActionListener(new ActionListener() {
193       public void actionPerformed(ActionEvent e) {
194         doAdd();
195       }// actionPerformed();
196     });// addActionListener();
197     removeButton.addActionListener(new ActionListener() {
198       public void actionPerformed(ActionEvent e) {
199         doRemove();
200       }// actionPerformed();
201     });// addActionListener();
202   }//initListeners()
203 
204   /** This method is called when the user press the OK button*/
205   private void doOk(){
206     buttonPressed = JFileChooser.APPROVE_OPTION;
207     this.setVisible(false);
208   }//doOk();
209 
210   /** This method is called when the user press the CANCEL button*/
211   private void doCancel(){
212     buttonPressed = JFileChooser.CANCEL_OPTION;
213     this.setVisible(false);
214   }//doCancel();
215   /** Called when user press remove button*/
216   private void doRemove(){
217     Object[] selectedItems = targetList.getSelectedValues();
218     for (int i = ; i < selectedItems.length; i ++){
219       sourceListModel.addElement(selectedItems[i]);
220       targetListModel.removeElement(selectedItems[i]);
221     }// end for
222   }// doRemove();
223   /** Called when user press add button*/
224   private void doAdd(){
225     Object[] selectedItems = sourceList.getSelectedValues();
226     for (int i = ; i < selectedItems.length; i ++){
227       targetListModel.addElement(selectedItems[i]);
228       sourceListModel.removeElement(selectedItems[i]);
229     }// end for
230   }// doAdd();
231   /** Returns the target collection*/
232   public Collection getSelectedCollection(){
233     ArrayList resultsList = new ArrayList();
234     for (int i=0; i<targetListModel.getSize(); i++){
235       resultsList.add(targetListModel.getElementAt(i));
236     }// End for
237     return (CollectionresultsList;
238   }// getSelectedCollection()
239 
240   /** This method displays the CollectionSelectionDialog*/
241   public int show(String aTitle,Collection aSourceData){
242     if (aTitle == null){
243       JOptionPane.showMessageDialog(mainFrame,
244       "Feature selection dialog coud not been created because title was null!",
245       "GATE", JOptionPane.ERROR_MESSAGE);
246       return buttonPressed;
247     }// End if
248     if (aSourceData == null){
249      JOptionPane.showMessageDialog(mainFrame,
250      "Feature selection dialog coud not been created because data source null!",
251      "GATE", JOptionPane.ERROR_MESSAGE);
252      return buttonPressed;
253     }// End if
254     this.setTitle(aTitle);
255     initLocalData(aSourceData);
256     initGuiComponents();
257     initListeners();
258     super.setVisible(true);
259     return buttonPressed;
260   }// show()
261 }//CollectionSelectionDialog class