KickJava   Java API By Example, From Geeks To Geeks.

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


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  * EditPanelHeaders.java
22  *
23  *
24  * Created: Fri Feb 9 2001
25  *
26  * @author Ana von Klopp
27  * @author Simran Gleason
28  * @version
29  */

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

34
35 package org.netbeans.modules.web.monitor.client;
36
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39 import java.util.ResourceBundle JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.ListSelectionModel JavaDoc;
43 import javax.swing.event.ListSelectionEvent JavaDoc;
44 import javax.swing.event.ListSelectionListener JavaDoc;
45 import javax.swing.event.TableModelEvent JavaDoc;
46 import javax.swing.event.TableModelListener JavaDoc;
47
48 import org.openide.util.NbBundle;
49 import org.openide.DialogDisplayer;
50 import org.openide.NotifyDescriptor;
51
52 import org.netbeans.modules.web.monitor.data.*;
53
54 class EditPanelHeaders extends DataDisplay {
55
56     private final static boolean debug = false;
57     
58     private DisplayTable headerTable = null;
59     private MonitorData monitorData = null;
60     private boolean setParams = false;
61
62     //
63
// Widgets
64
//
65
JButton JavaDoc newHeaderB;
66     JButton JavaDoc editHeaderB;
67     JButton JavaDoc deleteHeaderB;
68     
69     EditPanelHeaders() {
70     super();
71     }
72
73     // Replace this. Inefficient and prevents us from maintaining
74
// sorting.
75
void redisplayData() {
76     setData(monitorData);
77     this.revalidate();
78     this.repaint();
79     }
80
81     void setData(MonitorData md) {
82
83     this.monitorData = md;
84     setHeaderTable();
85     if(debug) log("setData()"); // NOI18N
86

87     this.removeAll();
88     
89     int gridy = -1;
90     int fullGridWidth = java.awt.GridBagConstraints.REMAINDER;
91
92
93     addGridBagComponent(this, createTopSpacer(), 0, ++gridy,
94                 fullGridWidth, 1, 0, 0,
95                 java.awt.GridBagConstraints.WEST,
96                 java.awt.GridBagConstraints.NONE,
97                 topSpacerInsets,
98                 0, 0);
99
100     String JavaDoc msg = NbBundle.getBundle(EditPanelHeaders.class).getString("MON_HTTP_Headers");
101     addGridBagComponent(this, createSortButtonLabel(msg, headerTable, NbBundle.getBundle(EditPanelHeaders.class).getString("MON_HTTP_Headers_2_Mnemonic").charAt(0), NbBundle.getBundle(EditPanelHeaders.class).getString("ACS_MON_HTTP_HeadersA11yDesc")), 0, ++gridy,
102                 1, 1, 0, 0,
103                 java.awt.GridBagConstraints.WEST,
104                 java.awt.GridBagConstraints.NONE,
105                 labelInsets,
106                 0, 0);
107
108     JScrollPane JavaDoc scrollpane = new JScrollPane JavaDoc(headerTable);
109     addGridBagComponent(this, scrollpane, 0, ++gridy,
110                 fullGridWidth, 1, 1.0, 1.0,
111                 java.awt.GridBagConstraints.WEST,
112                 //java.awt.GridBagConstraints.HORIZONTAL,
113
java.awt.GridBagConstraints.BOTH,
114                 tableInsets,
115                 0, 0);
116
117     newHeaderB = new JButton JavaDoc(NbBundle.getBundle(EditPanelHeaders.class).getString("MON_New_header"));
118         newHeaderB.setMnemonic(NbBundle.getBundle(EditPanelHeaders.class).getString("MON_New_header_Mnemonic").charAt(0));
119         newHeaderB.setToolTipText(NbBundle.getBundle(EditPanelHeaders.class).getString("ACS_MON_New_headerA11yDesc"));
120     newHeaderB.addActionListener(new ActionListener JavaDoc() {
121         public void actionPerformed(ActionEvent JavaDoc e) {
122             String JavaDoc title = NbBundle.getBundle(EditPanelHeaders.class).getString("MON_New_header");
123             ParamEditor pe = new ParamEditor("", "", //NOI18N
124
ParamEditor.Editable.BOTH,
125                              ParamEditor.Condition.HEADER,
126                              title);
127
128             if(debug) log("Now showing dialog");// NOI18N
129

130             pe.showDialog();
131
132             if(debug) log("Dialog closed"); // NOI18N
133

134             if (pe.getDialogOK()) {
135
136             if(debug) log("Dialog returned OK"); // NOI18N
137

138             String JavaDoc name = pe.getName();
139             int status = 0;
140
141             if(name.equalsIgnoreCase("cookie"))
142                 status = monitorData.getRequestData().addCookie(pe.getValue());
143             else
144                 status = monitorData.getRequestData().getHeaders().addParam(pe.getName(), pe.getValue());
145
146             if(debug)
147                 log("Headers are " + // NOI18N
148
monitorData.getRequestData().getHeaders().toString());
149             // if(status == 1) { /
150
// The new value was added to an existing header
151
// }
152
// if(status == -1) {
153
// The new value was not added because it was
154
// already in there.
155
//}
156

157             redisplayData();
158             }
159         }});
160
161     deleteHeaderB = new JButton JavaDoc(NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Delete_header"));
162         deleteHeaderB.setMnemonic(NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Delete_header_Mnemonic").charAt(0));
163         deleteHeaderB.setToolTipText(NbBundle.getBundle(EditPanelHeaders.class).getString("MON_New_header_Mnemonic"));
164
165     deleteHeaderB.addActionListener(new ActionListener JavaDoc() {
166
167         public void actionPerformed(ActionEvent JavaDoc e) {
168
169             int numRows = headerTable.getRowCount();
170
171             StringBuffer JavaDoc buf = new StringBuffer JavaDoc
172             (NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Confirm_Delete_Headers"));
173             buf.append("\n"); // NOI18N
174

175             for(int i=0; i<numRows; ++i) {
176
177             if(headerTable.isRowSelected(i)) {
178                 buf.append(headerTable.getValueAt(i, 0));
179                 buf.append(" "); // NOI18N
180
buf.append(headerTable.getValueAt(i, 1));
181                 buf.append("\n"); // NOI18N
182
}
183             }
184
185             showConfirmDialog(buf.toString());
186             
187             if(setParams) {
188
189             Headers hd = monitorData.getRequestData().getHeaders();
190
191             for(int i=0; i<numRows; ++i) {
192                 if(headerTable.isRowSelected(i)) {
193
194                 String JavaDoc name =
195                     (String JavaDoc)headerTable.getValueAt(i, 0);
196                 String JavaDoc value =
197                     (String JavaDoc)headerTable.getValueAt(i, 1);
198                 
199                 // Note that we get the params each
200
// time through that we don't run into
201
// null pointer exceptions.
202
Param[] myParams = hd.getParam();
203                 Param param = findParam(myParams, name, value);
204                 if (param != null)
205                     hd.removeParam(param);
206                 }
207             }
208             redisplayData();
209             }
210         }});
211     
212     int gridx = -1;
213     addGridBagComponent(this, createGlue(), ++gridx, ++gridy,
214                 1, 1, 1.0, 0,
215                 java.awt.GridBagConstraints.WEST,
216                 java.awt.GridBagConstraints.NONE,
217                 buttonInsets,
218                 0, 0);
219     addGridBagComponent(this, newHeaderB, ++gridx, gridy,
220                 1, 1, 0, 0,
221                 java.awt.GridBagConstraints.EAST,
222                 java.awt.GridBagConstraints.NONE,
223                 buttonInsets,
224                 0, 0);
225
226     addGridBagComponent(this, deleteHeaderB, ++gridx, gridy,
227                 1, 1, 0, 0,
228                 java.awt.GridBagConstraints.EAST,
229                 java.awt.GridBagConstraints.NONE,
230                 buttonInsets,
231                 0, 0);
232
233     setEnablings();
234
235     this.setMaximumSize(this.getPreferredSize());
236     this.repaint();
237     }
238     
239     
240
241
242     void showConfirmDialog(String JavaDoc msg) {
243
244     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION,
245                NotifyDescriptor.CANCEL_OPTION
246     };
247     
248     NotifyDescriptor confirmDialog =
249         new NotifyDescriptor((Object JavaDoc)msg,
250                  NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Confirmation_Required"),
251                  NotifyDescriptor.OK_CANCEL_OPTION,
252                  NotifyDescriptor.QUESTION_MESSAGE,
253                  options,
254                  NotifyDescriptor.CANCEL_OPTION);
255
256     DialogDisplayer.getDefault().notify(confirmDialog);
257     if(confirmDialog.getValue().equals(NotifyDescriptor.OK_OPTION))
258         setParams = true;
259     else
260         setParams = false;
261     }
262
263
264     void showErrorDialog() {
265
266     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION };
267     
268     NotifyDescriptor errorDialog =
269         new NotifyDescriptor((Object JavaDoc)NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Bad_header"),
270                  NbBundle.getBundle(EditPanelHeaders.class).getString("MON_Invalid_input"),
271                  NotifyDescriptor.DEFAULT_OPTION,
272                  NotifyDescriptor.ERROR_MESSAGE,
273                  options,
274                  NotifyDescriptor.OK_OPTION);
275
276     DialogDisplayer.getDefault().notify(errorDialog);
277     }
278
279      
280     void setEnablings() {
281
282     // Always enable the Add button.
283
newHeaderB.setEnabled(true);
284
285     // The delete row button is enabled if any rows are selected.
286
int selectedRows[] = headerTable.getSelectedRows();
287     deleteHeaderB.setEnabled(selectedRows.length > 0);
288     }
289
290     void setHeaderTable() {
291
292     Param[] params = monitorData.getRequestData().getHeaders().getParam();
293     if(params == null) params = new Param[0];
294     
295     headerTable =
296         new DisplayTable(params, DisplayTable.HEADERS, true);
297     headerTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(EditPanelHeaders.class).getString("ACS_MON_HTTP_HeadersTableA11yName"));
298         headerTable.setToolTipText(NbBundle.getBundle(EditPanelHeaders.class).getString("ACS_MON_HTTP_HeadersTableA11yDesc"));
299
300
301     ListSelectionModel JavaDoc selma = headerTable.getSelectionModel();
302     selma.addListSelectionListener(new ListSelectionListener JavaDoc() {
303         public void valueChanged(ListSelectionEvent JavaDoc evt) {
304         if(debug) log("got list selection event"); // NOI18N
305
setEnablings();
306         }
307     });
308
309     headerTable.addTableModelListener(new TableModelListener JavaDoc() {
310         public void tableChanged(TableModelEvent JavaDoc evt) {
311         
312         if(debug)
313             log("got table changed event"); //NOI18N
314
updateHeaders();
315         }
316     });
317     }
318     
319     private void updateHeaders() {
320         
321     int num = headerTable.getRowCount();
322     Headers hd = monitorData.getRequestData().getHeaders();
323     Param[] params = hd.getParam();
324             
325     boolean inputOK = true;
326             
327     for(int i=0; i < num; i++) {
328         String JavaDoc name = (String JavaDoc)headerTable.getValueAt(i, 0);
329         name = name.trim();
330
331         if(debug)
332         log("Name is " + name); //NOI18N
333

334         if(name.equals("")) { // NOI18N
335
headerTable.setValueAt(params[i].getName(), i, 0);
336         inputOK = false;
337         }
338         String JavaDoc value = (String JavaDoc)headerTable.getValueAt(i, 1);
339         value = value.trim();
340         
341         if(debug)
342         log("Value is " + value); //NOI18N
343

344         if(value.equals("")) { // NOI18N
345
headerTable.setValueAt(params[i].getValue(), i, 1);
346         inputOK = false;
347         }
348         
349         if(!inputOK) {
350         showErrorDialog();
351         return;
352         }
353         params[i].setName(name);
354         params[i].setValue(value);
355     }
356     }
357
358     public void repaint() {
359     super.repaint();
360     //if (editPanel != null)
361
//editPanel.repaint();
362
}
363
364     void log(String JavaDoc s) {
365     System.out.println("EditPanelHeaders::" + s); //NOI18N
366
}
367     
368
369 } // EditPanelHeader
370

371
Popular Tags