KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > ui > service > subpanels > TargetsPanel


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

19
20 package org.netbeans.modules.websvc.wsitconf.ui.service.subpanels;
21
22 import java.awt.Component JavaDoc;
23 import javax.swing.table.DefaultTableModel JavaDoc;
24 import javax.swing.table.TableCellEditor JavaDoc;
25 import org.netbeans.modules.websvc.wsitconf.ui.security.listmodels.*;
26 import org.netbeans.modules.websvc.wsitconf.wsdlmodelext.SecurityPolicyModelHelper;
27 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
28 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
29 import org.openide.DialogDescriptor;
30 import org.openide.DialogDisplayer;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.NbBundle;
33
34 import javax.swing.*;
35 import java.util.Vector JavaDoc;
36
37 /**
38  *
39  * @author Martin Grebac
40  */

41 public class TargetsPanel extends javax.swing.JPanel JavaDoc {
42     
43     private WSDLModel model;
44     private WSDLComponent comp;
45     
46     private Vector JavaDoc<Vector JavaDoc> targetsModel;
47     private MessagePartsModel targetsTableDataModel;
48     private Vector JavaDoc columnNames = new Vector JavaDoc();
49
50     private AddHeaderPanel addHeaderPanel;
51     
52     boolean inSync = false;
53     
54     /**
55      * Creates new form TargetsPanel
56      */

57     public TargetsPanel(WSDLComponent c) {
58         super();
59         this.model = c.getModel();
60         this.comp = c;
61         initComponents();
62
63         columnNames.add(NbBundle.getMessage(TargetsPanel.class, "LBL_Targets_MessagePart")); //NOI18N
64
columnNames.add(NbBundle.getMessage(TargetsPanel.class, "LBL_Targets_Sign")); //NOI18N
65
columnNames.add(NbBundle.getMessage(TargetsPanel.class, "LBL_Targets_Encrypt")); //NOI18N
66
columnNames.add(NbBundle.getMessage(TargetsPanel.class, "LBL_Targets_Require")); //NOI18N
67

68         sync();
69     }
70     
71     private void sync() {
72         inSync = true;
73         
74         targetsModel = SecurityPolicyModelHelper.getTargets(comp);
75         targetsTableDataModel = new MessagePartsModel(getTargetsModel(), columnNames);
76         jTable1.setModel(targetsTableDataModel);
77         jTable1.doLayout();
78         jTable1.setDefaultEditor(MessageElement.class, new XPathTableCellEditor());
79         jTable1.getColumnModel().getColumn(TargetElement.DATA).setCellEditor(new XPathTableCellEditor());
80         
81         enableDisable();
82         
83         inSync = false;
84     }
85     
86     private AddHeaderPanel getAddHeaderPanel() {
87         if (this.addHeaderPanel == null) {
88             addHeaderPanel = new AddHeaderPanel();
89         }
90         return addHeaderPanel;
91     }
92     
93     private void saveTargetsModel() {
94         if (!inSync) {
95             SecurityPolicyModelHelper.setTargets(comp, getTargetsModel());
96             jTable1.setModel(new MessagePartsModel(getTargetsModel(), columnNames));
97         }
98     }
99
100 // private void disableComponents(JComponent c, boolean enable) {
101
// Component[] comps = c.getComponents();
102
// if ((comps != null) && (comps.length > 0)) {
103
// for (Component comp : comps) {
104
// if (comp instanceof JComponent) {
105
// disableComponents((JComponent)comp, enable);
106
// }
107
// comp.setEnabled(enable);
108
// }
109
// }
110
// c.setEnabled(enable);
111
// }
112

113     private void enableDisable() {
114 // jTable1.setEnabled(true);
115
// jScrollPane2.setEnabled(!defaults);
116
// jScrollPane2.getViewport().getView().setEnabled(!defaults);
117
// jScrollPane2.getHorizontalScrollBar().setEnabled(!defaults);
118
// jScrollPane2.getVerticalScrollBar().setEnabled(!defaults);
119
// disableComponents(jScrollPane2, !defaults);
120
// addBodyButton.setEnabled(!defaults);
121
// addHeaderButton.setEnabled(!defaults);
122
// addPartButton.setEnabled(!defaults);
123
// removeButton.setEnabled(!defaults);
124
}
125
126     /** This method is called from within the constructor to
127      * initialize the form.
128      * WARNING: Do NOT modify this code. The content of this method is
129      * always regenerated by the Form Editor.
130      */

131     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
132
private void initComponents() {
133         addHeaderButton = new javax.swing.JButton JavaDoc();
134         removeButton = new javax.swing.JButton JavaDoc();
135         addPartButton = new javax.swing.JButton JavaDoc();
136         addBodyButton = new javax.swing.JButton JavaDoc();
137         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
138         jTable1 = new javax.swing.JTable JavaDoc();
139
140         addHeaderButton.setText(org.openide.util.NbBundle.getMessage(TargetsPanel.class, "LBL_AddHeader")); // NOI18N
141
addHeaderButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
142             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
143                 addHeaderButtonActionPerformed(evt);
144             }
145         });
146
147         removeButton.setText(org.openide.util.NbBundle.getMessage(TargetsPanel.class, "LBL_Remove")); // NOI18N
148
removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
149             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
150                 removeButtonActionPerformed(evt);
151             }
152         });
153
154         addPartButton.setText(org.openide.util.NbBundle.getMessage(TargetsPanel.class, "LBL_AddXPath")); // NOI18N
155
addPartButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
156             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
157                 addPartButtonActionPerformed(evt);
158             }
159         });
160
161         addBodyButton.setText(org.openide.util.NbBundle.getMessage(TargetsPanel.class, "LBL_AddBody")); // NOI18N
162
addBodyButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
163             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
164                 addBodyButtonActionPerformed(evt);
165             }
166         });
167
168         jTable1.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
169             new Object JavaDoc [][] {
170                 {null, null, null, null},
171                 {null, null, null, null},
172                 {null, null, null, null},
173                 {null, null, null, null}
174             },
175             new String JavaDoc [] {
176                 "Title 1", "Title 2", "Title 3", "Title 4"
177             }
178         ));
179         jTable1.setShowVerticalLines(false);
180         jTable1.setVerifyInputWhenFocusTarget(false);
181         jScrollPane2.setViewportView(jTable1);
182
183         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
184         this.setLayout(layout);
185         layout.setHorizontalGroup(
186             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
187             .add(layout.createSequentialGroup()
188                 .addContainerGap()
189                 .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
190                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
191                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
192                     .add(addBodyButton)
193                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
194                         .add(addPartButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
195                         .add(removeButton))
196                     .add(addHeaderButton))
197                 .addContainerGap())
198         );
199
200         layout.linkSize(new java.awt.Component JavaDoc[] {addBodyButton, addHeaderButton, addPartButton, removeButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);
201
202         layout.setVerticalGroup(
203             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
204             .add(layout.createSequentialGroup()
205                 .addContainerGap()
206                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
207                     .add(layout.createSequentialGroup()
208                         .add(addBodyButton)
209                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
210                         .add(addHeaderButton)
211                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
212                         .add(addPartButton)
213                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
214                         .add(removeButton))
215                     .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
216                 .addContainerGap())
217         );
218     }// </editor-fold>//GEN-END:initComponents
219

220     private void addBodyButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addBodyButtonActionPerformed
221
MessageBody body = new MessageBody();
222         if (!(SecurityPolicyModelHelper.targetExists(getTargetsModel(), body) != null)) {
223             Vector JavaDoc row = new Vector JavaDoc();
224             row.add(TargetElement.DATA, body);
225             row.add(TargetElement.SIGN, Boolean.TRUE);
226             row.add(TargetElement.ENCRYPT, Boolean.FALSE);
227             row.add(TargetElement.REQUIRE, Boolean.FALSE);
228             getTargetsModel().add(row);
229             saveTargetsModel();
230         }
231     }//GEN-LAST:event_addBodyButtonActionPerformed
232

233     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
234
int[] rows = jTable1.getSelectedRows();
235         for (int i=0; i<rows.length; i++) {
236             getTargetsModel().remove(rows[i]);
237         }
238         saveTargetsModel();
239     }//GEN-LAST:event_removeButtonActionPerformed
240

241     private void addPartButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addPartButtonActionPerformed
242
MessageElement e = new MessageElement(NbBundle.getMessage(TargetsPanel.class, "TXT_EditHere")); //NOI18N
243
Vector JavaDoc row = new Vector JavaDoc();
244         row.add(TargetElement.DATA, e);
245         row.add(TargetElement.SIGN, Boolean.FALSE);
246         row.add(TargetElement.ENCRYPT, Boolean.FALSE);
247         row.add(TargetElement.REQUIRE, Boolean.TRUE);
248         getTargetsModel().add(row);
249         jTable1.updateUI();
250     }//GEN-LAST:event_addPartButtonActionPerformed
251

252     private void addHeaderButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addHeaderButtonActionPerformed
253
AddHeaderPanel addHeaderPanel = getAddHeaderPanel();
254         DialogDescriptor dd = new DialogDescriptor(
255                 addHeaderPanel,
256                 NbBundle.getMessage(TargetsPanel.class, "LBL_SignEncryptChooser_AddHeaderTitle"), //NOI18N
257
true,
258                 DialogDescriptor.OK_CANCEL_OPTION,
259                 DialogDescriptor.CANCEL_OPTION,
260                 DialogDescriptor.DEFAULT_ALIGN,
261                 new HelpCtx(TargetsPanel.class),
262                 null);
263         if (DialogDisplayer.getDefault().notify(dd).equals(DialogDescriptor.OK_OPTION)) {
264             if (addHeaderPanel != null) {
265                 if (addHeaderPanel.isAllHeaders()) {
266                     for (String JavaDoc s : MessageHeader.ALL_HEADERS) {
267                         MessageHeader h = new MessageHeader(s);
268                         if (!(SecurityPolicyModelHelper.targetExists(getTargetsModel(), h) != null)) {
269                             Vector JavaDoc row = new Vector JavaDoc();
270                             row.add(TargetElement.DATA, h);
271                             row.add(TargetElement.SIGN, Boolean.TRUE);
272                             row.add(TargetElement.ENCRYPT, Boolean.FALSE);
273                             row.add(TargetElement.REQUIRE, Boolean.FALSE);
274                             getTargetsModel().add(row);
275                         }
276                     }
277                 } else {
278                     String JavaDoc header = addHeaderPanel.getHeader();
279                     MessageHeader h = new MessageHeader(header);
280                     if (!(SecurityPolicyModelHelper.targetExists(getTargetsModel(), h) != null)) {
281                         Vector JavaDoc row = new Vector JavaDoc();
282                         row.add(TargetElement.DATA, h);
283                         row.add(TargetElement.SIGN, Boolean.TRUE);
284                         row.add(TargetElement.ENCRYPT, Boolean.FALSE);
285                         row.add(TargetElement.REQUIRE, Boolean.FALSE);
286                         getTargetsModel().add(row);
287                     }
288                 }
289                 saveTargetsModel();
290             }
291         }
292     }//GEN-LAST:event_addHeaderButtonActionPerformed
293

294     public class XPathTableCellEditor extends AbstractCellEditor implements TableCellEditor JavaDoc {
295         // This is the component that will handle the editing of the cell value
296
JTextField component = new JTextField();
297     
298         // This method is called when a cell value is edited by the user.
299
public Component JavaDoc getTableCellEditorComponent(JTable table, Object JavaDoc value,
300                 boolean isSelected, int rowIndex, int vColIndex) {
301     
302             if (isSelected) {
303                     // cell (and perhaps other cells) are selected
304

305                 // Configure the component with the specified value
306
component.setText(((MessageElement)value).getElement());
307
308                 // Return the configured component
309
return component;
310                
311             }
312             return null;
313         }
314     
315         // This method is called when editing is completed.
316
// It must return the new value to be stored in the cell.
317
public Object JavaDoc getCellEditorValue() {
318 // component.setVisible(false);
319
return new MessageElement(component.getText());
320         }
321
322     }
323
324     private class MessagePartsModel extends DefaultTableModel JavaDoc {
325
326         public MessagePartsModel (Vector JavaDoc<Vector JavaDoc> data, Vector JavaDoc columnNames) {
327             super (data, columnNames);//NOI18N
328
}
329
330         @Override JavaDoc
331         public boolean isCellEditable(int row, int column) {
332             Vector JavaDoc rowVector = getTargetsModel().get(row);
333             if (column == 0) { // heading - allow editing only of xpath values
334
return (rowVector.get(TargetElement.DATA) instanceof MessageElement);
335             }
336             if ((column == 1) || (column ==2)) {
337                 return true;
338             }
339             if (column == 3) {
340                 return (rowVector.get(TargetElement.DATA) instanceof MessageElement);
341             }
342             return false;
343         }
344
345         @Override JavaDoc
346         public Class JavaDoc getColumnClass(int columnIndex) {
347             if (columnIndex == 0) {
348                 return TargetElement.class;
349             }
350             return Boolean JavaDoc.class;
351         }
352     }
353
354     public Vector JavaDoc<Vector JavaDoc> getTargetsModel() {
355         return targetsModel;
356     }
357     
358     // Variables declaration - do not modify//GEN-BEGIN:variables
359
private javax.swing.JButton JavaDoc addBodyButton;
360     private javax.swing.JButton JavaDoc addHeaderButton;
361     private javax.swing.JButton JavaDoc addPartButton;
362     private javax.swing.JScrollPane JavaDoc jScrollPane2;
363     private javax.swing.JTable JavaDoc jTable1;
364     private javax.swing.JButton JavaDoc removeButton;
365     // End of variables declaration//GEN-END:variables
366

367 }
368
369
Popular Tags