KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > ParameterizedQueryDialog


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /**
21  * A dialog that presents and asks user to input the parameter values
22  * for parameterized SQL
23  * @author Sanjay Dhamankar
24  */

25
26 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
27
28 import java.awt.Component JavaDoc;
29 import java.awt.Dialog JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32
33 import javax.swing.DefaultCellEditor JavaDoc;
34 import javax.swing.JDialog JavaDoc;
35 import javax.swing.JTable JavaDoc;
36 import javax.swing.JTextField JavaDoc;
37
38 import javax.swing.table.*;
39
40 import org.openide.DialogDescriptor;
41 import org.openide.DialogDisplayer;
42 import org.openide.util.HelpCtx;
43 import org.openide.util.NbBundle;
44
45 public class ParameterizedQueryDialog extends javax.swing.JPanel JavaDoc {
46
47     /** A return status code - returned if Cancel button has been pressed */
48     public static final int RETURNED_CANCEL = 0;
49
50     /** A return status code - returned if OK button has been pressed */
51     public static final int RETURNED_OK = 1;
52
53     public static final int PARAMETER_COLUMN = 0;
54
55     public static final int VALUE_COLUMN = 1;
56
57     private int returnStatus = RETURNED_CANCEL;
58
59     private Dialog JavaDoc dialog;
60
61     private DialogDescriptor dlg = null;
62
63     ParameterizedTableModel _pTableModel;
64
65     public ParameterizedQueryDialog() {
66         this(null, true);
67     }
68
69     /** Creates new form ParameterizedQueryDialog */
70     public ParameterizedQueryDialog(String JavaDoc[] parameters, boolean modal) {
71         _pTableModel = new ParameterizedTableModel();
72         initComponents();
73
74         setParameters(parameters);
75
76         TableColumn column = parameterValueTable.getColumnModel().getColumn(
77             VALUE_COLUMN);
78         column.setCellEditor(new FocusCellEditor(new JTextField JavaDoc()));
79         parameterValueTable.setRowSelectionAllowed(true);
80         parameterValueTable.setColumnSelectionAllowed(true);
81         parameterValueTable.setColumnSelectionInterval(VALUE_COLUMN,VALUE_COLUMN);
82         parameterValueTable.changeSelection(0, VALUE_COLUMN, false, false);
83         parameterValueTable.setRowSelectionInterval(0,0);
84
85         DefaultCellEditor JavaDoc dce =
86             (DefaultCellEditor JavaDoc)parameterValueTable.getDefaultEditor(Object JavaDoc.class);
87         dce.setClickCountToStart(1);
88
89         final JTable JavaDoc fTable = parameterValueTable;
90         Runnable JavaDoc r = new Runnable JavaDoc() {
91                 public void run() {
92                     // Re-focus the table
93
fTable.requestFocusInWindow();
94                 }
95             };
96
97         // Put Focus-Runnable into the Event Dispatch Thread
98
javax.swing.SwingUtilities.invokeLater(r);
99
100         ActionListener JavaDoc listener = new ActionListener JavaDoc() {
101                 public void actionPerformed(ActionEvent JavaDoc evt) {
102                     Object JavaDoc o = evt.getSource();
103
104                     Object JavaDoc[] option = dlg.getOptions();
105
106                     if (o == option[1]) {
107                         returnStatus = RETURNED_CANCEL;
108                         dialog.dispose();
109                     }
110                     if (o == option[0]) {
111                         returnStatus = RETURNED_OK;
112                         dialog.dispose();
113                     }
114                 }
115             };
116
117         dlg = new DialogDescriptor(this,
118                                    // Dialog Title : "Specify Parameter Values",
119
NbBundle.getMessage(ParameterizedQueryDialog.class,
120                                                        "SPECIFY_PARAMETER_VALUES"), // NOI18N
121
modal, listener);
122
123         dlg.setOptions(new Object JavaDoc[] { okButton, cancelButton });
124         dlg.setClosingOptions(new Object JavaDoc[] { okButton, cancelButton });
125
126          dlg.setHelpCtx (
127              new HelpCtx( "projrave_ui_elements_editors_about_query_editor" ) ); // NOI18N
128

129         dialog = (JDialog JavaDoc) DialogDisplayer.getDefault().createDialog(dlg);
130
131         dialog.setResizable(true);
132         // dialog.setPreferredSize(new java.awt.Dimension (500,350));
133
dialog.pack();
134         dialog.show();
135     }
136
137     /**
138      * @return the return status of this dialog - one of RET_OK or RET_CANCEL
139      */

140     public int getReturnStatus() {
141         return returnStatus;
142     }
143
144     /** @return the values input by the user
145      * starting first row.
146      **/

147     public String JavaDoc[] getParameterValues() {
148         int rows = _pTableModel.getRowCount();
149         String JavaDoc[] values = new String JavaDoc[rows];
150         for (int i = 0; i < rows; i++) {
151             values[i] = (String JavaDoc) _pTableModel.getValueAt(i, VALUE_COLUMN);
152         }
153         return values;
154     }
155
156     /**
157      * set the parameter values in the VALUE column
158      **/

159     public void setParameterValues(String JavaDoc[] values) {
160         for (int i = 0; i < values.length; i++) {
161             _pTableModel.setValueAt(values[i], i, VALUE_COLUMN);
162         }
163     }
164
165     /**
166      * get the parameter values from the VALUE column
167      **/

168     public String JavaDoc[] getParameters() {
169         int rows = _pTableModel.getRowCount();
170         String JavaDoc[] parameters = new String JavaDoc[rows];
171         for (int i = 0; i < rows; i++) {
172             parameters[i] = (String JavaDoc) _pTableModel.getValueAt(i,
173                                                              PARAMETER_COLUMN);
174         }
175         return parameters;
176     }
177
178     public void setParameters(String JavaDoc[] parameters) {
179         if (parameters == null || parameters.length == 0)
180             return;
181         _pTableModel.setRowCount(0);
182         for (int i = 0; i < parameters.length; i++) {
183             Object JavaDoc[] rowData = { parameters[i], "" // criteria order // NOI18N
184
}; // or... // NOI18N
185
_pTableModel.addRow(rowData);
186         }
187     }
188
189     class ParameterizedTable extends JTable JavaDoc {
190         // make sure to get the focus on keystroke. This will make sure
191
// all the values entered by users are captured. Otherwise the
192
// last cell value is not updated in the model.
193
public boolean getSurrendersFocusOnKeystroke() {
194             return true;
195         }
196
197         // This achieves the focus/selection traversal using keyboard.
198
// Here we need to traverse only through the second column
199
// as the first column is not selectable/editable.
200
// This is a general implementation and should work for
201
// any JTable.
202
// Case 1 : toggle: false, extend: false.
203
// Clear previous selection and ensure the new cell is selected.
204
// Case 2 : toggle: false, extend: true.
205
// Extend the previous selection to include the specified cell.
206
// Case 3 : toggle: true, extend: false.
207
// If the specified cell is selected, deselect it. If it is not
208
// selected, select it.
209
// Case 4 : toggle: true, extend: true.
210
// Leave the selection state as it is, but move the anchor
211
// index to the specified location.
212
//
213
public void changeSelection(int row,int col,boolean toggle,
214                                     boolean expand) {
215             // This method is called when the user tries to move to a
216
// different cell.
217
// If the cell they're trying to move to is not editable,
218
// we look for then next cell in the proper direction that
219
// is editable.
220
if (!this.getModel().isCellEditable(row,col)) {
221                 // Find the current row and column
222
int currentRow = getEditingRow();
223                 int currentCol = getEditingColumn();
224                 if (currentRow == -1) {
225                     currentRow = getSelectedRow();
226                 }
227                 if (currentCol == -1) {
228                     currentCol = getSelectedColumn();
229                 }
230
231                 // need to wrap-around.
232
int numberOfRows = getRowCount();
233                 int numberOfCols = getColumnCount();
234
235                 // If no cell is found to move to, stay here.
236
int nextRow = row;
237                 int nextCol = col;
238
239                 if (col==currentCol) {
240                     // Up or down motion - go only up or down.
241
int direction = row-currentRow;
242                     if (direction>1) {
243                         direction=1;
244                     }
245                     if (direction<-1) {
246                         direction=-1;
247                     }
248                     nextRow = getNextEditableRow(row,col,direction,
249                                                  numberOfRows,numberOfCols);
250                 } else if (row == currentRow) {
251                     int direction = col-currentCol;
252                     if (direction>1) {
253                         direction=1;
254                     }
255                     if (direction<-1) {
256                         direction=-1;
257                     }
258                     int[] nextCell = getNextEditableCell(row,col,direction,
259                                                          numberOfRows,numberOfCols);
260                     nextRow = nextCell[0];
261                     nextCol = nextCell[1];
262                 } else {
263                     int direction = row-currentRow;
264                     if (direction>1) {
265                         direction=1;
266                     }
267                     if (direction<-1) {
268                         direction=-1;
269                     }
270                     if ((row==0) && (currentRow==numberOfRows-1)) {
271                         direction=1;
272                     }
273                     int[] nextCell = getNextEditableCell(row,col,
274                                                          direction,numberOfRows,numberOfCols);
275                     nextRow = nextCell[0];
276                     nextCol = nextCell[1];
277                 }
278                 // Go to the found cell.
279
super.changeSelection(nextRow,nextCol,toggle,expand);
280             } else {
281                 // This is an editable cell, so leave the selection here.
282
super.changeSelection(row,col,toggle,expand);
283             }
284         }
285
286         // Search for the next editable cell starting at row,col
287
// Return array containing Row and Column
288
int[] getNextEditableCell(int row,int col,
289                                   int direction,int numberOfRows,int numberOfCols) {
290             int originalRow=row;
291             int originalCol=col;
292             // traverse till row/col are not equal to the original row/col
293
do {
294                 col = col+direction;
295                 if (col>=numberOfCols) {
296                     col = 0;
297                     row += direction;
298                 }
299                 if (col<0) {
300                     col = numberOfCols-1;
301                     row += direction;
302                 }
303                 if (row>=numberOfRows) {
304                     row = 0;
305                 }
306                 if (row<0) {
307                     row = numberOfRows-1;
308                 }
309                 if (isCellEditable(row,col)) {
310                     return new int[]{row,col};
311                 }
312             } while (!((row==originalRow)&&(col==originalCol)));
313
314             // Nothing editable found, stay here.
315
return new int[]{originalRow,originalCol};
316
317         }
318
319         // Search up/down for an editable cell.
320
int getNextEditableRow(int row,int col,int direction,int numberOfRows,int numberOfCols) {
321             int originalRow = row;
322             // traverse till row is not equal to the original row
323
do {
324                 row = row+direction;
325                 if (row<0) {
326                     row = numberOfRows-1;
327                 }
328                 if (row>=numberOfRows) {
329                     row=0;
330                 }
331                 if (isCellEditable(row,col)) {
332                     return row;
333                 }
334             } while (row != originalRow);
335             // Nothing editable found, stay here.
336
return originalRow;
337         }
338     }
339
340     class ParameterizedTableModel extends DefaultTableModel {
341
342         final String JavaDoc[] columnNames = {
343             // "Parameter",
344
NbBundle.getMessage(ParameterizedQueryDialog.class,
345                                 "PARAMETER"), // NOI18N
346
// "Value"
347
NbBundle.getMessage(ParameterizedQueryDialog.class,
348                                 "VALUE") // NOI18N
349
};
350
351         Object JavaDoc[][] data = { { "", "" } };
352
353         public ParameterizedTableModel() {
354             super(0, 2);
355             setColumnIdentifiers(columnNames);
356         }
357
358         public boolean isCellEditable(int row, int col) {
359             if (col < 1)
360                 return false;
361             else
362                 return true;
363         }
364     }
365
366     // cell editor to handle focus lost events on particular
367
// cells.
368
private class FocusCellEditor extends DefaultCellEditor JavaDoc {
369         Component JavaDoc c;
370
371         public FocusCellEditor(JTextField JavaDoc jtf) {
372             super(jtf);
373             addFocusListener(jtf);
374         }
375
376         private void addFocusListener(Component JavaDoc C) {
377             C.getClass();
378             super.getComponent().addFocusListener(
379                 new java.awt.event.FocusAdapter JavaDoc() {
380                     public void focusLost(java.awt.event.FocusEvent JavaDoc fe) {
381                         lostFocus();
382                     }
383                 });
384         }
385
386         public void lostFocus() {
387             stopCellEditing();
388         }
389     }
390
391     /**
392      * This method is called from within the constructor to initialize the form.
393      * WARNING: Do NOT modify this code. The content of this method is always
394      * regenerated by the Form Editor.
395      */

396     private void initComponents() {//GEN-BEGIN:initComponents
397

398         mainPanel = new javax.swing.JPanel JavaDoc();
399         messageAreaTablePanel = new javax.swing.JPanel JavaDoc();
400         messageAreaPanel = new javax.swing.JPanel JavaDoc();
401         messageArea = new javax.swing.JTextArea JavaDoc();
402         parameterValueTablePanel = new javax.swing.JPanel JavaDoc();
403         parameterValueTableScrollPane = new javax.swing.JScrollPane JavaDoc();
404         parameterValueTable = new ParameterizedTable();
405         
406         // setLayout(new java.awt.GridLayout(1, 1));
407
setLayout(new java.awt.GridLayout JavaDoc());
408
409         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(20, 20, 20, 20)));
410         mainPanel.setLayout(new java.awt.BorderLayout JavaDoc());
411
412         mainPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(0, 0, 0, 0)));
413
414         okButton = new javax.swing.JButton JavaDoc();
415         cancelButton = new javax.swing.JButton JavaDoc();
416         helpButton = new javax.swing.JButton JavaDoc();
417
418         mainPanel.setLayout(new java.awt.BorderLayout JavaDoc());
419
420         mainPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(
421                 new java.awt.Insets JavaDoc(0, 0, 0, 0)));
422         messageAreaTablePanel.setLayout(new java.awt.BorderLayout JavaDoc());
423
424         messageAreaTablePanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(
425                                             new java.awt.Insets JavaDoc(0, 0, 0, 0)));
426         messageAreaPanel.setLayout(new java.awt.BorderLayout JavaDoc());
427
428         messageAreaPanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(
429                                        new java.awt.Insets JavaDoc(0, 0, 0, 0)));
430         // messageAreaPanel.setPreferredSize(new java.awt.Dimension(450, 90));
431
messageArea.setBackground(new java.awt.Color JavaDoc(212, 208, 200));
432         messageArea.setEditable(false);
433         messageArea.setLineWrap(true);
434         messageArea
435             .setText( NbBundle.getMessage(ParameterizedQueryDialog.class,
436                                           "PAREMETERIZED_QURY_MESSAGE") ); // NOI18N
437
messageArea.setWrapStyleWord(true);
438         messageArea.setBorder(new javax.swing.border.EmptyBorder JavaDoc(
439                                   new java.awt.Insets JavaDoc(10, 10, 10, 10)));
440         messageAreaPanel.add(messageArea, java.awt.BorderLayout.CENTER);
441
442         messageAreaTablePanel
443             .add(messageAreaPanel, java.awt.BorderLayout.NORTH);
444
445         parameterValueTablePanel.setLayout(new java.awt.BorderLayout JavaDoc());
446
447         parameterValueTablePanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(
448                                                new java.awt.Insets JavaDoc(0, 0, 0, 0)));
449         parameterValueTableScrollPane
450             .setBorder(new javax.swing.border.EmptyBorder JavaDoc(
451                            new java.awt.Insets JavaDoc(0, 0, 0, 0)));
452         parameterValueTableScrollPane
453             .setViewportBorder(new javax.swing.border.EmptyBorder JavaDoc(
454                                    new java.awt.Insets JavaDoc(0, 0, 0, 0)));
455         parameterValueTable.setModel(_pTableModel);
456
457         parameterValueTableScrollPane.setViewportView(parameterValueTable);
458         parameterValueTable.setPreferredScrollableViewportSize (
459             new java.awt.Dimension JavaDoc(450,100));
460         parameterValueTable.setRowHeight(24);
461         java.awt.Dimension JavaDoc dim = parameterValueTable.getTableHeader()
462             .getPreferredSize();
463         java.awt.Dimension JavaDoc newDim = new java.awt.Dimension JavaDoc(
464             (int) dim.getWidth(), 25);
465         // parameterValueTable.getTableHeader().setPreferredSize(newDim);
466

467         parameterValueTableScrollPane.setViewportView(parameterValueTable);
468
469         parameterValueTablePanel.add(parameterValueTableScrollPane,
470                                      java.awt.BorderLayout.CENTER);
471
472         messageAreaTablePanel.add(parameterValueTablePanel,
473                                   java.awt.BorderLayout.CENTER);
474
475         mainPanel.add(messageAreaTablePanel, java.awt.BorderLayout.CENTER);
476
477         okButton.setText( NbBundle.getMessage(ParameterizedQueryDialog.class,
478                                               "OK") ); // NOI18N
479
// okButton.setPreferredSize(new java.awt.Dimension(100, 30));
480

481         cancelButton.setText(NbBundle.getMessage(ParameterizedQueryDialog.class,
482                                                  "CANCEL") ); // NOI18N
483
// cancelButton.setPreferredSize(new java.awt.Dimension(100, 30));
484

485         helpButton.setText(NbBundle.getMessage(ParameterizedQueryDialog.class,
486                                                "HELP") ); // NOI18N
487
// helpButton.setPreferredSize(new java.awt.Dimension(100, 30));
488

489         add(mainPanel);
490
491     }//GEN-END:initComponents
492

493     /**
494      * @param args the command line arguments
495      */

496     public static void main(String JavaDoc args[]) {
497         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
498                 public void run() {
499                     String JavaDoc[] parameters = new String JavaDoc[5];
500                     String JavaDoc[] values = new String JavaDoc[5];
501                     for (int i = 0; i < parameters.length; i++) {
502                         parameters[i] = new String JavaDoc("Table.column" + i);
503                     }
504                     ParameterizedQueryDialog pqDlg = new ParameterizedQueryDialog(
505                         parameters, true);
506                     System.out.println(pqDlg.getReturnStatus());
507                     if (pqDlg.getReturnStatus() == ParameterizedQueryDialog.RETURNED_OK) {
508                         values = pqDlg.getParameterValues();
509
510                         for (int i = 0; i < values.length; i++) {
511                             System.out.println(values[i] + "\n");
512                         }
513                     }
514                 }
515             });
516     }
517
518     // Variables declaration - do not modify//GEN-BEGIN:variables
519
private javax.swing.JPanel JavaDoc mainPanel;
520
521     private javax.swing.JButton JavaDoc helpButton;
522
523     private javax.swing.JButton JavaDoc cancelButton;
524
525     private javax.swing.JTextArea JavaDoc messageArea;
526
527     private javax.swing.JPanel JavaDoc messageAreaPanel;
528
529     private javax.swing.JPanel JavaDoc messageAreaTablePanel;
530
531     private javax.swing.JButton JavaDoc okButton;
532
533     private ParameterizedTable parameterValueTable;
534
535     private javax.swing.JPanel JavaDoc parameterValueTablePanel;
536
537     private javax.swing.JScrollPane JavaDoc parameterValueTableScrollPane;
538
539     // End of variables declaration//GEN-END:variables
540

541 }
542
Popular Tags