1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 29 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 30 import org.eclipse.core.runtime.IProgressMonitor; 31 import org.eclipse.core.runtime.IStatus; 32 import org.eclipse.core.runtime.MultiStatus; 33 import org.eclipse.core.runtime.ProgressMonitorWrapper; 34 import org.eclipse.core.runtime.Status; 35 import org.eclipse.core.runtime.jobs.Job; 36 37 38 public class ExtendedProgressMonitor extends ProgressMonitorWrapper 39 { 40 41 private boolean done; 42 43 private List errorStatusList; 44 45 private List cancelListenerList; 46 47 private Job checkCanceledJob; 48 49 50 public ExtendedProgressMonitor( IProgressMonitor monitor ) 51 { 52 super( monitor ); 53 this.done = false; 54 55 this.checkCanceledJob = new Job( BrowserCoreMessages.jobs__progressmonitor_check_cancellation ) 56 { 57 protected IStatus run( IProgressMonitor monitor ) 58 { 59 while ( !done ) 60 { 61 if ( isCanceled() ) 62 { 63 fireCancelRequested(); 64 break; 65 } 66 else 67 { 68 try 69 { 70 Thread.sleep( 1000 ); 71 } 72 catch ( InterruptedException e ) 73 { 74 } 75 } 76 } 77 return Status.OK_STATUS; 78 } 79 }; 80 this.checkCanceledJob.setSystem( true ); 81 this.checkCanceledJob.schedule(); 82 } 83 84 85 public void setCanceled( boolean b ) 86 { 87 super.setCanceled( b ); 88 if ( b ) 89 { 90 this.fireCancelRequested(); 91 } 92 } 93 94 95 public void done() 96 { 97 synchronized ( this ) 98 { 99 this.done = true; 100 super.done(); 101 } 102 } 103 104 105 public void addCancelListener( CancelListener listener ) 106 { 107 if ( cancelListenerList == null ) 108 cancelListenerList = new ArrayList (); 109 if ( !cancelListenerList.contains( listener ) ) 110 cancelListenerList.add( listener ); 111 } 112 113 114 public void removeCancelListener( CancelListener listener ) 115 { 116 if ( cancelListenerList != null && cancelListenerList.contains( listener ) ) 117 cancelListenerList.remove( listener ); 118 } 119 120 121 private void fireCancelRequested() 122 { 123 CancelEvent event = new CancelEvent( this ); 124 if ( cancelListenerList != null ) 125 { 126 for ( int i = 0; i < cancelListenerList.size(); i++ ) 127 { 128 CancelListener listener = ( CancelListener ) cancelListenerList.get( i ); 129 listener.cancelRequested( event ); 130 } 131 } 132 } 133 134 Job reportProgressJob = null; 135 136 String reportProgressMessage = null; 137 138 139 public void reportProgress( String message ) 140 { 141 synchronized ( this ) 142 { 143 if ( !done ) 144 { 145 if ( reportProgressJob == null ) 146 { 147 reportProgressJob = new Job( BrowserCoreMessages.jobs__progressmonitor_report_progress ) 148 { 149 protected IStatus run( IProgressMonitor monitor ) 150 { 151 synchronized ( ExtendedProgressMonitor.this ) 152 { 153 if ( !done ) 154 { 155 subTask( reportProgressMessage ); 156 } 157 return Status.OK_STATUS; 158 } 159 } 160 }; 161 reportProgressJob.setSystem( true ); 162 } 163 164 reportProgressMessage = message; 165 reportProgressJob.schedule( 1000 ); 166 } 167 } 168 } 169 170 171 public void reportError( String message ) 172 { 173 this.reportError( message, null ); 174 } 175 176 177 public void reportError( Throwable throwable ) 178 { 179 this.reportError( throwable.getMessage() != null ? throwable.getMessage() : throwable.toString(), throwable ); 180 } 181 182 183 public void reportError( String message, Throwable exception ) 184 { 185 186 if ( this.errorStatusList == null ) 187 this.errorStatusList = new ArrayList ( 3 ); 188 189 do 190 { 191 if ( message == null ) 192 message = ""; 194 Status errorStatus = new Status( IStatus.ERROR, BrowserCorePlugin.PLUGIN_ID, IStatus.ERROR, message, 195 exception ); 196 this.errorStatusList.add( errorStatus ); 197 198 if ( exception != null ) 199 { 200 exception = exception.getCause(); 201 } 202 if ( exception != null ) 203 { 204 message = exception.getMessage(); 205 } 206 } 207 while ( exception != null ); 208 209 } 210 211 212 public boolean errorsReported() 213 { 214 return this.errorStatusList != null; 215 } 216 217 218 public IStatus getErrorStatus( String message ) 219 { 220 221 if ( this.errorStatusList != null && !this.errorStatusList.isEmpty() ) 222 { 223 224 Throwable exception = null; 225 for ( Iterator it = this.errorStatusList.iterator(); it.hasNext(); ) 226 { 227 Status status = ( Status ) it.next(); 228 if ( status.getException() != null ) 229 { 230 exception = status.getException(); 231 break; 232 } 233 } 234 235 MultiStatus multiStatus = new MultiStatus( BrowserCorePlugin.PLUGIN_ID, IStatus.ERROR, message, exception ); 236 237 for ( Iterator it = this.errorStatusList.iterator(); it.hasNext(); ) 238 { 239 Status status = ( Status ) it.next(); 240 multiStatus.add( new Status( status.getSeverity(), status.getPlugin(), status.getCode(), status 241 .getMessage(), null ) ); 242 } 243 244 return multiStatus; 245 246 } 247 else 248 { 249 return Status.OK_STATUS; 250 } 251 } 252 253 public static class CancelEvent 254 { 255 private IProgressMonitor monitor; 256 257 258 public CancelEvent( IProgressMonitor monitor ) 259 { 260 this.monitor = monitor; 261 } 262 263 264 public IProgressMonitor getMonitor() 265 { 266 return monitor; 267 } 268 } 269 270 public interface CancelListener 271 { 272 public void cancelRequested( CancelEvent event ); 273 } 274 275 } 276 | Popular Tags |