1 19 20 package org.netbeans.modules.j2ee.common.method.impl; 21 22 import java.awt.event.KeyAdapter ; 23 import java.awt.event.KeyEvent ; 24 import java.awt.event.MouseAdapter ; 25 import java.awt.event.MouseEvent ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import javax.swing.event.TableModelEvent ; 30 import javax.swing.event.TableModelListener ; 31 import javax.swing.table.AbstractTableModel ; 32 import org.netbeans.modules.j2ee.common.method.MethodModel; 33 import org.openide.util.NbBundle; 34 35 39 public final class ParametersPanel extends javax.swing.JPanel { 40 41 private static final int COL_NAME_INDEX = 0; 42 private static final int COL_TYPE_INDEX = 1; 43 private static final int COL_FINAL_INDEX = 2; 44 private static final String [] columnNames = { 45 NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.LBL_Name"), 46 NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.LBL_Type"), 47 NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.LBL_Final"), 48 }; 49 50 private final ParamsTableModel tableModel; 51 52 public ParametersPanel(List <MethodModel.Variable> parameters) { 53 initComponents(); 54 tableModel = new ParamsTableModel(parameters); 55 table.setModel(tableModel); 56 table.addMouseListener(new MouseAdapter () { 57 public void mouseClicked(MouseEvent e) { 58 updateButtons(); 59 } 60 }); 61 table.addKeyListener(new KeyAdapter () { 62 public void keyPressed(KeyEvent e) { 63 updateButtons(); 64 } 65 }); 66 } 73 74 public List <MethodModel.Variable> getParameters() { 75 return tableModel.getParameters(); 76 } 77 78 83 private void initComponents() { 85 86 jScrollPane1 = new javax.swing.JScrollPane (); 87 table = new javax.swing.JTable (); 88 addButton = new javax.swing.JButton (); 89 removeButton = new javax.swing.JButton (); 90 upButton = new javax.swing.JButton (); 91 downButton = new javax.swing.JButton (); 92 93 table.setModel(new javax.swing.table.DefaultTableModel ( 94 new Object [][] { 95 96 }, 97 new String [] { 98 99 } 100 )); 101 jScrollPane1.setViewportView(table); 102 103 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.addButton.text")); addButton.addActionListener(new java.awt.event.ActionListener () { 105 public void actionPerformed(java.awt.event.ActionEvent evt) { 106 addButtonActionPerformed(evt); 107 } 108 }); 109 110 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.removeButton.text")); removeButton.addActionListener(new java.awt.event.ActionListener () { 112 public void actionPerformed(java.awt.event.ActionEvent evt) { 113 removeButtonActionPerformed(evt); 114 } 115 }); 116 117 org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.upButton.text")); upButton.setEnabled(false); 119 120 org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.downButton.text")); downButton.setEnabled(false); 122 123 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 124 this.setLayout(layout); 125 layout.setHorizontalGroup( 126 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 127 .add(layout.createSequentialGroup() 128 .addContainerGap() 129 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE) 130 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 131 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 132 .add(addButton) 133 .add(removeButton) 134 .add(upButton) 135 .add(downButton)) 136 .addContainerGap()) 137 ); 138 139 layout.linkSize(new java.awt.Component [] {addButton, downButton, removeButton, upButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL); 140 141 layout.setVerticalGroup( 142 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 143 .add(layout.createSequentialGroup() 144 .addContainerGap() 145 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 146 .add(layout.createSequentialGroup() 147 .add(addButton) 148 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 149 .add(removeButton) 150 .add(22, 22, 22) 151 .add(upButton) 152 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 153 .add(downButton)) 154 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)) 155 .addContainerGap()) 156 ); 157 } 159 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { int selectedRow = table.getSelectedRow(); 161 if (selectedRow > -1) { 162 tableModel.removeParameter(selectedRow); 163 } 164 if (selectedRow == table.getRowCount()) { 165 selectedRow--; 166 } 167 table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow); 168 updateButtons(); 169 } 171 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { int index = tableModel.addParameter(); 173 table.getSelectionModel().setSelectionInterval(index, index); 175 updateButtons(); 176 } 178 179 private javax.swing.JButton addButton; 181 private javax.swing.JButton downButton; 182 private javax.swing.JScrollPane jScrollPane1; 183 private javax.swing.JButton removeButton; 184 private javax.swing.JTable table; 185 private javax.swing.JButton upButton; 186 188 private void updateButtons() { 189 int selectedRowsCount = table.getSelectedRowCount(); 190 removeButton.setEnabled(selectedRowsCount != 0); 191 upButton.setEnabled(selectedRowsCount == 1); 192 downButton.setEnabled(selectedRowsCount == 1); 193 } 194 195 static class ParamsTableModel extends AbstractTableModel { 197 198 private final List <MethodModel.Variable> parameters; 199 200 public ParamsTableModel(List <MethodModel.Variable> parameters) { 201 this.parameters = new ArrayList <MethodModel.Variable>(parameters); 202 } 203 204 public List <MethodModel.Variable> getParameters() { 205 return parameters; 206 } 207 208 public int addParameter() { 209 String name = generateUniqueName("parameter"); 210 MethodModel.Variable parameter = MethodModel.Variable.create("java.lang.String", name, false); 211 int index = parameters.size(); 212 parameters.add(parameter); 213 fireTableRowsInserted(index, index); 214 return index; 215 } 216 217 public void removeParameter(int index) { 218 parameters.remove(index); 219 fireTableRowsDeleted(index, index); 220 } 221 222 public int getRowCount() { 223 return parameters.size(); 224 } 225 226 public int getColumnCount() { 227 return 3; 228 } 229 230 public Object getValueAt(int row, int column) { 231 Object result = null; 232 MethodModel.Variable parameter = parameters.get(row); 233 if (parameter != null) { 234 switch (column) { 235 case COL_NAME_INDEX: result = parameter.getName(); break; 236 case COL_TYPE_INDEX: result = parameter.getType(); break; 237 case COL_FINAL_INDEX: result = parameter.getFinalModifier(); break; 238 default: 239 } 240 } 241 return result; 242 } 243 244 public String getColumnName(int column) { 245 return columnNames[column]; 246 } 247 248 public boolean isCellEditable(int row, int column) { 249 return true; 250 } 251 252 public void setValueAt(Object aValue, int row, int column) { 253 MethodModel.Variable parameter = parameters.get(row); 254 MethodModel.Variable changedParameter = MethodModel.Variable.create( 255 column == COL_TYPE_INDEX ? (String ) aValue : parameter.getType(), 256 column == COL_NAME_INDEX ? (String ) aValue : parameter.getName(), 257 column == COL_FINAL_INDEX ? (Boolean ) aValue : parameter.getFinalModifier() 258 ); 259 parameters.set(row, changedParameter); 260 fireTableCellUpdated(row, column); 261 } 262 263 public Class getColumnClass(int c) { 267 return getValueAt(0, c).getClass(); 268 } 269 270 private String generateUniqueName(String name) { 271 List <Integer > numberSuffixes = new ArrayList <Integer >(); 272 for (MethodModel.Variable variable : parameters) { 273 if (!name.equals(variable.getName()) && variable.getName().startsWith(name)) { 274 String suffix = variable.getName().substring(name.length()); 275 if (isNumber(suffix)) { 276 numberSuffixes.add(Integer.parseInt(suffix)); 277 } 278 } 279 } 280 Collections.sort(numberSuffixes); 281 String result = name; 282 if (numberSuffixes.size() > 0) { 283 int newSuffix = numberSuffixes.get(numberSuffixes.size() - 1) + 1; 284 result = name + newSuffix; 285 } else if (parameters.size() > 0) { 286 result = name + 1; 287 } 288 return result; 289 } 290 291 private boolean isNumber(String value) { 292 for (char character : value.toCharArray()) { 293 if (!Character.isDigit(character)) { 294 return false; 295 } 296 } 297 return true; } 299 300 } 301 302 } 303 | Popular Tags |