1 20 21 package org.netbeans.modules.j2ee.ddloaders.web.multiview; 22 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import org.netbeans.core.api.multiview.MultiViewPerspective; 26 import org.netbeans.modules.j2ee.dd.api.common.SecurityRole; 27 import org.netbeans.modules.j2ee.dd.api.web.LoginConfig; 28 import org.netbeans.modules.j2ee.dd.api.web.SecurityConstraint; 29 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 30 import org.netbeans.modules.j2ee.ddloaders.web.DDDataObject; 31 import org.netbeans.modules.xml.multiview.ToolBarMultiViewElement; 32 import org.netbeans.modules.xml.multiview.ui.ConfirmDialog; 33 import org.netbeans.modules.xml.multiview.ui.EditDialog; 34 import org.netbeans.modules.xml.multiview.ui.SectionContainer; 35 import org.netbeans.modules.xml.multiview.ui.SectionContainerNode; 36 import org.netbeans.modules.xml.multiview.ui.SectionPanel; 37 import org.netbeans.modules.xml.multiview.ui.SectionView; 38 import org.netbeans.modules.xml.multiview.ui.SimpleDialogPanel; 39 import org.netbeans.modules.xml.multiview.ui.ToolBarDesignEditor; 40 import org.openide.nodes.AbstractNode; 41 import org.openide.nodes.Children; 42 import org.openide.nodes.Node; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 import org.openide.util.RequestProcessor; 46 47 54 public class SecurityMultiViewElement extends ToolBarMultiViewElement 55 implements PropertyChangeListener { 56 private SecurityView view; 57 private DDDataObject dObj; 58 private ToolBarDesignEditor editor; 59 private SecurityFactory factory; 60 private RequestProcessor.Task repaintingTask; 61 private WebApp webApp; 62 private AddConstraintAction addConstraintAction; 63 private RemoveConstraintAction removeConstraintAction; 64 private int index; 65 private boolean needInit = true; 66 67 private static final String SECURITY_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_SECURITY; 68 private static final String HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_SECURITY; 69 70 71 public SecurityMultiViewElement(DDDataObject dObj, int index) { 72 super(dObj); 73 74 this.dObj = dObj; 75 this.index = index; 76 editor = new ToolBarDesignEditor(); 77 factory = new SecurityFactory(editor, dObj); 78 addConstraintAction = new AddConstraintAction(dObj); 79 removeConstraintAction = new RemoveConstraintAction(); 80 81 setVisualEditor(editor); 82 83 repaintingTask = RequestProcessor.getDefault().create(new Runnable () { 84 public void run() { 85 javax.swing.SwingUtilities.invokeLater(new Runnable () { 86 public void run() { 87 repaintView(); 88 } 89 }); 90 } 91 }); 92 } 93 94 public SectionView getSectionView() { 95 return view; 96 } 97 98 public void componentShowing() { 99 super.componentShowing(); 100 dObj.setLastOpenView(index); 101 if (needInit || !dObj.isDocumentParseable()) { 102 repaintView(); 103 needInit=false; 104 } 105 } 106 107 public void componentOpened() { 108 super.componentOpened(); 109 dObj.getWebApp().addPropertyChangeListener(this); 110 } 111 112 public void componentClosed() { 113 super.componentClosed(); 114 dObj.getWebApp().removePropertyChangeListener(this); 115 } 116 117 public void propertyChange(PropertyChangeEvent evt) { 118 if (!dObj.isChangedFromUI()) { 119 String name = evt.getPropertyName(); 120 if ( name.indexOf("LoginConfig") > 0 || name.indexOf("Security") > 0 ) { MultiViewPerspective perspective = dObj.getSelectedPerspective(); 123 if (perspective != null && SECURITY_MV_ID.equals(perspective.preferredID())) { 124 repaintingTask.schedule(100); 125 } else { 126 needInit=true; 127 } 128 } 129 } 130 } 131 132 private void repaintView() { 133 webApp = dObj.getWebApp(); 134 view = new SecurityView(webApp); 135 editor.setContentView(view); 136 137 Object lastActive = editor.getLastActive(); 138 if (lastActive != null) { 139 ((SectionView)view).openPanel(lastActive); 140 } else { 141 SecurityView securityView = (SecurityView)view; 142 143 Node initialNode = view.getRolesNode(); 144 Children ch = initialNode.getChildren(); 145 if (ch.getNodesCount() > 0) 146 initialNode = ch.getNodes()[0]; 147 view.selectNode(initialNode); 148 } 149 view.checkValidity(); 150 dObj.checkParseable(); 151 152 } 153 154 private class SecurityView extends SectionView { 155 private WebApp webApp; 156 private SecurityRolesNode rolesNode; 157 private SectionContainerNode constraintsNode; 158 private SectionContainer constraintsContainer; 159 private LoginConfigNode configNode; 160 161 public SecurityView(WebApp webApp) { 162 super(factory); 163 this.webApp = webApp; 164 165 LoginConfig loginConfig = webApp.getSingleLoginConfig(); 166 configNode = new LoginConfigNode(); 167 addSection(new SectionPanel(this, configNode, "login_config")); 169 SecurityRole[] roles = webApp.getSecurityRole(); 170 rolesNode = new SecurityRolesNode(); 171 addSection(new SectionPanel(this, rolesNode, "security_roles")); 173 SecurityConstraint[] constraints = webApp.getSecurityConstraint(); 174 Node[] nodes = new Node[constraints.length]; 175 Children ch = new Children.Array(); 176 177 for (int i=0; i < nodes.length;i++) { 178 nodes[i] = new SecurityConstraintNode(constraints[i]); 179 } 180 181 ch.add(nodes); 182 constraintsNode = new SectionContainerNode(ch); 183 constraintsContainer = new SectionContainer(this, constraintsNode, 184 NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_SecurityConstraints"),false); 185 186 constraintsContainer.setHeaderActions(new javax.swing.Action []{addConstraintAction}); 187 188 SectionPanel[] pan = new SectionPanel[constraints.length]; 189 190 for (int i=0; i < nodes.length;i++) { 191 pan[i] = new SectionPanel(this, nodes[i], constraints[i]); 192 pan[i].setHeaderActions(new javax.swing.Action []{removeConstraintAction}); 193 constraintsContainer.addSection(pan[i]); 194 } 195 196 addSection(constraintsContainer); 197 constraintsNode.setDisplayName(NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_SecurityConstraints")); 199 201 ch = new Children.Array(); 202 ch.add(new Node[] {configNode, rolesNode, constraintsNode}); 203 AbstractNode root = new AbstractNode(ch); 204 setRoot(root); 205 } 206 207 public SecurityRolesNode getRolesNode() { 208 return rolesNode; 209 } 210 211 public SectionContainerNode getConstraintsNode() { 212 return constraintsNode; 213 } 214 215 public SectionContainer getConstraintsContainer() { 216 return constraintsContainer; 217 } 218 } 219 220 private class SecurityRolesNode extends AbstractNode { 221 public SecurityRolesNode() { 222 super(org.openide.nodes.Children.LEAF); 223 setDisplayName(NbBundle.getMessage(SecurityMultiViewElement.class,"TTL_SecurityRoles")); 224 } 225 226 public HelpCtx getHelpCtx() { 227 return new HelpCtx(HELP_ID_PREFIX+"securityrolesNode"); } 229 } 230 231 private class SecurityConstraintNode extends AbstractNode { 232 233 public SecurityConstraintNode(SecurityConstraint constraint) { 234 super(org.openide.nodes.Children.LEAF); 235 setDisplayName(constraint.getDefaultDisplayName()); 236 } 237 238 public HelpCtx getHelpCtx() { 239 return new HelpCtx(HELP_ID_PREFIX+"securityconstraintsNode"); } 241 } 242 243 private class LoginConfigNode extends AbstractNode { 244 public LoginConfigNode() { 245 super(org.openide.nodes.Children.LEAF); 246 setDisplayName(NbBundle.getMessage(SecurityMultiViewElement.class,"TTL_LoginConfig")); 247 } 248 249 public HelpCtx getHelpCtx() { 250 return new HelpCtx(HELP_ID_PREFIX+"loginconfigNode"); } 252 } 253 254 private class AddConstraintAction extends javax.swing.AbstractAction { 255 256 AddConstraintAction(final DDDataObject dObj) { 257 super(NbBundle.getMessage(SecurityMultiViewElement.class,"LBL_AddSecurityConstraint")); 258 char mnem = NbBundle.getMessage(SecurityMultiViewElement.class,"LBL_AddSecurityConstraint_mnem").charAt(0); 259 260 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 261 } 262 263 public void actionPerformed(java.awt.event.ActionEvent evt) { 264 dObj.modelUpdatedFromUI(); 265 dObj.setChangedFromUI(true); 266 267 try { 268 SecurityConstraint constraint = (SecurityConstraint) webApp.createBean("SecurityConstraint"); constraint.setDisplayName(getUniqueDefaultName()); 270 webApp.addSecurityConstraint(constraint); 271 272 SecurityView view = (SecurityView) editor.getContentView(); 273 Node node = new SecurityConstraintNode(constraint); 274 view.getConstraintsNode().getChildren().add(new Node[]{node}); 275 276 SectionPanel pan = new SectionPanel(view, node, constraint); 277 pan.setHeaderActions(new javax.swing.Action []{removeConstraintAction}); 278 view.getConstraintsContainer().addSection(pan, true); 279 } catch (ClassNotFoundException ex) { 280 } 281 } 282 283 private String getUniqueDefaultName() { 284 int counter = 0; 285 String defaultName = NbBundle.getMessage(SecurityMultiViewElement.class, 286 "TXT_DefaultConstraintName"); 287 SecurityConstraint[] constraints = webApp.getSecurityConstraint(); 288 289 while (true) { 290 String defaultNameEx = defaultName + (++counter); 291 292 boolean found = false; 293 for (int i = 0; i < constraints.length; i++) { 294 if (defaultNameEx.equals(constraints[i].getDefaultDisplayName())) { 295 found = true; 296 } 297 } 298 299 if (!found) return defaultNameEx; 300 } 301 } 302 } 303 304 305 private class RemoveConstraintAction extends javax.swing.AbstractAction { 306 307 RemoveConstraintAction() { 308 super(NbBundle.getMessage(SecurityMultiViewElement.class,"LBL_remove")); 309 char mnem = NbBundle.getMessage(SecurityMultiViewElement.class,"LBL_remove_mnem").charAt(0); 310 putValue(MNEMONIC_KEY,new Integer ((int)mnem)); 311 } 312 313 public void actionPerformed(java.awt.event.ActionEvent evt) { 314 org.openide.DialogDescriptor desc = new ConfirmDialog( 315 NbBundle.getMessage(SecurityMultiViewElement.class,"TXT_RemoveSecurityConstraintConfirm")); 316 java.awt.Dialog dialog = org.openide.DialogDisplayer.getDefault().createDialog(desc); 317 dialog.setVisible(true); 318 if (org.openide.DialogDescriptor.OK_OPTION.equals(desc.getValue())) { 319 SectionPanel sectionPanel = ((SectionPanel.HeaderButton)evt.getSource()).getSectionPanel(); 320 SecurityConstraint constraint = (SecurityConstraint) sectionPanel.getKey(); 321 dObj.modelUpdatedFromUI(); 323 dObj.setChangedFromUI(true); 324 try { 325 webApp.removeSecurityConstraint(constraint); 326 327 sectionPanel.getSectionView().removeSection(sectionPanel.getNode()); 329 } finally { 330 dObj.setChangedFromUI(false); 331 } 332 } 333 } 334 } 335 } 336 | Popular Tags |