001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * SimpleCancellableTask.java
044: *
045: * Created on August 15, 2005, 2:25 PM
046: *
047: */
048:
049: package org.netbeans.microedition.util;
050:
051: /**
052: * A simple implementation of <code>CancellableTask</code>. This implementation uses a supplied
053: * Runnable object, which is being run when this task starts.
054: * @author breh
055: */
056: public class SimpleCancellableTask implements CancellableTask {
057:
058: private Executable executable;
059: private Throwable caughtThrowable;
060:
061: /**
062: * Creates a new instance of SimpleCancellableTask
063: */
064: public SimpleCancellableTask() {
065: }
066:
067: /**
068: * Creates a new instance of SimpleCancellableTask with supplied executable
069: * object
070: * @param executable Executable to be used for execution.
071: */
072: public SimpleCancellableTask(Executable executable) {
073: this .executable = executable;
074: }
075:
076: /**
077: * Sets the executable object for this task. Also resets the failure message
078: * and the failure state.
079: *
080: * @param executable Executable to be used for execution.
081: */
082: public void setExecutable(Executable executable) {
083: caughtThrowable = null;
084: this .executable = executable;
085: }
086:
087: /**
088: * Cancel this task. In this implementation this method does not cancel the runnable
089: * task, this it always returns false.
090: * @return always returns false
091: */
092: public boolean cancel() {
093: // cancel does nothing in this simple implementation - always return false
094: return false;
095: }
096:
097: /**
098: * Gets the failure message of the failed task. Since this implementation considers
099: * as a failure an exception from the Runnable object (more exactly <code>run()</code>
100: * method), this methods returns a message from this exception.
101: * @return Message from failure exception
102: */
103: public String getFailureMessage() {
104: if (caughtThrowable != null) {
105: return caughtThrowable.getMessage();
106: } else {
107: return null;
108: }
109: }
110:
111: /**
112: * Checks whether the task has failed. In this implementation this means
113: * the the <code>execute()</code> method of the supplied <code>Executable</code> object has
114: * thrown an exception.
115: * @return true when the task has failed.
116: */
117: public boolean hasFailed() {
118: return caughtThrowable != null;
119: }
120:
121: /**
122: * Implementation of run method. This method basically calls <code>execute()</code> method
123: * from the suplied <code>Executable</code> object.
124: */
125: public void run() {
126: caughtThrowable = null;
127: if (executable != null) {
128: try {
129: executable.execute();
130: } catch (Throwable t) {
131: caughtThrowable = t;
132: }
133: }
134: }
135:
136: }
|