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.modules.vmd.midp.propertyeditors;
043:
044: import java.lang.ref.WeakReference;
045: import java.util.Arrays;
046: import java.util.Iterator;
047: import java.util.regex.Pattern;
048: import org.netbeans.modules.vmd.api.model.DesignComponent;
049: import org.netbeans.modules.vmd.api.model.PropertyValue;
050: import org.netbeans.modules.vmd.api.model.TypeID;
051: import org.netbeans.modules.vmd.api.properties.GroupPropertyEditor;
052: import org.netbeans.modules.vmd.api.properties.GroupValue;
053: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
054: import org.netbeans.modules.vmd.midp.components.MidpTypes;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.explorer.propertysheet.ExPropertyEditor;
058: import org.openide.util.NbBundle;
059:
060: /**
061: *
062: * @author Karol Harezlak
063: */
064: public class PropertyEditorArrayInteger extends GroupPropertyEditor
065: implements ExPropertyEditor {
066:
067: private static String ERROR_WARNING = NbBundle.getMessage(
068: PropertyEditorArrayInteger.class, "MSG_ILLEGAL_FORMATING"); // NOI18N
069: private WeakReference<DesignComponent> component;
070: private Object parentTypeID;
071:
072: public static DesignPropertyEditor create() {
073: return new PropertyEditorArrayInteger();
074: }
075:
076: public static DesignPropertyEditor create(TypeID parentTypeID) {
077: return new PropertyEditorArrayInteger(parentTypeID);
078: }
079:
080: private PropertyEditorArrayInteger() {
081: }
082:
083: private PropertyEditorArrayInteger(TypeID parentTypeID) {
084: this .parentTypeID = parentTypeID;
085: }
086:
087: @Override
088: public boolean supportsCustomEditor() {
089: return false;
090: }
091:
092: @Override
093: public String getAsText() {
094: StringBuffer text = new StringBuffer();
095: text.append('['); // NOI18N
096: GroupValue values = getValue();
097: for (Iterator<String> i = Arrays.asList(
098: getValue().getPropertyNames()).iterator(); i.hasNext();) {
099: PropertyValue value = (PropertyValue) values.getValue(i
100: .next());
101: text.append(value.getPrimitiveValue());
102: if (i.hasNext()) {
103: text.append(','); //NOI18N
104: }
105: }
106: text.append(']'); //NOI18N
107: return text.toString();
108: }
109:
110: @Override
111: public void setAsText(String text) {
112: String newText = decodeValuesFromText(text);
113:
114: if (newText == null) {
115: DialogDisplayer.getDefault().notify(
116: new NotifyDescriptor.Message(ERROR_WARNING + ' '
117: + text)); //NOI18N
118: } else {
119: GroupValue values = getValue();
120: Iterator<String> propertyNamesIter = Arrays.asList(
121: values.getPropertyNames()).iterator();
122: for (String number : newText.split(",")) {
123: //NOI18N
124: values.putValue(propertyNamesIter.next(), MidpTypes
125: .createIntegerValue(Integer.parseInt(number)));
126: }
127: setValue(values);
128: }
129: }
130:
131: private String decodeValuesFromText(String text) {
132: text = text.trim().replaceAll(
133: Pattern.compile("[\\[$\\]]").pattern(), ""); //NOI18N
134: if (Pattern.compile("[^0123456789,]").matcher(text).find()
135: || text.split(",").length != getValue()
136: .getPropertyNames().length) {
137: //NOI18N
138: return null;
139: }
140: return text;
141: }
142:
143: @Override
144: public Boolean canEditAsText() {
145: return true;
146: }
147:
148: @Override
149: public boolean canWrite() {
150: if (component.get() == null) {
151: return super .canWrite();
152: }
153: final DesignComponent[] isEditable = new DesignComponent[1];
154: component.get().getDocument().getTransactionManager()
155: .readAccess(new Runnable() {
156: public void run() {
157: isEditable[0] = component.get()
158: .getParentComponent();
159: }
160: });
161: if (parentTypeID != null && isEditable[0] != null
162: && isEditable[0].getType().equals(parentTypeID)) {
163: return false;
164: }
165: return super .canWrite();
166: }
167:
168: @Override
169: public void init(DesignComponent component) {
170: this .component = new WeakReference<DesignComponent>(component);
171: }
172:
173: @Override
174: public void customEditorResetToDefaultButtonPressed() {
175: if (!(getValue() instanceof GroupValue))
176: throw new IllegalArgumentException();
177: GroupValue currentValue = (GroupValue) getValue();
178: for (String propertyName : currentValue.getPropertyNames()) {
179: component.get().writeProperty(propertyName,
180: MidpTypes.createIntegerValue(0));
181: }
182: }
183:
184: }
|