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-2006 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.beaninfo.editors;
043:
044: import java.beans.PropertyEditorSupport;
045: import java.io.File;
046: import java.text.MessageFormat;
047: import org.netbeans.core.UIExceptions;
048: import org.openide.explorer.propertysheet.ExPropertyEditor;
049: import org.openide.explorer.propertysheet.PropertyEnv;
050:
051: import org.openide.loaders.DataFolder;
052: import org.openide.util.NbBundle;
053:
054: /**
055: *
056: * @author David Strupl
057: * @version
058: */
059: public class DataFolderEditor extends PropertyEditorSupport implements
060: ExPropertyEditor {
061:
062: /** Creates new DataFolderEditor */
063: public DataFolderEditor() {
064: }
065:
066: private DataFolderPanel dfPanel;
067:
068: PropertyEnv env;
069:
070: /**
071: * @return The property value as a human editable string.
072: * <p> Returns null if the value can't be expressed as an editable string.
073: * <p> If a non-null value is returned, then the PropertyEditor should
074: * be prepared to parse that string back in setAsText().
075: */
076: @Override
077: public String getAsText() {
078: DataFolder df = (DataFolder) getValue();
079: String result;
080: if (df == null) {
081: result = getString("LAB_DefaultDataFolder"); //NOI18N
082: } else {
083: result = df.getName();
084: }
085: if (result.length() == 0) {
086: result = File.pathSeparator;
087: }
088: return result;
089: }
090:
091: /** Set the property value by parsing a given String. May raise
092: * java.lang.IllegalArgumentException if either the String is
093: * badly formatted or if this kind of property can't be expressed
094: * as text.
095: * @param text The string to be parsed.
096: */
097: @Override
098: public void setAsText(String text) {
099: if (text == null || "".equals(text)
100: || text.equals(getString("LAB_DefaultDataFolder"))
101: || File.pathSeparator.equals(text)) {
102: //XXX Mysterious why a real implementation of setAsText is not here
103: setValue(null);
104: } else {
105: //Reasonable to assume any exceptions from core/jdk editors are legit
106: IllegalArgumentException iae = new IllegalArgumentException();
107: String msg = MessageFormat.format(NbBundle.getMessage(
108: DataFolderEditor.class, "FMT_DF_UNKNOWN"),
109: new Object[] { text }); //NOI18N
110: UIExceptions.annotateUser(iae, iae.getMessage(), msg, null,
111: new java.util.Date());
112: throw iae;
113: }
114: }
115:
116: @Override
117: public boolean supportsCustomEditor() {
118: return true;
119: }
120:
121: @Override
122: public java.awt.Component getCustomEditor() {
123: dfPanel = getDFPanel();
124: Object val = getValue();
125: if (val instanceof DataFolder) {
126: dfPanel.setTargetFolder((DataFolder) val);
127: }
128: return dfPanel;
129: }
130:
131: /** Calls super.setValue(newValue) and then updates
132: * the look of the associated DataFolderPanel by
133: * providing appropriate node to display.
134: */
135: @Override
136: public void setValue(Object newValue) {
137: Object oldValue = getValue();
138: super .setValue(newValue);
139: DataFolderPanel dfp = getDFPanel();
140: if ((newValue != oldValue) && (dfp != null)
141: && (newValue instanceof DataFolder)) {
142: dfp.setTargetFolder((DataFolder) newValue);
143: }
144:
145: }
146:
147: /** This method is called from DataFolderPanel, so it is similar to
148: * setValue but does not call DataFolderPanel.setTargetFolder()
149: */
150: void setDataFolder(DataFolder newDf) {
151: super .setValue(newDf);
152: }
153:
154: DataFolderPanel getDFPanel() {
155: if (dfPanel == null) {
156: dfPanel = new DataFolderPanel(this );
157: }
158: return dfPanel;
159: }
160:
161: private static String getString(String s) {
162: return org.openide.util.NbBundle.getBundle(
163: DataFolderEditor.class).getString(s);
164: }
165:
166: public void attachEnv(PropertyEnv env) {
167: this.env = env;
168: }
169: }
|