KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > abe > TweenerPanel


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.schema.abe;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Graphics2D JavaDoc;
26 import java.awt.dnd.DropTargetDragEvent JavaDoc;
27 import java.awt.dnd.DropTargetDropEvent JavaDoc;
28 import java.awt.dnd.DropTargetEvent JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.SwingConstants JavaDoc;
32 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
33
34 /**
35  *
36  *
37  * @author Todd Fast, todd.fast@sun.com
38  */

39 public class TweenerPanel extends ABEBaseDropPanel {
40     private static final long serialVersionUID = 7526472295622776147L;
41     private TranslucentLabel dropInfoLabel = new TranslucentLabel(new javax.swing.ImageIcon JavaDoc(getClass().
42             getResource("/org/netbeans/modules/xml/schema/abe/resources/bulb.png")));
43     /**
44      *
45      *
46      */

47     public TweenerPanel(int orientation, InstanceUIContext context) {
48         super(context);
49         this.orientation=orientation;
50         initialize();
51         //setBorder(new LineBorder(Color.BLACK));
52
}
53     
54     
55     ////////////////////////////////////////////////////////////////////////////
56
// Class members
57
////////////////////////////////////////////////////////////////////////////
58
private static final int HBAR_NOMINAL_WIDTH = 500;
59     private static final int HBAR_MAX_HEIGHT=2;
60     
61     private static final int VBAR_MAX_WIDTH=10;
62     private static final int VBAR_NOMINAL_HEIGHT = StartTagPanel.getTagHeight();
63     
64     private static final int PADDING=3;
65     
66     private static final int EXPAND_FACTOR = 3;
67     
68     List JavaDoc<TweenerListener> tweenerListeners = new ArrayList JavaDoc<TweenerListener>();
69     
70     /**
71      *
72      *
73      */

74     private void initialize() {
75         setOpaque(false);
76         
77         if (getOrientation()==SwingConstants.HORIZONTAL)
78             _setSize(HBAR_NOMINAL_WIDTH, HBAR_MAX_HEIGHT);
79         else
80             _setSize(VBAR_MAX_WIDTH, VBAR_NOMINAL_HEIGHT);
81     }
82     
83     
84     /**
85      *
86      *
87      */

88     public int getOrientation() {
89         return orientation;
90     }
91     
92     
93     /**
94      *
95      *
96      */

97     private void _setSize(int w, int h) {
98         setPreferredSize(new Dimension JavaDoc(w,h));
99         setMinimumSize(new Dimension JavaDoc(w,h));
100         setMaximumSize(new Dimension JavaDoc(w,h));
101         revalidate();
102     }
103     
104     
105     
106     
107     ////////////////////////////////////////////////////////////////////////////
108
// Accessors and mutators
109
////////////////////////////////////////////////////////////////////////////
110

111     /**
112      *
113      *
114      */

115     protected void handleActive(boolean value) {
116         boolean oldValue=active;
117         if (oldValue!=value) {
118             active=value;
119             if(!active)
120                 removeDropInfoLabel();
121             repaint();
122         }
123     }
124     
125     
126     
127     
128     ////////////////////////////////////////////////////////////////////////////
129
// Paint methods
130
////////////////////////////////////////////////////////////////////////////
131

132     /**
133      *
134      *
135      */

136     public void paint(Graphics JavaDoc g) {
137         Graphics2D JavaDoc g2d=(Graphics2D JavaDoc)g;
138         super.paint(g2d);
139         if (isActive()) {
140             if (getOrientation()==SwingConstants.HORIZONTAL) {
141                 // Horizontal
142
int x = 0;
143                 int y = (getHeight()/2) -1 ; //-2;
144
int w = getVisibleRect().width;
145                 
146                 g2d.setColor(Color.BLACK);
147                 
148                 int left=PADDING;
149                 int right=w-PADDING;
150                 
151                 // Left cap
152
g2d.drawLine(left,y-2,left,y+3);
153                 g2d.drawLine(left+1,y-1,left+1,y+2);
154                 
155                 // Right cap
156
g2d.drawLine(right,y-2,right,y+3);
157                 g2d.drawLine(right-1,y-1,right-1,y+2);
158                 
159                 // Horizontal line
160
g2d.drawLine(left,y,right,y);
161                 g2d.drawLine(left,y+1,right,y+1);
162             } else {
163                 // Vertical
164
int x = (getWidth() / 2) - 1;
165                 int h = getHeight();
166                 
167                 g2d.setColor(Color.BLACK);
168                 
169                 int top=PADDING;
170                 int bottom = h - (PADDING * 4) ;
171                 
172                 // Top cap
173
g2d.drawLine(x-2,top,x+3,top);
174                 g2d.drawLine(x-1,top+1,x+2,top+1);
175                 
176                 // Bottom cap
177
g2d.drawLine(x-2,bottom,x+3,bottom);
178                 g2d.drawLine(x-1,bottom-1,x+2,bottom-1);
179                 
180                 // Vertical line
181
g2d.drawLine(x,top,x,bottom);
182                 g2d.drawLine(x+1,top,x+1,bottom);
183             }
184         }
185     }
186     
187     public void addTweenerListener(TweenerListener tl){
188         tweenerListeners.add(tl);
189     }
190     
191     
192     private int orientation;
193     private boolean active;
194     
195     
196     private void showExpanded(){
197         Dimension JavaDoc dim = new Dimension JavaDoc(HBAR_NOMINAL_WIDTH, HBAR_MAX_HEIGHT * EXPAND_FACTOR);
198         setPreferredSize(dim);
199         setMinimumSize(dim);
200         revalidate();
201         //getParent().validate();
202
}
203     
204     private void showCollapsed(){
205         Dimension JavaDoc dim = new Dimension JavaDoc(HBAR_NOMINAL_WIDTH, HBAR_MAX_HEIGHT);
206         setPreferredSize(dim);
207         setMinimumSize(dim);
208         revalidate();
209         //getParent().validate();
210
}
211     
212     public void drop(DropTargetDropEvent JavaDoc event) {
213         context.setUserInducedEventMode(true);
214         try{
215             for(TweenerListener tl: tweenerListeners){
216                 if(!tl.dragAccept(DnDHelper.getDraggedPaletteItem(event))){
217                     event.rejectDrop();
218                     return;
219                 }
220             }
221             
222             for(TweenerListener tl: tweenerListeners){
223                 tl.drop(DnDHelper.getDraggedPaletteItem(event));
224             }
225             if(orientation == SwingConstants.HORIZONTAL){
226                 showCollapsed();
227                 for(TweenerListener tl: tweenerListeners){
228                     tl.dragExited();
229                 }
230             }
231         }finally{
232             context.setUserInducedEventMode(false);
233         }
234     }
235     
236     public void dragExit(DropTargetEvent JavaDoc event) {
237         if(orientation == SwingConstants.HORIZONTAL){
238             showCollapsed();
239         }
240         for(TweenerListener tl: tweenerListeners){
241             tl.dragExited();
242         }
243     }
244     
245     public void dragOver(DropTargetDragEvent JavaDoc event) {
246     }
247     
248     public void dragEnter(DropTargetDragEvent JavaDoc event) {
249         if(orientation == SwingConstants.HORIZONTAL){
250             showExpanded();
251         }
252         for(TweenerListener tl: tweenerListeners){
253             if(!tl.dragAccept(DnDHelper.getDraggedPaletteItem(event))){
254                 //addDropInfoLabel();
255
event.rejectDrag();
256                 return;
257             }
258         }
259         for(TweenerListener tl: tweenerListeners){
260             tl.dragEntered(DnDHelper.getDraggedPaletteItem(event));
261         }
262         addDropInfoLabel();
263     }
264     
265     
266     //NBGlassPaneAccessSupport gpSupport;
267

268     
269     private void addDropInfoLabel() {
270         String JavaDoc infoText = getDropInfoText();
271         if(infoText != null){
272             UIUtilities.showBulbMessageFor(infoText, context, this);
273         }
274     }
275     
276     private void removeDropInfoLabel() {
277         NBGlassPaneAccessSupport.disposeNBGlassPane();
278     }
279     
280     private String JavaDoc dropInfoText;
281     public String JavaDoc getDropInfoText() {
282         return dropInfoText;
283     }
284     
285     public void setDropInfoText(String JavaDoc dropInfoText) {
286         this.dropInfoText = dropInfoText;
287     }
288
289     public void accept(UIVisitor visitor) {
290         //does not contribute for UI traversal
291
}
292     
293     
294 }
295
Popular Tags