1 19 20 21 package org.netbeans.modules.i18n.java; 22 23 24 import java.util.ArrayList ; 25 import java.util.ResourceBundle ; 26 import javax.swing.AbstractListModel ; 27 import javax.swing.event.ListSelectionListener ; 28 import javax.swing.event.ListSelectionEvent ; 29 import javax.swing.JPanel ; 30 31 import org.netbeans.modules.i18n.I18nUtil; 32 33 import org.openide.util.HelpCtx; 34 35 36 41 public class ParamsPanel extends JPanel { 42 43 44 private ArrayList arguments = new ArrayList (); 45 46 47 private int editingRow = -1; 48 49 50 private ParamsListModel model; 51 52 private final ResourceBundle bundle; 53 54 55 static final long serialVersionUID =-3754019215574878093L; 56 57 58 59 public ParamsPanel() { 60 bundle = org.openide.util.NbBundle.getBundle(ParamsPanel.class); 61 initComponents (); 62 initAccessibility(); 63 paramsList.setModel(getListModel()); 64 paramsList.getSelectionModel().addListSelectionListener( 65 new ListSelectionListener () { 66 public void valueChanged(ListSelectionEvent e) { 67 if (paramsList.getSelectedIndex() != -1) 68 updateEditor(paramsList.getSelectedIndex()); 69 removeParamButton.setEnabled(paramsList.getSelectedIndex() != -1); 70 } 71 } 72 ); 73 removeParamButton.setEnabled(paramsList.getSelectedIndex() != -1); 74 HelpCtx.setHelpIDString(this, I18nUtil.HELP_ID_ADDPARAMS); 75 } 76 77 78 public void setArguments(String [] args) { 79 arguments.clear(); 80 for (int i = 0; i < args.length; i++) { 81 arguments.add(args[i]); 82 } 83 if (getListModel().getSize() > 0) 85 getListModel().fireIntervalAdded(0, getListModel().getSize() - 1); 86 if (getListModel().getSize() > 0) 87 editRow(0); 88 else 89 editRow(-1); 90 } 91 92 93 public String [] getArguments() { 94 commitChanges(); 95 96 int j = -1; 98 for (int i = 0; i < arguments.size(); i++) 99 if (((String )arguments.get(i)).trim().length() > 0) 100 j = i; 101 102 String [] args = new String [j + 1]; 103 for (int i = 0; i <= j; i++) 104 args[i] = (String )arguments.get(i); 105 106 return args; 107 } 108 109 110 private void commitChanges() { 111 if (editingRow != -1) { 113 arguments.set(editingRow, codePane.getText()); 115 getListModel().fireContentsChanged(editingRow, editingRow); 116 } 117 } 118 119 121 private void editRow(int row) { 122 if (row != -1) 123 paramsList.setSelectedIndex(row); 124 else 125 paramsList.setSelectedIndices(new int[0]); 126 } 127 128 129 private void updateEditor(int row) { 130 commitChanges(); 131 editingRow = row; 132 if (row == -1) { 133 codePane.setText(""); codePane.setEnabled(false); 137 } 138 else { 139 codePane.setText((String )arguments.get(editingRow)); 141 codePane.setEnabled(true); 143 codePane.requestFocus(); 144 } 145 } 146 147 148 private ParamsListModel getListModel() { 149 if (model == null) 150 model = new ParamsListModel (); 151 return model; 152 } 153 154 155 private void initAccessibility() { 156 this.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_ParamsPanel")); 157 removeParamButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_RemoveButton")); 158 addParamButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_CTL_AddButton")); 159 paramsList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_paramsList")); 160 } 161 162 167 private void initComponents() { 169 java.awt.GridBagConstraints gridBagConstraints; 170 171 paramLabel = new javax.swing.JLabel (); 172 codeLabel = new javax.swing.JLabel (); 173 paramsScroll = new javax.swing.JScrollPane (); 174 paramsList = new javax.swing.JList (); 175 addRemovePanel = new javax.swing.JPanel (); 176 addParamButton = new javax.swing.JButton (); 177 removeParamButton = new javax.swing.JButton (); 178 codeScroll = new javax.swing.JScrollPane (); 179 codePane = new javax.swing.JEditorPane (); 180 181 setLayout(new java.awt.GridBagLayout ()); 182 183 paramLabel.setLabelFor(paramsList); 184 org.openide.awt.Mnemonics.setLocalizedText(paramLabel, bundle.getString("LBL_Parameters")); gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridwidth = 2; 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 188 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 189 add(paramLabel, gridBagConstraints); 190 191 codeLabel.setLabelFor(codePane); 192 org.openide.awt.Mnemonics.setLocalizedText(codeLabel, bundle.getString("LBL_Code")); gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridx = 0; 195 gridBagConstraints.gridy = 2; 196 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 197 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 198 add(codeLabel, gridBagConstraints); 199 200 paramsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 201 paramsList.setVisibleRowCount(3); 202 paramsScroll.setViewportView(paramsList); 203 204 gridBagConstraints = new java.awt.GridBagConstraints (); 205 gridBagConstraints.gridx = 0; 206 gridBagConstraints.gridy = 1; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 208 gridBagConstraints.weightx = 1.0; 209 gridBagConstraints.weighty = 1.0; 210 gridBagConstraints.insets = new java.awt.Insets (2, 12, 0, 0); 211 add(paramsScroll, gridBagConstraints); 212 213 addRemovePanel.setLayout(new java.awt.GridBagLayout ()); 214 215 org.openide.awt.Mnemonics.setLocalizedText(addParamButton, bundle.getString("CTL_AddButton")); addParamButton.addActionListener(new java.awt.event.ActionListener () { 217 public void actionPerformed(java.awt.event.ActionEvent evt) { 218 addParamButtonActionPerformed(evt); 219 } 220 }); 221 gridBagConstraints = new java.awt.GridBagConstraints (); 222 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 223 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 224 gridBagConstraints.weightx = 1.0; 225 addRemovePanel.add(addParamButton, gridBagConstraints); 226 227 org.openide.awt.Mnemonics.setLocalizedText(removeParamButton, bundle.getString("CTL_RemoveButton")); removeParamButton.addActionListener(new java.awt.event.ActionListener () { 229 public void actionPerformed(java.awt.event.ActionEvent evt) { 230 removeParamButtonActionPerformed(evt); 231 } 232 }); 233 gridBagConstraints = new java.awt.GridBagConstraints (); 234 gridBagConstraints.gridx = 0; 235 gridBagConstraints.gridy = 1; 236 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 237 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; 238 gridBagConstraints.weightx = 1.0; 239 gridBagConstraints.weighty = 1.0; 240 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 241 addRemovePanel.add(removeParamButton, gridBagConstraints); 242 243 gridBagConstraints = new java.awt.GridBagConstraints (); 244 gridBagConstraints.gridx = 1; 245 gridBagConstraints.gridy = 1; 246 gridBagConstraints.gridheight = 2; 247 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 248 gridBagConstraints.weighty = 1.0; 249 gridBagConstraints.insets = new java.awt.Insets (2, 11, 0, 11); 250 add(addRemovePanel, gridBagConstraints); 251 252 codePane.setContentType("text/x-java"); 253 codePane.addFocusListener(new java.awt.event.FocusAdapter () { 254 public void focusLost(java.awt.event.FocusEvent evt) { 255 codePaneFocusLost(evt); 256 } 257 }); 258 codeScroll.setViewportView(codePane); 259 260 gridBagConstraints = new java.awt.GridBagConstraints (); 261 gridBagConstraints.gridx = 0; 262 gridBagConstraints.gridy = 3; 263 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 264 gridBagConstraints.weightx = 1.0; 265 gridBagConstraints.weighty = 1.0; 266 gridBagConstraints.insets = new java.awt.Insets (2, 12, 11, 0); 267 add(codeScroll, gridBagConstraints); 268 } 270 private void codePaneFocusLost (java.awt.event.FocusEvent evt) { commitChanges(); 272 } 274 private void removeParamButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = paramsList.getSelectedIndex(); 276 if (index == -1) return; 277 arguments.remove(index); 278 getListModel().fireIntervalRemoved(index, index); 279 if (index >= arguments.size()) index--; 280 editingRow = -1; editRow(index); 282 } 284 private void addParamButtonActionPerformed (java.awt.event.ActionEvent evt) { arguments.add(""); 286 getListModel().fireIntervalAdded(getListModel().getSize() - 1, getListModel().getSize() - 1); 288 editRow(getListModel().getSize() - 1); 289 } 291 292 private javax.swing.JButton addParamButton; 294 private javax.swing.JPanel addRemovePanel; 295 private javax.swing.JLabel codeLabel; 296 private javax.swing.JEditorPane codePane; 297 private javax.swing.JScrollPane codeScroll; 298 private javax.swing.JLabel paramLabel; 299 private javax.swing.JList paramsList; 300 private javax.swing.JScrollPane paramsScroll; 301 private javax.swing.JButton removeParamButton; 302 304 305 protected class ParamsListModel extends AbstractListModel { 306 307 308 static final long serialVersionUID =6832148996617470334L; 309 310 311 public ParamsListModel () { 312 } 313 314 315 316 public int getSize() { 317 return arguments.size(); 318 } 319 320 322 public Object getElementAt(int index) { 323 return "{" + index + "} " + (String )arguments.get(index); 324 } 325 326 329 public void fireContentsChanged(int index0, int index1) { 330 super.fireContentsChanged(this, index0, index1); 331 } 332 333 336 public void fireIntervalAdded(int index0, int index1) { 337 super.fireIntervalAdded(this, index0, index1); 338 } 339 340 343 public void fireIntervalRemoved(int index0, int index1) { 344 super.fireIntervalRemoved(this, index0, index1); 345 } 346 } 347 } 348 | Popular Tags |