KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > web > multiview > SecurityRolesEditorPanel


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 package org.netbeans.modules.j2ee.ddloaders.web.multiview;
22
23 import java.util.ArrayList JavaDoc;
24 import javax.swing.JTable JavaDoc;
25 import javax.swing.table.DefaultTableModel JavaDoc;
26 import javax.swing.table.TableModel JavaDoc;
27 import org.openide.util.NbBundle;
28
29 /**
30  * Editor panel for selecting security roles.
31  *
32  * @author ptliu
33  */

34 public class SecurityRolesEditorPanel extends javax.swing.JPanel JavaDoc {
35     
36     /** Creates new form SecurityRolesEditorPanel */
37     public SecurityRolesEditorPanel(String JavaDoc [] allRoles, String JavaDoc[] selectedRoles) {
38         initComponents();
39         
40         initTable(allRolesTable, getRemainingRoles(allRoles, selectedRoles),
41                 NbBundle.getMessage(SecurityConstraintPanel.class,"LBL_AllSecurityRoles"));
42         initTable(selectedRolesTable, selectedRoles,
43                 NbBundle.getMessage(SecurityConstraintPanel.class,"LBL_AllSecurityRoles"));
44     }
45     
46     public String JavaDoc[] getSelectedRoles() {
47         DefaultTableModel JavaDoc model = (DefaultTableModel JavaDoc) selectedRolesTable.getModel();
48         int rowCount = model.getRowCount();
49         String JavaDoc[] selectedRoles = new String JavaDoc[rowCount];
50         
51         for (int i = 0; i < rowCount; i++) {
52             selectedRoles[i] = (String JavaDoc) model.getValueAt(i, 0);
53         }
54         
55         return selectedRoles;
56     }
57     
58     private void initTable(JTable JavaDoc table, String JavaDoc[] data, String JavaDoc columnName) {
59         DefaultTableModel JavaDoc model = new DefaultTableModel JavaDoc() {
60             public boolean isCellEditable(int row, int column) {
61                 return false;
62             }
63         };
64         
65         model.addColumn(columnName);
66          
67         for (int i = 0; i < data.length; i++) {
68             model.addRow(new Object JavaDoc[] {data[i]});
69         }
70         
71         table.setModel(model);
72     }
73         
74     private String JavaDoc[] getRemainingRoles(String JavaDoc[] allRoles, String JavaDoc[] selectedRoles) {
75         ArrayList JavaDoc result = new ArrayList JavaDoc();
76         
77         for (int i = 0; i < allRoles.length; i++) {
78             String JavaDoc roleName = allRoles[i];
79             boolean found = false;
80             
81             for (int j = 0; j < selectedRoles.length; j++) {
82                 if (roleName.equals(selectedRoles[j])) {
83                     found = true;
84                     break;
85                 }
86             }
87             
88             if (!found) result.add(roleName);
89         }
90         
91         String JavaDoc[] remainingRoles = new String JavaDoc[result.size()];
92         return (String JavaDoc[]) result.toArray(remainingRoles);
93     }
94     
95     /** This method is called from within the constructor to
96      * initialize the form.
97      * WARNING: Do NOT modify this code. The content of this method is
98      * always regenerated by the Form Editor.
99      */

100     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
101
private void initComponents() {
102         addButton = new javax.swing.JButton JavaDoc();
103         removeButton = new javax.swing.JButton JavaDoc();
104         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
105         allRolesTable = new javax.swing.JTable JavaDoc();
106         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
107         selectedRolesTable = new javax.swing.JTable JavaDoc();
108
109         addButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ddloaders/web/multiview/Bundle").getString("LBL_AddSecurityRole"));
110         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
111             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
112                 addButtonActionPerformed(evt);
113             }
114         });
115
116         removeButton.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/ddloaders/web/multiview/Bundle").getString("LBL_RemoveSecurityRole"));
117         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
118             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
119                 removeButtonActionPerformed(evt);
120             }
121         });
122
123         allRolesTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
124             new Object JavaDoc [][] {
125                 {null, null, null, null},
126                 {null, null, null, null},
127                 {null, null, null, null},
128                 {null, null, null, null}
129             },
130             new String JavaDoc [] {
131                 "Title 1", "Title 2", "Title 3", "Title 4"
132             }
133         ));
134         jScrollPane1.setViewportView(allRolesTable);
135
136         selectedRolesTable.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
137             new Object JavaDoc [][] {
138                 {null, null, null, null},
139                 {null, null, null, null},
140                 {null, null, null, null},
141                 {null, null, null, null}
142             },
143             new String JavaDoc [] {
144                 "Title 1", "Title 2", "Title 3", "Title 4"
145             }
146         ));
147         jScrollPane2.setViewportView(selectedRolesTable);
148
149         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
150         this.setLayout(layout);
151         layout.setHorizontalGroup(
152             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
153             .add(layout.createSequentialGroup()
154                 .addContainerGap()
155                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 126, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
156                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
157                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
158                     .add(addButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
159                     .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
160                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
161                 .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 126, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
162                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
163         );
164         layout.setVerticalGroup(
165             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
166             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
167                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
168                     .add(layout.createSequentialGroup()
169                         .addContainerGap()
170                         .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 246, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
171                     .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
172                         .add(layout.createSequentialGroup()
173                             .add(83, 83, 83)
174                             .add(addButton)
175                             .add(35, 35, 35)
176                             .add(removeButton))
177                         .add(layout.createSequentialGroup()
178                             .addContainerGap()
179                             .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))))
180                 .addContainerGap())
181         );
182     }// </editor-fold>//GEN-END:initComponents
183

184     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
185
// TODO add your handling code here:
186
int[] selectedRows = selectedRolesTable.getSelectedRows();
187         DefaultTableModel JavaDoc allRolesTableModel = (DefaultTableModel JavaDoc) allRolesTable.getModel();
188         DefaultTableModel JavaDoc selectedRolesTableModel = (DefaultTableModel JavaDoc) selectedRolesTable.getModel();
189         
190         // Get the list of selected role names
191
for (int i = 0; i < selectedRows.length; i++) {
192             String JavaDoc roleName = (String JavaDoc) selectedRolesTableModel.getValueAt(selectedRows[i], 0);
193             allRolesTableModel.addRow(new Object JavaDoc[] {roleName});
194         }
195         
196         // Now remove the selected rows from the allRolesTable
197
for (int i = selectedRows.length-1; i >=0; i--) {
198             selectedRolesTableModel.removeRow(selectedRows[i]);
199         }
200     }//GEN-LAST:event_removeButtonActionPerformed
201

202     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
203
// TODO add your handling code here:
204
int[] selectedRows = allRolesTable.getSelectedRows();
205         DefaultTableModel JavaDoc allRolesTableModel = (DefaultTableModel JavaDoc) allRolesTable.getModel();
206         DefaultTableModel JavaDoc selectedRolesTableModel = (DefaultTableModel JavaDoc) selectedRolesTable.getModel();
207         
208         // Get the list of selected role names
209
for (int i = 0; i < selectedRows.length; i++) {
210             String JavaDoc roleName = (String JavaDoc) allRolesTableModel.getValueAt(selectedRows[i], 0);
211             selectedRolesTableModel.addRow(new Object JavaDoc[] {roleName});
212         }
213         
214         // Now remove the selected rows from the allRolesTable
215
for (int i = selectedRows.length-1; i >=0; i--) {
216             allRolesTableModel.removeRow(selectedRows[i]);
217         }
218     }//GEN-LAST:event_addButtonActionPerformed
219

220     
221     // Variables declaration - do not modify//GEN-BEGIN:variables
222
private javax.swing.JButton JavaDoc addButton;
223     private javax.swing.JTable JavaDoc allRolesTable;
224     private javax.swing.JScrollPane JavaDoc jScrollPane1;
225     private javax.swing.JScrollPane JavaDoc jScrollPane2;
226     private javax.swing.JButton JavaDoc removeButton;
227     private javax.swing.JTable JavaDoc selectedRolesTable;
228     // End of variables declaration//GEN-END:variables
229

230 }
231
Popular Tags