001    /**
002     * <copyright>
003     *
004     * Copyright (c) 2005-2006 IBM Corporation and others.
005     * All rights reserved.   This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     *
010     * Contributors:
011     *   IBM - Initial API and implementation
012     *
013     * </copyright>
014     *
015     * $Id: BasicMonitor.java,v 1.2 2006/12/05 20:19:56 emerks Exp $
016     */
017    package org.eclipse.emf.common.util;
018    
019    import java.io.PrintStream;
020    
021    import org.eclipse.core.runtime.IProgressMonitor;
022    import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
023    import org.eclipse.core.runtime.IStatus;
024    import org.eclipse.core.runtime.SubProgressMonitor;
025    
026    
027    /**
028     * The most basic implementation of a task monitor.
029     */
030    public class BasicMonitor implements Monitor 
031    {
032      private boolean isCanceled;
033      
034      private Diagnostic blockedReason;
035      
036      public BasicMonitor()
037      {
038        super();
039      }
040    
041      public boolean isCanceled()
042      {
043        return isCanceled;
044      }
045    
046      public void setCanceled(boolean isCanceled)
047      {
048        this.isCanceled = isCanceled;
049      }
050      
051      /**
052       * Returns the current reason for task being blocked, or <code>null</code>.
053       */
054      public Diagnostic getBlockedReason()
055      {
056        return blockedReason;
057      }
058      
059      public void setBlocked(Diagnostic reason)
060      {
061        this.blockedReason = reason;
062      }
063    
064      public void clearBlocked()
065      {
066        this.blockedReason = null;
067      }
068     
069      public void beginTask(String name, int totalWork)
070      {
071        // Do nothing.
072      }
073      
074      public void setTaskName(String name)
075      {
076        // Do nothing.
077      }
078    
079      public void subTask(String name)
080      {
081        // Do nothing.
082      }
083    
084      public void worked(int work)
085      {
086        // Do nothing.
087      }
088      
089      public void internalWorked(double work)
090      {
091        // Do nothing.
092      }
093        
094      public void done()
095      {
096        // Do nothing.
097      }
098      
099      /**
100       * A simple monitor that delegates to another monitor.
101       */
102      public static class Delegating
103      {
104        protected Monitor monitor;
105        
106        public Delegating(Monitor monitor)
107        {
108          this.monitor = monitor; 
109        }
110        
111        public boolean isCanceled()
112        {
113          return monitor.isCanceled();
114        }
115    
116        public void setCanceled(boolean value)
117        {
118          monitor.setCanceled(value);
119        }
120    
121        public void setBlocked(Diagnostic reason)
122        {
123          monitor.setBlocked(reason);
124        }
125        
126        public void clearBlocked()
127        {
128          monitor.clearBlocked();
129        }
130    
131        public void beginTask(String name, int totalWork)
132        {
133          monitor.beginTask(name, totalWork);
134        }
135    
136        public void setTaskName(String name)
137        {
138          monitor.setTaskName(name);
139        }
140    
141        public void subTask(String name)
142        {
143          monitor.subTask(name);
144        }
145    
146        public void worked(int work)
147        {
148          monitor.worked(work);
149        }
150        
151        public void internalWorked(double work)
152        {
153          monitor.internalWorked(work);
154        }
155    
156        public void done()
157        {
158          monitor.done();
159        }
160        
161        /**
162         * A simple monitor that delegates to another monitor, and implements the Eclipse API
163         */
164        private static class Eclipse extends Delegating implements IProgressMonitorWithBlocking
165        {
166          public Eclipse(Monitor monitor)
167          {
168            super(monitor);
169          }
170          
171          public void setBlocked(IStatus reason)
172          {
173            setBlocked
174              (new BasicDiagnostic
175                  (reason.getSeverity(), 
176                   reason.getPlugin(), 
177                   reason.getCode(), 
178                   reason.getMessage(), 
179                   null));
180          }
181      
182          public static IProgressMonitorWithBlocking createIProgressMonitorWithBlocking(Monitor monitor)
183          {
184            if (monitor instanceof IProgressMonitorWithBlocking)
185            {
186              return (IProgressMonitorWithBlocking)monitor;
187            }
188            else
189            {
190              return new Eclipse(monitor);
191            }
192          }
193          
194          public static IProgressMonitor createIProgressMonitor(Monitor monitor)
195          {
196            if (monitor instanceof IProgressMonitor)
197            {
198              return (IProgressMonitor)monitor;
199            }
200            else
201            {
202              return new Eclipse(monitor);
203            }
204          }
205        }
206      }
207      
208      /**
209       * Creates a delegating wrapper that allows the monitor to be used 
210       * in a context requiring an instance implementing the Eclipse API.
211       */
212      public static IProgressMonitor toIProgressMonitor(Monitor monitor)
213      {
214        return Delegating.Eclipse.createIProgressMonitor(monitor);
215      }
216      
217      /**
218       * Creates a delegating wrapper that allows the monitor to be used 
219       * in a context requiring an instance implementing the Eclipse API.
220       */
221      public static IProgressMonitorWithBlocking toIProgressMonitorWithBlocking(Monitor monitor)
222      {
223        return Delegating.Eclipse.createIProgressMonitorWithBlocking(monitor);
224      }
225      
226      /**
227       * A simple monitor that delegates to another Eclipse monitor.
228       */
229      private static class EclipseDelegating implements Monitor
230      {
231        protected IProgressMonitor progressMonitor;
232        protected IProgressMonitorWithBlocking progressMonitorWithBlocking;
233        
234        public EclipseDelegating(IProgressMonitor progressMonitor)
235        {
236          this.progressMonitor = progressMonitor; 
237          if (progressMonitor instanceof IProgressMonitorWithBlocking)
238          {
239            this.progressMonitorWithBlocking = (IProgressMonitorWithBlocking)progressMonitor;
240          }
241        }
242        
243        public EclipseDelegating(IProgressMonitorWithBlocking progressMonitorWithBlocking)
244        {
245          this.progressMonitor = progressMonitorWithBlocking; 
246          this.progressMonitorWithBlocking = progressMonitorWithBlocking; 
247        }
248        
249        public boolean isCanceled()
250        {
251          return progressMonitor.isCanceled();
252        }
253    
254        public void setCanceled(boolean value)
255        {
256          progressMonitor.setCanceled(value);
257        }
258    
259        public void setBlocked(Diagnostic reason)
260        {
261          if (progressMonitorWithBlocking != null)
262          {
263            progressMonitorWithBlocking.setBlocked(BasicDiagnostic.toIStatus(reason));
264          }
265        }
266    
267        public void clearBlocked()
268        {
269          if (progressMonitorWithBlocking != null)
270          {
271            progressMonitorWithBlocking.clearBlocked();
272          }
273        }
274    
275        public void beginTask(String name, int totalWork)
276        {
277          progressMonitor.beginTask(name, totalWork);
278        }
279        
280        public void setTaskName(String name)
281        {
282          progressMonitor.setTaskName(name);
283        }
284    
285        public void subTask(String name)
286        {
287          progressMonitor.subTask(name);
288        }
289    
290        public void worked(int work)
291        {
292          progressMonitor.worked(work);
293        }
294        
295        public void internalWorked(double work)
296        {
297          progressMonitor.internalWorked(work);
298        }
299    
300        public void done()
301        {
302          progressMonitor.done();
303        }
304      }
305      
306      /**
307       * Creates a delegating wrapper that allows the Eclipse progress monitor to be used 
308       * in a context requiring an instance implementing the monitor API.
309       */
310      public static Monitor toMonitor(IProgressMonitorWithBlocking progressMonitor)
311      {
312        return new EclipseDelegating(progressMonitor);
313      }
314      
315      /**
316       * Creates a delegating wrapper that allows the Eclipse progress monitor to be used 
317       * in a context requiring an instance implementing the monitor API.
318       */
319      public static Monitor toMonitor(IProgressMonitor progressMonitor)
320      {
321        return new EclipseDelegating(progressMonitor);
322      }
323      
324      /**
325       * An Eclipse subprogress monitor that directly implements the monitor API.
326       */
327      public static class EclipseSubProgress extends SubProgressMonitor implements Monitor
328      {
329        public EclipseSubProgress(IProgressMonitor monitor, int ticks)
330        {
331          super(monitor, ticks);
332        }
333    
334        public EclipseSubProgress(IProgressMonitor monitor, int ticks, int style)
335        {
336          super(monitor, ticks, style);
337        }
338    
339        public void setBlocked(Diagnostic reason)
340        {
341          super.setBlocked(BasicDiagnostic.toIStatus(reason));
342        }
343      }
344      
345      /**
346       * A simple monitor that prints progress to a print stream.
347       */
348      public static class Printing extends BasicMonitor
349      {
350        protected PrintStream printStream;
351    
352        public Printing(PrintStream printStream)
353        {
354          this.printStream = printStream;
355        }
356    
357        @Override
358        public void beginTask(String name, int totalWork)
359        {
360          if (name != null && name.length() != 0)
361          {
362            printStream.println(">>> " + name);
363          }
364        }
365    
366        @Override
367        public void setTaskName(String name)
368        {
369          if (name != null && name.length() != 0)
370          {
371            printStream.println("<>> " + name);
372          }
373        }
374    
375        @Override
376        public void subTask(String name)
377        {
378          if (name != null && name.length() != 0)
379          {
380            printStream.println(">>  " + name);
381          }
382        }
383        
384        @Override
385        public void setBlocked(Diagnostic reason)
386        {
387          super.setBlocked(reason);
388          printStream.println("#>  " + reason.getMessage());
389        }
390      
391        @Override
392        public void clearBlocked()
393        {
394          printStream.println("=>  " + getBlockedReason().getMessage());
395          super.clearBlocked();
396        }
397      }
398    }