KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > client > SvnProgressSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.client;
21
22 import java.text.DateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24 import org.netbeans.api.progress.ProgressHandle;
25 import org.netbeans.api.progress.ProgressHandleFactory;
26 import org.netbeans.modules.subversion.Diagnostics;
27 import org.netbeans.modules.subversion.OutputLogger;
28 import org.netbeans.modules.subversion.Subversion;
29 import org.openide.util.Cancellable;
30 import org.openide.util.RequestProcessor;
31 import org.openide.util.TaskListener;
32 import org.tigris.subversion.svnclientadapter.SVNClientException;
33 import org.tigris.subversion.svnclientadapter.SVNUrl;
34
35 /**
36  *
37  * @author Tomas Stupka
38  */

39 public abstract class SvnProgressSupport implements Runnable JavaDoc, Cancellable {
40
41     private Cancellable delegate;
42     private boolean canceled;
43     private Thread JavaDoc interruptibleThread;
44     
45     private ProgressHandle progressHandle = null;
46     private String JavaDoc displayName = ""; // NOI18N
47
private String JavaDoc originalDisplayName = ""; // NOI18N
48
private OutputLogger logger;
49     private SVNUrl repositoryRoot;
50     private RequestProcessor.Task task;
51     
52     public RequestProcessor.Task start(RequestProcessor rp, SVNUrl repositoryRoot, String JavaDoc displayName) {
53         setDisplayName(displayName);
54         this.repositoryRoot = repositoryRoot;
55         task = rp.post(this);
56         task.addTaskListener(new TaskListener() {
57             public void taskFinished(org.openide.util.Task task) {
58                 delegate = null;
59                 interruptibleThread = null;
60                 delegate = null;
61             }
62         });
63         return task;
64     }
65
66     public void setRepositoryRoot(SVNUrl repositoryRoot) {
67         this.repositoryRoot = repositoryRoot;
68         logger = null;
69     }
70
71     public void run() {
72         startProgress();
73         performIntern();
74     }
75
76     protected void performIntern() {
77         try {
78             interruptibleThread = Thread.currentThread();
79             Diagnostics.println("Start - " + displayName); // NOI18N
80
if(!canceled) {
81                 perform();
82             }
83             Diagnostics.println("End - " + displayName); // NOI18N
84
} finally {
85             finnishProgress();
86             getLogger().closeLog();
87         }
88     }
89
90     protected abstract void perform();
91
92     public synchronized boolean isCanceled() {
93         return canceled;
94     }
95
96     public synchronized boolean cancel() {
97         getLogger().flushLog();
98         if (canceled) {
99             return false;
100         }
101         if(task != null) {
102             task.cancel();
103         }
104         if(delegate != null) {
105             delegate.cancel();
106         }
107         if (interruptibleThread != null) {
108             interruptibleThread.interrupt();
109         }
110         canceled = true;
111         return true;
112     }
113
114     void setCancellableDelegate(Cancellable cancellable) {
115         this.delegate = cancellable;
116     }
117
118     public void setDisplayName(String JavaDoc displayName) {
119         if(originalDisplayName.equals("")) { // NOI18N
120
originalDisplayName = displayName;
121         }
122         this.displayName = displayName;
123         if(progressHandle!=null) {
124             progressHandle.progress(displayName);
125         }
126     }
127
128     protected String JavaDoc getDisplayName() {
129         return displayName;
130     }
131
132     protected ProgressHandle getProgressHandle() {
133         if(progressHandle==null) {
134             progressHandle = ProgressHandleFactory.createHandle(displayName, this);
135         }
136         return progressHandle;
137     }
138
139     protected void startProgress() {
140         getProgressHandle().start();
141         getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + originalDisplayName); // NOI18N
142
}
143
144     protected void finnishProgress() {
145         getProgressHandle().finish();
146         if (isCanceled() == false) {
147             getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + originalDisplayName + " " + org.openide.util.NbBundle.getMessage(SvnProgressSupport.class, "MSG_Progress_Finished")); // NOI18N
148
} else {
149             getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + originalDisplayName + " " + org.openide.util.NbBundle.getMessage(SvnProgressSupport.class, "MSG_Progress_Canceled")); // NOI18N
150
}
151     }
152     
153     protected OutputLogger getLogger() {
154         if (logger == null) {
155             logger = Subversion.getInstance().getLogger(repositoryRoot);
156         }
157         return logger;
158     }
159     
160     public void annotate(SVNClientException ex) {
161         ExceptionHandler eh = new ExceptionHandler(ex);
162         if(isCanceled()) {
163             eh.notifyException(false);
164         } else {
165             eh.notifyException();
166         }
167     }
168 }
169
Popular Tags