1 19 20 25 26 package org.netbeans.modules.css.visual.ui; 27 28 import org.netbeans.modules.css.visual.model.BorderModel; 29 import org.netbeans.modules.css.visual.model.PropertyWithUnitData; 30 import org.netbeans.modules.css.visual.model.BorderModel; 31 import java.beans.PropertyChangeSupport ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.JTextField ; 34 import javax.swing.SwingUtilities ; 35 import org.netbeans.modules.css.visual.model.Utils; 36 37 42 public class BorderWidthField extends javax.swing.JPanel { 43 PropertyWithUnitData borderWidthData = new PropertyWithUnitData(); 44 45 BorderModel borderModel = new BorderModel(); 46 47 48 public BorderWidthField() { 49 initComponents(); 50 borderWidthCombo.setModel(borderModel.getWidthList()); 51 borderWidthUnitCombo.setModel(borderModel.getWidthUnitList()); 52 53 final JTextField borderWidthComboEditor = (JTextField ) borderWidthCombo.getEditor().getEditorComponent(); 55 borderWidthComboEditor.addKeyListener(new java.awt.event.KeyAdapter () { 56 public void keyTyped(java.awt.event.KeyEvent evt) { 57 SwingUtilities.invokeLater(new Runnable (){ 58 public void run(){ 59 borderWidthUnitCombo.setEnabled(Utils.isInteger(borderWidthComboEditor.getText())); 60 } 61 }); 62 } 63 }); 64 } 65 66 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 67 68 72 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 73 propertyChangeSupport.addPropertyChangeListener(l); 74 } 75 76 80 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 81 propertyChangeSupport.removePropertyChangeListener(l); 82 } 83 84 public void setWidthString(String widthStr){ 85 if((widthStr != null) && !widthStr.equals("")){ 86 if(Utils.isInteger(widthStr)){ 87 setWidthValue(widthStr); 88 }else{ 89 String unit = getUnit(widthStr); 90 setWidthUnit(unit); 91 setWidthValue(widthStr.replaceAll(unit,"").trim()); 92 } 93 }else{ 94 setWidthValue(null); 95 setWidthUnit(null); 96 } 97 } 98 99 public String getWidthString(){ 100 return borderWidthData.toString(); 101 } 102 103 private String getUnit(String widthStr){ 104 DefaultComboBoxModel unitList = borderModel.getWidthUnitList(); 105 for(int i=0; i< unitList.getSize(); i++){ 106 String unit = (String )unitList.getElementAt(i); 107 if(widthStr.trim().endsWith(unit)){ 108 return unit; 109 } 110 } 111 return ""; 112 } 113 114 public void setWidthValue(String value){ 115 if((value == null) || value.equals("")){ 116 borderWidthCombo.setSelectedIndex(0); 117 }else{ 118 borderWidthCombo.setSelectedItem(value); 119 borderWidthData.setValue(value); 120 } 121 } 122 123 public void setWidthUnit(String value){ 124 if((value == null) || value.equals("")){ 125 borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf("px")); 126 }else{ 127 if(borderModel.getWidthUnitList().getIndexOf(value) != -1){ 128 borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf(value)); 129 }else{ 130 borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf("px")); 131 } 132 borderWidthData.setUnit(value); 133 } 134 } 135 136 private void initComponents() { 138 borderWidthCombo = new javax.swing.JComboBox (); 139 borderWidthUnitCombo = new javax.swing.JComboBox (); 140 141 setLayout(new java.awt.BorderLayout (3, 0)); 142 143 borderWidthCombo.setEditable(true); 144 borderWidthCombo.addItemListener(new java.awt.event.ItemListener () { 145 public void itemStateChanged(java.awt.event.ItemEvent evt) { 146 borderWidthComboItemStateChanged(evt); 147 } 148 }); 149 borderWidthCombo.addActionListener(new java.awt.event.ActionListener () { 150 public void actionPerformed(java.awt.event.ActionEvent evt) { 151 borderWidthComboActionPerformed(evt); 152 } 153 }); 154 borderWidthCombo.addFocusListener(new java.awt.event.FocusAdapter () { 155 public void focusLost(java.awt.event.FocusEvent evt) { 156 borderWidthComboFocusLost(evt); 157 } 158 }); 159 160 add(borderWidthCombo, java.awt.BorderLayout.CENTER); 161 borderWidthCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_FIELD_ACCESS_NAME")); 162 borderWidthCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_FIELD_ACCESS_DESC")); 163 164 borderWidthUnitCombo.setEnabled(false); 165 borderWidthUnitCombo.addItemListener(new java.awt.event.ItemListener () { 166 public void itemStateChanged(java.awt.event.ItemEvent evt) { 167 borderWidthUnitComboItemStateChanged(evt); 168 } 169 }); 170 borderWidthUnitCombo.addFocusListener(new java.awt.event.FocusAdapter () { 171 public void focusLost(java.awt.event.FocusEvent evt) { 172 borderWidthUnitComboFocusLost(evt); 173 } 174 }); 175 176 add(borderWidthUnitCombo, java.awt.BorderLayout.EAST); 177 borderWidthUnitCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_UNIT_ACCESS_NAME")); 178 borderWidthUnitCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_UNIT_ACCESS_DESC")); 179 180 } 181 183 private void borderWidthUnitComboFocusLost(java.awt.event.FocusEvent evt) { setBorderWidth(); 185 } 187 private void borderWidthUnitComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 189 setBorderWidth(); 190 } 191 } 193 public void setAccessibleName(String comboName, String unitName){ 195 borderWidthCombo.getAccessibleContext().setAccessibleName(comboName); 196 borderWidthUnitCombo.getAccessibleContext().setAccessibleName(unitName); 197 } 198 199 public void setAccessibleDescription(String comboDesc, String unitDesc){ 201 borderWidthCombo.getAccessibleContext().setAccessibleDescription(comboDesc); 202 borderWidthUnitCombo.getAccessibleContext().setAccessibleDescription(unitDesc); 203 } 204 205 private void borderWidthComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 207 setBorderWidth(); 208 borderWidthUnitCombo.setEnabled(Utils.isInteger(borderWidthCombo.getSelectedItem().toString())); 209 } 210 } 212 private void borderWidthComboFocusLost(java.awt.event.FocusEvent evt) { setBorderWidth(); 214 } 216 private void borderWidthComboActionPerformed(java.awt.event.ActionEvent evt) { setBorderWidth(); 218 } 220 private void setBorderWidth(){ 221 String oldValue = borderWidthData.toString(); 222 borderWidthData.setUnit(borderWidthUnitCombo.getSelectedItem().toString()); 223 borderWidthData.setValue( borderWidthCombo.getSelectedItem().toString()); 224 propertyChangeSupport.firePropertyChange("border-width", oldValue, borderWidthData.toString()); } 226 227 private javax.swing.JComboBox borderWidthCombo; 229 private javax.swing.JComboBox borderWidthUnitCombo; 230 232 } 233 | Popular Tags |