1 19 20 21 package org.netbeans.modules.j2ee.ddloaders.web.multiview; 22 23 import java.util.ArrayList ; 24 import javax.swing.JTable ; 25 import javax.swing.table.DefaultTableModel ; 26 import javax.swing.table.TableModel ; 27 import org.openide.util.NbBundle; 28 29 34 public class SecurityRolesEditorPanel extends javax.swing.JPanel { 35 36 37 public SecurityRolesEditorPanel(String [] allRoles, String [] 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 [] getSelectedRoles() { 47 DefaultTableModel model = (DefaultTableModel ) selectedRolesTable.getModel(); 48 int rowCount = model.getRowCount(); 49 String [] selectedRoles = new String [rowCount]; 50 51 for (int i = 0; i < rowCount; i++) { 52 selectedRoles[i] = (String ) model.getValueAt(i, 0); 53 } 54 55 return selectedRoles; 56 } 57 58 private void initTable(JTable table, String [] data, String columnName) { 59 DefaultTableModel model = new DefaultTableModel () { 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 [] {data[i]}); 69 } 70 71 table.setModel(model); 72 } 73 74 private String [] getRemainingRoles(String [] allRoles, String [] selectedRoles) { 75 ArrayList result = new ArrayList (); 76 77 for (int i = 0; i < allRoles.length; i++) { 78 String 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 [] remainingRoles = new String [result.size()]; 92 return (String []) result.toArray(remainingRoles); 93 } 94 95 100 private void initComponents() { 102 addButton = new javax.swing.JButton (); 103 removeButton = new javax.swing.JButton (); 104 jScrollPane1 = new javax.swing.JScrollPane (); 105 allRolesTable = new javax.swing.JTable (); 106 jScrollPane2 = new javax.swing.JScrollPane (); 107 selectedRolesTable = new javax.swing.JTable (); 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 () { 111 public void actionPerformed(java.awt.event.ActionEvent 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 () { 118 public void actionPerformed(java.awt.event.ActionEvent evt) { 119 removeButtonActionPerformed(evt); 120 } 121 }); 122 123 allRolesTable.setModel(new javax.swing.table.DefaultTableModel ( 124 new Object [][] { 125 {null, null, null, null}, 126 {null, null, null, null}, 127 {null, null, null, null}, 128 {null, null, null, null} 129 }, 130 new String [] { 131 "Title 1", "Title 2", "Title 3", "Title 4" 132 } 133 )); 134 jScrollPane1.setViewportView(allRolesTable); 135 136 selectedRolesTable.setModel(new javax.swing.table.DefaultTableModel ( 137 new Object [][] { 138 {null, null, null, null}, 139 {null, null, null, null}, 140 {null, null, null, null}, 141 {null, null, null, null} 142 }, 143 new String [] { 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 } 184 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { int[] selectedRows = selectedRolesTable.getSelectedRows(); 187 DefaultTableModel allRolesTableModel = (DefaultTableModel ) allRolesTable.getModel(); 188 DefaultTableModel selectedRolesTableModel = (DefaultTableModel ) selectedRolesTable.getModel(); 189 190 for (int i = 0; i < selectedRows.length; i++) { 192 String roleName = (String ) selectedRolesTableModel.getValueAt(selectedRows[i], 0); 193 allRolesTableModel.addRow(new Object [] {roleName}); 194 } 195 196 for (int i = selectedRows.length-1; i >=0; i--) { 198 selectedRolesTableModel.removeRow(selectedRows[i]); 199 } 200 } 202 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { int[] selectedRows = allRolesTable.getSelectedRows(); 205 DefaultTableModel allRolesTableModel = (DefaultTableModel ) allRolesTable.getModel(); 206 DefaultTableModel selectedRolesTableModel = (DefaultTableModel ) selectedRolesTable.getModel(); 207 208 for (int i = 0; i < selectedRows.length; i++) { 210 String roleName = (String ) allRolesTableModel.getValueAt(selectedRows[i], 0); 211 selectedRolesTableModel.addRow(new Object [] {roleName}); 212 } 213 214 for (int i = selectedRows.length-1; i >=0; i--) { 216 allRolesTableModel.removeRow(selectedRows[i]); 217 } 218 } 220 221 private javax.swing.JButton addButton; 223 private javax.swing.JTable allRolesTable; 224 private javax.swing.JScrollPane jScrollPane1; 225 private javax.swing.JScrollPane jScrollPane2; 226 private javax.swing.JButton removeButton; 227 private javax.swing.JTable selectedRolesTable; 228 230 } 231 | Popular Tags |