001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.design.decoration.components;
020:
021: import java.awt.Color;
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Point;
026: import java.awt.Rectangle;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.HierarchyEvent;
030: import java.awt.event.HierarchyListener;
031: import java.util.ArrayList;
032: import java.util.List;
033: import javax.swing.ButtonModel;
034: import javax.swing.Icon;
035: import javax.swing.JToggleButton;
036: import javax.swing.SwingUtilities;
037: import org.netbeans.modules.bpel.design.DesignView;
038: import org.netbeans.modules.bpel.design.OverlayPanel;
039:
040: /**
041: * @author aa160298
042: */
043: public class AbstractGlassPaneButton extends JToggleButton implements
044: ActionListener, HierarchyListener, DecorationComponent {
045:
046: public AbstractGlassPaneButton(Icon icon) {
047: this (icon, null, false, null);
048: }
049:
050: public AbstractGlassPaneButton(Icon icon, String text,
051: boolean editable, ActionListener actionListener) {
052: super (icon);
053: myIcon = icon;
054: myGlassPane = new GlassPane(text, actionListener, editable);
055: myActionListener = actionListener;
056: addActionListener(this );
057:
058: setOpaque(false);
059: setBorder(null);
060: setRolloverEnabled(true);
061: setContentAreaFilled(false);
062: setFocusable(false);
063:
064: updatePreferredSize();
065: myGlassPane.addHierarchyListener(this );
066: }
067:
068: protected void updatePreferredSize() {
069: setPreferredSize(new Dimension(myIcon.getIconWidth() + 6,
070: myIcon.getIconHeight() + 6));
071: }
072:
073: public void actionPerformed(ActionEvent e) {
074: if (!isGlassPaneShown()) {
075: showGlassPane();
076: } else {
077: hideGlassPane();
078: }
079: }
080:
081: public void setBounds(int x, int y, int w, int h) {
082: super .setBounds(x, y, w, h);
083:
084: if (isGlassPaneShown()) {
085: updateGlassPaneBounds();
086: }
087: }
088:
089: private void showGlassPane() {
090: getDesignView().getOverlayView().add(myGlassPane);
091: updateGlassPaneBounds();
092: myGlassPane.scrollRectToVisible(new Rectangle(0, 0, myGlassPane
093: .getWidth(), myGlassPane.getHeight()));
094: }
095:
096: protected void addTitle(Icon icon, String text, Color color) {
097: myGlassPane.addHeader(icon, text, color);
098: }
099:
100: private void hideGlassPane() {
101: Container parent = myGlassPane.getParent();
102: if (parent != null) {
103: parent.remove(myGlassPane);
104: }
105:
106: DesignView view = getDesignView();
107: if (view != null) {
108: view.revalidate();
109: view.repaint();
110: }
111:
112: if (myActionListener != null) {
113: SwingUtilities.invokeLater(new Runnable() {
114: public void run() {
115: myActionListener.actionPerformed(new ActionEvent(
116: myGlassPane.getText(), 0, null));
117: }
118: });
119: }
120: }
121:
122: private DesignView getDesignView() {
123: return (DesignView) SwingUtilities.getAncestorOfClass(
124: DesignView.class, this );
125: }
126:
127: private boolean isGlassPaneShown() {
128: return myGlassPane.getParent() != null;
129: }
130:
131: private void updateGlassPaneBounds() {
132: DesignView designView = getDesignView();
133: Point coords = SwingUtilities.convertPoint(this ,
134: getWidth() + 1, getHeight() / 2, designView);
135: Dimension size = myGlassPane.getPreferredSize();
136: myGlassPane.setBounds(coords.x, coords.y, size.width,
137: size.height);
138: designView.revalidate();
139: designView.repaint();
140: }
141:
142: public void hierarchyChanged(HierarchyEvent e) {
143: if (e.getSource() == this ) {
144: if ((getParent() == null) && isGlassPaneShown()) {
145: hideGlassPane();
146: }
147: } else if (e.getSource() == myGlassPane) {
148: if (myGlassPane.getParent() == null) {
149: setSelected(false);
150: } else {
151: setSelected(true);
152: }
153: }
154: }
155:
156: protected void paintComponent(Graphics g) {
157: ButtonModel model = getModel();
158:
159: if (model.isPressed()) {
160: ButtonRenderer.paintButton(this , g,
161: ButtonRenderer.PRESSED_FILL_COLOR, false,
162: ButtonRenderer.PRESSED_BORDER_COLOR,
163: ButtonRenderer.PRESSED_STROKE_WIDTH, myIcon);
164: } else if (model.isRollover()) {
165: ButtonRenderer.paintButton(this , g,
166: ButtonRenderer.ROLLOVER_FILL_COLOR, true,
167: ButtonRenderer.ROLLOVER_BORDER_COLOR,
168: ButtonRenderer.ROLLOVER_STROKE_WIDTH, myIcon);
169:
170: } else if (model.isSelected()) {
171: ButtonRenderer.paintButton(this , g, BACKGROUND, false,
172: ButtonRenderer.PRESSED_BORDER_COLOR,
173: ButtonRenderer.PRESSED_STROKE_WIDTH, myIcon);
174: } else {
175: ButtonRenderer.paintButton(this , g, BACKGROUND, false,
176: null, ButtonRenderer.NORMAL_STROKE_WIDTH, myIcon);
177: }
178: }
179:
180: protected void setMyIcon(Icon icon) {
181: myIcon = icon;
182: }
183:
184: protected Icon getMyIcon() {
185: return myIcon;
186: }
187:
188: protected GlassPane getGlassPane() {
189: return myGlassPane;
190: }
191:
192: private Icon myIcon;
193: private GlassPane myGlassPane;
194: private ActionListener myActionListener;
195: private static final Color BACKGROUND = new Color(0xCCFFFFFF, true);
196: }
|