1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.awt.Toolkit ; 30 import java.awt.event.FocusEvent ; 31 import java.awt.event.InputEvent ; 32 import java.awt.event.KeyEvent ; 33 import java.awt.event.MouseEvent ; 34 import java.beans.PropertyEditor ; 35 import javax.swing.JComponent ; 36 import javax.swing.JTextField ; 37 import javax.swing.KeyStroke ; 38 39 43 class StringInplaceEditor extends JTextField implements InplaceEditor { 44 45 protected PropertyEditor editor; 46 protected PropertyEnv env; 47 private boolean added; 48 private String valFromEditor; 49 private String valFromTextField; 50 private PropertyModel pm; 51 52 private KeyStroke [] strokes = new KeyStroke [] { 53 KeyStroke.getKeyStroke( 54 KeyEvent.VK_HOME, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | KeyEvent.SHIFT_DOWN_MASK 55 ), 56 KeyStroke.getKeyStroke( 57 KeyEvent.VK_END, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | KeyEvent.SHIFT_DOWN_MASK 58 ), KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false) 59 }; 60 61 public void removeNotify() { 62 super.removeNotify(); 63 } 64 65 public void clear() { 66 editor = null; 67 setEditable(true); 68 setEnabled(true); 69 setText(""); 70 pm = null; 71 env = null; 72 valFromEditor = null; 73 valFromTextField = null; 74 } 75 76 public void connect(PropertyEditor p, PropertyEnv env) { 77 setActionCommand(COMMAND_SUCCESS); 78 this.env = env; 79 80 if (editor == p) { 81 return; 82 } 83 84 editor = p; 85 86 boolean editable = PropUtils.checkEnabled(this, p, env); 87 setEnabled(editable); 88 89 if ((p.getTags() == null) && (p.getAsText() == null) && p.isPaintable()) { 92 editable = false; 93 } 94 95 setEditable(editable); 96 reset(); 97 added = false; 98 } 99 100 public void addNotify() { 101 super.addNotify(); 102 added = true; 103 } 104 105 public JComponent getComponent() { 106 return this; 107 } 108 109 public Object getValue() { 110 if ((valFromTextField != null) && valFromTextField.equals(getText())) { 111 return valFromEditor; 114 } else { 115 return getText(); 116 } 117 } 118 119 public void reset() { 120 String txt; 121 txt = editor.getAsText(); 122 123 if (editor instanceof PropUtils.DifferentValuesEditor) { 125 txt = ""; } 127 128 valFromEditor = txt; 129 if ((getClass() == StringInplaceEditor.class) && (env != null) && (env.getFeatureDescriptor() != null)) { 133 String initialEditValue = (String ) env.getFeatureDescriptor().getValue("initialEditValue"); 135 if (initialEditValue != null) { 136 txt = initialEditValue; 137 valFromEditor = txt; 138 } 139 } 140 141 if (txt == null) { 142 txt = ""; 143 } 144 145 setText(txt); 146 valFromTextField = getText(); 147 setSelectionStart(0); 148 setSelectionEnd(txt.length()); 149 } 150 151 public KeyStroke [] getKeyStrokes() { 152 return strokes; 153 } 154 155 public PropertyEditor getPropertyEditor() { 156 return editor; 157 } 158 159 private void handleInitialInputEvent(InputEvent e) { 160 String txt = getText(); 162 163 if (txt.length() > 0) { 164 setSelectionStart(0); 165 setSelectionEnd(getText().length()); 166 } 167 } 168 169 public void setValue(Object o) { 170 if ((null != o) && (null != editor) && editor.supportsCustomEditor()) { 171 editor.setValue(o); 172 setText(editor.getAsText()); 173 } else { 174 setText((o != null) ? o.toString() : ""); } 176 } 177 178 public boolean supportsTextEntry() { 179 return true; 180 } 181 182 public PropertyModel getPropertyModel() { 183 return pm; 184 } 185 186 public void setPropertyModel(PropertyModel pm) { 187 this.pm = pm; 188 } 189 190 public boolean isKnownComponent(Component c) { 191 return false; 192 } 193 194 public Dimension getPreferredSize() { 195 Graphics g = PropUtils.getScratchGraphics(this); 196 String s = getText(); 197 198 if (s.length() > 1000) { 199 return new Dimension (4196, g.getFontMetrics(getFont()).getHeight()); 201 } 202 203 FontMetrics fm = g.getFontMetrics(getFont()); 204 Dimension result = new Dimension (fm.stringWidth(s), fm.getHeight()); 205 result.width = Math.max(result.width, PropUtils.getMinimumPropPanelWidth()); 206 result.height = Math.max(result.height, PropUtils.getMinimumPropPanelHeight()); 207 208 if (getBorder() != null) { 209 Insets i = getBorder().getBorderInsets(this); 210 result.width += (i.right + i.left); 211 result.height += (i.top + i.bottom); 212 } 213 214 return result; 215 } 216 217 public void processMouseEvent(MouseEvent me) { 218 super.processMouseEvent(me); 219 220 if (added) { 221 handleInitialInputEvent(me); 222 } 223 224 added = false; 225 } 226 227 protected void processFocusEvent(FocusEvent fe) { 228 super.processFocusEvent(fe); 229 repaint(); 230 } 231 232 public void paintComponent(Graphics g) { 233 if ((editor != null) && !hasFocus() && editor.isPaintable()) { 235 Insets ins = getInsets(); 236 Color c = g.getColor(); 237 238 try { 239 g.setColor(getBackground()); 240 g.fillRect(0, 0, getWidth(), getHeight()); 241 } finally { 242 g.setColor(c); 243 } 244 245 ins.left += PropUtils.getTextMargin(); 246 editor.paintValue( 247 g, 248 new Rectangle ( 249 ins.left, ins.top, getWidth() - (ins.right + ins.left), getHeight() - (ins.top + ins.bottom) 250 ) 251 ); 252 } else { 253 super.paintComponent(g); 254 } 255 } 256 } 257 | Popular Tags |