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.midpnb.propertyeditors.table;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Component;
046: import java.lang.ref.WeakReference;
047: import javax.swing.BorderFactory;
048: import javax.swing.JPanel;
049: import org.netbeans.modules.vmd.api.model.Debug;
050: import org.netbeans.modules.vmd.api.model.DesignComponent;
051: import org.netbeans.modules.vmd.api.model.PropertyValue;
052: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
053: import org.netbeans.modules.vmd.midp.propertyeditors.api.resource.element.PropertyEditorResourceElement.DesignComponentWrapper;
054: import org.netbeans.modules.vmd.midp.propertyeditors.api.resource.element.PropertyEditorResourceElementEvent;
055: import org.netbeans.modules.vmd.midp.propertyeditors.api.resource.element.PropertyEditorResourceElementListener;
056: import org.netbeans.modules.vmd.midpnb.components.resources.SimpleTableModelCD;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Anton Chechel
062: */
063: public class PropertyEditorTableModel extends DesignPropertyEditor
064: implements PropertyEditorResourceElementListener {
065:
066: private WeakReference<DesignComponent> component;
067: private JPanel customEditorPanel;
068: private TableModelEditorElement customEditor;
069: private PropertyValue values;
070: private PropertyValue headers;
071:
072: private PropertyEditorTableModel() {
073: initComponents();
074: }
075:
076: public static PropertyEditorTableModel createInstance() {
077: return new PropertyEditorTableModel();
078: }
079:
080: private void initComponents() {
081: customEditor = new TableModelEditorElement();
082: customEditor.addPropertyEditorResourceElementListener(this );
083: customEditorPanel = new JPanel(new BorderLayout());
084: customEditorPanel.setBorder(BorderFactory.createEmptyBorder(12,
085: 12, 12, 12));
086: customEditorPanel.add(customEditor, BorderLayout.CENTER);
087: }
088:
089: @Override
090: public void init(DesignComponent component) {
091: if (component != null) {
092: this .component = new WeakReference<DesignComponent>(
093: component);
094: }
095: }
096:
097: @Override
098: public Component getCustomEditor() {
099: //if (customEditorPanel.isShowing()) {
100: if (component != null && component.get() != null) {
101: customEditor
102: .setDesignComponentWrapper(new DesignComponentWrapper(
103: component.get()));
104: }
105: customEditor.setAllEnabled(true);
106: //}
107: return customEditorPanel;
108: }
109:
110: @Override
111: public Boolean canEditAsText() {
112: return Boolean.FALSE;
113: }
114:
115: @Override
116: public String getAsText() {
117: return NbBundle.getMessage(PropertyEditorTableModel.class,
118: "DISP_PE_TableModel_GetAsText"); //NOI18N
119: }
120:
121: @Override
122: public boolean executeInsideWriteTransaction() {
123: if (component == null || component.get() == null) {
124: return false;
125: }
126:
127: DesignComponent _component = component.get();
128: if (headers != null) {
129: _component.writeProperty(
130: SimpleTableModelCD.PROP_COLUMN_NAMES, headers);
131: }
132: if (values != null) {
133: _component.writeProperty(SimpleTableModelCD.PROP_VALUES,
134: values);
135: }
136: return false;
137: }
138:
139: @Override
140: public boolean isExecuteInsideWriteTransactionUsed() {
141: return true;
142: }
143:
144: @Override
145: public boolean supportsDefaultValue() {
146: return true;
147: }
148:
149: public void elementChanged(PropertyEditorResourceElementEvent event) {
150: PropertyValue propertyValue = event.getPropertyValue();
151: String propertyName = event.getPropertyName();
152: if (SimpleTableModelCD.PROP_COLUMN_NAMES.equals(propertyName)) {
153: headers = propertyValue;
154: } else if (SimpleTableModelCD.PROP_VALUES.equals(propertyName)) {
155: values = propertyValue;
156: } else {
157: throw Debug
158: .illegalArgument("Illegal property value has been passed"); // NOI18N
159: }
160: }
161:
162: @Override
163: public boolean isDefaultValue() {
164: if (component == null || component.get() == null) {
165: return true;
166: }
167:
168: final boolean[] isDefaultValue = new boolean[] { true };
169: component.get().getDocument().getTransactionManager()
170: .readAccess(new Runnable() {
171:
172: public void run() {
173: DesignComponent _component = component.get();
174: boolean names = PropertyValue
175: .createNull()
176: .equals(
177: _component
178: .readProperty(SimpleTableModelCD.PROP_COLUMN_NAMES));
179: boolean values = PropertyValue
180: .createNull()
181: .equals(
182: _component
183: .readProperty(SimpleTableModelCD.PROP_VALUES));
184: isDefaultValue[0] = names && values;
185: }
186: });
187:
188: return isDefaultValue[0];
189: }
190:
191: @Override
192: public boolean isResetToDefaultAutomatically() {
193: return false;
194: }
195:
196: @Override
197: public void customEditorResetToDefaultButtonPressed() {
198: if (component != null && component.get() != null) {
199: DesignComponent _component = component.get();
200: _component.writeProperty(
201: SimpleTableModelCD.PROP_COLUMN_NAMES, PropertyValue
202: .createNull());
203: _component.writeProperty(SimpleTableModelCD.PROP_VALUES,
204: PropertyValue.createNull());
205: }
206: }
207: }
|