KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > file > ActionLock


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.actions.file;
20
21 import java.awt.event.*;
22
23 import javax.swing.*;
24
25 import org.openharmonise.him.actions.*;
26 import org.openharmonise.him.actions.rules.*;
27 import org.openharmonise.him.context.StateHandler;
28 import org.openharmonise.him.window.messages.builders.*;
29 import org.openharmonise.vfs.*;
30 import org.openharmonise.vfs.context.*;
31 import org.openharmonise.vfs.gui.*;
32 import org.openharmonise.vfs.status.*;
33
34
35 /**
36  * Action to lock a virtual file.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class ActionLock extends AbstractHIMAction implements HIMAction {
43
44     public static String JavaDoc ACTION_NAME = "LOCK";
45
46     /**
47      *
48      */

49     public ActionLock() {
50         super();
51         this.setup();
52     }
53
54     /**
55      * @param vfFile
56      */

57     public ActionLock(VirtualFile vfFile) {
58         super(vfFile);
59         this.setup();
60     }
61     
62     /**
63      * Configures this action.
64      *
65      */

66     private void setup() {
67         RuleGroup andGroup = new RuleGroup();
68         andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
69         PathRule pathRule = new PathRule("/webdav/Archive/");
70         pathRule.setResultComparator(false);
71         andGroup.addEnableRule(pathRule);
72         IsHistoricalRule histRule = new IsHistoricalRule();
73         histRule.setResultComparator(false);
74         andGroup.addEnableRule(histRule);
75         SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_LOCK);
76         andGroup.addEnableRule(secRule);
77         this.addEnableRule(andGroup);
78     }
79
80     /* (non-Javadoc)
81      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
82      */

83     public void actionPerformed(ActionEvent arg0) {
84         StateHandler.getInstance().addWait("LOCK-ACTION");
85         StatusData statusOverall = new VFSStatus();
86         
87         String JavaDoc sFileName = null;
88         String JavaDoc sLockUser = null;
89         try {
90             if(this.getLastContextFile().isLocked()) {
91                 sLockUser = this.getLastContextFile().getLockOwner();
92             }
93             sFileName = this.getLastContextFile().getVFS().getVirtualFileSystemView().getDisplayName(this.getLastContextFile());
94             StatusData status = this.getLastContextFile().lock();
95             statusOverall.addStatusData(status);
96             
97             if(status.isOK()) {
98 // super.fireSessionEvent("Locked", this.getLastContextFile().getVFS(), this.getLastContextFile().getFullPath(), SessionEventData.RESOURCE_LOCKED);
99
} else {
100             }
101         } catch (Exception JavaDoc e) {
102             e.printStackTrace(System.err);
103             statusOverall.setStatusLevel(StatusData.LEVEL_ERROR);
104         } finally {
105             VFSMessageBuilder.getInstance().fireMessage(ActionLock.ACTION_NAME, statusOverall, sFileName, sLockUser);
106             StateHandler.getInstance().removeWait("LOCK-ACTION");
107         }
108     }
109
110     /* (non-Javadoc)
111      * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
112      */

113     public JMenuItem getMenuItem() {
114         JMenuItem menuItem = super.getMenuItem();
115         menuItem.setAccelerator( KeyStroke.getKeyStroke(this.getAcceleratorKeycode(), this.getAcceleratorMask()) );
116         
117         return menuItem;
118     }
119
120     /* (non-Javadoc)
121      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
122      */

123     public String JavaDoc getDescription() {
124         return "Locks the currently selected resource so that others cannot edit it";
125     }
126
127     /* (non-Javadoc)
128      * @see com.simulacramedia.contentmanager.actions.CMAction#getText()
129      */

130     public String JavaDoc getText() {
131         return "Lock";
132     }
133
134     /* (non-Javadoc)
135      * @see com.simulacramedia.contentmanager.actions.CMAction#getToolTip()
136      */

137     public String JavaDoc getToolTip() {
138         return this.getDescription();
139     }
140
141     /* (non-Javadoc)
142      * @see com.simulacramedia.contentmanager.actions.CMAction#getIcon()
143      */

144     public Icon getIcon() {
145         return IconManager.getInstance().getIcon("16-command-lock.gif");
146     }
147
148     /* (non-Javadoc)
149      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
150      */

151     public int getAcceleratorKeycode() {
152         return KeyEvent.VK_L;
153     }
154
155     /* (non-Javadoc)
156      * @see com.simulacramedia.contentmanager.actions.CMAction#getMnemonic()
157      */

158     public String JavaDoc getMnemonic() {
159         return "L";
160     }
161
162     /* (non-Javadoc)
163      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
164      */

165     public int getAcceleratorMask() {
166         return InputEvent.CTRL_MASK;
167     }
168     
169     
170
171     /* (non-Javadoc)
172      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.contentmanager.context.ContextEvent)
173      */

174     public boolean isEnabled(ContextEvent ce) {
175         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_FILES && super.isEnabled(ce)) {
176             VirtualFile vfFile = ce.getVFS().getVirtualFile(ce.getPath()).getResource();
177             if(vfFile!=null) {
178                 this.setEnabled(!vfFile.isLocked());
179                 return !vfFile.isLocked();
180             } else {
181                 return false;
182             }
183         } else {
184             this.setEnabled(false);
185             return false;
186         }
187     }
188
189 }
190
Popular Tags