KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics2D JavaDoc;
24 import java.awt.Image JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.RenderingHints JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.geom.GeneralPath JavaDoc;
30 import java.awt.image.BufferedImage JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.WeakHashMap JavaDoc;
33 import org.netbeans.api.visual.widget.Scene;
34
35 /**
36  * Class ExpanderWidget provides a simple icon widget for controlling the
37  * expanded/collapsed state of another widget. This widget can be added to
38  * any widget, and given an instance of ExpandableWidget, it can make that
39  * object expand or collapse each time the icon is clicked by the user.
40  * It is up to the ExpandableWidget implementation to determine how the
41  * size of the widget is altered. This class provides one or more methods
42  * for altering the widget size.
43  *
44  * @author radval
45  * @author Nathan Fiedler
46  */

47 public class ExpanderWidget extends ButtonWidget implements ActionListener JavaDoc {
48     /** The expand button image. */
49     private static final Image JavaDoc IMAGE_EXPAND = new BufferedImage JavaDoc(12, 12,
50             BufferedImage.TYPE_INT_ARGB);
51     /** The collapse button image. */
52     private static final Image JavaDoc IMAGE_COLLAPSE = new BufferedImage JavaDoc(12, 12,
53             BufferedImage.TYPE_INT_ARGB);
54     /** Cache of the expanded state of ExpandableWidget instances. This
55      * is used to restore the original state of an expandable if it is
56      * created again, say as a result of an undo/redo operation. */

57     private static Map JavaDoc<Object JavaDoc, Boolean JavaDoc> expandedCache;
58     /** The expandable (content) widget. */
59     private ExpandableWidget expandable;
60     /** True if expanded, false if collapsed. */
61     private boolean isExpanded;
62
63     static {
64         expandedCache = new WeakHashMap JavaDoc<Object JavaDoc, Boolean JavaDoc>();
65
66         // Create the expand image.
67
Graphics2D JavaDoc g2 = ((BufferedImage JavaDoc) IMAGE_EXPAND).createGraphics();
68         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
69                 RenderingHints.VALUE_ANTIALIAS_ON);
70         float w = IMAGE_EXPAND.getWidth(null);
71         float h = IMAGE_EXPAND.getHeight(null);
72         float r = Math.min(w, h) * 0.5f * 0.75f;
73         GeneralPath JavaDoc gp = new GeneralPath JavaDoc();
74         float dx = (float) (r * Math.cos(Math.toRadians(-30)));
75         float dy = (float) (r * Math.sin(Math.toRadians(-30)));
76         gp.moveTo(dx, dy);
77         gp.lineTo(0, r);
78         gp.lineTo(-dx, dy);
79         gp.lineTo(dx, dy);
80         gp.closePath();
81         g2.translate(w / 2, h / 2);
82         g2.setPaint(new Color JavaDoc(0x888888));
83         g2.fill(gp);
84
85         // Create the collapse image.
86
g2 = ((BufferedImage JavaDoc) IMAGE_COLLAPSE).createGraphics();
87         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
88                 RenderingHints.VALUE_ANTIALIAS_ON);
89         w = IMAGE_EXPAND.getWidth(null);
90         h = IMAGE_EXPAND.getHeight(null);
91         r = Math.min(w, h) * 0.5f * 0.75f;
92         gp = new GeneralPath JavaDoc();
93         dx = (float) (r * Math.cos(Math.toRadians(30)));
94         dy = (float) (r * Math.sin(Math.toRadians(30)));
95         gp.moveTo(dx, dy);
96         gp.lineTo(0, -r);
97         gp.lineTo(-dx, dy);
98         gp.lineTo(dx, dy);
99         gp.closePath();
100         g2.translate(w / 2, h / 2);
101         g2.setPaint(new Color JavaDoc(0x888888));
102         g2.fill(gp);
103     }
104
105     /**
106      * Creates a new instance of ExpanderWidget.
107      *
108      * @param scene the Scene to contain this widget.
109      * @param expandable the expandable widget this expander will control.
110      * @param expanded true if widget is initially expanded, false if collapsed.
111      */

112     public ExpanderWidget(Scene scene, ExpandableWidget expandable,
113             boolean expanded) {
114         super(scene, expanded ? IMAGE_COLLAPSE : IMAGE_EXPAND);
115         this.expandable = expandable;
116         isExpanded = expanded;
117         setMargin(new Insets JavaDoc(2, 2, 2, 2));
118         setActionListener(this);
119     }
120
121 // /**
122
// * Animate the preferred bounds of the given widget to the desired size.
123
// *
124
// * @param content widget whose size will be changed.
125
// * @param size the new size for the widget.
126
// */
127
// public void setExpandableSize(Widget content, Dimension size) {
128
// Dimension delta = null;
129
// Rectangle bounds = content.getBounds();
130
// if (isExpanded) {
131
// delta = new Dimension(bounds.width - size.width,
132
// bounds.height - size.height);
133
// } else {
134
// delta = new Dimension(size.width - bounds.width,
135
// size.height - bounds.height);
136
// }
137
//
138
//// XXX: note that the child widgets also need to have their size changed,
139
//// but the question is, how do we determine the appropriate sizing?
140
//
141
// // Resize the expandable widget.
142
// SceneAnimator anim = getScene().getSceneAnimator();
143
// anim.animatePreferredBounds(content, new Rectangle(
144
// 0, 0, size.width, size.height));
145
//
146
// // Adjust the size of the parents up to the root of the tree.
147
// Widget parent = content.getParentWidget();
148
// while (parent != null) {
149
// Rectangle pbounds = parent.getBounds();
150
// anim.animatePreferredBounds(parent, new Rectangle(
151
// 0, 0, pbounds.width + delta.width,
152
// pbounds.height + delta.height));
153
// parent = parent.getParentWidget();
154
// }
155
// }
156

157     public void actionPerformed(ActionEvent JavaDoc e) {
158         setExpanded(!isExpanded);
159     }
160
161     /**
162      * Indicates if this expander is expanded or collapsed.
163      *
164      * @return true if expanded, false if collapsed.
165      */

166     public boolean isExpanded() {
167         return isExpanded;
168     }
169
170     /**
171      * Retrieve the former expanded state of the given expandable. If
172      * the expandable state was not cached (or the cache has been cleaned
173      * by the garbage collector), this method returns the value of the
174      * <code>def</code> parameter.
175      *
176      * @param expandable the ExpandableWidget to query.
177      * @param def default value for the expanded state.
178      * @return true if expanded, false if collapsed.
179      */

180     public static boolean isExpanded(ExpandableWidget expandable, boolean def) {
181         Boolean JavaDoc val = expandedCache.get(expandable.hashKey());
182         return val != null ? val.booleanValue() : def;
183     }
184
185     /**
186      * Set the expanded state of the widget.
187      *
188      * @param expanded true to expand, false to collapse.
189      */

190     public void setExpanded(boolean expanded) {
191         // Save the state of the expandable in case it gets recreated later.
192
expandedCache.put(expandable.hashKey(), Boolean.valueOf(expanded));
193         isExpanded = expanded;
194         setIcon(isExpanded ? IMAGE_COLLAPSE : IMAGE_EXPAND);
195         if (isExpanded) {
196             expandable.expandWidget(this);
197         } else {
198             expandable.collapseWidget(this);
199         }
200     }
201 }
202
Popular Tags