1 19 24 package org.openide.explorer.propertysheet; 25 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Component ; 32 import java.awt.event.*; 33 34 import java.lang.reflect.*; 35 36 import java.util.*; 37 38 import javax.swing.*; 39 import javax.swing.event.EventListenerList ; 40 import javax.swing.text.JTextComponent ; 41 42 43 54 class WrapperInplaceEditor extends JPanel implements InplaceEditor, ActionListener, FocusListener { 55 56 private EnhancedPropertyEditor enh; 57 58 59 private PropertyModel mdl; 60 61 62 private Component legacy = null; 63 64 65 private transient List <ActionListener> actionListenerList; 66 67 69 private boolean listenerAdded = false; 70 private boolean suspendEvents = false; 71 72 75 private javax.swing.event.EventListenerList listenerList = null; 76 77 78 WrapperInplaceEditor(EnhancedPropertyEditor enh) { 79 this.enh = enh; 80 setLayout(new BorderLayout ()); 81 getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter"); getActionMap().put("enter", new EnterKbdAction()); } 84 85 87 public void focusGained(FocusEvent e) { 88 e.setSource(this); 89 fireFocusGained(e); 90 } 91 92 94 public void focusLost(FocusEvent e) { 95 e.setSource(this); 96 fireFocusLost(e); 97 } 98 99 public void clear() { 100 if (legacy != null) { 101 removeAll(); 102 103 if (listenerAdded) { 104 tryRemoveActionListener(legacy); 105 } 106 107 legacy.removeFocusListener(this); 108 legacy = null; 109 } 110 111 enh = null; 112 listenerAdded = false; 113 } 114 115 117 private boolean tryAddActionListener(Component comp) { 118 try { 119 Method m = comp.getClass().getMethod("addActionListener", new Class [] { ActionListener.class }); 121 if (m != null) { 122 m.invoke(comp, this); 123 124 return true; 125 } 126 } catch (Exception e) { 127 } 129 130 return false; 131 } 132 133 135 private boolean tryRemoveActionListener(Component comp) { 136 try { 137 Method m = comp.getClass().getMethod("removeActionListener", new Class [] { ActionListener.class }); 139 if (m != null) { 140 m.invoke(comp, this); 141 142 return true; 143 } 144 } catch (Exception e) { 145 } 147 148 return false; 149 } 150 151 155 public void actionPerformed(ActionEvent ae) { 156 fireAction(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, InplaceEditor.COMMAND_SUCCESS)); 157 } 158 159 162 public void connect(java.beans.PropertyEditor pe, PropertyEnv env) { 163 if (legacy != null) { 164 clear(); 166 } 167 168 if (pe != enh) { 169 enh = (EnhancedPropertyEditor) pe; 170 } 171 172 Component comp = getLegacyInplaceEditor(); 173 add(comp, BorderLayout.CENTER); 174 listenerAdded = tryAddActionListener(comp); 175 comp.addFocusListener(this); 176 } 177 178 public JComponent getComponent() { 179 return this; 180 } 181 182 185 public KeyStroke[] getKeyStrokes() { 186 if (getLegacyInplaceEditor() instanceof JComboBox) { 187 return ComboInplaceEditor.cbKeyStrokes; 188 } else { 189 return null; 190 } 191 } 192 193 public java.beans.PropertyEditor getPropertyEditor() { 194 return enh; 195 } 196 197 public PropertyModel getPropertyModel() { 198 return mdl; 199 } 200 201 public Object getValue() { 202 return enh.getValue(); 203 } 204 205 public void handleInitialInputEvent(InputEvent e) { 206 } 208 209 public boolean isKnownComponent(Component c) { 210 return isAncestorOf(c); 211 } 212 213 215 public void reset() { 216 suspendEvents = true; 217 218 try { 219 if (legacy instanceof JTextComponent ) { 220 ((JTextComponent ) legacy).setText(enh.getAsText()); 221 } else if (legacy instanceof JComboBox) { 222 if (((JComboBox) legacy).isEditable()) { 223 if (((JComboBox) legacy).getEditor().getEditorComponent().isShowing()) { 224 ((JComboBox) legacy).getEditor().setItem(enh.getValue()); 225 } 226 } else { 227 ((JComboBox) legacy).setSelectedItem(enh.getValue()); 228 } 229 } 230 } catch (Exception e) { 231 Logger.getLogger(WrapperInplaceEditor.class.getName()).log(Level.WARNING, "Failure resetting legacy editor", e); } finally { 236 suspendEvents = false; 237 } 238 } 239 240 public void setPropertyModel(PropertyModel pm) { 241 mdl = pm; 242 } 243 244 public void setValue(Object o) { 245 suspendEvents = true; 246 247 try { 248 if (legacy instanceof JTextComponent ) { 249 ((JTextComponent ) legacy).setText(o.toString()); 250 } else if (legacy instanceof JComboBox) { 251 if (((JComboBox) legacy).isEditable()) { 252 if (((JComboBox) legacy).getEditor().getEditorComponent().isShowing()) { 253 ((JComboBox) legacy).getEditor().setItem(o.toString()); 254 } 255 } else { 256 ((JComboBox) legacy).setSelectedItem(o); 257 } 258 } 259 } catch (Exception e) { 260 Logger.getLogger(WrapperInplaceEditor.class.getName()).log(Level.WARNING, "Failure resetting legacy editor", e); } finally { 265 suspendEvents = false; 266 } 267 } 268 269 271 public boolean supportsTextEntry() { 272 if (legacy instanceof JTextComponent ) { 273 return true; 274 } else if ((legacy instanceof JComboBox) && ((JComboBox) legacy).isEditable()) { 275 return true; 276 } else { 277 return false; 278 } 279 } 280 281 283 private Component getLegacyInplaceEditor() { 284 if (legacy == null) { 285 legacy = enh.getInPlaceCustomEditor(); 286 } 287 288 return legacy; 289 } 290 291 public synchronized void addActionListener(ActionListener listener) { 292 if (actionListenerList == null) { 293 actionListenerList = new ArrayList<ActionListener>(); 294 } 295 296 actionListenerList.add(listener); 297 } 298 299 public synchronized void removeActionListener(ActionListener listener) { 300 if (actionListenerList != null) { 301 actionListenerList.remove(listener); 302 } 303 } 304 305 void fireAction(ActionEvent event) { 306 if (suspendEvents) { 307 return; 308 } 309 310 List list; 311 312 synchronized (this) { 313 if (actionListenerList == null) { 314 return; 315 } 316 317 list = (List ) ((ArrayList) actionListenerList).clone(); 318 } 319 320 for (int i = 0; i < list.size(); i++) { 321 ((ActionListener) list.get(i)).actionPerformed(event); 322 } 323 } 324 325 329 public synchronized void addFocusListener(FocusListener listener) { 330 if (listenerList == null) { 331 listenerList = new EventListenerList (); 332 } 333 334 listenerList.add(FocusListener.class, listener); 335 super.addFocusListener(listener); 336 } 337 338 342 public synchronized void removeFocusListener(FocusListener listener) { 343 listenerList.remove(java.awt.event.FocusListener .class, listener); 344 super.removeFocusListener(listener); 345 } 346 347 352 private void fireFocusGained(FocusEvent event) { 353 if (listenerList == null) { 354 return; 355 } 356 357 Object [] listeners = listenerList.getListenerList(); 358 359 for (int i = listeners.length - 2; i >= 0; i -= 2) { 360 if (listeners[i] == java.awt.event.FocusListener .class) { 361 ((java.awt.event.FocusListener ) listeners[i + 1]).focusGained(event); 362 } 363 } 364 } 365 366 371 private void fireFocusLost(FocusEvent event) { 372 if (listenerList == null) { 373 return; 374 } 375 376 Object [] listeners = listenerList.getListenerList(); 377 378 for (int i = listeners.length - 2; i >= 0; i -= 2) { 379 if (listeners[i] == java.awt.event.FocusListener .class) { 380 ((java.awt.event.FocusListener ) listeners[i + 1]).focusLost(event); 381 } 382 } 383 } 384 385 387 private class EnterKbdAction extends AbstractAction { 388 public void actionPerformed(ActionEvent ae) { 389 fireAction(new ActionEvent(WrapperInplaceEditor.this, ActionEvent.ACTION_PERFORMED, COMMAND_SUCCESS)); 390 } 391 } 392 } 393 | Popular Tags |