Coverage Report - org.xmlhammer.gui.xslt.ParametersPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
ParametersPanel
37% 
31% 
0
 
 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  132
 public class ParametersPanel extends JPanel {
 66  
         private static final long serialVersionUID = 6877164067547749433L;
 67  
 
 68  286
     private OverviewNode parent   = null;
 69  286
     private Page page   = null;
 70  
 
 71  286
     private JTable table = null;
 72  286
     private JButton addButton = null;
 73  286
     private JButton deleteButton = null;
 74  286
     private ThreeStateCheckBox ativateAllButton = null;
 75  286
     protected DefaultTableModel model = null;
 76  
 
 77  286
     private ArrayList<OverviewNode> nodes = new ArrayList<OverviewNode>();
 78  
 
 79  
     public ParametersPanel(Page page, OverviewNode parent) {
 80  286
                 super(new BorderLayout());
 81  
         
 82  286
         this.parent = parent;
 83  286
         this.page = page;
 84  
 
 85  286
         JPanel parameterListPanel = new JPanel(new BorderLayout());
 86  
         
 87  286
         table = new JTable(getModel());
 88  286
         table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
 89  286
         table.setFont(table.getFont().deriveFont((float)11));
 90  286
         table.getModel().addTableModelListener(new TableModelListener() {
 91  286
             public void tableChanged(TableModelEvent event) {
 92  66
                 updateSelectAllButton();
 93  66
                 table.repaint();
 94  66
            }
 95  
         });
 96  
 
 97  286
         JTextField field = new JTextField();
 98  286
         field.addFocusListener( new FocusAdapter() {
 99  286
             public void focusLost(FocusEvent arg0) {
 100  0
                 if (table.isEditing()) {
 101  0
                     table.getCellEditor().stopCellEditing();
 102  
                 }
 103  0
             }
 104  
         });
 105  286
         field.setBorder(new EmptyBorder(0, 1, 0, 1));
 106  286
         field.setFont(field.getFont().deriveFont((float)11));
 107  286
         table.setDefaultEditor(String.class, new DefaultCellEditor(field));
 108  286
         table.setDefaultRenderer(String.class, new ActiveTableCellRenderer());
 109  
         
 110  286
         int[] width = getColumnWidths();
 111  1144
         for (int i = 0; i < width.length; i++) {
 112  858
             table.getColumnModel().getColumn(i).setMaxWidth(width[i]);
 113  
         }
 114  
         
 115  286
         JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 116  286
         buttonPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
 117  
     
 118  286
         addButton = new JButton("Add");
 119  286
         addButton.addActionListener(new ActionListener() {
 120  286
             public void actionPerformed(ActionEvent e) {
 121  0
                 addButtonPressed();
 122  0
             }
 123  
         });
 124  286
         buttonPanel.add( addButton);
 125  
     
 126  286
         deleteButton = new JButton( "Delete");
 127  286
         deleteButton.addActionListener( new ActionListener() {
 128  286
             public void actionPerformed( ActionEvent e) {
 129  0
                 deleteButtonPressed();
 130  0
             }
 131  
         });
 132  286
         buttonPanel.add( deleteButton);
 133  
     
 134  286
         JPanel southPanel = new JPanel(new BorderLayout());
 135  286
         southPanel.add(buttonPanel, BorderLayout.EAST);
 136  
 
 137  286
         JPanel selectPanel = new JPanel(new FlowLayout()); 
 138  286
         ativateAllButton = new ThreeStateCheckBox("Activate All");
 139  286
         ativateAllButton.addActionListener(new ActionListener() {
 140  286
             public void actionPerformed(ActionEvent e) {
 141  0
                 boolean selected = ativateAllButton.isSelected();
 142  
                 
 143  0
                 for (int i = 0; i < getModel().getRowCount(); i++) {
 144  0
                     getModel().setValueAt(selected, i, 0);
 145  
                 }
 146  0
             }
 147  
         });
 148  286
         ativateAllButton.setMnemonic('A');
 149  286
         selectPanel.add(ativateAllButton);
 150  286
         updateSelectAllButton();
 151  286
         southPanel.add(selectPanel, BorderLayout.WEST);
 152  
 
 153  286
         JScrollPane scroller = new JScrollPane( table);
 154  286
         scroller.setPreferredSize(new Dimension(100, 100));
 155  286
         scroller.getViewport().setBackground( Color.white);
 156  286
         scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 157  
         
 158  286
         scroller.setPreferredSize( new Dimension( 100, 100));
 159  286
         parameterListPanel.add( scroller, BorderLayout.CENTER);
 160  286
         parameterListPanel.add(southPanel, BorderLayout.SOUTH);
 161  
     
 162  286
         add(parameterListPanel, BorderLayout.CENTER);
 163  286
     }
 164  
     
 165  
     private void updateSelectAllButton() {
 166  352
         boolean active = false;
 167  352
         boolean inactive = false;
 168  
         
 169  418
         for (int i = 0; i < table.getModel().getRowCount(); i++) {
 170  66
             Object value = table.getModel().getValueAt(i, 0);
 171  66
             if (value instanceof Boolean) {
 172  66
                 if ((Boolean)value) {
 173  66
                     active = true;
 174  66
                 } else {
 175  0
                     inactive = true;
 176  
                 }
 177  
             }
 178  
         }
 179  
         
 180  352
         if (ativateAllButton != null) {
 181  352
             if (active && inactive) {
 182  0
                 ativateAllButton.setSelected(ThreeStateCheckBox.DONT_CARE);
 183  0
             } else if (active) {
 184  66
                 ativateAllButton.setSelected(ThreeStateCheckBox.SELECTED);
 185  66
             } else if (inactive) {
 186  0
                 ativateAllButton.setSelected(ThreeStateCheckBox.NOT_SELECTED);
 187  
             }
 188  
         }
 189  352
     }
 190  
 
 191  
     public void dispose() {
 192  
         
 193  0
     }
 194  
     
 195  
     public void setEnabled( boolean enabled) {
 196  0
         table.setEnabled( enabled);
 197  0
         addButton.setEnabled( enabled);
 198  0
         deleteButton.setEnabled( enabled);
 199  0
     }
 200  
     
 201  
     protected JTable getTable() {
 202  0
         return table;
 203  
     }
 204  
 
 205  
     protected void addButtonPressed() {
 206  0
         getModel().addRow(getDefaultRowContent());
 207  0
         getTable().scrollRectToVisible(getTable().getCellRect(getModel().getRowCount()-1, 1, false));
 208  
 
 209  0
         ParameterNode parameter = new ParameterNode(this.parent, (String)model.getValueAt(getModel().getRowCount()-1, 1), (String)model.getValueAt(getModel().getRowCount()-1, 2));
 210  0
         nodes.add(parameter);
 211  
         
 212  0
         if (page != null) {
 213  0
             page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
 214  0
             page.getProjectView().getOverviewPanel().expandNode(parent);
 215  0
             page.getProjectView().getUndoManager().addEdit(new UndoableAddParameterEdit());
 216  0
             page.getProjectView().getFieldManager().setFieldsChanged(true);
 217  
         }
 218  0
     }
 219  
     
 220  
     public ArrayList<OverviewNode> getNodes() {
 221  8948
         return nodes;
 222  
     }
 223  
 
 224  
     protected void deleteButtonPressed() {
 225  0
         int[] indices = table.getSelectedRows();
 226  
         
 227  0
         for(int i = indices.length-1; i >= 0; i--) {              
 228  0
             int result = -1;
 229  
             
 230  0
             if( indices.length > 1) {
 231  0
                 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  0
             } else {
 234  0
                 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  0
             if(result == 2 && indices.length > 1) { // Remove ALL
 239  0
                 for (int j = i; j >= 0; j--) { 
 240  0
                     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  0
                     getModel().removeRow(indices[j]);
 242  
 
 243  0
                     if (page != null) {
 244  0
                         page.getProjectView().getOverviewPanel().selectNode(parent);
 245  0
                         page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(indices[j]));
 246  0
                         page.getProjectView().getUndoManager().addEdit(undoableEdit);
 247  0
                         page.getProjectView().getFieldManager().setFieldsChanged(true);
 248  
                     }
 249  
                     
 250  0
                     nodes.remove(indices[j]);
 251  
                 }
 252  
                 
 253  0
                 return;
 254  0
             } else if(result == JOptionPane.YES_OPTION) {
 255  0
                 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  0
                 getModel().removeRow(indices[i]);
 257  
 
 258  0
                 if (page != null) {
 259  0
                     page.getProjectView().getOverviewPanel().selectNode(parent);
 260  0
                     page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(indices[i]));
 261  0
                     page.getProjectView().getUndoManager().addEdit(undoableEdit);
 262  0
                     page.getProjectView().getFieldManager().setFieldsChanged(true);
 263  
                 }
 264  
                 
 265  0
                 nodes.remove(indices[i]);
 266  
             }
 267  
         }
 268  0
     }
 269  
     protected DefaultTableModel getModel() {
 270  1408
         if ( model == null) {
 271  286
             model = new ParameterTableModel();
 272  286
             model.setColumnCount(3);
 273  286
             model.setColumnIdentifiers(new String[] {" ", "Name", "Value"});
 274  
         }
 275  
         
 276  1408
         return model;
 277  
     }
 278  
     
 279  
     
 280  
     protected int[] getColumnWidths() {
 281  286
         return new int[] {25, 500, 500};
 282  
     }
 283  
 
 284  
     public void setParameters(List<Parameter> parameters) {
 285  66
         while (getModel().getRowCount() > 0) {
 286  0
             getModel().removeRow(0);
 287  0
         }
 288  
         
 289  66
         if (parameters != null) {
 290  66
             for (Parameter parameter : parameters) {
 291  66
                 getModel().addRow(new Object[] {parameter.isActive(), parameter.getName(), parameter.getValue()});
 292  
     
 293  66
                 ParameterNode param = new ParameterNode(this.parent, parameter.getName(), parameter.getValue());
 294  66
                 nodes.add(param);
 295  
                 
 296  66
                 if (page != null) {
 297  66
                     page.getProjectView().getOverviewPanel().getModel().nodeInserted(param);
 298  66
                     page.getProjectView().getOverviewPanel().expandNode(parent);
 299  
                 }
 300  66
             }
 301  
         }
 302  66
     }
 303  
     
 304  
     public List<Parameter> getParameters() {
 305  198
         List<Parameter> parameters = new ArrayList<Parameter>();
 306  
         
 307  396
         for ( int i = 0; i < getModel().getRowCount(); i++) {
 308  198
             Parameter parameter = new Parameter();
 309  198
             parameter.setActive((Boolean)getModel().getValueAt( i, 0));
 310  198
             parameter.setName((String)getModel().getValueAt( i, 1));
 311  198
             parameter.setValue((String)getModel().getValueAt( i, 2));
 312  
             
 313  198
             parameters.add(parameter);
 314  
         }
 315  
         
 316  198
         return parameters;
 317  
     }
 318  
     
 319  286
     public class ParameterTableModel extends ActivatableTableModel {
 320  
         private static final long serialVersionUID = -4387014110990368593L;
 321  
 
 322  
         public void setUndoValueAt(Object object, int row, int col) {
 323  0
             super.setValueAt(object, row, col);
 324  
 
 325  0
             if (col == 1) {
 326  0
                 ((ParameterNode)nodes.get(row)).setName((String)object);
 327  0
             } else if (col == 2){
 328  0
                 ((ParameterNode)nodes.get(row)).setValue((String)object);
 329  
             }
 330  0
         }
 331  
 
 332  
         public void setValueAt(Object object, int row, int col) {
 333  0
             UndoableChangeParameterEdit undoableEdit = new UndoableChangeParameterEdit(this, getValueAt(row, col), object, row, col);
 334  
 
 335  0
             super.setValueAt(object, row, col);
 336  
             
 337  0
             if (col == 1) {
 338  0
                 ((ParameterNode)nodes.get(row)).setName((String)object);
 339  0
             } else if (col == 2){
 340  0
                 ((ParameterNode)nodes.get(row)).setValue((String)object);
 341  
             }
 342  
             
 343  0
             if (page != null) {
 344  0
                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
 345  0
                 page.getProjectView().getUndoManager().addEdit(undoableEdit);
 346  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 347  
             }
 348  0
         }
 349  
     }
 350  
 
 351  
     protected Object[] getDefaultRowContent() {
 352  0
         return new Object[] {Boolean.TRUE, "New Parameter Name", "New Parameter Value"};
 353  
     }
 354  
     
 355  572
     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  0
             Component comp = super.getTableCellRendererComponent(table, obj, selected, focus, row, column);
 361  0
             Object activated = table.getModel().getValueAt(row, 0);
 362  
             
 363  0
             if (activated instanceof Boolean) {
 364  0
                 comp.setEnabled((Boolean)activated);
 365  0
             } else {
 366  0
                 comp.setEnabled(true);
 367  
             }
 368  
             
 369  0
             return comp;
 370  
         }
 371  
     }
 372  
     
 373  0
     private class UndoableAddParameterEdit extends AbstractUndoableEdit {
 374  
         private static final long serialVersionUID = 8615885869967750791L;
 375  
 
 376  
         public void undo() {
 377  0
             super.undo();
 378  
             
 379  0
             if (table.isEditing()) {
 380  0
                 table.getCellEditor().cancelCellEditing();
 381  
             }
 382  
 
 383  0
             int index = getModel().getRowCount()-1;
 384  
             
 385  0
             getModel().removeRow(index);
 386  
 
 387  0
             if (page != null) {
 388  0
                 page.getProjectView().getOverviewPanel().selectNode(parent);
 389  0
                 page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(index));
 390  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 391  
 
 392  0
                 nodes.remove(index);
 393  
             }
 394  0
         }
 395  
 
 396  
         public void redo() {
 397  0
             super.redo();
 398  
 
 399  0
             if (table.isEditing()) {
 400  0
                 table.getCellEditor().cancelCellEditing();
 401  
             }
 402  
 
 403  0
             getModel().addRow(getDefaultRowContent());
 404  0
             getTable().scrollRectToVisible(getTable().getCellRect(getModel().getRowCount()-1, 1, false));
 405  
 
 406  0
             ParameterNode parameter = new ParameterNode(parent, (String)model.getValueAt(getModel().getRowCount()-1, 1), (String)model.getValueAt(getModel().getRowCount()-1, 2));
 407  0
             nodes.add(parameter);
 408  
             
 409  0
             if (page != null) {
 410  0
                 page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
 411  0
                 page.getProjectView().getOverviewPanel().expandNode(parent);
 412  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 413  
             }
 414  0
         }
 415  
     }    
 416  
 
 417  
     public class UndoableRemoveParameterEdit extends AbstractUndoableEdit {
 418  
         private static final long serialVersionUID = 8615885869967750791L;
 419  
 
 420  0
         private int index = -1;
 421  0
         private boolean active = false;
 422  0
         private String name = null;
 423  0
         private String value = null;
 424  
         
 425  0
         public UndoableRemoveParameterEdit(int index, boolean active, String name, String value) {
 426  0
             super();
 427  
             
 428  0
             this.index = index;
 429  0
             this.active = active;
 430  0
             this.name = name;
 431  0
             this.value = value;
 432  0
         }
 433  
 
 434  
         public void undo() {
 435  0
             super.undo();
 436  
             
 437  0
             if (table.isEditing()) {
 438  0
                 table.getCellEditor().cancelCellEditing();
 439  
             }
 440  
 
 441  0
             getModel().insertRow(index, new Object[] {active, name, value});
 442  0
             getTable().scrollRectToVisible(getTable().getCellRect(index, 1, false));
 443  
 
 444  0
             ParameterNode parameter = new ParameterNode(parent, name, value);
 445  0
             nodes.add(index, parameter);
 446  
             
 447  0
             if (page != null) {
 448  0
                 page.getProjectView().getOverviewPanel().getModel().nodeInserted(parameter);
 449  0
                 page.getProjectView().getOverviewPanel().expandNode(parent);
 450  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 451  
             }
 452  0
         }
 453  
 
 454  
         public void redo() {
 455  0
             super.redo();
 456  
 
 457  0
             if (table.isEditing()) {
 458  0
                 table.getCellEditor().cancelCellEditing();
 459  
             }
 460  
 
 461  0
             getModel().removeRow(index);
 462  
 
 463  0
             if (page != null) {
 464  0
                 page.getProjectView().getOverviewPanel().selectNode(parent);
 465  0
                 page.getProjectView().getOverviewPanel().getModel().nodeRemoved(nodes.get(index));
 466  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 467  
 
 468  0
                 nodes.remove(index);
 469  
             }
 470  0
         }
 471  
     }    
 472  
 
 473  
     public class UndoableChangeParameterEdit extends AbstractUndoableEdit {
 474  
         private static final long serialVersionUID = 8615885869967750791L;
 475  
 
 476  0
         private ParameterTableModel model = null;
 477  0
         private Object oldValue = null;
 478  0
         private Object newValue = null;
 479  0
         private int row = -1;
 480  0
         private int col = -1;
 481  
         
 482  0
         public UndoableChangeParameterEdit(ParameterTableModel model, Object oldValue, Object newValue, int row, int col) {
 483  0
             super();
 484  
             
 485  0
             this.model = model;
 486  0
             this.oldValue = oldValue;
 487  0
             this.newValue = newValue;
 488  0
             this.row = row;
 489  0
             this.col = col;
 490  0
         }
 491  
 
 492  
         public void undo() {
 493  0
             super.undo();
 494  
             
 495  0
             if (table.isEditing()) {
 496  0
                 table.getCellEditor().cancelCellEditing();
 497  
             }
 498  
 
 499  0
             model.setUndoValueAt(oldValue, row, col);
 500  
 
 501  0
             if (page != null) {
 502  0
                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
 503  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 504  
             }
 505  0
         }
 506  
 
 507  
         public void redo() {
 508  0
             super.redo();
 509  
 
 510  0
             if (table.isEditing()) {
 511  0
                 table.getCellEditor().cancelCellEditing();
 512  
             }
 513  
 
 514  0
             model.setUndoValueAt(newValue, row, col);
 515  
 
 516  0
             if (page != null) {
 517  0
                 page.getProjectView().getOverviewPanel().getModel().nodeChanged(nodes.get(row));
 518  0
                 page.getProjectView().getFieldManager().setFieldsChanged(true);
 519  
             }
 520  0
         }
 521  
     }    
 522  
 }