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: package org.netbeans.modules.sql.framework.ui.graph.impl;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Cursor;
046: import java.awt.Graphics2D;
047: import java.awt.Point;
048: import java.awt.font.FontRenderContext;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.util.ArrayList;
052:
053: import com.nwoods.jgo.JGoGlobal;
054: import com.nwoods.jgo.JGoText;
055: import com.nwoods.jgo.JGoView;
056:
057: /**
058: * @author Ritesh Adval
059: * @version $Revision$
060: */
061: public class BasicText extends JGoText {
062:
063: /**
064: * ORIGINAL TEXT Property
065: */
066: public static final String ORIGINAL_TEXT = "original_text";
067:
068: private String originalText;
069: private boolean showDot = true;
070: private ArrayList listeners = new ArrayList();
071: private boolean isEditing = false;
072:
073: /**
074: * create a new instance of text area
075: *
076: * @param text text
077: */
078: public BasicText(String text) {
079: super (text);
080: setAutoResize(false);
081: setResizable(true);
082: setSelectable(false);
083: setOriginalText(text);
084: setDefaultFontSize(11);
085: setDefaultFontFaceName("SansSerif");
086: setClipping(true);
087: }
088:
089: /**
090: * set the original text. Original text is something that will be shown if this text
091: * area has enough space to show all of it
092: *
093: * @param text text
094: */
095: public void setOriginalText(String text) {
096: String oldVal = this .originalText;
097: this .originalText = text;
098: //also set text on the jgo text
099: this .setText(text);
100: firePropertyChangeEvent(ORIGINAL_TEXT, oldVal, originalText);
101: }
102:
103: /**
104: * get the original text. This text area modifies the text by adding ... so if we
105: * expand the text area we should see the full length text
106: *
107: * @return original text
108: */
109: public String getOriginalText() {
110: return this .originalText;
111: }
112:
113: /**
114: * set the flag so that this text are will calculate for ...
115: *
116: * @param show whether a calculation for ... is to be done
117: */
118: public void setShowDot(boolean show) {
119: showDot = show;
120: }
121:
122: /**
123: * get the minimum width
124: *
125: * @return minimum width
126: */
127: public int getMinimumWidth() {
128: //we at least want to show ...
129: return computeWidthFor("...");
130: }
131:
132: public int getMaximumWidth() {
133: return computeWidthFor(this .getOriginalText());
134: }
135:
136: /**
137: * @return
138: */
139: private int computeWidthFor(String text) {
140: int w = 10; // minimum width if we can't obtain required components for the
141: // computation.
142:
143: Component somecomp = JGoGlobal.getComponent();
144: if (somecomp == null) {
145: return w;
146: }
147:
148: Graphics2D g2 = (Graphics2D) somecomp.getGraphics();
149: if (g2 == null) {
150: return w;
151: }
152:
153: try {
154: FontRenderContext frc = g2.getFontRenderContext();
155: if (text.equals("")) {
156: return 0;
157: }
158:
159: double dwidth = getFont().getStringBounds(text, frc)
160: .getWidth();
161: w = (int) Math.ceil(dwidth);
162: } finally {
163: g2.dispose();
164: }
165:
166: return w;
167: }
168:
169: public void doEndEdit() {
170: isEditing = false;
171: this .setOriginalText(this .getText());
172: super .doEndEdit();
173: }
174:
175: /**
176: * paint this area
177: *
178: * @param g Graphics2D
179: * @param view view
180: */
181: public void paint(java.awt.Graphics2D g, JGoView view) {
182: if (showDot) {
183: GraphUtility.adjustText(g, this );
184: }
185: super .paint(g, view);
186: }
187:
188: /**
189: * add a property change listener
190: *
191: * @param l listener
192: */
193: public void addPropertyChangeListener(PropertyChangeListener l) {
194: listeners.add(l);
195: }
196:
197: /**
198: * remove a property change listener
199: *
200: * @param l listener
201: */
202: public void removePropertyChangeListener(PropertyChangeListener l) {
203: listeners.remove(l);
204: }
205:
206: protected synchronized void firePropertyChangeEvent(
207: String propertyName, Object oldValue, Object newValue) {
208: PropertyChangeEvent evt = new PropertyChangeEvent(this ,
209: propertyName, oldValue, newValue);
210:
211: for (int i = 0; i < listeners.size(); i++) {
212: PropertyChangeListener l = (PropertyChangeListener) listeners
213: .get(i);
214: l.propertyChange(evt);
215: }
216: }
217:
218: /**
219: * Change the cursor at the port
220: *
221: * @param flags
222: */
223: public boolean doUncapturedMouseMove(int flags, Point dc, Point vc,
224: JGoView view) {
225: if (this .isEditable() && getLayer() != null
226: && getLayer().isModifiable()) {
227: view.setCursor(Cursor
228: .getPredefinedCursor(Cursor.TEXT_CURSOR));
229: return true;
230: }
231: return false;
232: }
233:
234: public void setBackgroundColor(Color c) {
235: this .setBkColor(c);
236: }
237:
238: public String getText() {
239: if (isEditing) {
240: return this .getOriginalText();
241: }
242: return super .getText();
243: }
244:
245: /**
246: * Programmatically start the editing of the text for the user. This will cause a
247: * JTextComponent to appear.
248: * <p>
249: * This is responsible for starting a transaction.
250: *
251: * @param view the view in which the user will be editing
252: * @param vc the view coordinates where the user may have clicked
253: */
254: public void doStartEdit(JGoView view, Point vc) {
255: isEditing = true;
256: //we need to set text again to fix array index out of bound exception
257: this.setText(this.getOriginalText());
258: super.doStartEdit(view, vc);
259: }
260:
261: }
|