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.Point;
046: import java.awt.Rectangle;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049:
050: import javax.swing.Icon;
051: import javax.swing.ImageIcon;
052:
053: import com.nwoods.jgo.JGoImage;
054: import com.nwoods.jgo.JGoObject;
055: import com.nwoods.jgo.JGoText;
056: import com.nwoods.jgo.JGoView;
057:
058: /**
059: * @author Ritesh Adval
060: * @version $Revision$
061: */
062: public class CellArea extends CanvasArea implements
063: PropertyChangeListener {
064:
065: /**
066: * TEXT Property
067: */
068: public static final String TEXT = "text";
069:
070: private BasicText text;
071: private JGoImage img;
072: //this should not be here
073: private java.net.URL url = getClass()
074: .getResource(
075: "/org/netbeans/modules/sql/framework/ui/resources/images/filter16.gif");
076:
077: /**
078: * create an instance of CellArea
079: */
080: public CellArea() {
081: super ();
082: this .setSelectable(false);
083: this .setResizable(false);
084: this .setDraggable(true);
085: this .set4ResizeHandles(false);
086:
087: }
088:
089: /**
090: * Creates a new instance of CellArea
091: *
092: * @param val -
093: */
094: public CellArea(String val) {
095: this ();
096: initializeText(val);
097: ImageIcon icon = new ImageIcon(url);
098: initializeImage(icon);
099: img.setVisible(false);
100: }
101:
102: /**
103: * get maximum height
104: *
105: * @return max height
106: */
107: public int getMaximumHeight() {
108: return this .getHeight();
109: }
110:
111: /**
112: * get the maximum width
113: *
114: * @return max width
115: */
116: public int getMaximumWidth() {
117: return text.getMaximumWidth();
118: }
119:
120: public int getMinimumWidth() {
121: return text.getMaximumWidth();
122: }
123:
124: /**
125: * initialize the text
126: *
127: * @param val text
128: */
129: void initializeText(String val) {
130: if (val == null) {
131: return;
132: }
133: text = new BasicText(val);
134: text.setAlignment(JGoText.ALIGN_LEFT);
135: text.setSelectable(false);
136: text.setResizable(false);
137: text.setDraggable(false);
138: text.set4ResizeHandles(false);
139: text.addPropertyChangeListener(this );
140: //text.setBkColor(new Color(0xFF, 0xFF, 0xCC));
141: this .addObjectAtTail(text);
142:
143: }
144:
145: /**
146: * create an instance of CellArea
147: *
148: * @param icon icon
149: */
150: public CellArea(Icon icon) {
151: this ();
152: initializeImage(icon);
153: }
154:
155: /**
156: * set the text in this cell
157: *
158: * @param val text
159: */
160: public void setText(String val) {
161: text.setOriginalText(val);
162: layoutChildren();
163: }
164:
165: /**
166: * get the text of this cell
167: *
168: * @return text
169: */
170: public String getText() {
171: if (text != null) {
172: return text.getText();
173: }
174:
175: return null;
176: }
177:
178: public String getOriginalText() {
179: if (text != null) {
180: return text.getOriginalText();
181: }
182:
183: return null;
184: }
185:
186: /**
187: * set whether text in this area is editable
188: *
189: * @param editable text is editable
190: */
191: public void setTextEditable(boolean editable) {
192: text.setSelectable(editable);
193: text.setEditable(editable);
194: text.setEditOnSingleClick(editable);
195:
196: }
197:
198: /**
199: * Is the text editable
200: *
201: * @return whether the text is editable
202: */
203: public boolean isTextEditable() {
204: return text.isEditable();
205: }
206:
207: /**
208: * set the text alignment
209: *
210: * @param alignment alignment
211: */
212: public void setTextAlignment(int alignment) {
213: text.setAlignment(alignment);
214: }
215:
216: /**
217: * initialize the image
218: *
219: * @param icon icon
220: */
221: void initializeImage(Icon icon) {
222: if (icon == null) {
223: return;
224: }
225:
226: img = new JGoImage();
227: ImageIcon imgIcon = (ImageIcon) icon;
228: img.loadImage(imgIcon.getImage(), false);
229:
230: img.setSize(imgIcon.getImage().getWidth(null), imgIcon
231: .getImage().getHeight(null));
232: img.setSelectable(false);
233: img.setResizable(false);
234: addObjectAtTail(img);
235:
236: }
237:
238: /**
239: * set the icon in the cell area
240: *
241: * @param icon icon
242: */
243: public void setIcon(Icon icon) {
244: if (icon == null) {
245: text.setVisible(true);
246: } else {
247: if (img == null) {
248: initializeImage(icon);
249: } else {
250: this .removeObject(img);
251: initializeImage(icon);
252: }
253: text.setVisible(false);
254: }
255:
256: }
257:
258: /**
259: * set the icon to be visible
260: *
261: * @param visible true or false
262: */
263: public void setIconVisible(boolean visible) {
264: if (img != null && text != null) {
265: img.setVisible(visible);
266: text.setVisible(!visible);
267: }
268: }
269:
270: /**
271: * override this method to handle the changes in the geometry of this area we will lay
272: * out all the cell again
273: *
274: * @param prevRect previous bounds rectangle
275: */
276: protected void geometryChange(Rectangle prevRect) {
277: // handle any size changes by repositioning all the items
278: if (prevRect.width != getWidth()
279: || prevRect.height != getHeight()) {
280: layoutChildren();
281: } else {
282: super .geometryChange(prevRect);
283: }
284: }
285:
286: /**
287: * layout the children of this cell area
288: */
289: public void layoutChildren() {
290: Rectangle rectangle = this .getBoundingRect();
291: if (text != null) {
292: text.setBoundingRect(rectangle);
293: }
294: if (img != null) {
295: img.setBoundingRect(rectangle);
296: }
297: }
298:
299: /**
300: * paint this area
301: *
302: * @param g Graphics2D
303: * @param view view
304: */
305: public void paint(java.awt.Graphics2D g, JGoView view) {
306: super .paint(g, view);
307: }
308:
309: /**
310: * Let single click on a label mean start editing that label. Because the label is not
311: * selectable, a mouse click will be passed on up to its parent, which will be this
312: * area.
313: *
314: * @param modifiers mouse event modifiers
315: * @param dc document point
316: * @param vc view point
317: * @param view view
318: * @return true if mouse click is handled by this area
319: */
320: public boolean doMouseClick(int modifiers, Point dc, Point vc,
321: JGoView view) {
322: JGoObject obj = view.pickDocObject(dc, false);
323: if (obj instanceof JGoText && obj.getLayer() != null
324: && obj.getLayer().isModifiable()) {
325: JGoText lab = (JGoText) obj;
326: if (lab.isEditable() && lab.isEditOnSingleClick()) {
327: lab.doStartEdit(view, vc);
328: return true;
329: }
330: }
331: return false;
332: }
333:
334: /**
335: * This method gets called when a bound property is changed.
336: *
337: * @param evt A PropertyChangeEvent object describing the event source and the
338: * property that has changed.
339: */
340: public void propertyChange(PropertyChangeEvent evt) {
341: if (evt.getPropertyName().equals(BasicText.ORIGINAL_TEXT)) {
342: this .firePropertyChangeEvent(TEXT, evt.getOldValue(), evt
343: .getNewValue());
344: }
345: }
346:
347: public void setBackGroundColor(Color bkColor) {
348: this .text.setBkColor(bkColor);
349: }
350:
351: public void setTextColor(Color textColor) {
352: this .text.setTextColor(textColor);
353: }
354:
355: public Color getTextColor() {
356: return text.getTextColor();
357: }
358: }
|