KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > command > Command


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.command;
17
18 import org.columba.api.command.ICommand;
19 import org.columba.api.command.ICommandReference;
20 import org.columba.api.command.IWorkerStatusController;
21 import org.columba.core.base.Lock;
22
23 /**
24  * A Command uses the information provided from {@link DefaultCommandReference}
25  * to execute itself.
26  * <p>
27  * TODO: remove IFrameMediator dependency
28  *
29  * @author Timo Stich <tstich@users.sourceforge.net>
30  */

31 public abstract class Command implements ICommand {
32
33     /**
34      * Commands that can not be undone but previous commands can be undone, e.g.
35      * view message (default) line for constructor: commandType =
36      * Command.NORMAL_OPERATION;
37      */

38     public static final int NORMAL_OPERATION = 1;
39
40     /**
41      * Priorities: Commands that are started by an automated process, e.g.
42      * auto-check for new messages
43      */

44     public static final int DAEMON_PRIORITY = -10;
45
46     /**
47      * Normal priority for e.g. copying (default)
48      */

49     public static final int NORMAL_PRIORITY = 0;
50
51     /**
52      * Commands that the user waits for to finish, e.g. view message
53      */

54     public static final int REALTIME_PRIORITY = 10;
55
56     /**
57      * Never Use this!! - internally highest priority
58      */

59     public static final int DEFINETLY_NEXT_OPERATION_PRIORITY = 20;
60
61     /**
62      * Never use these!!! - for internal state control only
63      */

64     public static final int FIRST_EXECUTION = 0;
65
66     protected int priority;
67
68     protected int commandType;
69
70     protected boolean synchronize;
71
72     protected int timeStamp;
73
74     protected Lock[] folderLocks;
75
76     private ICommandReference reference;
77     
78
79     public Command(ICommandReference theReference) {
80         this.reference = theReference;
81
82         commandType = NORMAL_OPERATION;
83         priority = NORMAL_PRIORITY;
84         
85     }
86
87     public void process(Worker worker) throws Exception JavaDoc {
88         setTimeStamp(worker.getTimeStamp());
89         execute(worker);
90     }
91
92     /* (non-Javadoc)
93      * @see org.columba.api.command.ICommand#updateGUI()
94      */

95     public void updateGUI() throws Exception JavaDoc {
96         // nothing to do
97
}
98
99     /* (non-Javadoc)
100      * @see org.columba.api.command.ICommand#execute(org.columba.api.command.IWorkerStatusController)
101      */

102     public abstract void execute(IWorkerStatusController worker)
103             throws Exception JavaDoc;
104
105     public boolean canBeProcessed() {
106
107         boolean success = reference.tryToGetLock(this);
108         if (!success) {
109             releaseAllFolderLocks();
110         }
111         return success;
112
113     }
114
115     public void releaseAllFolderLocks() {
116
117         reference.releaseLock(this);
118
119     }
120
121     /** *********** Methods for interacting with the Operator ************ */
122
123     public int getCommandType() {
124         return commandType;
125     }
126
127     public int getPriority() {
128         return priority;
129     }
130
131     public void incPriority() {
132         priority++;
133     }
134
135     public boolean isSynchronize() {
136         return synchronize;
137     }
138
139     public void setSynchronize(boolean isSynchronize) {
140         this.synchronize = isSynchronize;
141     }
142
143     public void setPriority(int thePriority) {
144         this.priority = thePriority;
145     }
146
147     /**
148      * Returns the timeStamp.
149      *
150      * @return int
151      */

152     public int getTimeStamp() {
153         return timeStamp;
154     }
155
156     /**
157      * Sets the timeStamp.This method is for testing only!
158      *
159      * @param theTimeStamp
160      * The timeStamp to set
161      */

162     public void setTimeStamp(int theTimeStamp) {
163         this.timeStamp = theTimeStamp;
164     }
165
166     /* (non-Javadoc)
167      * @see org.columba.api.command.ICommand#getReference()
168      */

169     public ICommandReference getReference() {
170         return reference;
171     }
172
173     public void finish() throws Exception JavaDoc {
174         updateGUI();
175     }
176 }
Popular Tags