1 19 24 package org.openide.explorer.propertysheet; 25 26 import org.openide.nodes.Node.Property; 27 28 import java.awt.Component ; 29 import java.awt.Dimension ; 30 import java.awt.Graphics ; 31 32 import java.beans.PropertyEditor ; 33 34 import javax.swing.BorderFactory ; 35 import javax.swing.JCheckBox ; 36 import javax.swing.JComboBox ; 37 import javax.swing.JComponent ; 38 import javax.swing.SwingUtilities ; 39 40 41 45 final class RendererPropertyDisplayer extends JComponent implements PropertyDisplayer_Mutable, PropertyDisplayer_Inline { 46 private boolean showCustomEditorButton = true; 47 private boolean tableUI = false; 48 private int radioButtonMax = 0; 49 private boolean useLabels = true; 50 private Property prop; 51 private boolean radioBoolean; 52 private ReusablePropertyEnv reusableEnv = new ReusablePropertyEnv(); private ReusablePropertyModel reusableModel = new ReusablePropertyModel(reusableEnv); 54 boolean inGetRenderer = false; 55 private Dimension prefSize = null; 56 private RendererFactory rendererFactory1 = null; 57 private RendererFactory rendererFactory2 = null; 58 59 public RendererPropertyDisplayer(Property p) { 60 this.prop = p; 61 } 62 63 public Component getComponent() { 64 return this; 65 } 66 67 public Property getProperty() { 68 return prop; 69 } 70 71 public void refresh() { 72 repaint(); 73 } 74 75 public void validate() { 76 if (!tableUI) { 77 super.validate(); 78 } 79 } 80 81 public boolean isValid() { 82 if (tableUI) { 83 return true; 84 } else { 85 return super.isValid(); 86 } 87 } 88 89 public boolean isShowing() { 90 if (tableUI) { 91 return true; 92 } else { 93 return super.isShowing(); 94 } 95 } 96 97 public void setProperty(Property prop) { 98 if (prop == null) { 99 throw new NullPointerException ("Property cannot be null"); } 101 102 if (prop != this.prop) { 103 this.prop = prop; 104 prefSize = null; 105 106 if (isShowing()) { 107 firePropertyChange("preferredSize", null, null); } 109 110 repaint(); 111 } 112 } 113 114 public final boolean isShowCustomEditorButton() { 115 boolean result = showCustomEditorButton; 116 117 if (getProperty() != null) { 118 Boolean explicit = (Boolean ) getProperty().getValue("suppressCustomEditor"); 120 if (explicit != null) { 121 result = !explicit.booleanValue(); 122 } 123 } 124 125 return result; 126 } 127 128 public boolean isTableUI() { 129 return tableUI; 130 } 131 132 public boolean isUseLabels() { 133 return useLabels; 134 } 135 136 public void setUseLabels(boolean useLabels) { 137 if (useLabels != this.useLabels) { 138 Dimension oldPreferredSize = null; 139 140 if (isShowing()) { 141 JComponent innermost = findInnermostRenderer(getRenderer(this)); 142 143 if (innermost instanceof RadioInplaceEditor || innermost instanceof JCheckBox ) { 144 oldPreferredSize = getPreferredSize(); 145 } 146 } 147 148 this.useLabels = useLabels; 149 150 if (oldPreferredSize != null) { 151 firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); } 153 } 154 } 155 156 public void setShowCustomEditorButton(boolean val) { 157 if (getProperty() != null) { 160 Boolean explicit = (Boolean ) getProperty().getValue("suppressCustomEditor"); 162 if (explicit != null) { 163 val = explicit.booleanValue(); 164 } 165 } 166 167 if (showCustomEditorButton != val) { 168 prefSize = null; 169 170 Dimension oldPreferredSize = null; 171 172 if (isShowing()) { 173 oldPreferredSize = getPreferredSize(); 174 } 175 176 showCustomEditorButton = val; 177 178 if (oldPreferredSize != null) { 179 firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); repaint(); 181 } 182 } 183 } 184 185 public void setTableUI(boolean val) { 186 if (val != tableUI) { 187 tableUI = val; 188 repaint(); 189 } 190 } 191 192 public int getRadioButtonMax() { 193 return radioButtonMax; 194 } 195 196 public void setRadioButtonMax(int i) { 197 if (i != radioButtonMax) { 198 Dimension oldPreferredSize = null; 199 200 if (isShowing()) { 201 oldPreferredSize = getPreferredSize(); 202 } 203 204 int old = radioButtonMax; 205 radioButtonMax = i; 206 207 if (oldPreferredSize != null) { 208 PropertyEditor ed = PropUtils.getPropertyEditor(prop); 210 String [] tags = ed.getTags(); 211 212 if (tags != null) { 213 if ((tags.length >= i) != (tags.length >= old)) { 214 firePropertyChange("preferredSize", oldPreferredSize, getPreferredSize()); } 216 } 217 } 218 } 219 } 220 221 public String toString() { 222 StringBuffer sb = new StringBuffer ("Inline editor for property "); sb.append(getProperty().getDisplayName()); 224 sb.append(" = "); sb.append(getProperty()); 226 sb.append(" inplace editor="); 228 if (!inGetRenderer) { 229 try { 230 sb.append(getRenderer(this)); 231 } catch (Exception e) { 232 sb.append(e); 233 } 234 } 235 236 return sb.toString(); 237 } 238 239 public void paintComponent(Graphics g) { 240 reusableEnv.setNode(EditorPropertyDisplayer.findBeans(this)); 249 250 JComponent comp = getRenderer(this); 251 prepareRenderer(comp); 252 comp.setBounds(0, 0, getWidth(), getHeight()); 253 254 if (comp instanceof InplaceEditor) { 255 Component inner = findInnermostRenderer(comp); 256 SwingUtilities.paintComponent(g, comp, this, 0, 0, getWidth(), getHeight()); 257 258 return; 259 } 260 261 comp.paint(g); 262 } 263 264 protected void superPaintComponent(Graphics g) { 265 super.paintComponent(g); 266 } 267 268 JComponent getRenderer(PropertyDisplayer_Inline inline) { 269 inGetRenderer = true; 270 271 JComponent result = rfactory(inline).getRenderer(inline.getProperty()); 272 273 if (inline.isTableUI()) { 274 result.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 277 } 278 279 inGetRenderer = false; 280 281 return result; 282 } 283 284 protected void prepareRenderer(JComponent comp) { 285 comp.setBackground(getBackground()); 286 comp.setForeground(getForeground()); 287 comp.setBounds(0, 0, getWidth(), getHeight()); 288 289 JComponent innermost; 290 291 if ((innermost = findInnermostRenderer(comp)) instanceof JComboBox ) { 292 if (comp.getLayout() != null) { 293 comp.getLayout().layoutContainer(comp); 294 } 295 } 296 297 if (!isTableUI() && ((InplaceEditor) comp).supportsTextEntry()) { 298 innermost.setBackground(PropUtils.getTextFieldBackground()); 299 innermost.setForeground(PropUtils.getTextFieldForeground()); 300 } 301 } 302 303 public boolean isTitleDisplayed() { 304 if (!useLabels) { 305 return false; 306 } 307 308 JComponent jc = getRenderer(this); 309 310 if (jc instanceof InplaceEditor) { 311 InplaceEditor innermost = PropUtils.findInnermostInplaceEditor((InplaceEditor) jc); 312 313 return innermost instanceof CheckboxInplaceEditor || innermost instanceof RadioInplaceEditor; 314 } 315 316 return false; 317 } 318 319 static JComponent findInnermostRenderer(JComponent comp) { 320 if (comp instanceof InplaceEditor) { 321 InplaceEditor ine = (InplaceEditor) comp; 322 323 return PropUtils.findInnermostInplaceEditor(ine).getComponent(); 324 } else { 325 return comp; 326 } 327 } 328 329 public Dimension getPreferredSize() { 330 if (prefSize == null) { 333 JComponent jc = getRenderer(this); 334 prefSize = jc.getPreferredSize(); 335 } 336 337 return prefSize; 338 } 339 340 RendererFactory rfactory(final PropertyDisplayer_Inline inline) { 341 RendererFactory factory; 342 343 if (inline.isTableUI()) { 344 if (rendererFactory1 == null) { 345 rendererFactory1 = new RendererFactory( 346 inline.isTableUI(), inline.getReusablePropertyEnv(), 347 inline.getReusablePropertyEnv().getReusablePropertyModel() 348 ); 349 } 350 351 factory = rendererFactory1; 352 } else { 353 if (rendererFactory2 == null) { 354 rendererFactory2 = new RendererFactory( 355 inline.isTableUI(), inline.getReusablePropertyEnv(), 356 inline.getReusablePropertyEnv().getReusablePropertyModel() 357 ); 358 } 359 360 factory = rendererFactory2; 361 } 362 363 factory.setUseRadioBoolean(inline.isRadioBoolean()); 364 factory.setUseLabels(inline.isUseLabels()); 365 factory.setRadioButtonMax(inline.getRadioButtonMax()); 366 367 return factory; 368 } 369 370 public boolean isRadioBoolean() { 371 return radioBoolean; 372 } 373 374 public void setRadioBoolean(boolean b) { 375 radioBoolean = b; 376 } 377 378 public ReusablePropertyEnv getReusablePropertyEnv() { 379 return reusableEnv; 380 } 381 382 public void firePropertyChange(String name, Object old, Object nue) { 383 } 385 } 386 | Popular Tags |