KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > grapheditor > widget > ButtonWidget


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor.widget;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.GradientPaint JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.Paint JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.geom.RoundRectangle2D JavaDoc;
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 /**
41  *
42  * @author anjeleevich
43  */

44 public class ButtonWidget extends Widget {
45
46     private ImageWidget imageWidget = null;
47     private LabelWidget labelWidget = null;
48     
49     
50     private Insets JavaDoc margin = new Insets JavaDoc(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 JavaDoc actionListener = null;
60
61     
62     public ButtonWidget(Scene scene, String JavaDoc text) {
63         this(scene, null, text);
64     }
65     
66     
67     public ButtonWidget(Scene scene, Image JavaDoc icon) {
68         this(scene, icon, null);
69     }
70     
71     
72     public ButtonWidget(Scene scene, Image JavaDoc icon, String JavaDoc 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 JavaDoc getText() {
87         return (labelWidget == null) ? null : labelWidget.getLabel();
88     }
89     
90     
91     public Image JavaDoc getIcon() {
92         return (imageWidget == null) ? null : imageWidget.getImage();
93     }
94     
95     
96     public void setText(String JavaDoc text) {
97         setIconAndText(getIcon(), text);
98     }
99     
100     
101     public void setIcon(Image JavaDoc image) {
102         setIconAndText(image, getText());
103     }
104     
105     
106     public Insets JavaDoc getMargin() {
107         return new Insets JavaDoc(margin.top, margin.left, margin.bottom, margin.right);
108     }
109     
110     
111     public void setMargin(Insets JavaDoc 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 JavaDoc icon, String JavaDoc text) {
119         removeButtonChildren();
120         
121         String JavaDoc oldText = getText();
122         Image JavaDoc 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 JavaDoc actionListener) {
179         this.actionListener = actionListener;
180     }
181     
182     
183     private void fireActionPerformed() {
184         if ((actionListener != null) && enabled) {
185             actionListener.actionPerformed(new ActionEvent JavaDoc(this, 0,
186                     "button-pressed")); // NOI18N
187
}
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     /*
229      * Changed method name so that it doesnt clash with Widget.setEnabled.
230      */

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     /*
245      * Changed method name so that it doesnt clash with Widget.isEnabled.
246      */

247     public boolean isButtonEnabled() {
248         return enabled;
249     }
250     
251     
252     private class ButtonBorder implements Border {
253         public ButtonBorder() {}
254         
255         public Insets JavaDoc getInsets() {
256             return new Insets JavaDoc(margin.top, margin.left,
257                     margin.bottom, margin.right);
258         }
259
260         public void paint(Graphics2D JavaDoc g2, Rectangle JavaDoc rect) {
261             Paint JavaDoc oldPaing = g2.getPaint();
262             
263             if (enabled) {
264                 g2.setPaint(BORDER_COLOR);
265                 g2.fill(new RoundRectangle2D.Double JavaDoc(rect.x, rect.y,
266                             rect.width, rect.height, 6, 6));
267
268                 if (pressed) {
269                     g2.setPaint(new Color JavaDoc(0xCCCCCC));
270                 } else {
271                     g2.setPaint(new GradientPaint JavaDoc(
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 JavaDoc(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 JavaDoc(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 JavaDoc(rect.x, rect.y,
287                             rect.width, rect.height, 6, 6));
288                 
289                 g2.setPaint(BACKGROUND_COLOR_DISABLED);
290                 g2.fill(new RoundRectangle2D.Double JavaDoc(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 JavaDoc grayFilter(Color JavaDoc 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 JavaDoc(y, y, y);
315     }
316     
317     private static final Color JavaDoc BORDER_COLOR = new Color JavaDoc(0x7F9DB9);
318     private static final Color JavaDoc BACKGROUND_COLOR_1 = new Color JavaDoc(0xD2D2DD);
319     private static final Color JavaDoc BACKGROUND_COLOR_2 = new Color JavaDoc(0xF8F8F8);
320     private static final Color JavaDoc BACKGROUND_COLOR_DISABLED = new Color JavaDoc(0xE4E4E4);
321     private static final Color JavaDoc ENABLED_TEXT_COLOR = new Color JavaDoc(0x222222);
322     private static final Color JavaDoc DISABLED_TEXT_COLOR = new Color JavaDoc(0x888888);
323 }
324  
Popular Tags