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.subversion.client;
043:
044: import java.text.DateFormat;
045: import java.util.Date;
046: import org.netbeans.api.progress.ProgressHandle;
047: import org.netbeans.api.progress.ProgressHandleFactory;
048: import org.netbeans.modules.subversion.OutputLogger;
049: import org.netbeans.modules.subversion.Subversion;
050: import org.openide.util.Cancellable;
051: import org.openide.util.NbBundle;
052: import org.openide.util.RequestProcessor;
053: import org.openide.util.TaskListener;
054: import org.tigris.subversion.svnclientadapter.SVNClientException;
055: import org.tigris.subversion.svnclientadapter.SVNUrl;
056:
057: /**
058: *
059: * @author Tomas Stupka
060: */
061: public abstract class SvnProgressSupport implements Runnable,
062: Cancellable {
063:
064: private Cancellable delegate;
065: private volatile boolean canceled;
066:
067: private ProgressHandle progressHandle = null;
068: private String displayName = ""; // NOI18N
069: private String originalDisplayName = ""; // NOI18N
070: private OutputLogger logger;
071: private SVNUrl repositoryRoot;
072: private RequestProcessor.Task task;
073:
074: public RequestProcessor.Task start(RequestProcessor rp,
075: SVNUrl repositoryRoot, String displayName) {
076: setDisplayName(displayName);
077: this .repositoryRoot = repositoryRoot;
078: startProgress();
079: setProgressQueued();
080: task = rp.post(this );
081: task.addTaskListener(new TaskListener() {
082: public void taskFinished(org.openide.util.Task task) {
083: delegate = null;
084: }
085: });
086: return task;
087: }
088:
089: public void setRepositoryRoot(SVNUrl repositoryRoot) {
090: this .repositoryRoot = repositoryRoot;
091: logger = null;
092: }
093:
094: public void run() {
095: setProgress();
096: performIntern();
097: }
098:
099: protected void performIntern() {
100: try {
101: Subversion.LOG.fine("Start - " + displayName); // NOI18N
102: if (!canceled) {
103: perform();
104: }
105: Subversion.LOG.fine("End - " + displayName); // NOI18N
106: } finally {
107: finnishProgress();
108: getLogger().closeLog();
109: }
110: }
111:
112: protected abstract void perform();
113:
114: public synchronized boolean isCanceled() {
115: return canceled;
116: }
117:
118: public synchronized boolean cancel() {
119: if (canceled) {
120: return false;
121: }
122: getLogger().flushLog();
123: if (task != null) {
124: task.cancel();
125: }
126: if (delegate != null) {
127: delegate.cancel();
128: }
129: getProgressHandle().finish();
130: canceled = true;
131: return true;
132: }
133:
134: void setCancellableDelegate(Cancellable cancellable) {
135: this .delegate = cancellable;
136: }
137:
138: public void setDisplayName(String displayName) {
139: if (originalDisplayName.equals("")) { // NOI18N
140: originalDisplayName = displayName;
141: }
142: this .displayName = displayName;
143: setProgress();
144: }
145:
146: private void setProgressQueued() {
147: if (progressHandle != null) {
148: progressHandle.progress(NbBundle
149: .getMessage(SvnProgressSupport.class, "LBL_Queued",
150: displayName));
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: getLogger()
175: .logCommandLine(
176: "==[IDE]== "
177: + DateFormat.getDateTimeInstance()
178: .format(new Date()) + " "
179: + originalDisplayName); // NOI18N
180: }
181:
182: protected void finnishProgress() {
183: getProgressHandle().finish();
184: if (isCanceled() == false) {
185: getLogger().logCommandLine(
186: "==[IDE]== "
187: + DateFormat.getDateTimeInstance().format(
188: new Date())
189: + " "
190: + originalDisplayName
191: + " "
192: + org.openide.util.NbBundle.getMessage(
193: SvnProgressSupport.class,
194: "MSG_Progress_Finished")); // NOI18N
195: } else {
196: getLogger().logCommandLine(
197: "==[IDE]== "
198: + DateFormat.getDateTimeInstance().format(
199: new Date())
200: + " "
201: + originalDisplayName
202: + " "
203: + org.openide.util.NbBundle.getMessage(
204: SvnProgressSupport.class,
205: "MSG_Progress_Canceled")); // NOI18N
206: }
207: }
208:
209: protected OutputLogger getLogger() {
210: if (logger == null) {
211: logger = Subversion.getInstance().getLogger(repositoryRoot);
212: }
213: return logger;
214: }
215:
216: public void annotate(SVNClientException ex) {
217: SvnClientExceptionHandler.notifyException(ex, !isCanceled(),
218: true);
219: }
220: }
|