1 19 20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget; 21 22 import java.awt.Color ; 23 import java.awt.GradientPaint ; 24 import java.awt.Graphics2D ; 25 import java.awt.Image ; 26 import java.awt.Insets ; 27 import java.awt.Paint ; 28 import java.awt.Rectangle ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.geom.RoundRectangle2D ; 32 33 import org.netbeans.api.visual.border.Border; 34 import org.netbeans.api.visual.layout.LayoutFactory; 35 import org.netbeans.api.visual.widget.ImageWidget; 36 import org.netbeans.api.visual.widget.LabelWidget; 37 import org.netbeans.api.visual.widget.Scene; 38 import org.netbeans.api.visual.widget.Widget; 39 40 44 public class ButtonWidget extends Widget { 45 46 private ImageWidget imageWidget = null; 47 private LabelWidget labelWidget = null; 48 49 50 private Insets margin = new Insets (2, 8, 2, 8); 51 52 53 private boolean rollover = false; 54 private boolean pressed = false; 55 private boolean enabled = true; 56 private boolean parentSelectionAllowed = false; 57 58 59 private ActionListener actionListener = null; 60 61 62 public ButtonWidget(Scene scene, String text) { 63 this(scene, null, text); 64 } 65 66 67 public ButtonWidget(Scene scene, Image icon) { 68 this(scene, icon, null); 69 } 70 71 72 public ButtonWidget(Scene scene, Image icon, String text) { 73 super(scene); 74 75 setLayout(LayoutFactory.createHorizontalLayout(LayoutFactory 76 .SerialAlignment.CENTER, 4)); 77 setIconAndText(icon, text); 78 79 setBorder(new ButtonBorder()); 80 81 getActions().addAction(((PartnerScene) scene).getButtonAction()); 82 getActions().addAction(((PartnerScene) scene).getSelectAction()); 83 } 84 85 86 public String getText() { 87 return (labelWidget == null) ? null : labelWidget.getLabel(); 88 } 89 90 91 public Image getIcon() { 92 return (imageWidget == null) ? null : imageWidget.getImage(); 93 } 94 95 96 public void setText(String text) { 97 setIconAndText(getIcon(), text); 98 } 99 100 101 public void setIcon(Image image) { 102 setIconAndText(image, getText()); 103 } 104 105 106 public Insets getMargin() { 107 return new Insets (margin.top, margin.left, margin.bottom, margin.right); 108 } 109 110 111 public void setMargin(Insets margin) { 112 this.margin.set(margin.top, margin.left, margin.bottom, margin.right); 113 revalidate(); 114 repaint(); 115 } 116 117 118 public void setIconAndText(Image icon, String text) { 119 removeButtonChildren(); 120 121 String oldText = getText(); 122 Image oldIcon = getIcon(); 123 124 if (imageWidget == null) { 125 if (icon != null) { 126 imageWidget = new ImageWidget(getScene(), icon); 127 } 128 } else { 129 if (icon == null) { 130 imageWidget = null; 131 } else if (icon != oldIcon) { 132 imageWidget.setImage(icon); 133 } 134 } 135 136 if (labelWidget == null) { 137 if (text != null) { 138 labelWidget = new LabelWidget(getScene(), text); 139 labelWidget.setFont(getScene().getDefaultFont()); 140 labelWidget.setForeground((isButtonEnabled()) 141 ? ENABLED_TEXT_COLOR 142 : DISABLED_TEXT_COLOR); 143 } 144 } else { 145 if (text == null) { 146 labelWidget = null; 147 } else if (!((oldText != null) && text.equals(oldText))) { 148 labelWidget.setLabel(text); 149 } 150 } 151 152 addButtonChildren(); 153 } 154 155 156 private void removeButtonChildren() { 157 if (labelWidget != null) { 158 removeChild(labelWidget); 159 } 160 161 if (imageWidget != null) { 162 removeChild(imageWidget); 163 } 164 } 165 166 167 private void addButtonChildren() { 168 if (imageWidget != null) { 169 addChild(imageWidget); 170 } 171 172 if (labelWidget != null) { 173 addChild(labelWidget); 174 } 175 } 176 177 178 public void setActionListener(ActionListener actionListener) { 179 this.actionListener = actionListener; 180 } 181 182 183 private void fireActionPerformed() { 184 if ((actionListener != null) && enabled) { 185 actionListener.actionPerformed(new ActionEvent (this, 0, 186 "button-pressed")); } 188 } 189 190 191 public boolean isParenSelectionAllowed() { 192 return parentSelectionAllowed; 193 } 194 195 196 public void setParentSelectionAllowed(boolean allowed) { 197 parentSelectionAllowed = allowed; 198 } 199 200 201 public void mouseEntered() { 202 rollover = true; 203 repaint(); 204 } 205 206 207 public void mouseExited() { 208 rollover = false; 209 repaint(); 210 } 211 212 213 public void mousePressed() { 214 pressed = true; 215 repaint(); 216 } 217 218 219 public void mouseReleased(boolean inside) { 220 pressed = false; 221 repaint(); 222 223 if (inside) { 224 fireActionPerformed(); 225 } 226 } 227 228 231 public void setButtonEnabled(boolean v) { 232 enabled = v; 233 234 if (labelWidget != null) { 235 labelWidget.setForeground((v) 236 ? ENABLED_TEXT_COLOR 237 : DISABLED_TEXT_COLOR); 238 } 239 240 revalidate(); 241 repaint(); 242 } 243 244 247 public boolean isButtonEnabled() { 248 return enabled; 249 } 250 251 252 private class ButtonBorder implements Border { 253 public ButtonBorder() {} 254 255 public Insets getInsets() { 256 return new Insets (margin.top, margin.left, 257 margin.bottom, margin.right); 258 } 259 260 public void paint(Graphics2D g2, Rectangle rect) { 261 Paint oldPaing = g2.getPaint(); 262 263 if (enabled) { 264 g2.setPaint(BORDER_COLOR); 265 g2.fill(new RoundRectangle2D.Double (rect.x, rect.y, 266 rect.width, rect.height, 6, 6)); 267 268 if (pressed) { 269 g2.setPaint(new Color (0xCCCCCC)); 270 } else { 271 g2.setPaint(new GradientPaint ( 272 0, rect.y + 1, BACKGROUND_COLOR_1, 273 0, rect.y + rect.height * 0.5f, 274 BACKGROUND_COLOR_2, true)); 275 } 276 277 if (rollover) { 278 g2.fill(new RoundRectangle2D.Double (rect.x + 1.5, rect.y + 1.5, 279 rect.width - 3, rect.height - 3, 3, 3)); 280 } else { 281 g2.fill(new RoundRectangle2D.Double (rect.x + 1, rect.y + 1, 282 rect.width - 2, rect.height - 2, 4, 4)); 283 } 284 } else { 285 g2.setPaint(grayFilter(BORDER_COLOR)); 286 g2.fill(new RoundRectangle2D.Double (rect.x, rect.y, 287 rect.width, rect.height, 6, 6)); 288 289 g2.setPaint(BACKGROUND_COLOR_DISABLED); 290 g2.fill(new RoundRectangle2D.Double (rect.x + 1, rect.y + 1, 291 rect.width - 2, rect.height - 2, 4, 4)); 292 } 293 294 g2.setPaint(oldPaing); 295 } 296 297 public boolean isOpaque() { 298 return true; 299 } 300 } 301 302 303 private static Color grayFilter(Color color) { 304 int y = Math.round(0.299f * color.getRed() 305 + 0.587f * color.getGreen() 306 + 0.114f * color.getBlue()); 307 308 if (y < 0) { 309 y = 0; 310 } else if (y > 255) { 311 y = 255; 312 } 313 314 return new Color (y, y, y); 315 } 316 317 private static final Color BORDER_COLOR = new Color (0x7F9DB9); 318 private static final Color BACKGROUND_COLOR_1 = new Color (0xD2D2DD); 319 private static final Color BACKGROUND_COLOR_2 = new Color (0xF8F8F8); 320 private static final Color BACKGROUND_COLOR_DISABLED = new Color (0xE4E4E4); 321 private static final Color ENABLED_TEXT_COLOR = new Color (0x222222); 322 private static final Color DISABLED_TEXT_COLOR = new Color (0x888888); 323 } 324 | Popular Tags |