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