KickJava   Java API By Example, From Geeks To Geeks.

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


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.files.*;
29 import org.openharmonise.vfs.*;
30 import org.openharmonise.vfs.context.*;
31 import org.openharmonise.vfs.gui.*;
32
33
34 /**
35  * Action to submit the changes for a virtual file.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class ActionSynchronise extends AbstractHIMAction implements HIMAction {
42
43     public static String JavaDoc ACTION_NAME = "SYNC_FILE";
44
45     /**
46      * Wait identifier for the state handler.
47      */

48     private static final String JavaDoc WAIT_LABEL = "SYNC-ACTION";
49     /**
50      *
51      */

52     public ActionSynchronise() {
53         super();
54         this.setup();
55     }
56
57     /**
58      * @param vfFile
59      */

60     public ActionSynchronise(VirtualFile vfFile) {
61         super(vfFile);
62         this.setup();
63     }
64     
65     /**
66      * Configures this action.
67      *
68      */

69     private void setup() {
70         RuleGroup andGroup = new RuleGroup();
71         SecurityRule secRule = new SecurityRule(VirtualFile.METHOD_SYNC);
72         andGroup.addEnableRule(null);
73         IsLockedByOtherUser lockRule = new IsLockedByOtherUser();
74         lockRule.setResultComparator(false);
75         andGroup.addEnableRule(lockRule);
76         super.addEnableRule(andGroup);
77     }
78
79     /* (non-Javadoc)
80      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
81      */

82     public void actionPerformed(ActionEvent ae) {
83         StateHandler.getInstance().addWait(WAIT_LABEL);
84         String JavaDoc sFileName = null;
85         
86         try {
87             VirtualFile vfFile = ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_FILES).getVFS().getVirtualFile( ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_FILES).getPath() ).getResource();
88             
89             FilesSynchroniser filesSyncer = new FilesSynchroniser();
90             boolean success = filesSyncer.syncFile(vfFile);
91             if(success){
92                 vfFile.unlock();
93             }
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96         } finally {
97             StateHandler.getInstance().removeWait(WAIT_LABEL);
98         }
99     }
100
101     /* (non-Javadoc)
102      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getText()
103      */

104     public String JavaDoc getText() {
105         return "Submit Resource";
106     }
107
108     /* (non-Javadoc)
109      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getToolTip()
110      */

111     public String JavaDoc getToolTip() {
112         return this.getDescription();
113     }
114
115     /* (non-Javadoc)
116      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getIcon()
117      */

118     public Icon getIcon() {
119         return IconManager.getInstance().getIcon("16-command-sync-all.gif");
120     }
121
122     /* (non-Javadoc)
123      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getMnemonic()
124      */

125     public String JavaDoc getMnemonic() {
126         return "S";
127     }
128
129     /* (non-Javadoc)
130      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
131      */

132     public String JavaDoc getDescription() {
133         return "Submits the current resource to the server";
134     }
135
136     /* (non-Javadoc)
137      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
138      */

139     public int getAcceleratorKeycode() {
140         return 0;
141     }
142
143     /* (non-Javadoc)
144      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
145      */

146     public int getAcceleratorMask() {
147         return 0;
148     }
149
150     /* (non-Javadoc)
151      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.contentmanager.context.ContextEvent)
152      */

153     public boolean isEnabled(ContextEvent ce) {
154         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_FILES) {
155             if(ce!=null && ce.getVFS()!=null && ce.getPath()!=null && ce.getVFS().getVirtualFile(ce.getPath())!=null && ce.getVFS().getVirtualFile(ce.getPath()).getResource().isChanged()) {
156                 super.setEnabled(true);
157                 return true;
158             } else {
159                 super.setEnabled(false);
160                 return false;
161             }
162         } else {
163             super.setEnabled(false);
164             return false;
165         }
166     }
167
168 }
169
Popular Tags