KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > method > impl > ParametersPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.common.method.impl;
21
22 import java.awt.event.KeyAdapter JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.event.MouseAdapter JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.event.TableModelEvent JavaDoc;
30 import javax.swing.event.TableModelListener JavaDoc;
31 import javax.swing.table.AbstractTableModel JavaDoc;
32 import org.netbeans.modules.j2ee.common.method.MethodModel;
33 import org.openide.util.NbBundle;
34
35 /**
36  *
37  * @author Martin Adamek
38  */

39 public final class ParametersPanel extends javax.swing.JPanel JavaDoc {
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 JavaDoc[] 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 JavaDoc<MethodModel.Variable> parameters) {
53         initComponents();
54         tableModel = new ParamsTableModel(parameters);
55         table.setModel(tableModel);
56         table.addMouseListener(new MouseAdapter JavaDoc() {
57             public void mouseClicked(MouseEvent JavaDoc e) {
58                 updateButtons();
59             }
60         });
61         table.addKeyListener(new KeyAdapter JavaDoc() {
62             public void keyPressed(KeyEvent JavaDoc e) {
63                 updateButtons();
64             }
65         });
66 // tableModel.addTableModelListener(new TableModelListener() {
67
// public void tableChanged(TableModelEvent tableModelEvent) {
68
// System.out.println("### tableModelEvent " + tableModelEvent.getType());
69
// updateButtons();
70
// }
71
// });
72
}
73     
74     public List JavaDoc<MethodModel.Variable> getParameters() {
75         return tableModel.getParameters();
76     }
77
78     /** This method is called from within the constructor to
79      * initialize the form.
80      * WARNING: Do NOT modify this code. The content of this method is
81      * always regenerated by the Form Editor.
82      */

83     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
84
private void initComponents() {
85
86         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
87         table = new javax.swing.JTable JavaDoc();
88         addButton = new javax.swing.JButton JavaDoc();
89         removeButton = new javax.swing.JButton JavaDoc();
90         upButton = new javax.swing.JButton JavaDoc();
91         downButton = new javax.swing.JButton JavaDoc();
92
93         table.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
94             new Object JavaDoc [][] {
95
96             },
97             new String JavaDoc [] {
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")); // NOI18N
104
addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
105             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
106                 addButtonActionPerformed(evt);
107             }
108         });
109
110         org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.removeButton.text")); // NOI18N
111
removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
112             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
113                 removeButtonActionPerformed(evt);
114             }
115         });
116
117         org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.upButton.text")); // NOI18N
118
upButton.setEnabled(false);
119
120         org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getMessage(ParametersPanel.class, "ParametersPanel.downButton.text")); // NOI18N
121
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 JavaDoc[] {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     }// </editor-fold>//GEN-END:initComponents
158

159 private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
160
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 }//GEN-LAST:event_removeButtonActionPerformed
170

171 private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
172
int index = tableModel.addParameter();
173 // System.out.println("### INDEX: " + index + " from " + table.getRowCount());
174
table.getSelectionModel().setSelectionInterval(index, index);
175     updateButtons();
176 }//GEN-LAST:event_addButtonActionPerformed
177

178     
179     // Variables declaration - do not modify//GEN-BEGIN:variables
180
private javax.swing.JButton JavaDoc addButton;
181     private javax.swing.JButton JavaDoc downButton;
182     private javax.swing.JScrollPane JavaDoc jScrollPane1;
183     private javax.swing.JButton JavaDoc removeButton;
184     private javax.swing.JTable JavaDoc table;
185     private javax.swing.JButton JavaDoc upButton;
186     // End of variables declaration//GEN-END:variables
187

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     // accessible for test
196
static class ParamsTableModel extends AbstractTableModel JavaDoc {
197         
198         private final List JavaDoc<MethodModel.Variable> parameters;
199         
200         public ParamsTableModel(List JavaDoc<MethodModel.Variable> parameters) {
201             this.parameters = new ArrayList JavaDoc<MethodModel.Variable>(parameters);
202         }
203         
204         public List JavaDoc<MethodModel.Variable> getParameters() {
205             return parameters;
206         }
207         
208         public int addParameter() {
209             String JavaDoc 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 JavaDoc getValueAt(int row, int column) {
231             Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc) aValue : parameter.getType(),
256                     column == COL_NAME_INDEX ? (String JavaDoc) aValue : parameter.getName(),
257                     column == COL_FINAL_INDEX ? (Boolean JavaDoc) aValue : parameter.getFinalModifier()
258                     );
259             parameters.set(row, changedParameter);
260             fireTableCellUpdated(row, column);
261         }
262         
263         // JTable uses this method to determine the default renderer/editor for each cell.
264
// If we didn't implement this method, then the last column would contain
265
// text ("true"/"false"), rather than a check box.
266
public Class JavaDoc getColumnClass(int c) {
267             return getValueAt(0, c).getClass();
268         }
269         
270         private String JavaDoc generateUniqueName(String JavaDoc name) {
271             List JavaDoc<Integer JavaDoc> numberSuffixes = new ArrayList JavaDoc<Integer JavaDoc>();
272             for (MethodModel.Variable variable : parameters) {
273                 if (!name.equals(variable.getName()) && variable.getName().startsWith(name)) {
274                     String JavaDoc 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 JavaDoc 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 JavaDoc value) {
292             for (char character : value.toCharArray()) {
293                 if (!Character.isDigit(character)) {
294                   return false;
295                 }
296             }
297           return true;//!value.trim().equals("");
298
}
299         
300     }
301
302 }
303
Popular Tags