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: /*
043: * Created on May 16, 2005
044: *
045: * To change the template for this generated file go to
046: * Window - Preferences - Java - Code Generation - Code and Comments
047: */
048: package org.netbeans.modules.xml.wsdl.ui.api.property;
049:
050: import java.awt.Component;
051: import java.beans.FeatureDescriptor;
052: import java.beans.PropertyChangeEvent;
053: import java.beans.PropertyChangeListener;
054: import java.beans.PropertyEditorSupport;
055: import java.util.HashMap;
056: import java.util.Map;
057:
058: import org.netbeans.api.project.FileOwnerQuery;
059: import org.netbeans.api.project.Project;
060: import org.netbeans.modules.xml.schema.model.GlobalElement;
061: import org.netbeans.modules.xml.schema.model.GlobalType;
062: import org.netbeans.modules.xml.schema.model.SchemaComponent;
063: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
064: import org.netbeans.modules.xml.wsdl.ui.view.ElementOrTypeChooserPanel;
065: import org.netbeans.modules.xml.xam.ModelSource;
066: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
067: import org.netbeans.modules.xml.xam.ui.XAMUtils;
068: import org.openide.explorer.propertysheet.ExPropertyEditor;
069: import org.openide.explorer.propertysheet.PropertyEnv;
070: import org.openide.filesystems.FileObject;
071:
072: /**
073: * @author radval
074: *
075: * To change the template for this generated type comment go to Window -
076: * Preferences - Java - Code Generation - Code and Comments
077: */
078: public class ElementOrTypePropertyEditor extends PropertyEditorSupport
079: implements ExPropertyEditor {
080:
081: /** Environment passed to the ExPropertyEditor */
082: private PropertyEnv mEnv;
083:
084: private ElementOrTypeProvider mElementOrTypeProvider;
085:
086: public ElementOrTypePropertyEditor(
087: ElementOrTypeProvider elementOrTypeProvider) {
088: this .mElementOrTypeProvider = elementOrTypeProvider;
089:
090: }
091:
092: @Override
093: public String getAsText() {
094: if (mElementOrTypeProvider != null) {
095: ElementOrType eot = mElementOrTypeProvider
096: .getElementOrType();
097: if (eot != null) {
098: return eot.toString();
099: }
100: }
101: return "";
102: }
103:
104: /**
105: * This method is called by the IDE to pass the environment to the property
106: * editor.
107: *
108: * @param env
109: * Environment passed by the ide.
110: */
111: public void attachEnv(PropertyEnv env) {
112: this .mEnv = env;
113: FeatureDescriptor desc = env.getFeatureDescriptor();
114: // make this is not editable
115: desc.setValue("canEditAsText", Boolean.FALSE); // NOI18N
116: // add help id
117: desc
118: .setValue(ExPropertyEditor.PROPERTY_HELP_ID,
119: "org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypePropertyEditor");
120: }
121:
122: /** @return tags */
123: @Override
124: public String[] getTags() {
125: return null;
126: }
127:
128: /** @return true */
129: @Override
130: public boolean supportsCustomEditor() {
131: return XAMUtils.isWritable(mElementOrTypeProvider.getModel());
132: }
133:
134: /** @return editor component */
135: @Override
136: public Component getCustomEditor() {
137: WSDLModel model = mElementOrTypeProvider.getModel();
138: ModelSource modelSource = model.getModelSource();
139: FileObject wsdlFile = modelSource.getLookup().lookup(
140: FileObject.class);
141: if (wsdlFile != null) {
142: Project project = FileOwnerQuery.getOwner(wsdlFile);
143: if (project != null) {
144:
145: Map<String, String> namespaceToPrefixMap = new HashMap<String, String>();
146: Map<String, String> map = ((AbstractDocumentComponent) model
147: .getDefinitions()).getPrefixes();
148: for (String prefix : map.keySet()) {
149: namespaceToPrefixMap.put(map.get(prefix), prefix);
150: }
151: ElementOrType eot = mElementOrTypeProvider
152: .getElementOrType();
153: SchemaComponent comp = null;
154: if (eot != null) {
155: comp = eot.getElement();
156: if (comp == null) {
157: comp = eot.getType();
158: }
159: }
160:
161: final ElementOrTypeChooserPanel panel = new ElementOrTypeChooserPanel(
162: project, namespaceToPrefixMap, model, comp);
163:
164: mEnv.setState(PropertyEnv.STATE_INVALID);
165:
166: final PropertyChangeListener pcl = new PropertyChangeListener() {
167: public void propertyChange(PropertyChangeEvent evt) {
168: if (evt.getSource() == panel
169: && evt
170: .getPropertyName()
171: .equals(
172: ElementOrTypeChooserPanel.PROP_ACTION_APPLY)) {
173: Boolean b = (Boolean) evt.getNewValue();
174: mEnv
175: .setState(b.booleanValue() ? PropertyEnv.STATE_NEEDS_VALIDATION
176: : PropertyEnv.STATE_INVALID);
177:
178: }
179: }
180: };
181: panel.addPropertyChangeListener(
182: ElementOrTypeChooserPanel.PROP_ACTION_APPLY,
183: pcl);
184: mEnv
185: .addPropertyChangeListener(new PropertyChangeListener() {
186:
187: public void propertyChange(
188: PropertyChangeEvent evt) {
189: if ((PropertyEnv.PROP_STATE.equals(evt
190: .getPropertyName()) && evt
191: .getNewValue() == PropertyEnv.STATE_VALID)) {
192: SchemaComponent comp = panel
193: .getSelectedSchemaComponent();
194: if (comp instanceof GlobalType) {
195: setValue(new ElementOrType(
196: (GlobalType) comp,
197: mElementOrTypeProvider
198: .getModel()));
199: } else if (comp instanceof GlobalElement) {
200: setValue(new ElementOrType(
201: (GlobalElement) comp,
202: mElementOrTypeProvider
203: .getModel()));
204: }
205: }
206: }
207:
208: });
209: return panel;
210: }
211: }
212: return null;
213: }
214: }
|