KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > monitor > client > EditPanelQuery


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 /**
21  * EditPanelQuery.java
22  *
23  *
24  * Created: Mon Feb 5 13:34:46 2001
25  *
26  * @author Ana von Klopp
27  * @author Simran Gleason
28  * @version
29  */

30
31 /**
32  * Contains the Query sub-panel for the EditPanel
33  */

34 package org.netbeans.modules.web.monitor.client;
35
36 import java.awt.Component JavaDoc;
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39 import java.awt.event.FocusEvent JavaDoc;
40 import java.awt.event.FocusListener JavaDoc;
41 import java.util.ResourceBundle JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JPanel JavaDoc;
44 import javax.swing.JTextField JavaDoc;
45 import javax.swing.JScrollPane JavaDoc;
46 import javax.swing.ListSelectionModel JavaDoc;
47 import javax.swing.event.ListSelectionEvent JavaDoc;
48 import javax.swing.event.ListSelectionListener JavaDoc;
49 import javax.swing.event.TableModelEvent JavaDoc;
50 import javax.swing.event.TableModelListener JavaDoc;
51
52 import org.openide.NotifyDescriptor;
53 import org.openide.DialogDisplayer;
54 import org.openide.util.NbBundle;
55
56 import org.netbeans.modules.web.monitor.data.*;
57
58 class EditPanelQuery extends DataDisplay {
59
60     private final static boolean debug = false;
61
62     private DisplayTable paramTable = null;
63     private MonitorData monitorData = null;
64     private boolean setParams = false;
65
66     // Widgets
67
JButton JavaDoc newParamB;
68     JButton JavaDoc deleteParamB;
69     JTextField JavaDoc queryStringText;
70     JTextField JavaDoc uploadFileText;
71  
72     EditPanelQuery() {
73     super();
74     }
75
76     // Redesign this, inefficient and prevents us from maintaining
77
// sorting state
78
void redisplayData() {
79     if(debug) log("::redisplayData()");
80     setData(monitorData);
81     this.revalidate();
82     this.repaint();
83     }
84     
85     void setData(MonitorData md) {
86
87     if(debug) {
88         log("setData()"); // NOI18N
89
log("\tMonitor data is:"); // NOI18N
90
log("t" + md.dumpBeanNode()); // NOI18N
91
}
92     this.monitorData = md;
93     this.removeAll();
94
95     int fullGridWidth = java.awt.GridBagConstraints.REMAINDER;
96     int gridy = -1;
97      
98
99     addGridBagComponent(this, createTopSpacer(), 0, ++gridy,
100                 fullGridWidth, 1, 0, 0,
101                 java.awt.GridBagConstraints.WEST,
102                 java.awt.GridBagConstraints.NONE,
103                 topSpacerInsets,
104                 0, 0);
105     
106     // The query parameters: PENDING
107
//
108
// 1. Get request - add a parameter one at a time. We should not
109
// allow the user to edit the query string.
110
// 2. POST request. Allow the user to edit the query string,
111
// and then there are two cases:
112
// a) add a parameter one at a time
113
// b) add data from file
114
// 3. PUT request. Allow the user to enter data from a file
115
// only. They can edit the query string as well.
116

117     final RequestData rd = monitorData.getRequestData();
118
119     String JavaDoc method = rd.getAttributeValue(EditPanel.METHOD);
120     if (EditPanel.POST.equals(method)) {
121         queryStringText =
122         new JTextField JavaDoc(rd.getAttributeValue("queryString")); // NOI18N
123

124         queryStringText.addFocusListener(new FocusListener JavaDoc() {
125         public void focusGained(FocusEvent JavaDoc evt) {
126         }
127         public void focusLost(FocusEvent JavaDoc evt) {
128             // PENDING - check that this works
129
rd.setAttributeValue("queryString", //NOI18N
130
queryStringText.getText());
131         }
132         });
133
134         addGridBagComponent(this, createHeaderLabel(NbBundle.getBundle(EditPanelQuery.class).getString("MON_Querystring"), NbBundle.getBundle(EditPanelQuery.class).getString("MON_Querystring_Mnemonic").charAt(0), NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_QuerystringA11yDesc"), paramTable),
135                                 0, ++gridy,
136                 1, 1, 0, 0,
137                 java.awt.GridBagConstraints.WEST,
138                 java.awt.GridBagConstraints.NONE,
139                 labelInsets,
140                 0, 0);
141
142         addGridBagComponent(this, queryStringText, 0, ++gridy,
143                 fullGridWidth, 1, 1.0, 0,
144                 java.awt.GridBagConstraints.WEST,
145                 java.awt.GridBagConstraints.HORIZONTAL,
146                 tableInsets,
147                 0, 0);
148     }
149         
150     String JavaDoc msg2 = null;
151     Component JavaDoc msg2Label;
152
153     if (EditPanel.PUT.equals(method)) {
154         msg2 = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Upload_File");
155         msg2Label = createDataLabel(msg2);
156         addGridBagComponent(this, msg2Label, 0, ++gridy,
157                 fullGridWidth, 1, 0, 0,
158                 java.awt.GridBagConstraints.WEST,
159                 java.awt.GridBagConstraints.NONE,
160                 labelInsets,
161                 0, 0);
162         
163
164         String JavaDoc uploadFileMsg = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Upload_File_Not_Supported");
165         uploadFileText = new JTextField JavaDoc(uploadFileMsg);
166         uploadFileText.setEnabled(false);
167         addGridBagComponent(this, uploadFileText, 0, ++gridy,
168                 fullGridWidth, 1, 1.0, 0,
169                 java.awt.GridBagConstraints.WEST,
170                 java.awt.GridBagConstraints.HORIZONTAL,
171                 labelInsets,
172                 0, 0);
173         addGridBagComponent(this, createGlue(), 0, ++gridy,
174                 1, 1, 1.0, 1.0,
175                 java.awt.GridBagConstraints.WEST,
176                 java.awt.GridBagConstraints.BOTH,
177                 labelInsets,
178                 0, 0);
179
180     } else if (method != null) { // GET or POST
181

182         Param[] params2 = rd.getParam();
183         if (params2 == null) params2 = new Param[0];
184         setParameters(params2);
185             char mnemonic = ' ';
186             String JavaDoc ad = null;
187
188         if(method.equals(EditPanel.GET)) {
189         msg2 = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Query_parameters");
190                 mnemonic = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Query_parameters_Mnemonic").charAt(0);
191                 ad = NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_Query_parametersA11yDesc");
192                 paramTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_ParametersTableA11yDesc"));
193                 paramTable.setToolTipText(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_ParametersTableA11yDesc"));
194         
195         } else if(method.equals(EditPanel.POST)) {
196         msg2 = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Posted_data");
197                 mnemonic = NbBundle.getBundle(EditPanelQuery.class).getString("MON_Posted_data_Mnemonic").charAt(0);
198                 ad = NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_Posted_dataA11yDesc");
199                 paramTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_Posted_dataTableA11yName"));
200                 paramTable.setToolTipText(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_Posted_dataTableA11yDesc"));
201         }
202
203         msg2Label = createSortButtonLabel(msg2, paramTable, mnemonic, ad);
204
205         addGridBagComponent(this, msg2Label, 0, ++gridy,
206                 1, 1, 0, 0,
207                 java.awt.GridBagConstraints.WEST,
208                 java.awt.GridBagConstraints.NONE,
209                 labelInsets,
210                 0, 0);
211
212         gridy = addParamTable(this, params2, gridy);
213     }
214     
215     setEnablings();
216
217     // Housekeeping
218
this.setMaximumSize(this.getPreferredSize());
219     this.repaint();
220
221     }
222
223     private int addParamTable(JPanel JavaDoc panel, Param[] params, int gridy) {
224     
225     JScrollPane JavaDoc scrollpane = new JScrollPane JavaDoc(paramTable);
226
227     addGridBagComponent(panel, scrollpane, 0, ++gridy,
228                 fullGridWidth, 1, 1.0, 1.0,
229                 java.awt.GridBagConstraints.WEST,
230                 //java.awt.GridBagConstraints.HORIZONTAL,
231
java.awt.GridBagConstraints.BOTH,
232                 tableInsets,
233                 0, 0);
234
235     newParamB = new JButton JavaDoc(NbBundle.getBundle(EditPanelQuery.class).getString("MON_New_param"));
236         newParamB.setMnemonic(NbBundle.getBundle(EditPanelQuery.class).getString("MON_New_param_Mnemonic").charAt(0));
237         newParamB.setToolTipText(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_New_paramA11yDesc"));
238     newParamB.addActionListener(new ActionListener JavaDoc() {
239         public void actionPerformed(ActionEvent JavaDoc e) {
240
241             String JavaDoc title = NbBundle.getBundle(EditPanelQuery.class).getString("MON_New_param");
242             ParamEditor pe = new ParamEditor("", "", //NOI18N
243
ParamEditor.Editable.BOTH,
244                              ParamEditor.Condition.NONE,
245                              title);
246
247             if(debug) log("Now showing dialog"); // NOI18N
248

249             pe.showDialog();
250
251             if(debug) log("Dialog closed"); // NOI18N
252

253             if (pe.getDialogOK()) {
254
255             if(debug) log("Dialog returned OK"); // NOI18N
256
String JavaDoc name = pe.getName();
257             String JavaDoc value = pe.getValue();
258             Param newParam = new Param(name, value);
259             monitorData.getRequestData().addParam(newParam);
260             redisplayData();
261             }
262         }});
263
264     deleteParamB = new JButton JavaDoc(NbBundle.getBundle(EditPanelQuery.class).getString("MON_Delete_param"));
265         deleteParamB.setMnemonic(NbBundle.getBundle(EditPanelQuery.class).getString("MON_Delete_param_Mnemonic").charAt(0));
266         deleteParamB.setToolTipText(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_Delete_paramA11yDesc"));
267
268     deleteParamB.addActionListener(new ActionListener JavaDoc() {
269
270         public void actionPerformed(ActionEvent JavaDoc e) {
271         
272             int numRows = paramTable.getRowCount();
273             RequestData rd = monitorData.getRequestData();
274         
275             StringBuffer JavaDoc buf = new StringBuffer JavaDoc
276             (NbBundle.getBundle(EditPanelQuery.class).getString("MON_Confirm_Delete_Params"));
277             buf.append("\n"); //NOI18N
278

279             for(int i=0; i<numRows; ++i) {
280             
281             if(paramTable.isRowSelected(i)) {
282                 buf.append(paramTable.getValueAt(i, 0));
283                 buf.append("="); //NOI18N
284
buf.append(paramTable.getValueAt(i, 1));
285                 buf.append("\n"); //NOI18N
286
}
287             }
288         
289             showConfirmDialog(buf.toString());
290         
291             if(setParams) {
292
293             for(int i=0; i<numRows; ++i) {
294             
295                 if(paramTable.isRowSelected(i)) {
296                 String JavaDoc name =
297                     (String JavaDoc)paramTable.getValueAt(i, 0);
298                 String JavaDoc value =
299                     (String JavaDoc)paramTable.getValueAt(i, 1);
300
301                 // Note that we get the params each
302
// time through so that we don't run
303
// into null pointer exceptions.
304
Param[] params2 = rd.getParam();
305                 Param param = findParam(params2, name, value);
306                 if (param != null)
307                     rd.removeParam(param);
308                 }
309             }
310             redisplayData();
311             }
312         }});
313     int gridx = -1;
314     addGridBagComponent(this, createGlue(), ++gridx, ++gridy,
315                 1, 1, 1.0, 0,
316                 java.awt.GridBagConstraints.WEST,
317                 java.awt.GridBagConstraints.NONE,
318                 buttonInsets,
319                 0, 0);
320     addGridBagComponent(this, newParamB, ++gridx, gridy,
321                 1, 1, 0, 0,
322                 java.awt.GridBagConstraints.EAST,
323                 java.awt.GridBagConstraints.NONE,
324                 buttonInsets,
325                 0, 0);
326
327     addGridBagComponent(this, deleteParamB, ++gridx, gridy,
328                 1, 1, 0, 0,
329                 java.awt.GridBagConstraints.EAST,
330                 java.awt.GridBagConstraints.NONE,
331                 buttonInsets,
332                 0, 0);
333     return gridy;
334     }
335
336     void setEnablings() {
337     //
338
// Always enable the Add button.
339
//
340
newParamB.setEnabled(true);
341
342     // The delete row button is enabled if any rows are selected.
343
int selectedRows[] = paramTable.getSelectedRows();
344     deleteParamB.setEnabled(selectedRows.length > 0);
345     }
346
347     void setParameters(Param[] newParams) {
348
349     paramTable = new DisplayTable(newParams, DisplayTable.PARAMS, true);
350         paramTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_QuerystringTableA11yName"));
351         paramTable.setToolTipText(NbBundle.getBundle(EditPanelQuery.class).getString("ACS_MON_QuerystringTableA11yDesc"));
352
353     ListSelectionModel JavaDoc selma = paramTable.getSelectionModel();
354     selma.addListSelectionListener(new ListSelectionListener JavaDoc() {
355         public void valueChanged(ListSelectionEvent JavaDoc evt) {
356         if(debug) log("paramTable list selection listener"); // NOI18N
357
setEnablings();
358         }
359     });
360
361     paramTable.addTableModelListener(new TableModelListener JavaDoc() {
362         public void tableChanged(TableModelEvent JavaDoc evt) {
363         updateParams();
364         }
365         });
366     }
367     
368
369     void updateParams() {
370     int num = paramTable.getRowCount();
371     RequestData rd = monitorData.getRequestData();
372     Param[] params = rd.getParam();
373             
374     for(int i=0; i < num; i++) {
375         String JavaDoc name = (String JavaDoc)paramTable.getValueAt(i, 0);
376         name = name.trim();
377         if(name.equals("")) { //NOI18N
378
paramTable.setValueAt(params[i].getName(), i, 0);
379         showErrorDialog();
380         return;
381         }
382         String JavaDoc value = (String JavaDoc)paramTable.getValueAt(i, 1);
383         value = value.trim();
384         params[i].setName(name);
385         params[i].setValue(value);
386     }
387     }
388     
389     void showConfirmDialog(String JavaDoc msg) {
390
391     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION,
392                NotifyDescriptor.CANCEL_OPTION
393     };
394     
395     NotifyDescriptor confirmDialog =
396         new NotifyDescriptor((Object JavaDoc)msg,
397                  NbBundle.getBundle(EditPanelQuery.class).getString("MON_Confirmation_Required"),
398                  NotifyDescriptor.OK_CANCEL_OPTION,
399                  NotifyDescriptor.QUESTION_MESSAGE,
400                  options,
401                  NotifyDescriptor.CANCEL_OPTION);
402
403     DialogDisplayer.getDefault().notify(confirmDialog);
404     if(confirmDialog.getValue().equals(NotifyDescriptor.OK_OPTION))
405         setParams = true;
406     else
407         setParams = false;
408     }
409
410     void showErrorDialog() {
411
412     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION };
413     
414     NotifyDescriptor errorDialog =
415         new NotifyDescriptor((Object JavaDoc)NbBundle.getBundle(EditPanelQuery.class).getString("MON_Bad_param"),
416                  NbBundle.getBundle(EditPanelQuery.class).getString("MON_Invalid_input"),
417                  NotifyDescriptor.DEFAULT_OPTION,
418                  NotifyDescriptor.ERROR_MESSAGE,
419                  options,
420                  NotifyDescriptor.OK_OPTION);
421
422     DialogDisplayer.getDefault().notify(errorDialog);
423     }
424
425
426     void log(String JavaDoc s) {
427     System.out.println("EditPanelQuery::" + s); //NOI18N
428
}
429
430 } // EditPanel
431
Popular Tags