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