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 PaddingWidthField extends javax.swing.JPanel { 42 PropertyWithUnitData borderPaddingData = new PropertyWithUnitData(); 43 MarginPaddingModel marginPaddingModel = new MarginPaddingModel(); 44 45 46 public PaddingWidthField() { 47 initComponents(); 48 borderPaddingCombo.setModel(marginPaddingModel.getPaddingList()); 49 borderPaddingUnitCombo.setModel(marginPaddingModel.getPaddingUnitList()); 50 51 final JTextField borderPaddingComboEditor = (JTextField ) borderPaddingCombo.getEditor().getEditorComponent(); 53 borderPaddingComboEditor.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 borderPaddingUnitCombo.setEnabled(Utils.isInteger(borderPaddingComboEditor.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 public void setPaddingString(String paddingStr){ 75 if((paddingStr != null) && !paddingStr.equals("")){ 76 if(Utils.isInteger(paddingStr)){ 77 setWidthValue(paddingStr); 78 }else{ 79 String unit = getUnit(paddingStr); 80 setWidthUnit(unit); 81 setWidthValue(paddingStr.replaceAll(unit,"").trim()); 82 } 83 }else{ 84 setWidthValue(null); 85 setWidthUnit(null); 86 } 87 } 88 89 public String getPaddingString(){ 90 return borderPaddingData.toString(); 91 } 92 private String getUnit(String paddingStr){ 93 DefaultComboBoxModel unitList = marginPaddingModel.getPaddingUnitList(); 94 for(int i=0; i< unitList.getSize(); i++){ 95 String unit = (String )unitList.getElementAt(i); 96 if(paddingStr.trim().endsWith(unit)){ 97 return unit; 98 } 99 } 100 return ""; 101 } 102 103 public void setWidthValue(String value){ 104 if((value == null) || value.equals("")){ 105 borderPaddingCombo.setSelectedIndex(0); 106 }else{ 107 borderPaddingCombo.setSelectedItem(value); 108 borderPaddingData.setValue(value); 109 } 110 } 111 112 public void setWidthUnit(String value){ 113 if((value == null) || value.equals("")){ 114 borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf("px")); }else{ 116 if(marginPaddingModel.getMarginUnitList().getIndexOf(value) != -1){ 117 borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf(value)); 118 }else{ 119 borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf("px")); } 121 borderPaddingData.setUnit(value); 122 } 123 } 124 125 129 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 130 propertyChangeSupport.removePropertyChangeListener(l); 131 } 132 133 private void initComponents() { 135 borderPaddingCombo = new javax.swing.JComboBox (); 136 borderPaddingUnitCombo = new javax.swing.JComboBox (); 137 138 setLayout(new java.awt.BorderLayout (5, 0)); 139 140 borderPaddingCombo.setEditable(true); 141 borderPaddingCombo.addActionListener(new java.awt.event.ActionListener () { 142 public void actionPerformed(java.awt.event.ActionEvent evt) { 143 borderPaddingComboActionPerformed(evt); 144 } 145 }); 146 borderPaddingCombo.addFocusListener(new java.awt.event.FocusAdapter () { 147 public void focusLost(java.awt.event.FocusEvent evt) { 148 borderPaddingComboFocusLost(evt); 149 } 150 }); 151 borderPaddingCombo.addItemListener(new java.awt.event.ItemListener () { 152 public void itemStateChanged(java.awt.event.ItemEvent evt) { 153 borderPaddingComboItemStateChanged(evt); 154 } 155 }); 156 157 add(borderPaddingCombo, java.awt.BorderLayout.CENTER); 158 borderPaddingCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDIN_WIDTH_ACCESS_NAME")); 159 borderPaddingCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_FIELD_ACCESS_DESC")); 160 161 borderPaddingUnitCombo.setEnabled(false); 162 borderPaddingUnitCombo.addItemListener(new java.awt.event.ItemListener () { 163 public void itemStateChanged(java.awt.event.ItemEvent evt) { 164 borderPaddingUnitComboItemStateChanged(evt); 165 } 166 }); 167 borderPaddingUnitCombo.addFocusListener(new java.awt.event.FocusAdapter () { 168 public void focusLost(java.awt.event.FocusEvent evt) { 169 borderPaddingUnitComboFocusLost(evt); 170 } 171 }); 172 173 add(borderPaddingUnitCombo, java.awt.BorderLayout.EAST); 174 borderPaddingUnitCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_UNIT_ACCESS_NAME")); 175 borderPaddingUnitCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_UNIT_ACCESS_DESC")); 176 177 } 178 180 private void borderPaddingUnitComboFocusLost(java.awt.event.FocusEvent evt) { setBorderPadding(); 182 } 184 private void borderPaddingUnitComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 186 setBorderPadding(); 187 } 188 } public void setAccessibleName(String comboName, String unitName){ 191 borderPaddingCombo.getAccessibleContext().setAccessibleName(comboName); 192 borderPaddingUnitCombo.getAccessibleContext().setAccessibleName(unitName); 193 } 194 195 public void setAccessibleDescription(String comboDesc, String unitDesc){ 197 borderPaddingCombo.getAccessibleContext().setAccessibleDescription(comboDesc); 198 borderPaddingUnitCombo.getAccessibleContext().setAccessibleDescription(unitDesc); 199 } 200 201 private void borderPaddingComboItemStateChanged(java.awt.event.ItemEvent evt) { if (evt.getStateChange() != evt.DESELECTED) { 203 borderPaddingUnitCombo.setEnabled(Utils.isInteger(borderPaddingCombo.getSelectedItem().toString())); 204 setBorderPadding(); 205 } 206 } 208 private void borderPaddingComboFocusLost(java.awt.event.FocusEvent evt) { setBorderPadding(); 210 } 212 private void borderPaddingComboActionPerformed(java.awt.event.ActionEvent evt) { setBorderPadding(); 214 } 216 private void setBorderPadding(){ 217 String oldValue = borderPaddingData.toString(); 218 borderPaddingData.setUnit(borderPaddingUnitCombo.getSelectedItem().toString()); 219 borderPaddingData.setValue( borderPaddingCombo.getSelectedItem().toString()); 220 propertyChangeSupport.firePropertyChange("padding-width", oldValue, borderPaddingData.toString()); } 222 223 private javax.swing.JComboBox borderPaddingCombo; 225 private javax.swing.JComboBox borderPaddingUnitCombo; 226 228 } 229 | Popular Tags |