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.resource.elements;
043:
044: import org.netbeans.modules.vmd.midp.propertyeditors.api.resource.element.PropertyEditorResourceElement;
045: import java.util.Arrays;
046: import java.util.List;
047: import java.util.Map;
048: import javax.swing.JComponent;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import org.netbeans.modules.vmd.api.model.DesignComponent;
052: import org.netbeans.modules.vmd.api.model.PropertyValue;
053: import org.netbeans.modules.vmd.api.model.TypeID;
054: import org.netbeans.modules.vmd.midp.components.MidpTypes;
055: import org.netbeans.modules.vmd.midp.components.resources.TickerCD;
056:
057: /**
058: *
059: * @author Anton Chechel
060: */
061: public class TickerEditorElement extends PropertyEditorResourceElement
062: implements DocumentListener {
063:
064: private long componentID;
065: private boolean doNotFireEvent;
066:
067: public TickerEditorElement() {
068: initComponents();
069: tickerTextField.getDocument().addDocumentListener(this );
070: }
071:
072: public JComponent getJComponent() {
073: return this ;
074: }
075:
076: public TypeID getTypeID() {
077: return TickerCD.TYPEID;
078: }
079:
080: public List<String> getPropertyValueNames() {
081: return Arrays.asList(TickerCD.PROP_STRING);
082: }
083:
084: public void setDesignComponentWrapper(
085: final DesignComponentWrapper wrapper) {
086: if (wrapper == null) {
087: // UI stuff
088: setText(null);
089: setAllEnabled(false);
090: return;
091: }
092:
093: this .componentID = wrapper.getComponentID();
094: final String[] _tickerText = new String[1];
095:
096: final DesignComponent component = wrapper.getComponent();
097: if (component != null) {
098: // existing component
099: if (!component.getType().equals(getTypeID())) {
100: throw new IllegalArgumentException(
101: "Passed component must have typeID "
102: + getTypeID() + " instead passed "
103: + component.getType()); // NOI18N
104: }
105:
106: this .componentID = component.getComponentID();
107: component.getDocument().getTransactionManager().readAccess(
108: new Runnable() {
109:
110: public void run() {
111: PropertyValue propertyValue = component
112: .readProperty(TickerCD.PROP_STRING);
113: if (!isPropertyValueAUserCodeType(propertyValue)) {
114: _tickerText[0] = MidpTypes
115: .getString(propertyValue);
116: }
117: }
118: });
119: }
120:
121: if (wrapper.hasChanges()) {
122: Map<String, PropertyValue> changes = wrapper.getChanges();
123: for (String propertyName : changes.keySet()) {
124: final PropertyValue propertyValue = changes
125: .get(propertyName);
126: if (TickerCD.PROP_STRING.equals(propertyName)) {
127: _tickerText[0] = MidpTypes.getString(propertyValue);
128: }
129: }
130: }
131:
132: // UI stuff
133: setAllEnabled(true);
134: setText(_tickerText[0]);
135: }
136:
137: private synchronized void setText(String text) {
138: doNotFireEvent = true;
139: tickerTextField.setText(text);
140: doNotFireEvent = false;
141: }
142:
143: private void setAllEnabled(boolean isEnabled) {
144: tickerLabel.setEnabled(isEnabled);
145: tickerTextField.setEnabled(isEnabled);
146: }
147:
148: public void insertUpdate(DocumentEvent e) {
149: if (tickerTextField.hasFocus()) {
150: textChanged();
151: }
152: }
153:
154: public void removeUpdate(DocumentEvent e) {
155: if (tickerTextField.hasFocus()) {
156: textChanged();
157: }
158: }
159:
160: public void changedUpdate(DocumentEvent e) {
161: }
162:
163: private synchronized void textChanged() {
164: // if (isShowing() && !doNotFireEvent) {
165: if (!doNotFireEvent) {
166: fireElementChanged(componentID, TickerCD.PROP_STRING,
167: MidpTypes.createStringValue(tickerTextField
168: .getText()));
169: }
170: }
171:
172: /** This method is called from within the constructor to
173: * initialize the form.
174: * WARNING: Do NOT modify this code. The content of this method is
175: * always regenerated by the Form Editor.
176: */
177: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
178: private void initComponents() {
179:
180: tickerLabel = new javax.swing.JLabel();
181: tickerTextField = new javax.swing.JTextField();
182:
183: tickerLabel.setLabelFor(tickerTextField);
184: org.openide.awt.Mnemonics.setLocalizedText(tickerLabel,
185: org.openide.util.NbBundle.getMessage(
186: TickerEditorElement.class,
187: "TickerEditorElement.tickerLabel.text")); // NOI18N
188: tickerLabel.setEnabled(false);
189:
190: tickerTextField.setEnabled(false);
191:
192: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
193: this );
194: this .setLayout(layout);
195: layout.setHorizontalGroup(layout.createParallelGroup(
196: org.jdesktop.layout.GroupLayout.LEADING).add(
197: layout.createSequentialGroup().add(2, 2, 2).add(
198: tickerLabel).addContainerGap()).add(
199: tickerTextField,
200: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 320,
201: Short.MAX_VALUE));
202: layout
203: .setVerticalGroup(layout
204: .createParallelGroup(
205: org.jdesktop.layout.GroupLayout.LEADING)
206: .add(
207: layout
208: .createSequentialGroup()
209: .add(tickerLabel)
210: .addPreferredGap(
211: org.jdesktop.layout.LayoutStyle.RELATED)
212: .add(
213: tickerTextField,
214: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
215: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
216: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
217: .addContainerGap(110,
218: Short.MAX_VALUE)));
219: }// </editor-fold>//GEN-END:initComponents
220:
221: // Variables declaration - do not modify//GEN-BEGIN:variables
222: private javax.swing.JLabel tickerLabel;
223: private javax.swing.JTextField tickerTextField;
224: // End of variables declaration//GEN-END:variables
225: }
|