1 19 20 package org.openide.explorer.propertysheet; 21 22 import org.openide.nodes.Node.*; 23 import org.openide.util.NbBundle; 24 25 import java.awt.Dimension ; 26 import java.awt.FontMetrics ; 27 import java.awt.Graphics ; 28 import java.awt.Insets ; 29 import java.awt.event.*; 30 31 import java.beans.*; 32 33 import java.text.MessageFormat ; 34 35 import java.util.*; 36 37 import javax.swing.*; 38 import javax.swing.event.*; 39 40 41 50 class CheckboxInplaceEditor extends JCheckBox implements InplaceEditor { 51 protected PropertyEditor editor = null; 52 protected PropertyEnv env = null; 53 private boolean useTitle = false; 54 private String text = null; 55 private PropertyModel pm = null; 56 57 public CheckboxInplaceEditor() { 58 setActionCommand(COMMAND_SUCCESS); 59 } 60 61 public void setUseTitle(boolean val) { 62 if (useTitle != val) { 63 useTitle = val; 64 text = null; 65 66 if (env != null) { 67 setText(env.getFeatureDescriptor().getDisplayName()); 68 } 69 } 70 } 71 72 public void setSelected(boolean val) { 73 boolean fire = val == isSelected(); 74 String s = getText(); 75 super.setSelected(val); 76 77 if (fire) { 78 firePropertyChange("text", s, getText()); } 80 } 81 82 public void connect(PropertyEditor p, PropertyEnv env) { 83 text = null; 84 85 if (editor instanceof PropUtils.NoPropertyEditorEditor) { 86 setSelected(false); 88 89 return; 90 } 91 92 if (editor == p) { 93 return; 94 } 95 96 editor = p; 97 setSelected(Boolean.TRUE.equals(p.getValue())); 98 reset(); 99 this.env = env; 100 101 if (env != null) { 102 if (useTitle) { 103 setText(env.getFeatureDescriptor().getDisplayName()); 104 } 105 } 106 } 107 108 public void clear() { 109 editor = null; 110 pm = null; 111 env = null; 112 113 text = null; 115 getModel().setRollover(false); 116 } 117 118 public JComponent getComponent() { 119 return this; 120 } 121 122 public Object getValue() { 123 return isSelected() ? Boolean.TRUE : Boolean.FALSE; 124 } 125 126 public void reset() { 127 if (editor instanceof PropUtils.NoPropertyEditorEditor) { 128 return; 130 } 131 132 if (editor != null) { 133 Boolean value = (Boolean ) editor.getValue(); 134 135 if (value == null) { 136 getModel().setArmed(true); 137 } else { 138 setSelected(value.booleanValue()); 139 } 140 } 141 } 142 143 public String getText() { 144 if (text == null) { 147 if (useTitle || (editor == null) || (editor.getTags() == null)) { 148 text = super.getText(); 151 } else if (PropUtils.noCheckboxCaption) { 152 text = ""; } else { 154 String prepend = NbBundle.getMessage(CheckboxInplaceEditor.class, "BOOLEAN_PREPEND"); String append = NbBundle.getMessage(CheckboxInplaceEditor.class, "BOOLEAN_APPEND"); java.text.MessageFormat mf = new MessageFormat ( 157 NbBundle.getMessage(CheckboxInplaceEditor.class, "FMT_BOOLEAN") 158 ); 160 String s; 161 Boolean sel = isSelected() ? Boolean.TRUE : Boolean.FALSE; 162 163 if (sel.equals(editor.getValue())) { 164 s = editor.getAsText(); 165 } else { 166 String [] tags = editor.getTags(); 167 168 if (tags[0].equals(editor.getAsText())) { 169 s = tags[1]; 170 } else { 171 s = tags[0]; 172 } 173 } 174 175 text = mf.format(new String [] { prepend, s, append }); 176 } 177 } 178 179 return text; 180 } 181 182 public KeyStroke[] getKeyStrokes() { 183 return null; 184 } 185 186 public PropertyEditor getPropertyEditor() { 187 return editor; 188 } 189 190 public void handleInitialInputEvent(InputEvent e) { 191 boolean toggle = false; 192 193 if (e instanceof MouseEvent) { 194 toggle = true; 195 } else if (e instanceof KeyEvent) { 196 if (((KeyEvent) e).getKeyCode() == KeyEvent.VK_SPACE) { 197 toggle = true; 198 } 199 } 200 201 if (toggle) { 202 setSelected(!isSelected()); 203 fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, COMMAND_SUCCESS)); 204 getModel().setPressed(false); } 206 } 207 208 public void setValue(Object o) { 209 if (o == null) { 210 setSelected(false); 212 } 213 214 if (Boolean.TRUE.equals(o)) { 215 setSelected(true); 216 } else if (Boolean.FALSE.equals(o)) { 217 setSelected(false); 218 } 219 } 220 221 public boolean supportsTextEntry() { 222 return false; 223 } 224 225 public PropertyModel getPropertyModel() { 226 return pm; 227 } 228 229 public void setPropertyModel(PropertyModel pm) { 230 this.pm = pm; 231 } 232 233 public boolean isKnownComponent(java.awt.Component c) { 234 return false; 235 } 236 237 239 public Dimension getPreferredSize() { 240 if (isShowing()) { 241 return super.getPreferredSize(); 242 } 243 244 Dimension result = PropUtils.getMinimumPanelSize(); 245 Graphics g = PropUtils.getScratchGraphics(this); 246 g.setFont(getFont()); 247 248 String txt = getText(); 249 Icon i = getIcon(); 250 FontMetrics fm = g.getFontMetrics(getFont()); 251 int w = fm.stringWidth(txt); 252 int h = fm.getHeight(); 253 254 if (i != null) { 255 w += (i.getIconWidth() + getIconTextGap()); 256 h = Math.max(h, result.height); 257 } 258 259 Insets ins = getInsets(); 260 261 if (ins != null) { 262 w += (ins.left + ins.right); 263 h += (ins.top + ins.bottom); 264 } 265 266 w += 22; 268 result.width = Math.max(result.width, w); 269 result.height = Math.max(result.height, h); 270 271 return result; 272 } 273 } 274 | Popular Tags |