1 19 20 25 26 package org.netbeans.modules.css.visual.ui; 27 28 import org.netbeans.modules.css.visual.model.MarginPaddingModel; 29 import org.netbeans.modules.css.visual.model.PropertyWithUnitData; 30 import org.netbeans.modules.css.visual.model.Utils; 31 import java.beans.PropertyChangeSupport ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.JTextField ; 34 import javax.swing.SwingUtilities ; 35 36 41 public class MarginWidthField extends javax.swing.JPanel { 42 PropertyWithUnitData borderMarginData = new PropertyWithUnitData(); 43 MarginPaddingModel marginPaddingModel = new MarginPaddingModel(); 44 45 46 public MarginWidthField() { 47 initComponents(); 48 borderMarginCombo.setModel(marginPaddingModel.getMarginList()); 49 borderMarginUnitCombo.setModel(marginPaddingModel.getMarginUnitList()); 50 51 final JTextField borderMarginComboEditor = (JTextField ) borderMarginCombo.getEditor().getEditorComponent(); 53 borderMarginComboEditor.addKeyListener(new java.awt.event.KeyAdapter () { 54 public void keyTyped(java.awt.event.KeyEvent evt) { 55 SwingUtilities.invokeLater(new Runnable (){ 56 public void run(){ 57 borderMarginUnitCombo.setEnabled(Utils.isInteger(borderMarginComboEditor.getText())); 58 } 59 }); 60 } 61 }); 62 } 63 64 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 65 66 70 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 71 propertyChangeSupport.addPropertyChangeListener(l); 72 } 73 74 78 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 79 propertyChangeSupport.removePropertyChangeListener(l); 80 } 81 82 public void setMarginString(String marginStr){ 83 if((marginStr != null) && !marginStr.equals("")){ 84 if(Utils.isInteger(marginStr)){ 85 setMarginValue(marginStr); 86 }else{ 87 String unit = getUnit(marginStr); 88 setMarginUnit(unit); 89 setMarginValue(marginStr.replaceAll(unit,"").trim()); 90 } 91 }else{ 92 setMarginValue(null); 93 setMarginUnit(null); 94 } 95 } 96 97 public String getMarginString(){ 98 return borderMarginData.toString(); 99 } 100 101 private String getUnit(String positionStr){ 102 DefaultComboBoxModel unitList = marginPaddingModel.getMarginUnitList(); 103 for(int i=0; i< unitList.getSize(); i++){ 104 String unit = (String )unitList.getElementAt(i); 105 if(positionStr.trim().endsWith(unit)){ 106 return unit; 107 } 108 } 109 return ""; 110 } 111 112 public void setMarginValue(String value){ 113 if((value == null) || value.equals("")){ 114 borderMarginCombo.setSelectedIndex(0); 115 }else{ 116 borderMarginCombo.setSelectedItem(value); 117 borderMarginData.setValue(value); 118 } 119 } 120 121 public void setMarginUnit(String value){ 122 if((value == null) || value.equals("")){ 123 borderMarginUnitCombo.setSelectedIndex(marginPaddingModel.getMarginUnitList().getIndexOf("px")); }else{ 125 if(marginPaddingModel.getMarginUnitList().getIndexOf(value) != -1){ 126 borderMarginUnitCombo.setSelectedIndex(marginPaddingModel.getMarginUnitList().getIndexOf(value)); 127 }else{ 128 borderMarginUnitCombo.setSelectedIndex(marginPaddingModel.getMarginUnitList().getIndexOf("px")); } 130 borderMarginData.setUnit(value); 131 } 132 } 133 134 private void initComponents() { 136 borderMarginCombo = new javax.swing.JComboBox (); 137 borderMarginUnitCombo = new javax.swing.JComboBox (); 138 139 setLayout(new java.awt.BorderLayout (3, 0)); 140 141 borderMarginCombo.setEditable(true); 142 borderMarginCombo.addActionListener(new java.awt.event.ActionListener () { 143 public void actionPerformed(java.awt.event.ActionEvent evt) { 144 borderMarginComboActionPerformed(evt); 145 } 146 }); 147 borderMarginCombo.addFocusListener(new java.awt.event.FocusAdapter () { 148 public void focusLost(java.awt.event.FocusEvent evt) { 149 borderMarginComboFocusLost(evt); 150 } 151 }); 152 borderMarginCombo.addItemListener(new java.awt.event.ItemListener () { 153 public void itemStateChanged(java.awt.event.ItemEvent evt) { 154 borderMarginComboItemStateChanged(evt); 155 } 156 }); 157 158 add(borderMarginCombo, java.awt.BorderLayout.CENTER); 159 borderMarginCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MarginWidthField.class, "MARGIN_WIDTH_FIELD_ACCESS_NAME")); 160 borderMarginCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MarginWidthField.class, "MARGIN_WIDTH_FIELD_ACCESS_DESC")); 161 162 borderMarginUnitCombo.setEnabled(false); 163 borderMarginUnitCombo.addItemListener(new java.awt.event.ItemListener () { 164 public void itemStateChanged(java.awt.event.ItemEvent evt) { 165 borderMarginUnitComboItemStateChanged(evt); 166 } 167 }); 168 borderMarginUnitCombo.addFocusListener(new java.awt.event.FocusAdapter () { 169 public void focusLost(java.awt.event.FocusEvent evt) { 170 borderMarginUnitComboFocusLost(evt); 171 } 172 }); 173 174 add(borderMarginUnitCombo, java.awt.BorderLayout.EAST); 175 borderMarginUnitCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MarginWidthField.class, "MARGIN_WIDTH_UNIT_ACCESS_NAME")); 176 borderMarginUnitCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(MarginWidthField.class, "MARGIN_WIDTH_UNIT_ACCESS_DESC")); 177 178 } 179 181 private void borderMarginUnitComboFocusLost(java.awt.event.FocusEvent evt) { setBorderMargin(); 183 } 185 private void borderMarginUnitComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 187 setBorderMargin(); 188 } 189 } public void setAccessibleName(String comboName, String unitName){ 192 borderMarginCombo.getAccessibleContext().setAccessibleName(comboName); 193 borderMarginUnitCombo.getAccessibleContext().setAccessibleName(unitName); 194 } 195 196 public void setAccessibleDescription(String comboDesc, String unitDesc){ 198 borderMarginCombo.getAccessibleContext().setAccessibleDescription(comboDesc); 199 borderMarginUnitCombo.getAccessibleContext().setAccessibleDescription(unitDesc); 200 } 201 202 private void borderMarginComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 204 borderMarginUnitCombo.setEnabled(Utils.isInteger(borderMarginCombo.getSelectedItem().toString())); 205 setBorderMargin(); 206 } 207 } 209 private void borderMarginComboFocusLost(java.awt.event.FocusEvent evt) { setBorderMargin(); 211 } 213 private void borderMarginComboActionPerformed(java.awt.event.ActionEvent evt) { setBorderMargin(); 215 } 217 private void setBorderMargin(){ 218 String oldValue = borderMarginData.toString(); 219 borderMarginData.setUnit(borderMarginUnitCombo.getSelectedItem().toString()); 220 borderMarginData.setValue( borderMarginCombo.getSelectedItem().toString()); 221 propertyChangeSupport.firePropertyChange("margin-width", oldValue, borderMarginData.toString()); } 223 224 private javax.swing.JComboBox borderMarginCombo; 226 private javax.swing.JComboBox borderMarginUnitCombo; 227 229 } 230 | Popular Tags |