001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema.abe;
043:
044: import java.awt.Color;
045: import java.awt.Dimension;
046: import java.awt.Graphics;
047: import java.awt.Graphics2D;
048: import java.awt.dnd.DropTargetDragEvent;
049: import java.awt.dnd.DropTargetDropEvent;
050: import java.awt.dnd.DropTargetEvent;
051: import java.util.ArrayList;
052: import java.util.List;
053: import javax.swing.SwingConstants;
054: import org.netbeans.modules.xml.schema.abe.palette.DnDHelper;
055:
056: /**
057: *
058: *
059: * @author Todd Fast, todd.fast@sun.com
060: */
061: public class TweenerPanel extends ABEBaseDropPanel {
062: private static final long serialVersionUID = 7526472295622776147L;
063: private TranslucentLabel dropInfoLabel = new TranslucentLabel(
064: new javax.swing.ImageIcon(
065: getClass()
066: .getResource(
067: "/org/netbeans/modules/xml/schema/abe/resources/bulb.png")));
068:
069: /**
070: *
071: *
072: */
073: public TweenerPanel(int orientation, InstanceUIContext context) {
074: super (context);
075: this .orientation = orientation;
076: initialize();
077: //setBorder(new LineBorder(Color.BLACK));
078: }
079:
080: ////////////////////////////////////////////////////////////////////////////
081: // Class members
082: ////////////////////////////////////////////////////////////////////////////
083: private static final int HBAR_NOMINAL_WIDTH = 500;
084: private static final int HBAR_MAX_HEIGHT = 2;
085:
086: private static final int VBAR_MAX_WIDTH = 10;
087: private static final int VBAR_NOMINAL_HEIGHT = StartTagPanel
088: .getTagHeight();
089:
090: private static final int PADDING = 3;
091:
092: private static final int EXPAND_FACTOR = 3;
093:
094: List<TweenerListener> tweenerListeners = new ArrayList<TweenerListener>();
095:
096: /**
097: *
098: *
099: */
100: private void initialize() {
101: setOpaque(false);
102:
103: if (getOrientation() == SwingConstants.HORIZONTAL)
104: _setSize(HBAR_NOMINAL_WIDTH, HBAR_MAX_HEIGHT);
105: else
106: _setSize(VBAR_MAX_WIDTH, VBAR_NOMINAL_HEIGHT);
107: }
108:
109: /**
110: *
111: *
112: */
113: public int getOrientation() {
114: return orientation;
115: }
116:
117: /**
118: *
119: *
120: */
121: private void _setSize(int w, int h) {
122: setPreferredSize(new Dimension(w, h));
123: setMinimumSize(new Dimension(w, h));
124: setMaximumSize(new Dimension(w, h));
125: revalidate();
126: }
127:
128: ////////////////////////////////////////////////////////////////////////////
129: // Accessors and mutators
130: ////////////////////////////////////////////////////////////////////////////
131:
132: /**
133: *
134: *
135: */
136: protected void handleActive(boolean value) {
137: boolean oldValue = active;
138: if (oldValue != value) {
139: active = value;
140: if (!active)
141: removeDropInfoLabel();
142: repaint();
143: }
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147: // Paint methods
148: ////////////////////////////////////////////////////////////////////////////
149:
150: /**
151: *
152: *
153: */
154: public void paint(Graphics g) {
155: Graphics2D g2d = (Graphics2D) g;
156: super .paint(g2d);
157: if (isActive()) {
158: if (getOrientation() == SwingConstants.HORIZONTAL) {
159: // Horizontal
160: int x = 0;
161: int y = (getHeight() / 2) - 1; //-2;
162: int w = getVisibleRect().width;
163:
164: g2d.setColor(Color.BLACK);
165:
166: int left = PADDING;
167: int right = w - PADDING;
168:
169: // Left cap
170: g2d.drawLine(left, y - 2, left, y + 3);
171: g2d.drawLine(left + 1, y - 1, left + 1, y + 2);
172:
173: // Right cap
174: g2d.drawLine(right, y - 2, right, y + 3);
175: g2d.drawLine(right - 1, y - 1, right - 1, y + 2);
176:
177: // Horizontal line
178: g2d.drawLine(left, y, right, y);
179: g2d.drawLine(left, y + 1, right, y + 1);
180: } else {
181: // Vertical
182: int x = (getWidth() / 2) - 1;
183: int h = getHeight();
184:
185: g2d.setColor(Color.BLACK);
186:
187: int top = PADDING;
188: int bottom = h - (PADDING * 4);
189:
190: // Top cap
191: g2d.drawLine(x - 2, top, x + 3, top);
192: g2d.drawLine(x - 1, top + 1, x + 2, top + 1);
193:
194: // Bottom cap
195: g2d.drawLine(x - 2, bottom, x + 3, bottom);
196: g2d.drawLine(x - 1, bottom - 1, x + 2, bottom - 1);
197:
198: // Vertical line
199: g2d.drawLine(x, top, x, bottom);
200: g2d.drawLine(x + 1, top, x + 1, bottom);
201: }
202: }
203: }
204:
205: public void addTweenerListener(TweenerListener tl) {
206: tweenerListeners.add(tl);
207: }
208:
209: private int orientation;
210: private boolean active;
211:
212: private void showExpanded() {
213: Dimension dim = new Dimension(HBAR_NOMINAL_WIDTH,
214: HBAR_MAX_HEIGHT * EXPAND_FACTOR);
215: setPreferredSize(dim);
216: setMinimumSize(dim);
217: revalidate();
218: //getParent().validate();
219: }
220:
221: private void showCollapsed() {
222: Dimension dim = new Dimension(HBAR_NOMINAL_WIDTH,
223: HBAR_MAX_HEIGHT);
224: setPreferredSize(dim);
225: setMinimumSize(dim);
226: revalidate();
227: //getParent().validate();
228: }
229:
230: public void drop(DropTargetDropEvent event) {
231: context.setUserInducedEventMode(true);
232: try {
233: for (TweenerListener tl : tweenerListeners) {
234: if (!tl.dragAccept(DnDHelper
235: .getDraggedPaletteItem(event))) {
236: event.rejectDrop();
237: return;
238: }
239: }
240:
241: for (TweenerListener tl : tweenerListeners) {
242: tl.drop(DnDHelper.getDraggedPaletteItem(event));
243: }
244: if (orientation == SwingConstants.HORIZONTAL) {
245: showCollapsed();
246: for (TweenerListener tl : tweenerListeners) {
247: tl.dragExited();
248: }
249: }
250: } finally {
251: context.setUserInducedEventMode(false);
252: }
253: }
254:
255: public void dragExit(DropTargetEvent event) {
256: if (orientation == SwingConstants.HORIZONTAL) {
257: showCollapsed();
258: }
259: for (TweenerListener tl : tweenerListeners) {
260: tl.dragExited();
261: }
262: }
263:
264: public void dragOver(DropTargetDragEvent event) {
265: }
266:
267: public void dragEnter(DropTargetDragEvent event) {
268: if (orientation == SwingConstants.HORIZONTAL) {
269: showExpanded();
270: }
271: for (TweenerListener tl : tweenerListeners) {
272: if (!tl.dragAccept(DnDHelper.getDraggedPaletteItem(event))) {
273: //addDropInfoLabel();
274: event.rejectDrag();
275: return;
276: }
277: }
278: for (TweenerListener tl : tweenerListeners) {
279: tl.dragEntered(DnDHelper.getDraggedPaletteItem(event));
280: }
281: addDropInfoLabel();
282: }
283:
284: //NBGlassPaneAccessSupport gpSupport;
285:
286: private void addDropInfoLabel() {
287: String infoText = getDropInfoText();
288: if (infoText != null) {
289: UIUtilities.showBulbMessageFor(infoText, context, this );
290: }
291: }
292:
293: private void removeDropInfoLabel() {
294: NBGlassPaneAccessSupport.disposeNBGlassPane();
295: }
296:
297: private String dropInfoText;
298:
299: public String getDropInfoText() {
300: return dropInfoText;
301: }
302:
303: public void setDropInfoText(String dropInfoText) {
304: this .dropInfoText = dropInfoText;
305: }
306:
307: public void accept(UIVisitor visitor) {
308: //does not contribute for UI traversal
309: }
310:
311: }
|