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 com.sun.rave.designtime.impl;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.reflect.Constructor;
047: import java.util.Vector;
048: import java.awt.Component;
049: import java.awt.Image;
050: import com.sun.rave.designtime.Customizer2;
051: import com.sun.rave.designtime.DesignBean;
052: import com.sun.rave.designtime.Result;
053:
054: /**
055: * A basic implementation of Customizer2 to subclass and/or use for convenience. The 'panelClass'
056: * defines the piece of UI to use for the customizer. If the panelClass has a constructor that
057: * takes a DesignBean, that will be used. Otherwise, a null constructor will be used.
058: *
059: * @author Joe Nuxoll
060: * @version 1.0
061: * @see Customizer2
062: */
063: public class BasicCustomizer2 implements Customizer2 {
064:
065: public BasicCustomizer2() {
066: }
067:
068: public BasicCustomizer2(Class panelClass) {
069: this (panelClass, "Customizer", null, null); // NOI18N
070: }
071:
072: public BasicCustomizer2(Class panelClass, String displayName) {
073: this (panelClass, displayName, null, null);
074: }
075:
076: public BasicCustomizer2(Class panelClass, String displayName,
077: String description) {
078: this (panelClass, displayName, description, null);
079: }
080:
081: public BasicCustomizer2(Class panelClass, String displayName,
082: String description, String helpKey) {
083: this .panelClass = panelClass;
084: this .displayName = displayName;
085: this .description = description;
086: this .helpKey = helpKey;
087: }
088:
089: protected Class panelClass;
090:
091: public void setPanelClass(Class panelClass) {
092: this .panelClass = panelClass;
093: }
094:
095: public Class getPanelClass() {
096: return panelClass;
097: }
098:
099: protected Component createCustomizerPanel() {
100: if (panelClass != null) {
101: Object panel = null;
102: try {
103: Constructor con = panelClass
104: .getConstructor(new Class[] { DesignBean.class });
105: panel = con.newInstance(new Object[] { designBean });
106: } catch (Exception x) {
107: try {
108: panel = panelClass.newInstance();
109: } catch (Exception ex) {
110: }
111: }
112: if (panel instanceof Component) {
113: return (Component) panel;
114: }
115: }
116: return null;
117: }
118:
119: public Component getCustomizerPanel(DesignBean designBean) {
120: this .designBean = designBean;
121: return createCustomizerPanel();
122: }
123:
124: protected DesignBean designBean;
125:
126: public DesignBean getDesignBean() {
127: return designBean;
128: }
129:
130: protected boolean applyCapable = true;
131:
132: public void setApplyCapable(boolean applyCapable) {
133: this .applyCapable = applyCapable;
134: }
135:
136: public boolean isApplyCapable() {
137: return applyCapable;
138: }
139:
140: protected boolean modified = false;
141:
142: public void setModified(boolean modified) {
143: this .modified = modified;
144: firePropertyChange();
145: }
146:
147: public boolean isModified() {
148: return modified;
149: }
150:
151: public Result applyChanges() {
152: // do stuff here!
153: this .modified = false;
154: firePropertyChange();
155: return Result.SUCCESS;
156: }
157:
158: protected String displayName;
159:
160: public void setDisplayName(String displayName) {
161: this .displayName = displayName;
162: }
163:
164: public String getDisplayName() {
165: return displayName;
166: }
167:
168: protected String description;
169:
170: public void setDescription(String description) {
171: this .description = description;
172: }
173:
174: public String getDescription() {
175: return description;
176: }
177:
178: protected Image largeIcon;
179:
180: public void setLargeIcon(Image largeIcon) {
181: this .largeIcon = largeIcon;
182: }
183:
184: public Image getLargeIcon() {
185: return largeIcon;
186: }
187:
188: protected Image smallIcon;
189:
190: public void setSmallIcon(Image smallIcon) {
191: this .smallIcon = smallIcon;
192: }
193:
194: public Image getSmallIcon() {
195: return smallIcon;
196: }
197:
198: protected String helpKey;
199:
200: public void setHelpKey(String helpKey) {
201: this .helpKey = helpKey;
202: }
203:
204: public String getHelpKey() {
205: return helpKey;
206: }
207:
208: protected Vector propertyChangeListeners;
209:
210: public synchronized void addPropertyChangeListener(
211: PropertyChangeListener l) {
212: Vector v = propertyChangeListeners == null ? new Vector(2)
213: : (Vector) propertyChangeListeners.clone();
214: if (!v.contains(l)) {
215: v.addElement(l);
216: propertyChangeListeners = v;
217: }
218: }
219:
220: public synchronized void removePropertyChangeListener(
221: PropertyChangeListener l) {
222: if (propertyChangeListeners != null
223: && propertyChangeListeners.contains(l)) {
224: Vector v = (Vector) propertyChangeListeners.clone();
225: v.removeElement(l);
226: propertyChangeListeners = v;
227: }
228: }
229:
230: public PropertyChangeListener[] getPropertyChangeListeners() {
231: Vector v = propertyChangeListeners == null ? new Vector(2)
232: : (Vector) propertyChangeListeners.clone();
233: return (PropertyChangeListener[]) v
234: .toArray(new PropertyChangeListener[v.size()]);
235: }
236:
237: public void firePropertyChange() {
238: if (designBean != null) {
239: PropertyChangeEvent e = new PropertyChangeEvent(designBean
240: .getInstance(), null, null, null);
241: firePropertyChange(e);
242: }
243: }
244:
245: public void firePropertyChange(String propName) {
246: if (designBean != null) {
247: PropertyChangeEvent e = new PropertyChangeEvent(designBean
248: .getInstance(), propName, null, null);
249: firePropertyChange(e);
250: }
251: }
252:
253: public void firePropertyChange(String propName, Object oldValue,
254: Object newValue) {
255: if (designBean != null) {
256: PropertyChangeEvent e = new PropertyChangeEvent(designBean
257: .getInstance(), propName, oldValue, newValue);
258: firePropertyChange(e);
259: }
260: }
261:
262: public void firePropertyChange(PropertyChangeEvent e) {
263: if (propertyChangeListeners != null) {
264: Vector listeners = propertyChangeListeners;
265: int count = listeners.size();
266: for (int i = 0; i < count; i++) {
267: ((PropertyChangeListener) listeners.elementAt(i))
268: .propertyChange(e);
269: }
270: }
271: }
272: }
|