1 19 20 package org.netbeans.modules.subversion.client; 21 22 import java.text.DateFormat ; 23 import java.util.Date ; 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 39 public abstract class SvnProgressSupport implements Runnable , Cancellable { 40 41 private Cancellable delegate; 42 private boolean canceled; 43 private Thread interruptibleThread; 44 45 private ProgressHandle progressHandle = null; 46 private String displayName = ""; private String originalDisplayName = ""; private OutputLogger logger; 49 private SVNUrl repositoryRoot; 50 private RequestProcessor.Task task; 51 52 public RequestProcessor.Task start(RequestProcessor rp, SVNUrl repositoryRoot, String 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); if(!canceled) { 81 perform(); 82 } 83 Diagnostics.println("End - " + displayName); } 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 displayName) { 119 if(originalDisplayName.equals("")) { originalDisplayName = displayName; 121 } 122 this.displayName = displayName; 123 if(progressHandle!=null) { 124 progressHandle.progress(displayName); 125 } 126 } 127 128 protected String 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 ()) + " " + originalDisplayName); } 143 144 protected void finnishProgress() { 145 getProgressHandle().finish(); 146 if (isCanceled() == false) { 147 getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date ()) + " " + originalDisplayName + " " + org.openide.util.NbBundle.getMessage(SvnProgressSupport.class, "MSG_Progress_Finished")); } else { 149 getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date ()) + " " + originalDisplayName + " " + org.openide.util.NbBundle.getMessage(SvnProgressSupport.class, "MSG_Progress_Canceled")); } 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 |