1
14
15 package gate.gui;
16
17 import java.awt.Component;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.util.*;
21
22 import javax.swing.*;
23
24 import gate.Gate;
25 import gate.creole.ResourceData;
26 import gate.util.*;
27
28
31 public class ListEditorDialog extends JDialog {
32
33
42 public ListEditorDialog(Component owner, List data, String itemType) {
43 this.itemType = itemType == null ? "java.lang.String" : itemType;
44 setLocationRelativeTo(owner);
45 initLocalData(data);
46 initGuiComponents();
47 initListeners();
48 }
49
50 protected void initLocalData(List data){
51 listModel = new DefaultListModel();
52 if(data != null){
53 Iterator elemIter = data.iterator();
54 while(elemIter.hasNext()){
55 listModel.addElement(elemIter.next());
56 }
57 }
58
59 try{
60 ResourceData rData = (ResourceData)Gate.getCreoleRegister().get(itemType);
61 itemTypeClass = rData == null ?
62 Class.forName(itemType, true, Gate.getClassLoader()) :
63 rData.getResourceClass();
64 }catch(ClassNotFoundException cnfe){
65 throw new GateRuntimeException(cnfe.toString());
66 }
67
68 finiteType = Gate.isGateType(itemType);
69
70 ResourceData rData = (ResourceData)Gate.getCreoleRegister().get(itemType);
71 setTitle("List of " + ((rData== null) ? itemType :rData.getName()));
72
73 addAction = new AddAction();
74 removeAction = new RemoveAction();
75 }
76
77 protected void initGuiComponents(){
78 getContentPane().setLayout(new BoxLayout(getContentPane(),
79 BoxLayout.Y_AXIS));
80
81 JComponent editComp = null;
83 if(finiteType){
84 editComp = combo = new JComboBox(new ResourceComboModel());
85 combo.setRenderer(new ResourceRenderer());
86 if(combo.getModel().getSize() > 0){
87 combo.getModel().setSelectedItem(combo.getModel().getElementAt(0));
88 }
89 }else{
90 editComp = textField = new JTextField(20);
91 }
92
93 getContentPane().add(editComp);
94 getContentPane().add(Box.createVerticalStrut(5));
95
96 Box buttonsBox = Box.createHorizontalBox();
98 addBtn = new JButton(addAction);
99 removeBtn = new JButton(removeAction);
100 buttonsBox.add(Box.createHorizontalGlue());
101 buttonsBox.add(addBtn);
102 buttonsBox.add(Box.createHorizontalStrut(5));
103 buttonsBox.add(removeBtn);
104 buttonsBox.add(Box.createHorizontalGlue());
105 getContentPane().add(buttonsBox);
106 getContentPane().add(Box.createVerticalStrut(5));
107
108 Box horBox = Box.createHorizontalBox();
110 listComponent = new JList(listModel);
111 listComponent.setSelectionMode(ListSelectionModel.
112 MULTIPLE_INTERVAL_SELECTION);
113 listComponent.setCellRenderer(new ResourceRenderer());
114 horBox.add(new JScrollPane(listComponent));
115 Box verBox = Box.createVerticalBox();
117 verBox.add(Box.createVerticalGlue());
118 moveUpBtn = new JButton(MainFrame.getIcon("moveup.gif"));
119 verBox.add(moveUpBtn);
120 verBox.add(Box.createVerticalStrut(5));
121 moveDownBtn = new JButton(MainFrame.getIcon("movedown.gif"));
122 verBox.add(moveDownBtn);
123 verBox.add(Box.createVerticalGlue());
124 horBox.add(Box.createHorizontalStrut(3));
125 horBox.add(verBox);
126 horBox.add(Box.createHorizontalStrut(3));
127 getContentPane().add(horBox);
128 getContentPane().add(Box.createVerticalStrut(5));
129
130 buttonsBox = Box.createHorizontalBox();
132 buttonsBox.add(Box.createHorizontalGlue());
133 okButton = new JButton("OK");
134 buttonsBox.add(okButton);
135 buttonsBox.add(Box.createHorizontalStrut(5));
136 cancelButton = new JButton("Cancel");
137 buttonsBox.add(cancelButton);
138 buttonsBox.add(Box.createHorizontalGlue());
139 getContentPane().add(buttonsBox);
140 }
141
142 protected void initListeners(){
143 okButton.addActionListener(new ActionListener() {
144 public void actionPerformed(ActionEvent e) {
145 userCancelled = false;
146 setVisible(false);
147 }
148 });
149
150 cancelButton.addActionListener(new ActionListener() {
151 public void actionPerformed(ActionEvent e) {
152 userCancelled = true;
153 setVisible(false);
154 }
155 });
156
157
158 moveUpBtn.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
160 int rows[] = listComponent.getSelectedIndices();
161 if(rows == null || rows.length == 0){
162 JOptionPane.showMessageDialog(
163 ListEditorDialog.this,
164 "Please select some items to be moved ",
165 "GATE", JOptionPane.ERROR_MESSAGE);
166 }else{
167 Arrays.sort(rows);
169 for(int i = 0; i < rows.length; i++){
171 int row = rows[i];
172 if(row > 0){
173 Object value = listModel.remove(row);
175 listModel.add(row - 1, value);
176 }
177 }
178 for(int i = 0; i < rows.length; i++){
180 int newRow = -1;
181 if(rows[i] > 0) newRow = rows[i] - 1;
182 else newRow = rows[i];
183 listComponent.addSelectionInterval(newRow, newRow);
184 }
185 }
186
187 } });
189
190
191 moveDownBtn.addActionListener(new ActionListener() {
192 public void actionPerformed(ActionEvent e) {
193 int rows[] = listComponent.getSelectedIndices();
194 if(rows == null || rows.length == 0){
195 JOptionPane.showMessageDialog(
196 ListEditorDialog.this,
197 "Please select some items to be moved ",
198 "GATE", JOptionPane.ERROR_MESSAGE);
199 } else {
200 Arrays.sort(rows);
202 for(int i = rows.length - 1; i >= 0; i--){
204 int row = rows[i];
205 if(row < listModel.size() -1){
206 Object value = listModel.remove(row);
208 listModel.add(row + 1, value);
209 }
210 }
211 for(int i = 0; i < rows.length; i++){
213 int newRow = -1;
214 if(rows[i] < listModel.size() - 1) newRow = rows[i] + 1;
215 else newRow = rows[i];
216 listComponent.addSelectionInterval(newRow, newRow);
217 }
218 }
219
220 } });
222
223 }
224
225
231 public List showDialog(){
232 pack();
233 userCancelled = true;
234 setModal(true);
235 super.setVisible(true);
236 return userCancelled ? null : Arrays.asList(listModel.toArray());
237 }
238
239
242 public static void main(String[] args){
243 try{
244 Gate.init();
245 }catch(Exception e){
246 e.printStackTrace();
247 }
248 JFrame frame = new JFrame("Foo frame");
249
250 ListEditorDialog dialog = new ListEditorDialog(frame,
251 new ArrayList(),
252 "java.lang.Integer");
253
254 frame.setSize(300, 300);
255 frame.setVisible(true);
256 System.out.println(dialog.showDialog());
257 }
258
259
263 protected class AddAction extends AbstractAction{
264 AddAction(){
265 super("Add");
266 putValue(SHORT_DESCRIPTION, "Add the edited value to the list");
267 }
268 public void actionPerformed(ActionEvent e){
269 if(finiteType){
270 listModel.addElement(combo.getSelectedItem());
271 }else{
272 Object value = null;
273 String stringValue = textField.getText();
275 if(stringValue == null || stringValue.length() == 0) stringValue = null;
276
277 if(itemTypeClass.isAssignableFrom(String.class)){
278 value = stringValue;
280 }else{
281 try{
283 value = itemTypeClass.getConstructor(new Class[]{String.class}).
284 newInstance( new Object[]{stringValue} );
285 }catch(Exception ex){
286 JOptionPane.showMessageDialog(
287 ListEditorDialog.this,
288 "Invalid value!\nIs it the right type?",
289 "GATE", JOptionPane.ERROR_MESSAGE);
290 return;
291 }
292 }
293 listModel.addElement(value);
294 textField.setText("");
295 }
296 }
297 }
298
299
302 protected class RemoveAction extends AbstractAction{
303 RemoveAction(){
304 super("Remove");
305 putValue(SHORT_DESCRIPTION, "Remove the selected value(s) from the list");
306 }
307
308 public void actionPerformed(ActionEvent e){
309 int[] indices = listComponent.getSelectedIndices();
310 Arrays.sort(indices);
311 for(int i = indices.length -1; i >= 0; i--){
312 listModel.remove(indices[i]);
313 }
314 }
315 }
316
317
318
321 protected class ResourceComboModel extends AbstractListModel
322 implements ComboBoxModel{
323
324 public int getSize(){
325 java.util.List loadedResources = null;
327 try{
328 loadedResources = Gate.getCreoleRegister().
329 getAllInstances(itemType);
330 }catch(GateException ge){
331 ge.printStackTrace(Err.getPrintWriter());
332 }
333
334 return loadedResources == null ? 0 : loadedResources.size();
335 }
336
337 public Object getElementAt(int index){
338 java.util.List loadedResources = null;
340 try{
341 loadedResources = Gate.getCreoleRegister().
342 getAllInstances(itemType);
343 }catch(GateException ge){
344 ge.printStackTrace(Err.getPrintWriter());
345 }
346 return loadedResources == null? null : loadedResources.get(index);
347 }
348
349 public void setSelectedItem(Object anItem){
350 if(anItem == null) selectedItem = null;
351 else selectedItem = anItem;
352 }
353
354 public Object getSelectedItem(){
355 return selectedItem;
356 }
357
358 void fireDataChanged(){
359 fireContentsChanged(this, 0, getSize());
360 }
361
362 Object selectedItem = null;
363 }
364
365
368 String itemType;
369
370
373 Class itemTypeClass;
374
375
378 JList listComponent;
379
380
383 JComboBox combo;
384
385
388 JTextField textField;
389
390
393 JButton removeBtn;
394
395
398 JButton addBtn;
399
400
403 JButton moveUpBtn;
404
405
408 JButton moveDownBtn;
409
410
413 DefaultListModel listModel;
414
415
418 boolean finiteType;
419
420
423 Action addAction;
424
425
428 Action removeAction;
429
430
433 JButton okButton;
434
435
438 JButton cancelButton;
439
440
443 boolean userCancelled;
444 }
445