1 19 package org.netbeans.modules.xml.tax.beans.customizer; 20 21 import java.awt.event.KeyEvent ; 22 import javax.swing.JPanel ; 23 import javax.swing.SwingUtilities ; 24 import java.beans.Customizer ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 28 import org.netbeans.tax.TreeObject; 29 30 35 public abstract class AbstractTreeCustomizer extends JPanel implements Customizer , PropertyChangeListener { 36 37 38 private static final long serialVersionUID =7141277140374364170L; 39 40 41 private static final String TEXT_DEFAULT = Util.THIS.getString ("TEXT_DEFAULT"); 43 44 public static final String MIME_XML = "text/xml"; 46 47 public static final String MIME_DTD = "text/x-dtd"; 49 50 public static final String MIME_TXT = "text/plain"; 52 53 54 protected boolean initializing; 55 56 57 private TreeObject treeObject; 58 59 60 private boolean treeListening = false; 61 62 63 67 68 public AbstractTreeCustomizer () { 69 super (); 70 71 treeObject = null; 72 initializing = false; 73 } 74 75 76 80 83 public final void setObject (Object bean) throws IllegalArgumentException { 84 try { 85 initializing = true; 86 87 if (! (bean instanceof TreeObject)) 88 throw new IllegalArgumentException (bean + Util.THIS.getString ("PROP__invalid_instance")); 90 treeObject = (TreeObject)bean; 91 92 ownInitComponents (); 93 94 initValues (); 95 } finally { 96 initializing = false; 97 } 98 } 99 100 101 105 107 protected final TreeObject getTreeObject () { 108 return treeObject; 109 } 110 111 113 private final void initValues () { 114 initComponentValues (); 115 updateReadOnlyStatus (); 116 initListeners (); 117 } 118 119 121 abstract protected void initComponentValues (); 122 123 125 protected void ownInitComponents () { 126 } 127 128 130 private void updateReadOnlyStatus () { 131 updateReadOnlyStatus (!!! getTreeObject ().isReadOnly ()); 132 } 133 134 136 abstract protected void updateReadOnlyStatus (boolean editable); 137 138 139 143 145 private void initListeners () { 146 if (!treeListening) { 147 treeObject.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange (this, treeObject)); 148 treeListening = true; 149 } 150 } 151 152 155 protected void safePropertyChange (PropertyChangeEvent pche) { 156 if (pche.getPropertyName ().equals (TreeObject.PROP_READ_ONLY)) { 157 updateReadOnlyStatus (); 158 } 159 } 160 161 165 public final void propertyChange (final PropertyChangeEvent e) { 166 if (initializing) 167 return; 168 169 if (SwingUtilities.isEventDispatchThread ()) { 170 safePropertyChange (e); 171 } else { 172 SwingUtilities.invokeLater (new Runnable () { 173 public void run () { 174 AbstractTreeCustomizer.this.safePropertyChange (e); 175 } 176 }); 177 } 178 } 179 180 181 185 protected static String text2null (String text) { 186 if ( text.equals (TEXT_DEFAULT) ) 187 return null; 188 if ( text.length () == 0 ) 189 return null; 190 if ( text.trim ().length () == 0 ) 191 return null; 192 return text; 193 } 194 195 protected static String null2text (String maybeNull) { 196 if ( maybeNull == null ) 197 return TEXT_DEFAULT; 198 return maybeNull; 199 } 200 201 protected static boolean applyKeyPressed (KeyEvent evt) { 202 return (evt.isControlDown () && (evt.getKeyCode () == KeyEvent.VK_ENTER)); 203 } 204 205 } 206 | Popular Tags |