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: package org.netbeans.modules.mercurial;
043:
044: import java.text.DateFormat;
045: import java.util.Date;
046: import java.util.logging.Level;
047: import org.netbeans.api.progress.ProgressHandle;
048: import org.netbeans.api.progress.ProgressHandleFactory;
049: import org.openide.util.Cancellable;
050: import org.openide.util.NbBundle;
051: import org.openide.util.RequestProcessor;
052: import org.openide.util.TaskListener;
053:
054: /**
055: *
056: * @author Tomas Stupka
057: */
058: public abstract class HgProgressSupport implements Runnable,
059: Cancellable {
060:
061: private Cancellable delegate;
062: private volatile boolean canceled;
063:
064: private ProgressHandle progressHandle = null;
065: private String displayName = ""; // NOI18N
066: private String originalDisplayName = ""; // NOI18N
067: private OutputLogger logger;
068: private String repositoryRoot;
069: private RequestProcessor.Task task;
070:
071: public RequestProcessor.Task start(RequestProcessor rp,
072: String repositoryRoot, String displayName) {
073: setDisplayName(displayName);
074: this .repositoryRoot = repositoryRoot;
075: startProgress();
076: setProgressQueued();
077: task = rp.post(this );
078: task.addTaskListener(new TaskListener() {
079: public void taskFinished(org.openide.util.Task task) {
080: delegate = null;
081: }
082: });
083: return task;
084: }
085:
086: public void setRepositoryRoot(String repositoryRoot) {
087: this .repositoryRoot = repositoryRoot;
088: logger = null;
089: }
090:
091: public void run() {
092: setProgress();
093: performIntern();
094: }
095:
096: protected void performIntern() {
097: try {
098: Mercurial.LOG.log(Level.FINE, "Start - {0}", displayName); // NOI18N
099: if (!canceled) {
100: perform();
101: }
102: Mercurial.LOG.log(Level.FINE, "End - {0}", displayName); // NOI18N
103: } finally {
104: finnishProgress();
105: if (logger != null)
106: logger.closeLog();
107: }
108: }
109:
110: protected abstract void perform();
111:
112: public synchronized boolean isCanceled() {
113: return canceled;
114: }
115:
116: public synchronized boolean cancel() {
117: if (delegate != null) {
118: if (!delegate.cancel())
119: return false;
120: }
121: if (canceled) {
122: return false;
123: }
124: if (task != null) {
125: task.cancel();
126: }
127: Mercurial.getInstance().clearRequestProcessor(repositoryRoot);
128: getProgressHandle().finish();
129: canceled = true;
130: return true;
131: }
132:
133: public void setCancellableDelegate(Cancellable cancellable) {
134: this .delegate = cancellable;
135: }
136:
137: public void setDisplayName(String displayName) {
138: if (originalDisplayName.equals("")) { // NOI18N
139: originalDisplayName = displayName;
140: }
141: this .displayName = displayName;
142: setProgress();
143: }
144:
145: private void setProgressQueued() {
146: if (progressHandle != null) {
147: progressHandle
148: .progress(NbBundle.getMessage(
149: HgProgressSupport.class, "LBL_Queued",
150: displayName)); // NOI18N
151: }
152: }
153:
154: private void setProgress() {
155: if (progressHandle != null) {
156: progressHandle.progress(displayName);
157: }
158: }
159:
160: protected String getDisplayName() {
161: return displayName;
162: }
163:
164: protected ProgressHandle getProgressHandle() {
165: if (progressHandle == null) {
166: progressHandle = ProgressHandleFactory.createHandle(
167: displayName, this );
168: }
169: return progressHandle;
170: }
171:
172: protected void startProgress() {
173: getProgressHandle().start();
174: }
175:
176: protected void finnishProgress() {
177: getProgressHandle().finish();
178: }
179:
180: public OutputLogger getLogger() {
181: if (logger == null) {
182: logger = Mercurial.getInstance().getLogger(repositoryRoot);
183: }
184: return logger;
185: }
186:
187: public void annotate(HgException ex) {
188: ExceptionHandler eh = new ExceptionHandler(ex);
189: if (isCanceled()) {
190: eh.notifyException(false);
191: } else {
192: eh.notifyException();
193: }
194: }
195: }
|