View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.xslt;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Component;
27  import java.awt.Dimension;
28  import java.awt.FlowLayout;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.ActionListener;
31  import java.awt.event.FocusAdapter;
32  import java.awt.event.FocusEvent;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import javax.swing.DefaultCellEditor;
37  import javax.swing.JButton;
38  import javax.swing.JOptionPane;
39  import javax.swing.JPanel;
40  import javax.swing.JScrollPane;
41  import javax.swing.JTable;
42  import javax.swing.JTextField;
43  import javax.swing.ListSelectionModel;
44  import javax.swing.border.EmptyBorder;
45  import javax.swing.event.TableModelEvent;
46  import javax.swing.event.TableModelListener;
47  import javax.swing.table.DefaultTableCellRenderer;
48  import javax.swing.table.DefaultTableModel;
49  import javax.swing.undo.AbstractUndoableEdit;
50  
51  import org.bounce.ThreeStateCheckBox;
52  import org.xmlhammer.gui.Page;
53  import org.xmlhammer.gui.overview.OverviewNode;
54  import org.xmlhammer.gui.preferences.ActivatableTableModel;
55  import org.xmlhammer.gui.util.ParameterNode;
56  import org.xmlhammer.model.project.Parameter;
57  
58  /***
59   * Parameters Panel.
60   * 
61   * @version $Revision$, $Date$
62   * @author Edwin Dankert <edankert@gmail.com>
63   */
64  
65  public class ParametersPanel extends JPanel {
66  	private static final long serialVersionUID = 6877164067547749433L;
67  
68      private OverviewNode parent   = null;
69      private Page page   = null;
70  
71      private JTable table = null;
72      private JButton addButton = null;
73      private JButton deleteButton = null;
74      private ThreeStateCheckBox ativateAllButton = null;
75      protected DefaultTableModel model = null;
76  
77      private ArrayList<OverviewNode> nodes = new ArrayList<OverviewNode>();
78  
79      public ParametersPanel(Page page, OverviewNode parent) {
80  		super(new BorderLayout());
81          
82          this.parent = parent;
83          this.page = page;
84  
85          JPanel parameterListPanel = new JPanel(new BorderLayout());
86          
87          table = new JTable(getModel());
88          table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
89          table.setFont(table.getFont().deriveFont((float)11));
90          table.getModel().addTableModelListener(new TableModelListener() {
91              public void tableChanged(TableModelEvent event) {
92                  updateSelectAllButton();
93                  table.repaint();
94             }
95          });
96  
97          JTextField field = new JTextField();
98          field.addFocusListener( new FocusAdapter() {
99              public void focusLost(FocusEvent arg0) {
100                 if (table.isEditing()) {
101                     table.getCellEditor().stopCellEditing();
102                 }
103             }
104         });
105         field.setBorder(new EmptyBorder(0, 1, 0, 1));
106         field.setFont(field.getFont().deriveFont((float)11));
107         table.setDefaultEditor(String.class, new DefaultCellEditor(field));
108         table.setDefaultRenderer(String.class, new ActiveTableCellRenderer());
109         
110         int[] width = getColumnWidths();
111         for (int i = 0; i < width.length; i++) {
112             table.getColumnModel().getColumn(i).setMaxWidth(width[i]);
113         }
114         
115         JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
116         buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
117     
118         addButton = new JButton("Add");
119         addButton.addActionListener(new ActionListener() {
120             public void actionPerformed(ActionEvent e) {
121                 addButtonPressed();
122             }
123         });
124         buttonPanel.add( addButton);
125     
126         deleteButton = new JButton( "Delete");
127         deleteButton.addActionListener( new ActionListener() {
128             public void actionPerformed( ActionEvent e) {
129                 deleteButtonPressed();
130             }
131         });
132         buttonPanel.add( deleteButton);
133     
134         JPanel southPanel = new JPanel(new BorderLayout());
135         southPanel.add(buttonPanel, BorderLayout.EAST);
136 
137         JPanel selectPanel = new JPanel(new FlowLayout()); 
138         ativateAllButton = new ThreeStateCheckBox("Activate All");
139         ativateAllButton.addActionListener(new ActionListener() {
140             public void actionPerformed(ActionEvent e) {
141                 boolean selected = ativateAllButton.isSelected();
142                 
143                 for (int i = 0; i < getModel().getRowCount(); i++) {
144                     getModel().setValueAt(selected, i, 0);
145                 }
146             }
147         });
148         ativateAllButton.setMnemonic('A');
149         selectPanel.add(ativateAllButton);
150         updateSelectAllButton();
151         southPanel.add(selectPanel, BorderLayout.WEST);
152 
153         JScrollPane scroller = new JScrollPane( table);
154         scroller.setPreferredSize(new Dimension(100, 100));
155         scroller.getViewport().setBackground( Color.white);
156         scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
157         
158         scroller.setPreferredSize( new Dimension( 100, 100));
159         parameterListPanel.add( scroller, BorderLayout.CENTER);
160         parameterListPanel.add(southPanel, BorderLayout.SOUTH);
161     
162         add(parameterListPanel, BorderLayout.CENTER);
163     }
164     
165     private void updateSelectAllButton() {
166         boolean active = false;
167         boolean inactive = false;
168         
169         for (int i = 0; i < table.getModel().getRowCount(); i++) {
170             Object value = table.getModel().getValueAt(i, 0);
171             if (value instanceof Boolean) {
172                 if ((Boolean)value) {
173                     active = true;
174                 } else {
175                     inactive = true;
176                 }
177             }
178         }
179         
180         if (ativateAllButton != null) {
181             if (active && inactive) {
182                 ativateAllButton.setSelected(ThreeStateCheckBox.DONT_CARE);
183             } else if (active) {
184                 ativateAllButton.setSelected(ThreeStateCheckBox.SELECTED);
185             } else if (inactive) {
186                 ativateAllButton.setSelected(ThreeStateCheckBox.NOT_SELECTED);
187             }
188         }
189     }
190 
191     public void dispose() {
192         
193     }
194     
195     public void setEnabled( boolean enabled) {
196         table.setEnabled( enabled);
197         addButton.setEnabled( enabled);
198         deleteButton.setEnabled( enabled);
199     }
200     
201     protected JTable getTable() {
202         return table;
203     }
204 
205     protected void addButtonPressed() {
206         getModel().addRow(getDefaultRowContent());
207         getTable().scrollRectToVisible(getTable().getCellRect(getModel().getRowCount()-1, 1, false));
208 
209         ParameterNode parameter = new ParameterNode(this.parent, (String)model.getValueAt(getModel().getRowCount()-1, 1), (String)model.getValueAt(getModel().getRowCount()-1, 2));
210         nodes.add(parameter);
211         
212         if (page != null) {
213             page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
214             page.getProjectView().getOverviewPanel().expandNode(parent);
215             page.getProjectView().getUndoManager().addEdit(new UndoableAddParameterEdit());
216             page.getProjectView().getFieldManager().setFieldsChanged(true);
217         }
218     }
219     
220     public ArrayList<OverviewNode> getNodes() {
221         return nodes;
222     }
223 
224     protected void deleteButtonPressed() {
225         int[] indices = table.getSelectedRows();
226         
227         for(int i = indices.length-1; i >= 0; i--) {              
228             int result = -1;
229             
230             if( indices.length > 1) {
231                 result = JOptionPane.showOptionDialog(this, "Are you sure you want to delete:\n"+getModel().getValueAt( indices[i], 1).toString(), "Please Confirm", JOptionPane.YES_NO_CANCEL_OPTION,
232                     JOptionPane.QUESTION_MESSAGE, null, new String[] {"Yes", "No", "All"}, "Yes");
233             } else {
234                 result = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete:\n"+getModel().getValueAt( indices[i], 1).toString(), "Please Confirm", JOptionPane.YES_NO_OPTION);
235             }
236                                 
237 
238             if(result == 2 && indices.length > 1) { // Remove ALL
239                 for (int j = i; j >= 0; j--) { 
240                     UndoableRemoveParameterEdit undoableEdit = new UndoableRemoveParameterEdit(indices[j], (Boolean)getModel().getValueAt(indices[j], 0), (String)getModel().getValueAt(indices[j], 1), (String)getModel().getValueAt(indices[j], 2));
241                     getModel().removeRow(indices[j]);
242 
243                     if (page != null) {
244                         page.getProjectView().getOverviewPanel().selectNode(parent);
245                         page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(indices[j]));
246                         page.getProjectView().getUndoManager().addEdit(undoableEdit);
247                         page.getProjectView().getFieldManager().setFieldsChanged(true);
248                     }
249                     
250                     nodes.remove(indices[j]);
251                 }
252                 
253                 return;
254             } else if(result == JOptionPane.YES_OPTION) {
255                 UndoableRemoveParameterEdit undoableEdit = new UndoableRemoveParameterEdit(indices[i], (Boolean)getModel().getValueAt(indices[i], 0), (String)getModel().getValueAt(indices[i], 1), (String)getModel().getValueAt(indices[i], 2));
256                 getModel().removeRow(indices[i]);
257 
258                 if (page != null) {
259                     page.getProjectView().getOverviewPanel().selectNode(parent);
260                     page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(indices[i]));
261                     page.getProjectView().getUndoManager().addEdit(undoableEdit);
262                     page.getProjectView().getFieldManager().setFieldsChanged(true);
263                 }
264                 
265                 nodes.remove(indices[i]);
266             }
267         }
268     }
269     protected DefaultTableModel getModel() {
270         if ( model == null) {
271             model = new ParameterTableModel();
272             model.setColumnCount(3);
273             model.setColumnIdentifiers(new String[] {" ", "Name", "Value"});
274         }
275         
276         return model;
277     }
278     
279     
280     protected int[] getColumnWidths() {
281         return new int[] {25, 500, 500};
282     }
283 
284     public void setParameters(List<Parameter> parameters) {
285         while (getModel().getRowCount() > 0) {
286             getModel().removeRow(0);
287         }
288         
289         if (parameters != null) {
290             for (Parameter parameter : parameters) {
291                 getModel().addRow(new Object[] {parameter.isActive(), parameter.getName(), parameter.getValue()});
292     
293                 ParameterNode param = new ParameterNode(this.parent, parameter.getName(), parameter.getValue());
294                 nodes.add(param);
295                 
296                 if (page != null) {
297                     page.getProjectView().getOverviewPanel().getModel().nodeInserted(param);
298                     page.getProjectView().getOverviewPanel().expandNode(parent);
299                 }
300             }
301         }
302     }
303     
304     public List<Parameter> getParameters() {
305         List<Parameter> parameters = new ArrayList<Parameter>();
306         
307         for ( int i = 0; i < getModel().getRowCount(); i++) {
308             Parameter parameter = new Parameter();
309             parameter.setActive((Boolean)getModel().getValueAt( i, 0));
310             parameter.setName((String)getModel().getValueAt( i, 1));
311             parameter.setValue((String)getModel().getValueAt( i, 2));
312             
313             parameters.add(parameter);
314         }
315         
316         return parameters;
317     }
318     
319     public class ParameterTableModel extends ActivatableTableModel {
320         private static final long serialVersionUID = -4387014110990368593L;
321 
322         public void setUndoValueAt(Object object, int row, int col) {
323             super.setValueAt(object, row, col);
324 
325             if (col == 1) {
326                 ((ParameterNode)nodes.get(row)).setName((String)object);
327             } else if (col == 2){
328                 ((ParameterNode)nodes.get(row)).setValue((String)object);
329             }
330         }
331 
332         public void setValueAt(Object object, int row, int col) {
333             UndoableChangeParameterEdit undoableEdit = new UndoableChangeParameterEdit(this, getValueAt(row, col), object, row, col);
334 
335             super.setValueAt(object, row, col);
336             
337             if (col == 1) {
338                 ((ParameterNode)nodes.get(row)).setName((String)object);
339             } else if (col == 2){
340                 ((ParameterNode)nodes.get(row)).setValue((String)object);
341             }
342             
343             if (page != null) {
344                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
345                 page.getProjectView().getUndoManager().addEdit(undoableEdit);
346                 page.getProjectView().getFieldManager().setFieldsChanged(true);
347             }
348         }
349     }
350 
351     protected Object[] getDefaultRowContent() {
352         return new Object[] {Boolean.TRUE, "New Parameter Name", "New Parameter Value"};
353     }
354     
355     private class ActiveTableCellRenderer extends DefaultTableCellRenderer {
356         private static final long serialVersionUID = 6609794766790303814L;
357 
358         @Override
359         public Component getTableCellRendererComponent(JTable table, Object obj, boolean selected, boolean focus, int row, int column) {
360             Component comp = super.getTableCellRendererComponent(table, obj, selected, focus, row, column);
361             Object activated = table.getModel().getValueAt(row, 0);
362             
363             if (activated instanceof Boolean) {
364                 comp.setEnabled((Boolean)activated);
365             } else {
366                 comp.setEnabled(true);
367             }
368             
369             return comp;
370         }
371     }
372     
373     private class UndoableAddParameterEdit extends AbstractUndoableEdit {
374         private static final long serialVersionUID = 8615885869967750791L;
375 
376         public void undo() {
377             super.undo();
378             
379             if (table.isEditing()) {
380                 table.getCellEditor().cancelCellEditing();
381             }
382 
383             int index = getModel().getRowCount()-1;
384             
385             getModel().removeRow(index);
386 
387             if (page != null) {
388                 page.getProjectView().getOverviewPanel().selectNode(parent);
389                 page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(index));
390                 page.getProjectView().getFieldManager().setFieldsChanged(true);
391 
392                 nodes.remove(index);
393             }
394         }
395 
396         public void redo() {
397             super.redo();
398 
399             if (table.isEditing()) {
400                 table.getCellEditor().cancelCellEditing();
401             }
402 
403             getModel().addRow(getDefaultRowContent());
404             getTable().scrollRectToVisible(getTable().getCellRect(getModel().getRowCount()-1, 1, false));
405 
406             ParameterNode parameter = new ParameterNode(parent, (String)model.getValueAt(getModel().getRowCount()-1, 1), (String)model.getValueAt(getModel().getRowCount()-1, 2));
407             nodes.add(parameter);
408             
409             if (page != null) {
410                 page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
411                 page.getProjectView().getOverviewPanel().expandNode(parent);
412                 page.getProjectView().getFieldManager().setFieldsChanged(true);
413             }
414         }
415     }    
416 
417     public class UndoableRemoveParameterEdit extends AbstractUndoableEdit {
418         private static final long serialVersionUID = 8615885869967750791L;
419 
420         private int index = -1;
421         private boolean active = false;
422         private String name = null;
423         private String value = null;
424         
425         public UndoableRemoveParameterEdit(int index, boolean active, String name, String value) {
426             super();
427             
428             this.index = index;
429             this.active = active;
430             this.name = name;
431             this.value = value;
432         }
433 
434         public void undo() {
435             super.undo();
436             
437             if (table.isEditing()) {
438                 table.getCellEditor().cancelCellEditing();
439             }
440 
441             getModel().insertRow(index, new Object[] {active, name, value});
442             getTable().scrollRectToVisible(getTable().getCellRect(index, 1, false));
443 
444             ParameterNode parameter = new ParameterNode(parent, name, value);
445             nodes.add(index, parameter);
446             
447             if (page != null) {
448                 page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
449                 page.getProjectView().getOverviewPanel().expandNode(parent);
450                 page.getProjectView().getFieldManager().setFieldsChanged(true);
451             }
452         }
453 
454         public void redo() {
455             super.redo();
456 
457             if (table.isEditing()) {
458                 table.getCellEditor().cancelCellEditing();
459             }
460 
461             getModel().removeRow(index);
462 
463             if (page != null) {
464                 page.getProjectView().getOverviewPanel().selectNode(parent);
465                 page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(index));
466                 page.getProjectView().getFieldManager().setFieldsChanged(true);
467 
468                 nodes.remove(index);
469             }
470         }
471     }    
472 
473     public class UndoableChangeParameterEdit extends AbstractUndoableEdit {
474         private static final long serialVersionUID = 8615885869967750791L;
475 
476         private ParameterTableModel model = null;
477         private Object oldValue = null;
478         private Object newValue = null;
479         private int row = -1;
480         private int col = -1;
481         
482         public UndoableChangeParameterEdit(ParameterTableModel model, Object oldValue, Object newValue, int row, int col) {
483             super();
484             
485             this.model = model;
486             this.oldValue = oldValue;
487             this.newValue = newValue;
488             this.row = row;
489             this.col = col;
490         }
491 
492         public void undo() {
493             super.undo();
494             
495             if (table.isEditing()) {
496                 table.getCellEditor().cancelCellEditing();
497             }
498 
499             model.setUndoValueAt(oldValue, row, col);
500 
501             if (page != null) {
502                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
503                 page.getProjectView().getFieldManager().setFieldsChanged(true);
504             }
505         }
506 
507         public void redo() {
508             super.redo();
509 
510             if (table.isEditing()) {
511                 table.getCellEditor().cancelCellEditing();
512             }
513 
514             model.setUndoValueAt(newValue, row, col);
515 
516             if (page != null) {
517                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
518                 page.getProjectView().getFieldManager().setFieldsChanged(true);
519             }
520         }
521     }    
522 }