Guest User

AbstractTimeConsumingCustomFeature

a guest
May 7th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2014, IETR/INSA of Rennes
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *
  8.  *   * Redistributions of source code must retain the above copyright notice,
  9.  *     this list of conditions and the following disclaimer.
  10.  *   * Redistributions in binary form must reproduce the above copyright notice,
  11.  *     this list of conditions and the following disclaimer in the documentation
  12.  *     and/or other materials provided with the distribution.
  13.  *   * Neither the name of the IETR/INSA of Rennes nor the names of its
  14.  *     contributors may be used to endorse or promote products derived from this
  15.  *     software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  26.  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29. import java.util.Collection;
  30. import java.util.Collections;
  31.  
  32. import org.eclipse.core.runtime.IProgressMonitor;
  33. import org.eclipse.core.runtime.IStatus;
  34. import org.eclipse.core.runtime.OperationCanceledException;
  35. import org.eclipse.core.runtime.Status;
  36. import org.eclipse.core.runtime.jobs.Job;
  37. import org.eclipse.emf.transaction.RecordingCommand;
  38. import org.eclipse.emf.transaction.TransactionalEditingDomain;
  39. import org.eclipse.emf.transaction.util.TransactionUtil;
  40. import org.eclipse.graphiti.features.IFeatureProvider;
  41. import org.eclipse.graphiti.features.context.IContext;
  42. import org.eclipse.graphiti.features.context.ICustomContext;
  43. import org.eclipse.graphiti.features.custom.AbstractCustomFeature;
  44.  
  45. /**
  46.  * <p>
  47.  * This class should be used if for some reason a CustomFeature can be long to
  48.  * execute. It runs {@link #execute(ICustomContext, IProgressMonitor)} in a
  49.  * Command on top of current TransactionalEditingDomain. This command itself is
  50.  * run in a Job, and can use the associated IProgressMonitor
  51.  * </p>
  52.  * <p>
  53.  * This is useful to indicate user the job is running, but eclipse is not
  54.  * crashing
  55.  * </p>
  56.  *
  57.  * @author Antoine Lorence
  58.  *
  59.  */
  60. public abstract class AbstractTimeConsumingCustomFeature extends
  61.         AbstractCustomFeature {
  62.  
  63.     public AbstractTimeConsumingCustomFeature(IFeatureProvider fp) {
  64.         super(fp);
  65.     }
  66.  
  67.     /**
  68.      * Concrete code to execute. Sub-classes should use correctly given monitor:
  69.      * create tasks (and eventually sub-tasks), notify for worked and done tasks
  70.      * and check if user cancelled the task.
  71.      *
  72.      * @param context
  73.      *            The CustomFeature context
  74.      * @param monitor
  75.      *            The monitor used to manage progress bar and Job cancellation
  76.      * @return The execution status
  77.      */
  78.     protected abstract void execute(ICustomContext context,
  79.             IProgressMonitor monitor);
  80.  
  81.     /**
  82.      * Callback executed just before job scheduling, in the Feature execution
  83.      * Thread. Default implementation is empty.
  84.      */
  85.     protected void beforeJobExecution() {
  86.     }
  87.  
  88.     /**
  89.      * Callback launched immediately after job execution in the Job Thread.
  90.      * Default implementation is empty.
  91.      */
  92.     protected void afterJobExecution() {
  93.     }
  94.  
  95.     /**
  96.      * Initialize the Job.
  97.      *
  98.      * @param context
  99.      *            The CustomContext that will be given to
  100.      *            {@link #execute(ICustomContext, IProgressMonitor)}.
  101.      * @return The Job instance
  102.      */
  103.     protected Job initializeJob(final ICustomContext context) {
  104.         return new Job(getName()) {
  105.             @Override
  106.             protected IStatus run(final IProgressMonitor monitor) {
  107.  
  108.                 final TransactionalEditingDomain editDomain = TransactionUtil
  109.                         .getEditingDomain(getDiagram());
  110.  
  111.                 final RecordingCommand command = new RecordingCommand(
  112.                         editDomain, getName()) {
  113.  
  114.                     private IStatus result = null;
  115.  
  116.                     @Override
  117.                     protected void doExecute() {
  118.                         try {
  119.                             AbstractTimeConsumingCustomFeature.this.execute(
  120.                                     context, monitor);
  121.                             result = Status.OK_STATUS;
  122.                         } catch (OperationCanceledException e) {
  123.                             result = Status.CANCEL_STATUS;
  124.                         }
  125.                     }
  126.  
  127.                     @Override
  128.                     public Collection<?> getResult() {
  129.                         return result == null ? Collections.EMPTY_LIST
  130.                                 : Collections.singletonList(result);
  131.                     }
  132.                 };
  133.  
  134.                 // Execute (synchrnously) the defined command in a proper EMF
  135.                 // transaction
  136.                 editDomain.getCommandStack().execute(command);
  137.  
  138.                 // Update the diagram dirtiness state
  139.                 getDiagramBehavior().getDiagramContainer().updateDirtyState();
  140.  
  141.                 // Callback
  142.                 afterJobExecution();
  143.  
  144.                 return (IStatus) command.getResult().iterator().next();
  145.             }
  146.         };
  147.     }
  148.  
  149.     /**
  150.      * Initialize parameters of the given Job
  151.      *
  152.      * @param job
  153.      *            The Job instance to configure
  154.      */
  155.     protected void configureJob(Job job) {
  156.         job.setUser(true);
  157.         job.setPriority(Job.LONG);
  158.     }
  159.  
  160.     // Prevent sub-classes from overriding this method
  161.     @Override
  162.     final public void execute(IContext context) {
  163.         super.execute(context);
  164.     }
  165.  
  166.     // Prevent sub-classes from overriding this method
  167.     @Override
  168.     final public void execute(ICustomContext context) {
  169.  
  170.         final Job job = initializeJob(context);
  171.         configureJob(job);
  172.  
  173.         // Callback
  174.         beforeJobExecution();
  175.  
  176.         // Job is run
  177.         job.schedule();
  178.     }
  179.  
  180.     // Prevent sub-classes from overriding this method
  181.     @Override
  182.     final public boolean hasDoneChanges() {
  183.         return false;
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment