Source code: org/merlotxml/merlot/plugins/configeditor/ConfigEditorActions.java
1 package org.merlotxml.merlot.plugins.configeditor;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.table.*;
8
9 // Xerces or Java 1.4
10 import org.w3c.dom.*;
11
12 // www.merlotxml.org
13 import org.merlotxml.merlot.*;
14 import org.merlotxml.util.xml.*;
15
16 /**
17 *
18 * Actions used by the Config Editor Action plugin
19 *
20 * @author Ron Broberg
21 *
22 */
23 public class ConfigEditorActions
24 {
25 //implements MerlotConstants
26 //private ConfigEditorFrame _frame;
27 /**
28 * the singleton instance of this class
29 */
30 public static ConfigEditorActions _actions;
31
32 //private static ConfigEditorActions _instance = null;
33
34 protected AddRecordAction _addRecordAction;
35 protected DeleteRecordAction _deleteRecordAction;
36 protected EditRecordAction _editRecordAction;
37 protected RaiseRecordAction _raiseRecordAction;
38 protected LowerRecordAction _lowerRecordAction;
39 protected ApplyRecordAction _applyRecordAction;
40 protected ResetRecordAction _resetRecordAction;
41 protected QuitRecordAction _quitRecordAction;
42 protected NewTableAction _newTableAction;
43 protected NewTextAction _newTextAction;
44 protected RefreshFileAction _refreshFileAction;
45
46 private JTable _table = null;
47 private JDialog _dialog = null;
48 private MerlotDOMNode _node = null;
49 private MerlotDOMNode _child = null;
50 private DefaultTableModel _table_model = null;
51 private ConfigEditorCfgPanel _cfgTable = null;
52 private ConfigEditorRecordPanel _cfgRecord = null;
53 private int[] selected;
54 private int row;
55
56
57 public ConfigEditorActions()
58 {
59 _actions = this;
60 initActions();
61 }
62
63 public static ConfigEditorActions getSharedInstance()
64 {
65 return _actions;
66 }
67
68 public void setTable(JTable jt)
69 {
70 _table = jt;
71 }
72
73 public JDialog getDialog()
74 {
75 return _dialog;
76 }
77
78 public void setTableModel(DefaultTableModel tm)
79 {
80 _table_model = tm;
81 }
82
83 public void setCfgTable(ConfigEditorCfgPanel ct)
84 {
85 _cfgTable = ct;
86 }
87
88
89 public void setNode(MerlotDOMNode n)
90 {
91 _node = n;
92 }
93
94 public void setChildNode(MerlotDOMNode n)
95 {
96 _child = n;
97 }
98
99 private void initActions()
100 {
101 _addRecordAction = new AddRecordAction();
102 _deleteRecordAction = new DeleteRecordAction();
103 _editRecordAction = new EditRecordAction();
104 _raiseRecordAction = new RaiseRecordAction();
105 _lowerRecordAction = new LowerRecordAction();
106 _applyRecordAction = new ApplyRecordAction();
107 _resetRecordAction = new ResetRecordAction();
108 _quitRecordAction = new QuitRecordAction();
109 _newTableAction = new NewTableAction();
110 _newTextAction = new NewTextAction();
111 _refreshFileAction = new RefreshFileAction();
112 }
113
114 public void createRecordDialog(MerlotDOMNode child) {
115 // set the current child to the dialog child
116 _child = child;
117
118 // build the Record edit panel
119 _dialog = new JDialog(XMLEditorFrame.getSharedInstance());
120 _cfgRecord = new ConfigEditorRecordPanel(_child);
121
122 // add a button bar for: accept, reset, quit
123 JButton applyB = new JButton (_applyRecordAction);
124 JButton resetB = new JButton (_resetRecordAction);
125 JButton quitB = new JButton(_quitRecordAction);
126 JPanel b = new JPanel();
127 //b.setPreferredSize(new Dimension(350,50));
128
129 b.add(applyB);
130 b.add(resetB);
131 b.add(quitB);
132
133 _dialog.getContentPane().add("Center", _cfgRecord);
134 _dialog.getContentPane().add("South", b);
135 _dialog.setSize(new Dimension(320,150));
136 _dialog.setVisible(true);
137
138 }
139
140 protected class AddRecordAction extends AbstractAction
141 {
142 public AddRecordAction ()
143 {
144 Action a = this;
145 a.putValue(Action.NAME,"Add");
146 }
147
148 public void actionPerformed (ActionEvent evt)
149 {
150 System.out.println("AddRecord actionPerformed");
151
152 selected = _table.getSelectedRows();
153 row=selected[0];
154
155 // clone the first record/node in the table
156 _child = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row);
157 MerlotDOMNode newchild = (MerlotDOMNode)_child.clone();
158
159 // insert cloned node before the previous node
160 newchild.insertBefore(_child);
161
162 // zero the fields in the new record and
163 // parse the node data into a table row
164 Vector rowData = new Vector();
165 String blank = new String("");
166 Enumeration e;
167 DTDAttribute attr;
168 Attr a = null;
169 MerlotDOMNode[] newfields = newchild.getChildNodes();
170 int count = newfields.length;
171 for (int i=0; i<count; i++)
172 {
173 e = newfields[i].getDTDAttributes();
174 if (e != null) {
175 NamedNodeMap node_attributes = newfields[i].getAttributes();
176 System.out.println("nm length ="+node_attributes.getLength());
177 // attr = name
178 attr=(DTDAttribute)e.nextElement();
179 a = (Attr)node_attributes.getNamedItem(attr.getName());
180 System.out.println("name: "+attr.getName());
181 // attr = value
182 attr=(DTDAttribute)e.nextElement();
183 a = (Attr)node_attributes.getNamedItem(attr.getName());
184 System.out.println("name: "+attr.getName());
185 if (a != null ) {
186 a.setValue("");
187 System.out.println("set value: "+i);
188 }
189 }
190 rowData.add(blank);
191 }
192
193 // insert the record into the table
194 _table_model.insertRow(row, rowData);
195
196 // clear selections and select the new row
197 _table.clearSelection();
198 _table.setRowSelectionInterval(row,row);
199
200 // reinit the child vectors
201 _cfgTable.init();
202
203 // display this record/node in a record dialog
204 createRecordDialog(newchild);
205
206 }
207
208 public String toString()
209 {
210 return NAME;
211 }
212 }
213
214 protected class DeleteRecordAction extends AbstractAction
215 {
216 public DeleteRecordAction ()
217 {
218 Action a = this;
219 a.putValue(Action.NAME,"Delete");
220 }
221
222 public void actionPerformed (ActionEvent evt)
223 {
224 System.out.println("DeleteRecord actionPerformed");
225 //_cfgTable = new ConfigEditorCfgPanel(_node);
226
227 selected = _table.getSelectedRows();
228 int count = selected.length;
229 Node n;
230 MerlotDOMNode nd;
231 for (int i=count-1; i>-1; i--) {
232 // set the row index
233 row=selected[i];
234
235 // remove record node
236 nd = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row);
237 _node.removeChild(nd);
238
239 // remove row
240 _table_model.removeRow(row);
241
242 }
243 // refresh the child node arrays for the table
244 _cfgTable.init();
245 }
246 }
247
248 protected class EditRecordAction extends AbstractAction
249 {
250 public EditRecordAction ()
251 {
252 Action a = this;
253 a.putValue(Action.NAME,"Edit");
254 }
255
256 public void actionPerformed (ActionEvent evt)
257 {
258 System.out.println("EditRecord actionPerformed");
259 //_cfgTable = new ConfigEditorCfgPanel(_node);
260
261 selected = _table.getSelectedRows();
262 row = selected[0];
263 _child = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row);
264
265 createRecordDialog(_child);
266 }
267 }
268
269 protected class ApplyRecordAction extends AbstractAction
270 {
271 public ApplyRecordAction ()
272 {
273 Action a = this;
274 a.putValue(Action.NAME,"Apply");
275 }
276
277 public void actionPerformed (ActionEvent evt)
278 {
279 System.out.println("ApplyRecord actionPerformed");
280
281 try {
282 _cfgRecord.save();
283 } catch (Exception e) {
284 System.out.println("ConfigEditorActions::ApplyRecordAction::_cfgRecord.save()::"+e);
285 }
286
287 Enumeration e = null;
288 String value = null;
289 DTDAttribute attr = null;
290 Attr a = null;
291 selected = _table.getSelectedRows();
292 row = selected[0];
293 for (int i=0; i<_cfgTable.chNodes[row].size(); i++) {
294 MerlotDOMNode child = (MerlotDOMNode)_cfgTable.chNodes[row].elementAt(i);
295 e=child.getDTDAttributes();
296 value="";
297 if (e != null) {
298 attr=(DTDAttribute)e.nextElement();
299 attr=(DTDAttribute)e.nextElement();
300 NamedNodeMap node_attributes = child.getAttributes();
301 a = (Attr)node_attributes.getNamedItem(attr.getName());
302 if (a != null) {
303 value=a.getValue();
304 }
305 _table.setValueAt(value,row,i);
306 }
307 }
308 _dialog.setVisible(false);
309 }
310 }
311
312
313 protected class ResetRecordAction extends AbstractAction
314 {
315 public ResetRecordAction ()
316 {
317 Action a = this;
318 a.putValue(Action.NAME,"Reset");
319 }
320
321 public void actionPerformed (ActionEvent evt)
322 {
323 System.out.println("ResetRecord actionPerformed");
324 //_dialog.setVisible(false);
325 ConfigEditorRecordPanel newCfgRecord = new ConfigEditorRecordPanel(_child);
326 _dialog.getContentPane().remove(_cfgRecord);
327 _dialog.getContentPane().add("Center", newCfgRecord);
328 _dialog.hide();
329 _dialog.show();
330 //_dialog.update();
331 _cfgRecord = newCfgRecord;
332
333 }
334 }
335
336 protected class QuitRecordAction extends AbstractAction
337 {
338 public QuitRecordAction ()
339 {
340 Action a = this;
341 a.putValue(Action.NAME,"Quit");
342 }
343
344 public void actionPerformed (ActionEvent evt)
345 {
346 System.out.println("QuitRecord actionPerformed");
347 _dialog.setVisible(false);
348 }
349 }
350
351 protected class RaiseRecordAction extends AbstractAction
352 {
353 public RaiseRecordAction ()
354 {
355 Action a = this;
356 a.putValue(Action.NAME,"Raise");
357 }
358
359 public void actionPerformed (ActionEvent evt)
360 {
361 System.out.println("Raise Record actionPerformed");
362
363 // if this is not already the first row
364 selected = _table.getSelectedRows();
365 row = selected[0];
366 if (row > 0) {
367 //_cfgTable = new ConfigEditorCfgPanel(_node);
368
369 _child = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row);
370 MerlotDOMNode prevchild = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row-1);
371 MerlotDOMNode newchild = (MerlotDOMNode)_child.clone();
372 //System.out.println("index: "+_node.getChildIndex(_child));
373
374 // move corresponding row in table model
375 _table_model.moveRow(row,row,row-1);
376
377 // keep the same row selected
378 _table.clearSelection();
379 _table.setRowSelectionInterval(row-1,row-1);
380
381 // insert cloned node before the previous node
382 // (the node apparently begins at 1)
383 newchild.insertBefore(prevchild);
384
385 // remove node X
386 _node.removeChild(_child);
387
388 // refresh the child node arrays for the table
389 _cfgTable.init();
390 }
391 }
392 }
393
394 protected class LowerRecordAction extends AbstractAction
395 {
396 public LowerRecordAction ()
397 {
398 Action a = this;
399 a.putValue(Action.NAME,"Lower");
400 }
401
402 public void actionPerformed (ActionEvent evt)
403 {
404 System.out.println("LowerRecord actionPerformed");
405 selected = _table.getSelectedRows();
406 row = selected[0];
407 // if this is not already the last row
408 if (row < _table_model.getRowCount()-1.) {
409
410 //_cfgTable = new ConfigEditorCfgPanel(_node);
411
412 _child = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row);
413 MerlotDOMNode postchild = (MerlotDOMNode)_cfgTable.childNodes.elementAt(row+1);
414 MerlotDOMNode newchild = (MerlotDOMNode)_child.clone();
415 System.out.println("index: "+_node.getChildIndex(_child));
416
417 // insert cloned node before the previous node
418 // (the node apparently begins at 1)
419 newchild.insertAfter(postchild);
420
421 // remove node X
422 _node.removeChild(_child);
423
424 // move corresponding row in table model
425 _table_model.moveRow(row,row,row+1);
426 _table_model.fireTableDataChanged();
427
428 // keep the same row selected
429 _table.clearSelection();
430 _table.setRowSelectionInterval(row+1,row+1);
431
432 // refresh the child node arrays for the table
433 _cfgTable.init();
434 }
435 }
436 }
437
438 protected class NewTableAction extends AbstractAction
439 {
440 public NewTableAction ()
441 {
442 Action a = this;
443 a.putValue(Action.NAME,"New Table");
444 }
445
446 public void actionPerformed (ActionEvent evt)
447 {
448 System.out.println("NewTable actionPerformed");
449 }
450 }
451
452 protected class NewTextAction extends AbstractAction
453 {
454 public NewTextAction ()
455 {
456 Action a = this;
457 a.putValue(Action.NAME,"New Text");
458 }
459
460 public void actionPerformed (ActionEvent evt)
461 {
462 System.out.println("NewText actionPerformed");
463 }
464 }
465
466 protected class RefreshFileAction extends AbstractAction
467 {
468 public RefreshFileAction ()
469 {
470 Action a = this;
471 a.putValue(Action.NAME,"Refresh File");
472 }
473
474 public void actionPerformed (ActionEvent evt)
475 {
476 System.out.println("RefreshFile actionPerformed");
477 }
478 }
479
480 }