001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.command;
017:
018: import org.columba.api.command.ICommand;
019: import org.columba.api.command.ICommandReference;
020: import org.columba.api.command.IWorkerStatusController;
021: import org.columba.core.base.Lock;
022:
023: /**
024: * A Command uses the information provided from {@link DefaultCommandReference}
025: * to execute itself.
026: * <p>
027: * TODO: remove IFrameMediator dependency
028: *
029: * @author Timo Stich <tstich@users.sourceforge.net>
030: */
031: public abstract class Command implements ICommand {
032:
033: /**
034: * Commands that can not be undone but previous commands can be undone, e.g.
035: * view message (default) line for constructor: commandType =
036: * Command.NORMAL_OPERATION;
037: */
038: public static final int NORMAL_OPERATION = 1;
039:
040: /**
041: * Priorities: Commands that are started by an automated process, e.g.
042: * auto-check for new messages
043: */
044: public static final int DAEMON_PRIORITY = -10;
045:
046: /**
047: * Normal priority for e.g. copying (default)
048: */
049: public static final int NORMAL_PRIORITY = 0;
050:
051: /**
052: * Commands that the user waits for to finish, e.g. view message
053: */
054: public static final int REALTIME_PRIORITY = 10;
055:
056: /**
057: * Never Use this!! - internally highest priority
058: */
059: public static final int DEFINETLY_NEXT_OPERATION_PRIORITY = 20;
060:
061: /**
062: * Never use these!!! - for internal state control only
063: */
064: public static final int FIRST_EXECUTION = 0;
065:
066: protected int priority;
067:
068: protected int commandType;
069:
070: protected boolean synchronize;
071:
072: protected int timeStamp;
073:
074: protected Lock[] folderLocks;
075:
076: private ICommandReference reference;
077:
078: public Command(ICommandReference theReference) {
079: this .reference = theReference;
080:
081: commandType = NORMAL_OPERATION;
082: priority = NORMAL_PRIORITY;
083:
084: }
085:
086: public void process(Worker worker) throws Exception {
087: setTimeStamp(worker.getTimeStamp());
088: execute(worker);
089: }
090:
091: /* (non-Javadoc)
092: * @see org.columba.api.command.ICommand#updateGUI()
093: */
094: public void updateGUI() throws Exception {
095: // nothing to do
096: }
097:
098: /* (non-Javadoc)
099: * @see org.columba.api.command.ICommand#execute(org.columba.api.command.IWorkerStatusController)
100: */
101: public abstract void execute(IWorkerStatusController worker)
102: throws Exception;
103:
104: public boolean canBeProcessed() {
105:
106: boolean success = reference.tryToGetLock(this );
107: if (!success) {
108: releaseAllFolderLocks();
109: }
110: return success;
111:
112: }
113:
114: public void releaseAllFolderLocks() {
115:
116: reference.releaseLock(this );
117:
118: }
119:
120: /** *********** Methods for interacting with the Operator ************ */
121:
122: public int getCommandType() {
123: return commandType;
124: }
125:
126: public int getPriority() {
127: return priority;
128: }
129:
130: public void incPriority() {
131: priority++;
132: }
133:
134: public boolean isSynchronize() {
135: return synchronize;
136: }
137:
138: public void setSynchronize(boolean isSynchronize) {
139: this .synchronize = isSynchronize;
140: }
141:
142: public void setPriority(int thePriority) {
143: this .priority = thePriority;
144: }
145:
146: /**
147: * Returns the timeStamp.
148: *
149: * @return int
150: */
151: public int getTimeStamp() {
152: return timeStamp;
153: }
154:
155: /**
156: * Sets the timeStamp.This method is for testing only!
157: *
158: * @param theTimeStamp
159: * The timeStamp to set
160: */
161: public void setTimeStamp(int theTimeStamp) {
162: this .timeStamp = theTimeStamp;
163: }
164:
165: /* (non-Javadoc)
166: * @see org.columba.api.command.ICommand#getReference()
167: */
168: public ICommandReference getReference() {
169: return reference;
170: }
171:
172: public void finish() throws Exception {
173: updateGUI();
174: }
175: }
|