KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > serverconfig > permissions > PermissionsServerConfigOptions


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.serverconfig.permissions;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27 import javax.swing.tree.*;
28
29 import org.openharmonise.him.*;
30 import org.openharmonise.him.configuration.*;
31 import org.openharmonise.him.context.StateHandler;
32 import org.openharmonise.him.editors.*;
33 import org.openharmonise.him.serverconfig.*;
34 import org.openharmonise.him.serverconfig.permissions.list.*;
35 import org.openharmonise.him.serverconfig.permissions.tree.*;
36 import org.openharmonise.him.swing.resourcetree.TreeModel;
37 import org.openharmonise.swing.*;
38 import org.openharmonise.vfs.*;
39 import org.openharmonise.vfs.context.*;
40 import org.openharmonise.vfs.event.*;
41 import org.openharmonise.vfs.gui.*;
42 import org.openharmonise.vfs.metadata.*;
43 import org.openharmonise.vfs.metadata.range.*;
44 import org.openharmonise.vfs.servers.*;
45 import org.openharmonise.vfs.status.*;
46
47 /**
48  * Panel for configuring the permissions options of a Harmonise Information
49  * Server.
50  *
51  * @author Matthew Large
52  * @version $Revision: 1.1 $
53  *
54  */

55 public class PermissionsServerConfigOptions
56     extends AbstractServerConfigOptions
57     implements LayoutManager, ActionListener, ApplyChangesListener, PermissionsSelectionListener, Runnable JavaDoc, VirtualFileListener {
58     
59     /**
60      * Description.
61      */

62     private JTextArea m_descriptionTextArea = null;
63
64     /**
65      * Scroll pane.
66      */

67     private JScrollPane m_scroller;
68
69     /**
70      * Virtual File System that roles are on.
71      */

72     private AbstractVirtualFileSystem m_vfs = null;
73     
74     /**
75      * List of roles.
76      */

77     private PermissionsSelectionList m_list = null;
78     
79     /**
80      * Permissions tree.
81      */

82     private JTree m_tree = null;
83     
84     /**
85      * New role button.
86      */

87     private JButton m_newRoleButton = null;
88     
89     /**
90      * Username for the current user.
91      */

92     private String JavaDoc m_sUsername = null;
93     
94     /**
95      * Password for the current user.
96      */

97     private String JavaDoc m_sPassword = null;
98     
99     /**
100      * Currently selected role.
101      */

102     private VirtualFile m_vfCurrentRole = null;
103
104     /**
105      * Constructs a new cache configuration options panel.
106      *
107      * @param dialog configuration dialog that this panel will be in.
108      */

109     public PermissionsServerConfigOptions(ServerConfigDialog dialog) {
110         super(dialog);
111         dialog.addApplyChangesListener(this);
112         this.setup();
113     }
114     
115     /**
116      * Initialises this component.
117      *
118      */

119     private void setup() {
120         this.m_vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
121         this.setLayout(this);
122             
123         this.m_sUsername = ServerList.getInstance().getHarmoniseServer().getVFS().getAuthentication().getUsername();
124         this.m_sPassword = ServerList.getInstance().getHarmoniseServer().getVFS().getAuthentication().getPassword();
125         
126         String JavaDoc fontName = "Dialog";
127         int fontSize = 11;
128         Font font = new Font(fontName, Font.PLAIN, fontSize);
129             
130         this.m_descriptionTextArea = new JTextArea("Use the list below to grant or deny a user group permission to access different functionality.");
131         this.m_descriptionTextArea.setEditable(false);
132         this.m_descriptionTextArea.setBorder(BorderFactory.createEmptyBorder());
133         this.m_descriptionTextArea.setWrapStyleWord(true);
134         this.m_descriptionTextArea.setLineWrap(true);
135         this.m_descriptionTextArea.setOpaque(false);
136         this.add(m_descriptionTextArea);
137         
138         this.m_list = new PermissionsSelectionList();
139         this.m_list.addPermissionsSelectionListener(this);
140         this.add(m_list);
141         VirtualFile vfRoleCollection = this.getRoleCollection();
142         Iterator itor = vfRoleCollection.getChildren().iterator();
143         while (itor.hasNext()) {
144             String JavaDoc sChildPath = (String JavaDoc) itor.next();
145             if(!sChildPath.equals("/webdav/RBS_VALS/roles/BROWSER")) {
146                 this.m_list.addFile(sChildPath, vfRoleCollection.getVFS());
147             }
148         }
149         
150         DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
151         TreeModel treeModel = new TreeModel(rootNode);
152         this.m_tree = new JTree(treeModel);
153         this.m_tree.setRootVisible(false);
154     
155         this.m_tree.setShowsRootHandles(true);
156         this.m_tree.setLargeModel(true);
157
158         this.m_tree.setCellRenderer( new PermissionsTreeCellRenderer());
159         m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
160         this.m_tree.setVisible(true);
161         this.m_tree.addTreeSelectionListener( new PermissionsTreeSelectionListener(this.m_tree));
162         
163         m_scroller = new JScrollPane(this.m_tree, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
164                                                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
165         m_scroller.setBorder(BorderFactory.createLineBorder(Color.BLACK));
166         this.add(m_scroller);
167         
168         this.m_newRoleButton = new JButton("New role");
169         this.m_newRoleButton.setFont(font);
170         this.m_newRoleButton.setActionCommand("NEW_ROLE");
171         this.m_newRoleButton.addActionListener(this);
172         this.add(this.m_newRoleButton);
173         
174         SwingUtilities.invokeLater(this);
175     }
176
177     /* (non-Javadoc)
178      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
179      */

180     public void layoutContainer(Container arg0) {
181             
182         int nHeight = 20;
183             
184         this.m_descriptionTextArea.setSize(350, 30);
185         this.m_descriptionTextArea.setLocation(20, nHeight);
186         nHeight = nHeight + this.m_descriptionTextArea.getSize().height + 10;
187         
188         this.m_list.setLocation(20, nHeight);
189         this.m_list.setSize(340, 100);
190         nHeight = nHeight + this.m_list.getSize().height + 10;
191         
192         this.m_newRoleButton.setLocation(280, nHeight);
193         this.m_newRoleButton.setSize(80, 20);
194         nHeight = nHeight + this.m_newRoleButton.getSize().height + 10;
195         
196         this.m_scroller.setLocation(20, nHeight);
197         this.m_scroller.setSize(340, 180);
198         nHeight = nHeight + this.m_scroller.getSize().height + 10;
199     }
200
201     /* (non-Javadoc)
202      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
203      */

204     public void actionPerformed(ActionEvent ae) {
205         if(ae.getActionCommand().equals("NEW_ROLE")) {
206             JFrame frame = new JFrame();
207             frame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
208         
209         
210             SingleTextEntryDialog txtEntry = new SingleTextEntryDialog(frame, "New Role Name");
211             txtEntry.setLabelText("Enter the name of the new role.");
212             txtEntry.show();
213             
214             String JavaDoc sValue = txtEntry.getTextValue();
215             if(sValue!=null && sValue.trim().length()>0) {
216                 StateHandler.getInstance().addWait("NEW_ROLE");
217                 try {
218                     VirtualFile vfRoleDir = this.getRoleCollection();
219                     String JavaDoc sPath = vfRoleDir.getFullPath() + "/" + txtEntry.getTextValue();
220                     if( EditorController.getInstance().createNew(sPath, vfRoleDir.getVFS()).isOK() ) {
221                         VirtualFile vfTemp = this.m_vfs.getVirtualFile(sPath).getResource();
222                         if(vfTemp.isVersionable() && ((VersionedVirtualFile)vfTemp).hasPendingVersion()) {
223                             VersionedVirtualFile vfPending = (VersionedVirtualFile) vfTemp.getVFS().getVirtualFile( ((VersionedVirtualFile)vfTemp).getPendingVersionPath() ).getResource();
224                             if(vfPending!=null) {
225                                 vfPending.checkin();
226                             }
227                         } else if(vfTemp.isVersionable() && ((VersionedVirtualFile)vfTemp).getState()==VirtualFile.STATE_PENDING) {
228                                 ((VersionedVirtualFile)vfTemp).checkin();
229                         }
230                     }
231                     
232                     this.remove(m_list);
233                     this.m_list = new PermissionsSelectionList();
234                     this.m_list.addPermissionsSelectionListener(this);
235                     this.add(m_list);
236                     VirtualFile vfRoleCollection = this.getRoleCollection();
237                     Iterator itor = vfRoleCollection.getChildren().iterator();
238                     while (itor.hasNext()) {
239                         String JavaDoc sChildPath = (String JavaDoc) itor.next();
240                         if(!sChildPath.equals("/webdav/RBS_VALS/roles/BROWSER")) {
241                             this.m_list.addFile(sChildPath, vfRoleCollection.getVFS());
242                         }
243                     }
244                     
245                     this.m_list.selectPermission(sPath);
246                 } catch(Exception JavaDoc e) {
247                     e.printStackTrace();
248                 } finally {
249                     StateHandler.getInstance().removeWait("NEW_ROLE");
250                 }
251             }
252         }
253     }
254
255     /* (non-Javadoc)
256      * @see com.simulacramedia.contentmanager.serverconfig.permissions.list.PermissionsSelectionListener#permissionSelected(java.lang.String)
257      */

258     public void permissionSelected(String JavaDoc sPath) {
259         StateHandler.getInstance().addWait(this.m_dialog, "PERMISSIONS-SELECTION");
260         try {
261             DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
262             TreeModel treeModel = new TreeModel(rootNode);
263             this.m_tree.setModel(treeModel);
264             this.m_tree.addMouseListener( new PermissionsTreeSelectionListener(this.m_tree));
265         
266             if(this.m_vfCurrentRole!=null) {
267                 this.m_vfCurrentRole.removeVirtualFileListener(this);
268             }
269         
270             VirtualFile vfRole = this.m_vfs.getVirtualFile(sPath).getResource();
271             
272             vfRole.addVirtualFileListener(this);
273         
274             Iterator itor = getObjectPropertyDefinitions().iterator();
275             while (itor.hasNext()) {
276                 Property prop = (Property) itor.next();
277                 ValueRange range = (ValueRange)prop.getRange();
278                 PropertyInstance propInst = vfRole.getProperty(prop.getNamespace(), prop.getName());
279             
280                 if(range.getValueGroups().size()>0) {
281                     PermissionsTreeNode node = new PermissionsTreeNode(propInst, (ValueGroup)range.getValueGroups().get(0), true);
282                     rootNode.add(node);
283                 } else {
284                     System.err.println("PERMISSIONS: No ValueGroup for Property [" + prop.getHREF() + "]");
285                 }
286             }
287         
288             this.validateTree();
289         
290             SwingUtilities.invokeLater(this);
291         } catch (Exception JavaDoc e) {
292             e.printStackTrace(System.err);
293         } finally {
294             StateHandler.getInstance().removeWait(this.m_dialog, "PERMISSIONS-SELECTION");
295         }
296
297     }
298
299     /* (non-Javadoc)
300      * @see com.simulacramedia.contentmanager.configuration.ApplyChangesListener#applyChanges()
301      */

302     public boolean applyChanges() {
303         boolean bOk = true;
304         Iterator itor = this.getRoleCollection().getChildren().iterator();
305         while (itor.hasNext()) {
306             String JavaDoc sPath = (String JavaDoc) itor.next();
307             VirtualFile vfTemp = this.m_vfs.getVirtualFile(sPath).getResource();
308             if(vfTemp.isChanged()) {
309                 StatusData status = vfTemp.sync();
310                 if(status.isOK()) {
311                     vfTemp = this.m_vfs.getVirtualFile(sPath).getResource();
312                     if(vfTemp.isVersionable() && ((VersionedVirtualFile)vfTemp).hasPendingVersion()) {
313                         VersionedVirtualFile vfPending = (VersionedVirtualFile) vfTemp.getVFS().getVirtualFile( ((VersionedVirtualFile)vfTemp).getPendingVersionPath() ).getResource();
314                         if(vfPending!=null) {
315                             vfPending.checkin();
316                         }
317                     } else if(vfTemp.getState()==VirtualFile.STATE_PENDING && vfTemp.isVersionable() && ((VersionedVirtualFile)vfTemp).getLiveVersionPath()==null) {
318                         ((VersionedVirtualFile)vfTemp).checkin();
319                     }
320                     this.permissionSelected(sPath);
321                 } else {
322                     bOk = false;
323                 }
324             }
325         }
326         return bOk;
327     }
328
329     /* (non-Javadoc)
330      * @see com.simulacramedia.contentmanager.configuration.ApplyChangesListener#discardChanges()
331      */

332     public void discardChanges() {
333         Iterator itor = this.getRoleCollection().getChildren().iterator();
334         while (itor.hasNext()) {
335             String JavaDoc sPath = (String JavaDoc) itor.next();
336             this.m_vfs.discardFileChanges(sPath);
337         }
338     }
339     
340     /* (non-Javadoc)
341     * @see java.lang.Runnable#run()
342     */

343    public void run() {
344        this.doLayout();
345        this.m_tree.expandPath( new TreePath(((DefaultMutableTreeNode)this.m_tree.getModel().getRoot()).getPath()) );
346    }
347     
348     /**
349      * Returns the collection containing the roles.
350      *
351      * @return collection containing the roles.
352      */

353     private VirtualFile getRoleCollection() {
354         return this.m_vfs.getVirtualFile("/webdav/RBS_VALS/roles").getResource();
355     }
356     
357     /**
358      * Returns a list of for Object Properties.
359      *
360      * @return List of {@link Property} objects.
361      */

362     public static List JavaDoc getObjectPropertyDefinitions() {
363         ArrayList aProperties = new ArrayList();
364         Server server = null;
365         server = ServerList.getInstance().getHarmoniseServer();
366         VirtualFile vfCollection = server.getVFS().getVirtualFile("/webdav/RBS_PROPS/object").getResource();
367         Iterator itor = vfCollection.getChildren().iterator();
368         while (itor.hasNext()) {
369             String JavaDoc sPath = (String JavaDoc) itor.next();
370             Property prop = PropertyCache.getInstance().getPropertyByPath(sPath);
371             aProperties.add(prop);
372         }
373         
374         return aProperties;
375     }
376     
377
378     /* (non-Javadoc)
379      * @see java.awt.Component#getPreferredSize()
380      */

381     public Dimension getPreferredSize() {
382         return new Dimension(this.getParent().getSize().width-50, 100);
383     }
384
385     /* (non-Javadoc)
386      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
387      */

388     public Dimension minimumLayoutSize(Container arg0) {
389         return this.getPreferredSize();
390     }
391
392     /* (non-Javadoc)
393      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
394      */

395     public Dimension preferredLayoutSize(Container arg0) {
396         return this.getPreferredSize();
397     }
398
399     /* (non-Javadoc)
400      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
401      */

402     public void removeLayoutComponent(Component arg0) {
403     }
404
405     /* (non-Javadoc)
406      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
407      */

408     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
409     }
410
411     /* (non-Javadoc)
412      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
413      */

414     public void virtualFileChanged(VirtualFileEvent vfe) {
415         if(vfe.getEventType()==VirtualFileEvent.FILE_METADATA_CHANGED) {
416             this.fireChangesMade();
417         }
418     }
419
420 }
421
Popular Tags