Coverage Report - org.xmlhammer.gui.FieldManager
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldManager
80% 
95% 
0
 
 1  
 /*
 2  
  * $Id: FieldManager.java,v 1.12 2006/10/25 10:07:42 edankert Exp $
 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;
 23  
 
 24  
 import java.awt.Component;
 25  
 import java.awt.event.FocusEvent;
 26  
 import java.awt.event.FocusListener;
 27  
 import java.awt.event.ItemEvent;
 28  
 import java.awt.event.ItemListener;
 29  
 import java.util.ArrayList;
 30  
 
 31  
 import javax.swing.JComboBox;
 32  
 import javax.swing.JComponent;
 33  
 import javax.swing.JTextField;
 34  
 import javax.swing.JToggleButton;
 35  
 import javax.swing.event.ChangeEvent;
 36  
 import javax.swing.event.ChangeListener;
 37  
 import javax.swing.event.DocumentEvent;
 38  
 import javax.swing.event.DocumentListener;
 39  
 import javax.swing.event.EventListenerList;
 40  
 
 41  
 import org.apache.log4j.Logger;
 42  
 
 43  
 /**
 44  
  * Manages the fields for a project, handling undo and focus ...
 45  
  * 
 46  
  * @version $Revision: 1.12 $, $Date: 2006/10/25 10:07:42 $
 47  
  * @author Edwin Dankert <edankert@gmail.com>
 48  
  */
 49  
 
 50  
 public class FieldManager implements FocusListener, ItemListener, DocumentListener {
 51  572
     protected EventListenerList listeners = new EventListenerList();
 52  
 
 53  572
     private ArrayList<JComponent> fields = null;
 54  572
         private ProjectView view = null;
 55  572
         private JComponent lastFocusedComponent = null;
 56  572
     private boolean fieldsChanged = false;
 57  572
     private boolean propertiesChanged = false;
 58  572
     private boolean ignore = false;
 59  
 
 60  
         /**
 61  
          * @param view the view this field manager should manage.
 62  
          */
 63  572
         public FieldManager( ProjectView view) {
 64  572
                 fields = new ArrayList<JComponent>();
 65  572
                 this.view = view;
 66  572
         }
 67  
         
 68  
         /**
 69  
          * @param field the field to listen to changes on.
 70  
          */
 71  
         public void addField( JComponent field) {
 72  14762
                 fields.add( field);
 73  
                 
 74  14762
                 if ( field instanceof JToggleButton) {
 75  3740
                         ((JToggleButton)field).addItemListener( this);
 76  3740
             field.addFocusListener( this);
 77  3740
                 } else if ( field instanceof JComboBox) {
 78  10780
             if ( ((JComboBox)field).isEditable()) {
 79  8162
                             Component editor = ((JComboBox)field).getEditor().getEditorComponent();
 80  
                             
 81  8162
                             if ( editor instanceof JTextField) {
 82  8162
                                     fields.add( (JTextField)editor);
 83  8162
                                     ((JTextField)editor).getDocument().addDocumentListener(this);
 84  8162
                                     ((JTextField)editor).addFocusListener( this);
 85  
                             }
 86  8162
             } else {
 87  2618
                 field.addFocusListener( this);
 88  2618
                 ((JComboBox)field).addItemListener( this);
 89  
             }
 90  2618
                 } else if ( field instanceof JTextField) {
 91  0
                         ((JTextField)field).getDocument().addDocumentListener( this);
 92  0
             field.addFocusListener( this);
 93  
                 }
 94  14762
         }
 95  
         
 96  
         /**
 97  
          * @param field the field to listen to changes for.
 98  
          */
 99  
         public void removeField( JComponent field) {
 100  462
                 fields.remove( field);
 101  462
                 field.removeFocusListener( this);
 102  
 
 103  462
                 if ( field instanceof JToggleButton) {
 104  0
                         ((JToggleButton)field).removeItemListener( this);
 105  0
                 } else if ( field instanceof JComboBox) {
 106  462
                         Component editor = ((JComboBox)field).getEditor().getEditorComponent();
 107  462
                         if ( editor instanceof JTextField) {
 108  462
                                 ((JTextField)editor).getDocument().removeDocumentListener( this);
 109  462
                                 ((JTextField)editor).removeFocusListener( this);
 110  
                         }
 111  462
                         ((JComboBox)field).removeItemListener( this);
 112  462
                 } else if ( field instanceof JTextField) {
 113  0
                         ((JTextField)field).getDocument().removeDocumentListener( this);
 114  
                 }
 115  462
         }
 116  
         
 117  
         /**
 118  
          * @param event invoked when a field gained focus.
 119  
          */
 120  
         public void focusGained( FocusEvent event) {
 121  0
                 lastFocusedComponent = (JComponent)event.getSource();
 122  0
                 view.setSelected( true);
 123  0
         }
 124  
 
 125  
         /**
 126  
          * @param event invoked when a field lost focus.
 127  
          */
 128  0
         public void focusLost( FocusEvent event) {}
 129  
         
 130  
         /**
 131  
          * @return the field which had focus last.
 132  
          */
 133  
         public JComponent getLastFocusedField() {
 134  0
                 return lastFocusedComponent;
 135  
         }
 136  
         
 137  
         /**
 138  
          * @return true when any of the fields have been changed by the user.
 139  
          */
 140  
         public boolean isChanged() {
 141  22091
                 return fieldsChanged || propertiesChanged;
 142  
         }
 143  
         
 144  
         /**
 145  
          * @param changed overrides the changed state from the outside (view saved/initialised).
 146  
          */
 147  
         public void setChanged( boolean changed) {
 148  682
                 setPropertiesChanged(changed);
 149  682
         setFieldsChanged(changed);
 150  
 
 151  682
         notifyChange();
 152  682
         }
 153  
         
 154  
     /**
 155  
      * @param changed overrides the changed state from the outside.
 156  
      */
 157  
     public void setFieldsChanged( boolean changed) {
 158  2046
         this.fieldsChanged = changed;
 159  
 
 160  2046
         notifyChange();
 161  2046
     }
 162  
 
 163  
     /**
 164  
      * @param changed overrides the changed state from the outside.
 165  
      */
 166  
     public void setPropertiesChanged( boolean changed) {
 167  682
         this.propertiesChanged = changed;
 168  
 
 169  682
         notifyChange();
 170  682
     }
 171  
 
 172  
         /**
 173  
          * @param e state has changed.
 174  
          */
 175  
         public void itemStateChanged( ItemEvent e) {
 176  5368
         if (!ignore) {
 177  4356
             fieldsChanged = true;
 178  4356
                     notifyChange(); 
 179  
         }
 180  5368
         }
 181  
         
 182  
     /**
 183  
      * @param e state has changed.
 184  
      */
 185  
     public void setIgnoreChanges( boolean ignore) {
 186  1012
         this.ignore = ignore;
 187  1012
     }
 188  
 
 189  
     /**
 190  
      * @param e state has changed.
 191  
      */
 192  
     public boolean isIgnoreChanges() {
 193  0
         return ignore;
 194  
     }
 195  
 
 196  
     /**
 197  
          * notifies user changes to the underlying project view.
 198  
          *
 199  
          */
 200  
         public void notifyChange() {
 201  7766
                    view.setChanged( isChanged());
 202  
 
 203  7766
         Object[] list = listeners.getListenerList();
 204  
         
 205  
         // Process the listeners last to first, notifying
 206  
         // those that are interested in this event
 207  14102
         for ( int i = list.length-2; i >= 0; i -= 2) {
 208  6336
             if ( list[i] == ChangeListener.class) {
 209  6336
                 ((ChangeListener)list[i+1]).stateChanged( new ChangeEvent( this));
 210  
             }
 211  
         }
 212  7766
         }
 213  
 
 214  
     public void insertUpdate(DocumentEvent event) {
 215  5898
         Logger.getLogger(this.getClass()).debug("insertUpdate( "+event+")");
 216  
         
 217  5898
         if (!ignore) {
 218  5856
             for (JComponent component : fields) {
 219  333184
                 if (component instanceof JTextField) {
 220  123264
                     JTextField field = (JTextField)component;
 221  123264
                     if (field.getDocument() == event.getDocument() && field.hasFocus()) {
 222  0
                         fieldsChanged = true;
 223  0
                         notifyChange();
 224  0
                         break;
 225  
                     }
 226  
                 }
 227  333184
             }
 228  
         }
 229  5898
     }
 230  
 
 231  
     public void removeUpdate(DocumentEvent event) {
 232  1960
         Logger.getLogger(this.getClass()).debug("removeUpdate( "+event+")");
 233  
 
 234  1960
         if (!ignore) {
 235  1960
             for (JComponent component : fields) {
 236  114074
                 if (component instanceof JTextField) {
 237  42422
                     JTextField field = (JTextField)component;
 238  42422
                     if (field.getDocument() == event.getDocument() && field.hasFocus()) {
 239  0
                         fieldsChanged = true;
 240  0
                         notifyChange();
 241  0
                         break;
 242  
                     }
 243  
                 }
 244  114074
             }
 245  
         }
 246  1960
     }
 247  
 
 248  
     public void addChangeListener(ChangeListener l) {
 249  462
         listeners.add(ChangeListener.class, l);
 250  462
     }
 251  
 
 252  
     public void removeChangeListener(ChangeListener l) {
 253  0
         listeners.remove(ChangeListener.class, l);
 254  0
     }
 255  
 
 256  0
     public void changedUpdate(DocumentEvent arg0) {}
 257  
 }