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.event.ItemEvent;
046: import java.awt.event.ItemListener;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Vector;
051:
052: import javax.swing.JComboBox;
053:
054: import org.netbeans.modules.sql.framework.ui.graph.IGraphFieldNode;
055: import org.netbeans.modules.sql.framework.ui.graph.IGraphNode;
056: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
057:
058: import com.nwoods.jgo.JGoBrush;
059: import com.nwoods.jgo.JGoPen;
060: import com.nwoods.jgo.JGoRectangle;
061: import com.nwoods.jgo.JGoText;
062:
063: /**
064: * @author Ritesh Adval
065: * @version $Revision$
066: */
067: public class BasicComboBoxArea extends CanvasArea implements
068: ItemListener, IGraphFieldNode {
069:
070: private BasicCellArea cellArea;
071: private ComboBoxArea cbArea;
072: private JGoRectangle rect;
073: private String areaName;
074: private List itemListeners = new ArrayList();
075:
076: public BasicComboBoxArea(String cbName, Vector items) {
077: this (cbName, null, items, null, null);
078: }
079:
080: /** Creates a new instance of BasicComboBoxArea */
081: public BasicComboBoxArea(String cbName, String cbLabel, Vector items) {
082: this (cbName, cbLabel, items, null, null);
083: }
084:
085: public BasicComboBoxArea(String cbName, String cbLabel,
086: Vector items, Vector displayLabels, String tooltipText) {
087: this (cbName, cbLabel, items, null, null, false);
088: }
089:
090: public BasicComboBoxArea(String cbName, String cbLabel,
091: Vector items, Vector displayLabels, String tooltipText,
092: boolean isEditable) {
093: super ();
094:
095: this .setSelectable(false);
096: this .setResizable(false);
097:
098: areaName = cbName;
099:
100: rect = new JGoRectangle();
101: rect.setPen(JGoPen.makeStockPen(Color.lightGray));
102: rect.setBrush(JGoBrush.makeStockBrush(Color.WHITE));
103: rect.setSelectable(false);
104: rect.setResizable(false);
105: addObjectAtHead(rect);
106:
107: if (cbLabel != null) {
108: cellArea = new BasicCellArea(cbLabel);
109: cellArea.setTextAlignment(JGoText.ALIGN_CENTER);
110: this .addObjectAtTail(cellArea);
111: }
112:
113: cbArea = new ComboBoxArea(items, displayLabels, tooltipText,
114: isEditable);
115: cbArea.addItemListener(this );
116: this .addObjectAtTail(cbArea);
117:
118: this .setSize(getMaximumWidth(), getMaximumHeight());
119: }
120:
121: public int getMaximumHeight() {
122: int h = this .insets.top + this .insets.bottom;
123:
124: if (cellArea != null) {
125: h += cellArea.getMaximumHeight();
126: }
127:
128: if (cbArea != null) {
129: h += cbArea.getHeight();
130: }
131:
132: return h;
133: }
134:
135: public int getMaximumWidth() {
136: int w = this .insets.left + this .insets.right;
137: int width = 0;
138:
139: if (cellArea != null) {
140: width = cellArea.getMaximumWidth();
141: }
142:
143: if (cbArea != null && cbArea.getWidth() > width) {
144: width = cbArea.getWidth();
145: }
146:
147: w += width;
148:
149: return w;
150: }
151:
152: public int getMinimumWidth() {
153: int width = 0;
154:
155: if (cellArea != null) {
156: width = cellArea.getMinimumWidth();
157: }
158:
159: if (cbArea != null) {
160: JComboBox comboBox = cbArea.getComboBox();
161: if (comboBox != null) {
162: int cbWidth = comboBox.getPreferredSize().width;
163: if (cbWidth > width) {
164: width = cbWidth;
165: }
166: }
167: }
168:
169: // Always account for horizontal insets.
170: width += this .getInsets().left + this .getInsets().right;
171: return width;
172: }
173:
174: public void layoutChildren() {
175: rect.setBoundingRect(this .getBoundingRect());
176:
177: int rectleft = getLeft();
178: int recttop = getTop();
179: int rectwidth = getWidth();
180:
181: int left = rectleft + insets.left;
182: int top = recttop + insets.top;
183: int width = rectwidth - insets.left - insets.right;
184: int cbTop = top;
185: if (cellArea != null) {
186: cbTop = top + cellArea.getHeight();
187: }
188:
189: cbArea.setBoundingRect(left, cbTop, width, cbArea.getHeight());
190:
191: if (cellArea != null) {
192: cellArea.setBoundingRect(left, top, width, cellArea
193: .getHeight());
194: }
195:
196: }
197:
198: //we need to call set visible on our Jgo control so that it can be
199: //removed from the view
200: public void setVisible(boolean bFlag) {
201: cbArea.setVisible(bFlag);
202: super .setVisible(bFlag);
203: }
204:
205: public void setSelectedItem(Object anObject) {
206: cbArea.setSelectedItem(anObject);
207: }
208:
209: public void addItemListener(ItemListener l) {
210: if (l != null) {
211: synchronized (itemListeners) {
212: itemListeners.add(l);
213: }
214: }
215: }
216:
217: public void removeItemListener(ItemListener l) {
218: if (l != null) {
219: synchronized (itemListeners) {
220: itemListeners.remove(l);
221: }
222: }
223: }
224:
225: public void setComboBoxEnabled(boolean enabled) {
226: cbArea.setComboBoxEnabled(enabled);
227: }
228:
229: public String getName() {
230: return areaName;
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
237: */
238: public void itemStateChanged(ItemEvent e) {
239: // Redispatch this ItemEvent with this as the (proxy) source.
240: ItemEvent newEvent = new ItemEvent(e.getItemSelectable(), e
241: .getID(), e.getItem(), e.getStateChange());
242: newEvent.setSource(this );
243:
244: synchronized (itemListeners) {
245: Iterator iter = itemListeners.iterator();
246: while (iter.hasNext()) {
247: ItemListener l = (ItemListener) iter.next();
248: try {
249: l.itemStateChanged(newEvent);
250: } catch (Exception ignore) {
251: // Ignore and continue with other listeners.
252: }
253: }
254: }
255: }
256:
257: public JComboBox getComboBox() {
258: return cbArea.getComboBox();
259: }
260:
261: public List getAcceptableValues() {
262: return cbArea.getAcceptableValues();
263: }
264:
265: public List getAcceptableDisplayValues() {
266: return cbArea.getAcceptableDisplayValues();
267: }
268:
269: public boolean isEditable() {
270: return cbArea.isEditable();
271: }
272:
273: public Object getDataObject() {
274: return null;
275: }
276:
277: public IGraphNode getGraphNode() {
278: return null;
279: }
280:
281: public void setObject(Object obj) {
282:
283: }
284:
285: public IGraphPort getLeftGraphPort() {
286: return null;
287: }
288:
289: public IGraphPort getRightGraphPort() {
290: return null;
291: }
292: }
|