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: package org.netbeans.modules.visualweb.propertyeditors.css;
042:
043: import org.netbeans.modules.visualweb.propertyeditors.css.model.BorderModel;
044: import org.netbeans.modules.visualweb.propertyeditors.css.model.CssStyleData;
045: import org.netbeans.modules.visualweb.propertyeditors.css.model.PropertyWithUnitData;
046: import org.netbeans.modules.visualweb.propertyeditors.css.model.Utils;
047: import java.beans.PropertyChangeSupport;
048: import javax.swing.DefaultComboBoxModel;
049: import javax.swing.JTextField;
050: import javax.swing.SwingUtilities;
051:
052: /**
053: *
054: * @author Winston Prakash
055: */
056: public class BorderWidthField extends javax.swing.JPanel {
057: PropertyWithUnitData borderWidthData = new PropertyWithUnitData();
058:
059: BorderModel borderModel = new BorderModel();
060:
061: /** Creates new form BorderWidthField */
062: public BorderWidthField() {
063: initComponents();
064: borderWidthCombo.setModel(borderModel.getWidthList());
065: borderWidthUnitCombo.setModel(borderModel.getWidthUnitList());
066:
067: // Add editor listeners to the border width combobox
068: final JTextField borderWidthComboEditor = (JTextField) borderWidthCombo
069: .getEditor().getEditorComponent();
070: borderWidthComboEditor
071: .addKeyListener(new java.awt.event.KeyAdapter() {
072: public void keyTyped(java.awt.event.KeyEvent evt) {
073: SwingUtilities.invokeLater(new Runnable() {
074: public void run() {
075: borderWidthUnitCombo
076: .setEnabled(Utils
077: .isInteger(borderWidthComboEditor
078: .getText()));
079: }
080: });
081: }
082: });
083: }
084:
085: private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
086: this );
087:
088: /**
089: * Adds a PropertyChangeListener to the listener list.
090: * @param l The listener to add.
091: */
092: public void addCssPropertyChangeListener(
093: java.beans.PropertyChangeListener l) {
094: propertyChangeSupport.addPropertyChangeListener(l);
095: }
096:
097: /**
098: * Removes a PropertyChangeListener from the listener list.
099: * @param l The listener to remove.
100: */
101: public void removeCssPropertyChangeListener(
102: java.beans.PropertyChangeListener l) {
103: propertyChangeSupport.removePropertyChangeListener(l);
104: }
105:
106: public void setWidthString(String widthStr) {
107: if ((widthStr != null) && !widthStr.equals("")) {
108: if (Utils.isInteger(widthStr)) {
109: setWidthValue(widthStr);
110: } else {
111: String unit = getUnit(widthStr);
112: setWidthUnit(unit);
113: setWidthValue(widthStr.replaceAll(unit, "").trim());
114: }
115: } else {
116: setWidthValue(null);
117: setWidthUnit(null);
118: }
119: }
120:
121: private String getUnit(String positionStr) {
122: DefaultComboBoxModel unitList = borderModel.getWidthUnitList();
123: for (int i = 0; i < unitList.getSize(); i++) {
124: String unit = (String) unitList.getElementAt(i);
125: if (positionStr.trim().endsWith(unit)) {
126: return unit;
127: }
128: }
129: return "";
130: }
131:
132: public void setWidthValue(String value) {
133: if ((value == null) || value.equals("")) {
134: borderWidthCombo.setSelectedIndex(0);
135: } else {
136: borderWidthCombo.setSelectedItem(value);
137: borderWidthData.setValue(value);
138: }
139: }
140:
141: public void setWidthUnit(String value) {
142: if ((value == null) || value.equals("")) {
143: borderWidthUnitCombo.setSelectedIndex(borderModel
144: .getWidthUnitList().getIndexOf("px"));
145: } else {
146: if (borderModel.getWidthUnitList().getIndexOf(value) != -1) {
147: borderWidthUnitCombo.setSelectedIndex(borderModel
148: .getWidthUnitList().getIndexOf(value));
149: } else {
150: borderWidthUnitCombo.setSelectedIndex(borderModel
151: .getWidthUnitList().getIndexOf("px"));
152: }
153: borderWidthData.setUnit(value);
154: }
155: }
156:
157: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
158: private void initComponents() {
159: borderWidthCombo = new javax.swing.JComboBox();
160: borderWidthUnitCombo = new javax.swing.JComboBox();
161:
162: setLayout(new java.awt.BorderLayout(3, 0));
163:
164: borderWidthCombo.setEditable(true);
165: borderWidthCombo
166: .addItemListener(new java.awt.event.ItemListener() {
167: public void itemStateChanged(
168: java.awt.event.ItemEvent evt) {
169: borderWidthComboItemStateChanged(evt);
170: }
171: });
172: borderWidthCombo
173: .addActionListener(new java.awt.event.ActionListener() {
174: public void actionPerformed(
175: java.awt.event.ActionEvent evt) {
176: borderWidthComboActionPerformed(evt);
177: }
178: });
179: borderWidthCombo
180: .addFocusListener(new java.awt.event.FocusAdapter() {
181: public void focusLost(java.awt.event.FocusEvent evt) {
182: borderWidthComboFocusLost(evt);
183: }
184: });
185:
186: add(borderWidthCombo, java.awt.BorderLayout.CENTER);
187: borderWidthCombo.getAccessibleContext().setAccessibleName(
188: "Test");
189: borderWidthCombo.getAccessibleContext()
190: .setAccessibleDescription("Test1");
191:
192: borderWidthUnitCombo
193: .addItemListener(new java.awt.event.ItemListener() {
194: public void itemStateChanged(
195: java.awt.event.ItemEvent evt) {
196: borderWidthUnitComboItemStateChanged(evt);
197: }
198: });
199: borderWidthUnitCombo
200: .addFocusListener(new java.awt.event.FocusAdapter() {
201: public void focusLost(java.awt.event.FocusEvent evt) {
202: borderWidthUnitComboFocusLost(evt);
203: }
204: });
205:
206: add(borderWidthUnitCombo, java.awt.BorderLayout.EAST);
207:
208: }
209:
210: // </editor-fold>//GEN-END:initComponents
211:
212: private void borderWidthUnitComboFocusLost(
213: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderWidthUnitComboFocusLost
214: setBorderWidth();
215: }//GEN-LAST:event_borderWidthUnitComboFocusLost
216:
217: private void borderWidthUnitComboItemStateChanged(
218: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderWidthUnitComboItemStateChanged
219: if (evt.getStateChange() != evt.DESELECTED) {
220: setBorderWidth();
221: }
222: }//GEN-LAST:event_borderWidthUnitComboItemStateChanged
223:
224: // For accessibility
225: public void setAccessibleName(String comboName, String unitName) {
226: borderWidthCombo.getAccessibleContext().setAccessibleName(
227: comboName);
228: borderWidthUnitCombo.getAccessibleContext().setAccessibleName(
229: unitName);
230: }
231:
232: // For accessibility
233: public void setAccessibleDescription(String comboDesc,
234: String unitDesc) {
235: borderWidthCombo.getAccessibleContext()
236: .setAccessibleDescription(comboDesc);
237: borderWidthUnitCombo.getAccessibleContext()
238: .setAccessibleDescription(unitDesc);
239: }
240:
241: private void borderWidthComboItemStateChanged(
242: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderWidthComboItemStateChanged
243: if (evt.getStateChange() != evt.DESELECTED) {
244: setBorderWidth();
245: }
246: }//GEN-LAST:event_borderWidthComboItemStateChanged
247:
248: private void borderWidthComboFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderWidthComboFocusLost
249: setBorderWidth();
250: }//GEN-LAST:event_borderWidthComboFocusLost
251:
252: private void borderWidthComboActionPerformed(
253: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borderWidthComboActionPerformed
254: setBorderWidth();
255: }//GEN-LAST:event_borderWidthComboActionPerformed
256:
257: private void setBorderWidth() {
258: String oldValue = borderWidthData.toString();
259: borderWidthData.setUnit(borderWidthUnitCombo.getSelectedItem()
260: .toString());
261: borderWidthData.setValue(borderWidthCombo.getSelectedItem()
262: .toString());
263: propertyChangeSupport.firePropertyChange("border-width",
264: oldValue, borderWidthData.toString());//NOI18N
265: }
266:
267: // Variables declaration - do not modify//GEN-BEGIN:variables
268: private javax.swing.JComboBox borderWidthCombo;
269: private javax.swing.JComboBox borderWidthUnitCombo;
270: // End of variables declaration//GEN-END:variables
271:
272: }
|