KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
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 /**
48  * SecurityMultiViewElement.java
49  *
50  * Multiview element for creating the Security view.
51  *
52  * @author ptliu
53  */

54 public class SecurityMultiViewElement extends ToolBarMultiViewElement
55         implements PropertyChangeListener JavaDoc {
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 JavaDoc SECURITY_MV_ID=DDDataObject.DD_MULTIVIEW_PREFIX+DDDataObject.MULTIVIEW_SECURITY;
68     private static final String JavaDoc HELP_ID_PREFIX=DDDataObject.HELP_ID_PREFIX_SECURITY;
69     
70     /** Creates a new instance of SecurityMultiViewElement */
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 JavaDoc() {
84             public void run() {
85                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc evt) {
118         if (!dObj.isChangedFromUI()) {
119             String JavaDoc name = evt.getPropertyName();
120             if ( name.indexOf("LoginConfig") > 0 || name.indexOf("Security") > 0 ) { //NOI18N
121
// repaint view if the view is active and something is changed with the security view
122
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 JavaDoc 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")); //NOI18N
168

169             SecurityRole[] roles = webApp.getSecurityRole();
170             rolesNode = new SecurityRolesNode();
171             addSection(new SectionPanel(this, rolesNode, "security_roles")); //NOI18N
172

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 JavaDoc[]{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 JavaDoc[]{removeConstraintAction});
193                 constraintsContainer.addSection(pan[i]);
194             }
195             
196             addSection(constraintsContainer);
197             //root.setDisplayName("<Servlets>");
198
constraintsNode.setDisplayName(NbBundle.getMessage(ServletsMultiViewElement.class,"TTL_SecurityConstraints"));
199             //servletsNode.setName(HELP_ID_PREFIX+"servletsNode"); //NOI18N
200

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"); //NOI18N
228
}
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"); //NOI18N
240
}
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"); //NOI18N
251
}
252     }
253     
254     private class AddConstraintAction extends javax.swing.AbstractAction JavaDoc {
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 JavaDoc((int)mnem));
261         }
262         
263         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
264             dObj.modelUpdatedFromUI();
265             dObj.setChangedFromUI(true);
266             
267             try {
268                 SecurityConstraint constraint = (SecurityConstraint) webApp.createBean("SecurityConstraint"); //NOI18N
269
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 JavaDoc[]{removeConstraintAction});
278                 view.getConstraintsContainer().addSection(pan, true);
279             } catch (ClassNotFoundException JavaDoc ex) {
280             }
281         }
282         
283         private String JavaDoc getUniqueDefaultName() {
284             int counter = 0;
285             String JavaDoc defaultName = NbBundle.getMessage(SecurityMultiViewElement.class,
286                     "TXT_DefaultConstraintName");
287             SecurityConstraint[] constraints = webApp.getSecurityConstraint();
288             
289             while (true) {
290                 String JavaDoc 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 JavaDoc {
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 JavaDoc((int)mnem));
311         }
312         
313         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
314             org.openide.DialogDescriptor desc = new ConfirmDialog(
315                     NbBundle.getMessage(SecurityMultiViewElement.class,"TXT_RemoveSecurityConstraintConfirm"));
316             java.awt.Dialog JavaDoc 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                 // updating data model
322
dObj.modelUpdatedFromUI();
323                 dObj.setChangedFromUI(true);
324                 try {
325                     webApp.removeSecurityConstraint(constraint);
326                     
327                     // removing section
328
sectionPanel.getSectionView().removeSection(sectionPanel.getNode());
329                 } finally {
330                     dObj.setChangedFromUI(false);
331                 }
332             }
333         }
334     }
335 }
336
Popular Tags