1 19 20 package org.openide.explorer.propertysheet; 21 22 import org.openide.util.Utilities; 23 24 import java.awt.Color ; 25 import java.awt.Component ; 26 import java.awt.Container ; 27 import java.awt.Dimension ; 28 import java.awt.Font ; 29 import java.awt.Graphics ; 30 import java.awt.Insets ; 31 import java.awt.event.FocusListener ; 32 33 import java.beans.FeatureDescriptor ; 34 35 import javax.swing.Icon ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.JComponent ; 38 39 40 47 class IconPanel extends JComponent implements InplaceEditor { 48 private InplaceEditor inplaceEditor; 49 private Icon icon; 50 private boolean needLayout = true; 51 private PropertyEnv env = null; 52 private Component comp; 53 54 55 public IconPanel() { 56 setOpaque(true); 57 } 58 59 63 public void setInplaceEditor(InplaceEditor inplaceEditor) { 64 this.inplaceEditor = inplaceEditor; 65 setComponent(inplaceEditor.getComponent()); 66 } 67 68 public InplaceEditor getInplaceEditor() { 69 return inplaceEditor; 70 } 71 72 public void setEnabled(boolean val) { 73 if (comp != null) { 74 comp.setEnabled(val); 76 } 77 78 super.setEnabled(val); 79 } 80 81 public void setBackground(Color c) { 82 if (comp != null) { 83 comp.setBackground(c); 85 } 86 87 super.setBackground(c); 88 } 89 90 public void setForeground(Color c) { 91 if (comp != null) { 92 comp.setForeground(c); 94 } 95 96 super.setForeground(c); 97 } 98 99 public void setFont(Font f) { 100 if (comp != null) { 101 comp.setFont(f); 102 } 103 104 super.setFont(f); 105 } 106 107 108 private void setComponent(Component c) { 109 if (comp != null) { 110 remove(comp); 111 } 112 113 if (c != null) { 114 add(c); 115 } 116 117 comp = c; 118 needLayout = true; 119 } 120 121 122 public void setIcon(Icon i) { 123 this.icon = i; 124 needLayout = true; 125 } 126 127 128 public void paintComponent(Graphics g) { 129 if (needLayout) { 130 doLayout(); 131 } 132 133 if (icon != null) { 134 Color c = g.getColor(); 135 136 try { 137 g.setColor(getBackground()); 138 139 int right = (comp != null) ? (comp.getLocation().x + icon.getIconWidth()) : (icon.getIconWidth() + 2); 140 g.fillRect(0, 0, right, getHeight()); 141 142 Insets ins = getInsets(); 143 int x = ins.left; 144 int y = ins.top + Math.max((getHeight() / 2) - (icon.getIconHeight() / 2), 0); 145 icon.paintIcon(this, g, x, y); 146 } finally { 147 g.setColor(c); 148 } 149 } 150 151 super.paintComponent(g); 152 } 153 154 155 public void addActionListener(java.awt.event.ActionListener al) { 156 inplaceEditor.addActionListener(al); 157 } 158 159 160 public void clear() { 161 inplaceEditor.clear(); 162 setIcon(null); 163 setComponent(null); 164 env = null; 165 } 166 167 168 public void connect(java.beans.PropertyEditor pe, PropertyEnv env) { 169 inplaceEditor.connect(pe, env); 170 this.env = env; 171 updateIcon(); 172 } 173 174 private void updateIcon() { 175 if (env != null) { 176 Icon ic = null; 177 FeatureDescriptor fd = env.getFeatureDescriptor(); 178 179 if (env.getState() == env.STATE_INVALID) { 180 ic = new ImageIcon (Utilities.loadImage("org/openide/resources/propertysheet/invalid.gif")); } else if (fd != null) { 182 ic = (Icon ) fd.getValue("valueIcon"); } 184 185 setIcon(ic); 186 needLayout = true; 187 } 188 } 189 190 public void setOpaque(boolean val) { 191 if (getInplaceEditor() != null) { 192 getInplaceEditor().getComponent().setOpaque(true); 193 } 194 } 195 196 197 public javax.swing.JComponent getComponent() { 198 return this; 199 } 200 201 202 public javax.swing.KeyStroke [] getKeyStrokes() { 203 return inplaceEditor.getKeyStrokes(); 204 } 205 206 207 public java.beans.PropertyEditor getPropertyEditor() { 208 return inplaceEditor.getPropertyEditor(); 209 } 210 211 212 public PropertyModel getPropertyModel() { 213 return inplaceEditor.getPropertyModel(); 214 } 215 216 217 public Object getValue() { 218 return inplaceEditor.getValue(); 219 } 220 221 222 public boolean isKnownComponent(java.awt.Component c) { 223 return ((c == this) || inplaceEditor.isKnownComponent(c)); 224 } 225 226 227 public void removeActionListener(java.awt.event.ActionListener al) { 228 inplaceEditor.removeActionListener(al); 229 } 230 231 232 public void reset() { 233 inplaceEditor.reset(); 234 updateIcon(); 235 } 236 237 238 public void setPropertyModel(PropertyModel pm) { 239 inplaceEditor.setPropertyModel(pm); 240 } 241 242 243 public void setValue(Object o) { 244 inplaceEditor.setValue(o); 245 } 246 247 248 public boolean supportsTextEntry() { 249 return inplaceEditor.supportsTextEntry(); 250 } 251 252 public void requestFocus() { 253 comp.requestFocus(); 254 } 255 256 public boolean requestFocusInWindow() { 257 return comp.requestFocusInWindow(); 258 } 259 260 public void addFocusListener(FocusListener fl) { 261 if (comp != null) { 262 comp.addFocusListener(fl); 263 } else { 264 super.addFocusListener(fl); 265 } 266 } 267 268 public void removeFocusListener(FocusListener fl) { 269 if (comp != null) { 270 comp.removeFocusListener(fl); 271 } else { 272 super.removeFocusListener(fl); 273 } 274 } 275 276 @SuppressWarnings ("deprecation") 277 public void layout() { 278 Insets ins = getInsets(); 279 280 int iconWidth = Math.max(icon.getIconWidth() + PropUtils.getTextMargin(), 18); 283 284 int x = (icon == null) ? ins.left : (ins.left + iconWidth); 285 int y = ins.top; 286 287 synchronized (getTreeLock()) { 288 Component c = comp; 289 290 if (c == null) { 291 return; 292 } 293 294 c.setBounds(x, y, getWidth() - (x + ins.right), getHeight() - ins.bottom); 295 296 if (c instanceof Container ) { 297 ((Container ) c).doLayout(); 298 } 299 } 300 } 301 302 public Dimension getPreferredSize() { 303 Insets ins = getInsets(); 304 Component c = comp; 305 Dimension result = new Dimension (0, 0); 306 307 if (icon != null) { 308 result.width = icon.getIconWidth() + PropUtils.getTextMargin(); 309 result.height = icon.getIconHeight(); 310 } 311 312 if (c != null) { 313 Dimension ps = c.getPreferredSize(); 314 result.width += ps.width; 315 result.height = Math.max(ps.height, result.height); 316 } 317 318 result.width += (ins.left + ins.right); 319 result.height += (ins.top + ins.bottom); 320 321 return result; 322 } 323 324 public Dimension getMinimumSize() { 325 return getPreferredSize(); 326 } 327 } 328 | Popular Tags |