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: * Margin Width field wiith text field and unit combo box
054: * @author Winston Prakash
055: */
056: public class MarginWidthField extends javax.swing.JPanel {
057: PropertyWithUnitData borderMarginData = new PropertyWithUnitData();
058: MarginPaddingModel marginPaddingModel = new MarginPaddingModel();
059:
060: /** Creates new form borderMarginField */
061: public MarginWidthField() {
062: initComponents();
063: borderMarginCombo.setModel(marginPaddingModel.getMarginList());
064: borderMarginUnitCombo.setModel(marginPaddingModel
065: .getMarginUnitList());
066:
067: // Add editor listeners to the margin width combobox
068: final JTextField borderMarginComboEditor = (JTextField) borderMarginCombo
069: .getEditor().getEditorComponent();
070: borderMarginComboEditor
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: borderMarginUnitCombo
076: .setEnabled(Utils
077: .isInteger(borderMarginComboEditor
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 setMarginString(String marginStr) {
107: if ((marginStr != null) && !marginStr.equals("")) {
108: if (Utils.isInteger(marginStr)) {
109: setMarginValue(marginStr);
110: } else {
111: String unit = getUnit(marginStr);
112: setMarginUnit(unit);
113: setMarginValue(marginStr.replaceAll(unit, "").trim());
114: }
115: } else {
116: setMarginValue(null);
117: setMarginUnit(null);
118: }
119: }
120:
121: public String getMarginString() {
122: return borderMarginData.toString();
123: }
124:
125: private String getUnit(String positionStr) {
126: DefaultComboBoxModel unitList = marginPaddingModel
127: .getMarginUnitList();
128: for (int i = 0; i < unitList.getSize(); i++) {
129: String unit = (String) unitList.getElementAt(i);
130: if (positionStr.trim().endsWith(unit)) {
131: return unit;
132: }
133: }
134: return "";
135: }
136:
137: public void setMarginValue(String value) {
138: if ((value == null) || value.equals("")) {
139: borderMarginCombo.setSelectedIndex(0);
140: } else {
141: borderMarginCombo.setSelectedItem(value);
142: borderMarginData.setValue(value);
143: }
144: }
145:
146: public void setMarginUnit(String value) {
147: if ((value == null) || value.equals("")) {
148: borderMarginUnitCombo.setSelectedIndex(marginPaddingModel
149: .getMarginUnitList().getIndexOf("px")); //NOI18N
150: } else {
151: if (marginPaddingModel.getMarginUnitList()
152: .getIndexOf(value) != -1) {
153: borderMarginUnitCombo
154: .setSelectedIndex(marginPaddingModel
155: .getMarginUnitList().getIndexOf(value));
156: } else {
157: borderMarginUnitCombo
158: .setSelectedIndex(marginPaddingModel
159: .getMarginUnitList().getIndexOf("px")); //NOI18N
160: }
161: borderMarginData.setUnit(value);
162: }
163: }
164:
165: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
166: private void initComponents() {
167: borderMarginCombo = new javax.swing.JComboBox();
168: borderMarginUnitCombo = new javax.swing.JComboBox();
169:
170: setLayout(new java.awt.BorderLayout(3, 0));
171:
172: borderMarginCombo.setEditable(true);
173: borderMarginCombo
174: .addItemListener(new java.awt.event.ItemListener() {
175: public void itemStateChanged(
176: java.awt.event.ItemEvent evt) {
177: borderMarginComboItemStateChanged(evt);
178: }
179: });
180: borderMarginCombo
181: .addActionListener(new java.awt.event.ActionListener() {
182: public void actionPerformed(
183: java.awt.event.ActionEvent evt) {
184: borderMarginComboActionPerformed(evt);
185: }
186: });
187: borderMarginCombo
188: .addFocusListener(new java.awt.event.FocusAdapter() {
189: public void focusLost(java.awt.event.FocusEvent evt) {
190: borderMarginComboFocusLost(evt);
191: }
192: });
193:
194: add(borderMarginCombo, java.awt.BorderLayout.CENTER);
195:
196: borderMarginUnitCombo
197: .addItemListener(new java.awt.event.ItemListener() {
198: public void itemStateChanged(
199: java.awt.event.ItemEvent evt) {
200: borderMarginUnitComboItemStateChanged(evt);
201: }
202: });
203: borderMarginUnitCombo
204: .addFocusListener(new java.awt.event.FocusAdapter() {
205: public void focusLost(java.awt.event.FocusEvent evt) {
206: borderMarginUnitComboFocusLost(evt);
207: }
208: });
209:
210: add(borderMarginUnitCombo, java.awt.BorderLayout.EAST);
211:
212: }
213:
214: // </editor-fold>//GEN-END:initComponents
215:
216: private void borderMarginUnitComboItemStateChanged(
217: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderMarginUnitComboItemStateChanged
218: if (evt.getStateChange() != evt.DESELECTED) {
219: setborderMargin();
220: }
221: }//GEN-LAST:event_borderMarginUnitComboItemStateChanged
222:
223: private void borderMarginUnitComboFocusLost(
224: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderMarginUnitComboFocusLost
225: setborderMargin();
226: }//GEN-LAST:event_borderMarginUnitComboFocusLost
227:
228: // For accessibility
229: public void setAccessibleName(String comboName, String unitName) {
230: borderMarginCombo.getAccessibleContext().setAccessibleName(
231: comboName);
232: borderMarginUnitCombo.getAccessibleContext().setAccessibleName(
233: unitName);
234: }
235:
236: // For accessibility
237: public void setAccessibleDescription(String comboDesc,
238: String unitDesc) {
239: borderMarginCombo.getAccessibleContext()
240: .setAccessibleDescription(comboDesc);
241: borderMarginUnitCombo.getAccessibleContext()
242: .setAccessibleDescription(unitDesc);
243: }
244:
245: private void borderMarginComboItemStateChanged(
246: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderMarginComboItemStateChanged
247: if (evt.getStateChange() != evt.DESELECTED) {
248: setborderMargin();
249: }
250: }//GEN-LAST:event_borderMarginComboItemStateChanged
251:
252: private void borderMarginComboFocusLost(
253: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderMarginComboFocusLost
254: setborderMargin();
255: }//GEN-LAST:event_borderMarginComboFocusLost
256:
257: private void borderMarginComboActionPerformed(
258: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borderMarginComboActionPerformed
259: setborderMargin();
260: }//GEN-LAST:event_borderMarginComboActionPerformed
261:
262: private void setborderMargin() {
263: String oldValue = borderMarginData.toString();
264: borderMarginData.setUnit(borderMarginUnitCombo
265: .getSelectedItem().toString());
266: borderMarginData.setValue(borderMarginCombo.getSelectedItem()
267: .toString());
268: propertyChangeSupport.firePropertyChange("margin-width",
269: oldValue, borderMarginData.toString()); //NOI18N
270: }
271:
272: // Variables declaration - do not modify//GEN-BEGIN:variables
273: private javax.swing.JComboBox borderMarginCombo;
274: private javax.swing.JComboBox borderMarginUnitCombo;
275: // End of variables declaration//GEN-END:variables
276:
277: }
|