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.sql.framework.ui.graph.impl;
043:
044: import java.awt.Color;
045: import java.awt.Insets;
046: import java.awt.Point;
047: import java.awt.Rectangle;
048: import java.awt.event.ActionEvent;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051:
052: import javax.swing.Action;
053: import javax.swing.Icon;
054: import javax.swing.ImageIcon;
055:
056: import com.nwoods.jgo.JGoBrush;
057: import com.nwoods.jgo.JGoImage;
058: import com.nwoods.jgo.JGoPen;
059: import com.nwoods.jgo.JGoRectangle;
060: import com.nwoods.jgo.JGoView;
061:
062: /**
063: * @author Ritesh Adval
064: * @version $Revision$
065: */
066: public class ToolBarItemArea extends CanvasArea {
067:
068: private JGoImage itemImg;
069:
070: private JGoRectangle rect;
071:
072: private Action action;
073:
074: private String actionCommand;
075:
076: /** Creates a new instance of ToolBarItemArea */
077: public ToolBarItemArea() {
078:
079: this .setPickableBackground(false);
080: rect = new JGoRectangle();
081: rect = new JGoRectangle();
082: rect.setSelectable(false);
083: rect.setResizable(false);
084:
085: rect.setPen(JGoPen.lightGray);
086: rect.setBrush(JGoBrush.makeStockBrush(Color.white));
087: addObjectAtHead(rect);
088:
089: this .insets = new Insets(1, 1, 1, 1);
090: }
091:
092: /**
093: * initialize the toolbar item
094: *
095: * @param icon icon
096: */
097: public void initialize(Icon icon) {
098: //add title image
099: itemImg = new JGoImage();
100: ImageIcon imgIcon = (ImageIcon) icon;
101: itemImg.loadImage(imgIcon.getImage(), false);
102:
103: itemImg.setSize(imgIcon.getImage().getWidth(null), imgIcon
104: .getImage().getHeight(null));
105: itemImg.setSelectable(false);
106: itemImg.setResizable(false);
107: addObjectAtTail(itemImg);
108:
109: }
110:
111: /**
112: * create a new instance of toolbar item
113: *
114: * @param action action
115: */
116: public ToolBarItemArea(Action action) {
117: this ();
118: this .action = action;
119: ItemPropertyChangeListener listener = new ItemPropertyChangeListener();
120: action.addPropertyChangeListener(listener);
121:
122: Icon icon = (Icon) action.getValue(Action.SMALL_ICON);
123: //String toolTip = (String) action.getValue(Action.SHORT_DESCRIPTION);
124: actionCommand = (String) action
125: .getValue(Action.ACTION_COMMAND_KEY);
126: initialize(icon);
127: }
128:
129: /**
130: * handle mouse click
131: *
132: * @param modifiers mouse event modifiers
133: * @param dc document point
134: * @param vc view point
135: * @param view view
136: * @return bool
137: */
138: public boolean doMouseClick(int modifiers, Point dc, Point vc,
139: JGoView view) {
140:
141: ActionEvent actionEvent = new ActionEvent(this ,
142: ActionEvent.ACTION_PERFORMED, actionCommand);
143:
144: action.actionPerformed(actionEvent);
145: return false;
146: }
147:
148: /**
149: * override this method to handle the changes in the geometry of this area we will lay
150: * out all images again
151: *
152: * @param prevRect previous bounds rectangle
153: */
154: protected void geometryChange(Rectangle prevRect) {
155: // handle any size changes by repositioning all the items
156: if (prevRect.width != getWidth()
157: || prevRect.height != getHeight()) {
158: layoutChildren();
159: } else {
160: super .geometryChange(prevRect);
161: }
162: }
163:
164: /**
165: * layout all the children of this table area
166: */
167: public void layoutChildren() {
168:
169: rect.setBoundingRect(this .getBoundingRect());
170: Insets insets1 = getInsets();
171:
172: //get the bounding rectangle of this table area
173: int x = getLeft() + insets1.left;
174: int y = getTop() + insets1.top;
175: int width = getWidth() - insets1.left - insets1.right;
176: int height = getHeight() - insets1.top - insets1.bottom;
177:
178: itemImg.setBoundingRect(x, y, width, height);
179: }
180:
181: class ItemPropertyChangeListener implements PropertyChangeListener {
182:
183: /**
184: * listen for property change event
185: *
186: * @param evt PropertyChangeEvent
187: */
188: public void propertyChange(PropertyChangeEvent evt) {
189:
190: }
191:
192: }
193: }
|