1
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
29 public class CollectionSelectionDialog extends JDialog {
30
31
34 DefaultListModel sourceListModel = null;
35
36 DefaultListModel targetListModel = null;
37
38 int buttonPressed = JFileChooser.CANCEL_OPTION;
39
42 JButton removeButton = null;
43
44 JButton addButton = null;
45
46 JList sourceList = null;
47
48 JList targetList = null;
49
50 JButton okButton = null;
51
52 JButton cancelButton = null;
53
54 JLabel sourceLabel = null;
55
56 JLabel targetLabel = null;
57
58 Frame mainFrame = null;
59
60
64 public CollectionSelectionDialog(Frame aFrame, boolean aModal){
65 super(aFrame,aModal);
66 this.setLocationRelativeTo(aFrame);
67 mainFrame = aFrame;
68 }
70
74 public CollectionSelectionDialog(){
75 this(null, true);
76 }
78
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 } }
93
94 protected void initGuiComponents(){
95 this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),
96 BoxLayout.Y_AXIS));
97 sourceLabel = new JLabel("Source");
99 sourceLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
100 sourceList = new JList(sourceListModel);
102 sourceList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
103 sourceList.setVisibleRowCount(10);
104 sourceList.setAlignmentX(Component.LEFT_ALIGNMENT);
105
106 targetLabel = new JLabel("Target");
108 targetLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
109 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 addButton = new JButton(">>>");
117 removeButton = new JButton("<<<");
119 okButton = new JButton("Ok");
121 cancelButton = new JButton("Cancel");
123
127 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 Box currentBox = Box.createVerticalBox();
135 currentBox.add(sourceLabel);
136 currentBox.add(new JScrollPane(sourceList));
137
138 firstLevelBox.add(currentBox);
140 firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
141
142 currentBox = Box.createVerticalBox();
144 currentBox.add(addButton);
145 currentBox.add(Box.createRigidArea(new Dimension(0,10)));
146 currentBox.add(removeButton);
147
148 firstLevelBox.add(currentBox);
150 firstLevelBox.add(Box.createRigidArea(new Dimension(10,0)));
151
152 currentBox = Box.createVerticalBox();
154 currentBox.add(targetLabel);
155 currentBox.add(new JScrollPane(targetList));
156
157 firstLevelBox.add(currentBox);
159 firstLevelBox.add(Box.createRigidArea(new Dimension(20,0)));
160
161 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 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 this.getContentPane().add(componentsBox);
176 this.pack();
177 }
179
180 protected void initListeners(){
181 okButton.addActionListener(new ActionListener() {
182 public void actionPerformed(ActionEvent e) {
183 doOk();
184 } }); cancelButton.addActionListener(new ActionListener() {
187 public void actionPerformed(ActionEvent e) {
188 doCancel();
189 } }); addButton.addActionListener(new ActionListener() {
192 public void actionPerformed(ActionEvent e) {
193 doAdd();
194 } }); removeButton.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
198 doRemove();
199 } }); }
203
204 private void doOk(){
205 buttonPressed = JFileChooser.APPROVE_OPTION;
206 this.setVisible(false);
207 }
209
210 private void doCancel(){
211 buttonPressed = JFileChooser.CANCEL_OPTION;
212 this.setVisible(false);
213 }
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 } }
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 } }
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 } return (Collection) resultsList;
237 }
239
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 } 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 } this.setTitle(aTitle);
254 initLocalData(aSourceData);
255 initGuiComponents();
256 initListeners();
257 super.setVisible(true);
258 return buttonPressed;
259 }}