KickJava   Java API By Example, From Geeks To Geeks.

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


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

29
30 /**
31  * Contains the Cookie sub-panel for the EditPanel
32  */

33
34 package org.netbeans.modules.web.monitor.client;
35
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.event.ActionListener JavaDoc;
38 import java.util.ResourceBundle JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.util.Vector JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.ListSelectionModel JavaDoc;
44 import javax.swing.event.ListSelectionEvent JavaDoc;
45 import javax.swing.event.ListSelectionListener JavaDoc;
46 import javax.swing.event.TableModelEvent JavaDoc;
47 import javax.swing.event.TableModelListener JavaDoc;
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 EditPanelCookies extends DataDisplay {
55
56     private final static boolean debug = false;
57     
58     private DisplayTable cookieTable = null;
59     private MonitorData monitorData = null;
60     private boolean setCookies = false;
61
62     //
63
// Widgets
64
//
65
JButton JavaDoc newCookieB;
66     JButton JavaDoc editCookieB;
67     JButton JavaDoc deleteCookieB;
68     
69     EditPanelCookies() {
70     super();
71     }
72
73     //
74
// Redesign this, inefficient.
75
//
76
void redisplayData() {
77     setData(monitorData);
78     this.revalidate();
79     this.repaint();
80     }
81
82     // We're treating these as if they are all strings at the
83
// moment. In reality they can be of different types, though maybe
84
// that does not matter...
85
void setData(MonitorData md) {
86
87     this.monitorData = md;
88     
89     setCookieTable();
90      
91     this.removeAll();
92     
93     // Cookies
94
String JavaDoc msg = NbBundle.getBundle(EditPanelCookies.class).getString("MON_Cookies_4");
95      
96     int gridy = -1;
97     int fullGridWidth = java.awt.GridBagConstraints.REMAINDER;
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     addGridBagComponent(this, createSortButtonLabel(msg, cookieTable, NbBundle.getBundle(EditPanelCookies.class).getString("MON_Cookies_Mnemonic").charAt(0), NbBundle.getBundle(EditPanelCookies.class).getString("ACS_MON_CookiesA11yDesc")), 0, ++gridy,
107                 1, 1, 0, 0,
108                 java.awt.GridBagConstraints.WEST,
109                 java.awt.GridBagConstraints.NONE,
110                 labelInsets,
111                 0, 0);
112
113     JScrollPane JavaDoc scrollpane = new JScrollPane JavaDoc(cookieTable);
114     addGridBagComponent(this, scrollpane, 0, ++gridy,
115                 fullGridWidth, 1, 1.0, 1.0,
116                 java.awt.GridBagConstraints.WEST,
117                 //java.awt.GridBagConstraints.HORIZONTAL,
118
java.awt.GridBagConstraints.BOTH,
119                 tableInsets,
120                 0, 0);
121
122     newCookieB = new JButton JavaDoc(NbBundle.getBundle(EditPanelCookies.class).getString("MON_New_cookie"));
123         newCookieB.setMnemonic(NbBundle.getBundle(EditPanelCookies.class).getString("MON_New_cookie_Mnemonic").charAt(0));
124         newCookieB.setToolTipText(NbBundle.getBundle(EditPanelCookies.class).getString("ACS_MON_New_cookieA11yDesc"));
125     newCookieB.addActionListener(new ActionListener JavaDoc() {
126         public void actionPerformed(ActionEvent JavaDoc e) {
127             String JavaDoc title = NbBundle.getBundle(EditPanelCookies.class).getString("MON_New_cookie");
128             ParamEditor pe = new ParamEditor("", "", //NOI18N
129
ParamEditor.Editable.BOTH,
130                              ParamEditor.Condition.COOKIE,
131                              title);
132
133             if(debug) log(" Now showing dialog");// NOI18N
134

135             pe.showDialog();
136
137             if(debug) log(" Dialog closed"); // NOI18N
138

139             if (pe.getDialogOK()) {
140
141             if(debug) log(" Dialog returned OK"); // NOI18N
142
String JavaDoc name = pe.getName();
143             String JavaDoc value = pe.getValue();
144             if(debug) log(name + " " + value); //NOI18N
145
monitorData.getRequestData().addCookie(name,value);
146             redisplayData();
147             }
148         }});
149
150     deleteCookieB = new JButton JavaDoc(NbBundle.getBundle(EditPanelCookies.class).getString("MON_Delete_cookie"));
151         deleteCookieB.setMnemonic(NbBundle.getBundle(EditPanelCookies.class).getString("MON_Delete_cookie_Mnemonic").charAt(0));
152         deleteCookieB.setToolTipText(NbBundle.getBundle(EditPanelCookies.class).getString("MON_New_cookie_Mnemonic"));
153
154     deleteCookieB.addActionListener(new ActionListener JavaDoc() {
155
156         public void actionPerformed(ActionEvent JavaDoc e) {
157
158             int numRows = cookieTable.getRowCount();
159             StringBuffer JavaDoc buf = new StringBuffer JavaDoc
160             (NbBundle.getBundle(EditPanelCookies.class).getString("MON_Confirm_Delete_Cookies"));
161             buf.append("\n"); // NOI18N
162

163             for(int i=0; i<numRows; ++i) {
164
165             if(cookieTable.isRowSelected(i)) {
166                 buf.append(cookieTable.getValueAt(i, 0));
167                 buf.append(" "); // NOI18N
168
buf.append(cookieTable.getValueAt(i, 1));
169                 buf.append("\n"); // NOI18N
170
}
171             }
172
173             showConfirmDialog(buf.toString());
174             if(setCookies) {
175             
176             for(int i=0; i<numRows; ++i) {
177                 if(cookieTable.isRowSelected(i)) {
178
179                 if(debug) log(" deleting cookie " + //NOI18N
180
String.valueOf(i));
181         
182                 String JavaDoc name =
183                     (String JavaDoc)cookieTable.getValueAt(i, 0);
184                 String JavaDoc value =
185                     (String JavaDoc)cookieTable.getValueAt(i,
186                                    1);
187
188                 if(debug) log(name + ":" + value); //NOI18N
189
monitorData.getRequestData().deleteCookie(name, value);
190                 }
191             }
192             redisplayData();
193             }
194         }});
195     
196     int gridx = -1;
197     addGridBagComponent(this, createGlue(), ++gridx, ++gridy,
198                 1, 1, 1.0, 0,
199                 java.awt.GridBagConstraints.WEST,
200                 java.awt.GridBagConstraints.NONE,
201                 buttonInsets,
202                 0, 0);
203     addGridBagComponent(this, newCookieB, ++gridx, gridy,
204                 1, 1, 0, 0,
205                 java.awt.GridBagConstraints.EAST,
206                 java.awt.GridBagConstraints.NONE,
207                 buttonInsets,
208                 0, 0);
209
210     addGridBagComponent(this, deleteCookieB, ++gridx, gridy,
211                 1, 1, 0, 0,
212                 java.awt.GridBagConstraints.EAST,
213                 java.awt.GridBagConstraints.NONE,
214                 buttonInsets,
215                 0, 0);
216
217     setEnablings();
218     
219     this.setMaximumSize(this.getPreferredSize());
220     this.repaint();
221     }
222     
223     void showConfirmDialog(String JavaDoc msg) {
224
225     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION,
226                NotifyDescriptor.CANCEL_OPTION
227     };
228     
229     NotifyDescriptor confirmDialog =
230         new NotifyDescriptor((Object JavaDoc)msg,
231                  NbBundle.getBundle(EditPanelCookies.class).getString("MON_Confirmation_Required"),
232                  NotifyDescriptor.OK_CANCEL_OPTION,
233                  NotifyDescriptor.QUESTION_MESSAGE,
234                  options,
235                  NotifyDescriptor.CANCEL_OPTION);
236
237     DialogDisplayer.getDefault().notify(confirmDialog);
238     if(confirmDialog.getValue().equals(NotifyDescriptor.OK_OPTION))
239         setCookies = true;
240     else
241         setCookies = false;
242     }
243
244
245     void showErrorDialog() {
246
247     Object JavaDoc[] options = { NotifyDescriptor.OK_OPTION };
248     
249     NotifyDescriptor errorDialog =
250         new NotifyDescriptor((Object JavaDoc)NbBundle.getBundle(EditPanelCookies.class).getString("MON_Bad_cookie"),
251                  NbBundle.getBundle(EditPanelCookies.class).getString("MON_Invalid_input"),
252                  NotifyDescriptor.DEFAULT_OPTION,
253                  NotifyDescriptor.ERROR_MESSAGE,
254                  options,
255                  NotifyDescriptor.OK_OPTION);
256
257     DialogDisplayer.getDefault().notify(errorDialog);
258     }
259
260      
261     void setEnablings() {
262     // Always enable the Add button.
263
newCookieB.setEnabled(true);
264
265     // The delete row button is enabled if any rows are selected.
266
int selectedRows[] = cookieTable.getSelectedRows();
267     deleteCookieB.setEnabled(selectedRows.length > 0);
268     }
269
270     void setCookieTable() {
271
272     Param[] params = monitorData.getRequestData().getCookiesAsParams();
273     cookieTable = new DisplayTable(params, DisplayTable.COOKIES, true);
274
275         cookieTable.getAccessibleContext().setAccessibleName(NbBundle.getBundle(EditPanelCookies.class).getString("ACS_MON_CookiesTableA11yName"));
276         cookieTable.setToolTipText(NbBundle.getBundle(EditPanelCookies.class).getString("ACS_MON_CookiesTableA11yDesc"));
277
278     ListSelectionModel JavaDoc selma = cookieTable.getSelectionModel();
279     selma.addListSelectionListener(new ListSelectionListener JavaDoc() {
280         public void valueChanged(ListSelectionEvent JavaDoc evt) {
281         if(debug) log(" list selection event"); // NOI18N
282
setEnablings();
283         }
284     });
285
286     cookieTable.addTableModelListener(new TableModelListener JavaDoc() {
287         public void tableChanged(TableModelEvent JavaDoc evt) {
288         if(debug) log(" table model changed"); //NOI18N
289
updateCookieHeader();
290         }
291     });
292     }
293
294
295     void updateCookieHeader() {
296
297     if(debug) log("updateCookieHeader()"); //NOI18N
298
int numRows = cookieTable.getRowCount();
299     if(debug) log("Number of rows is: " + // NOI18N
300
String.valueOf(numRows));
301     if(numRows == 0) {
302         monitorData.getRequestData().setCookieHeader(""); //NOI18N
303
return;
304     }
305     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
306     for(int i=0; i<numRows; ++i) {
307         if(i>0) buf.append(";"); //NOI18N
308
buf.append(cookieTable.getValueAt(i,0));
309         buf.append("="); //NOI18N
310
buf.append(cookieTable.getValueAt(i,1));
311     }
312     monitorData.getRequestData().setCookieHeader(buf.toString());
313     if(debug) log(" new cookie string is " + buf.toString()); //NOI18N
314
}
315
316
317     public void repaint() {
318     super.repaint();
319     //if (editPanel != null)
320
//editPanel.repaint();
321
}
322
323     void log(String JavaDoc s) {
324     System.out.println("EditPanelCookies::" + s);//NOI18N
325
}
326
327 } // EditPanelCookies
328

329
330
Popular Tags