Skip to main content
summaryrefslogtreecommitdiffstats
blob: 243175fbf910a780df70ce8509176cb352605c05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060223   129232 pmoogk@ca.ibm.com - Peter Moogk
 *******************************************************************************/
package org.eclipse.wst.command.internal.env.ui.widgets;

import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.wst.command.internal.env.core.data.DataFlowManager;
import org.eclipse.wst.command.internal.env.core.fragment.CommandFragment;
import org.eclipse.wst.command.internal.env.core.fragment.CommandFragmentEngine;
import org.eclipse.wst.command.internal.env.core.fragment.FragmentListener;
import org.eclipse.wst.command.internal.env.eclipse.EclipseEnvironment;
import org.eclipse.wst.command.internal.env.ui.eclipse.EclipseStatusHandler;
import org.eclipse.wst.common.environment.ILog;


/**
 * This class manages the execution of Commands in a flow of CommandFragments.
 * The bulk of this work is done by the CommandFragmentEngine.  The main job of
 * this class is run the fragments in a particular transactional environment.
 * A fragment has a method called doNotRunInTransaction which is used to determine
 * if its commands should be run in a transaction or not.  If this method returns
 * true(ie. not in a transaction) the Commands are wrapped in a IRunnableWithProgress.
 * If false( ie. in a transaction ) the Commands are wrapped in a WorkspaceModifyOperation. 
 *
 */
public class SimpleCommandEngineManager
{
  protected CommandFragmentEngine engine_;
  protected DataFlowManager       dataManager_;
  protected   EclipseEnvironment    environment_;
  
  private   boolean               foundStop_;
  private   boolean               doNotRunInTransaction_;
  
  public SimpleCommandEngineManager( EclipseEnvironment environment, DataFlowManager dataManager )
  {
    environment_ = environment;
    dataManager_ = dataManager;
  }
  
  /**
   * Sets the root fragment.  The execution engine will start executing commands
   * at the beginning of this root fragment.
   * 
   * @param root the root fragment.
   */
  public void setRootFragment( CommandFragment root )
  {
  	engine_ = new CommandFragmentEngine( root, dataManager_, environment_ );
	
	  environment_.setCommandManager( engine_ );
  	
    engine_.setPeekFragmentListener( 
      new FragmentListener()
      {
        public  boolean notify( CommandFragment fragment )
        {
          return peekFragment( fragment );
        }
      } );  
                  
    engine_.setNextFragmentListener( 
      new FragmentListener()
      {
        public  boolean notify( CommandFragment fragment )
        {
          return nextFragment( fragment );
        }
      } );  
    
    engine_.setAfterExecuteFragmentListener( 
        new FragmentListener()
        {
          public  boolean notify( CommandFragment fragment )
          {
            return afterExecuteNextFragment( fragment );
          }
        } );  
                                                             
	  engine_.setUndoFragmentListener(
      new FragmentListener()
      {
        public boolean notify( CommandFragment fragment )
        {
          return undoFragment( fragment );
        }
      } );               
  }

  protected boolean afterExecuteNextFragment( CommandFragment fragment )
  {
    boolean              continueExecute = true;
    EclipseStatusHandler statusHandler   = (EclipseStatusHandler)environment_.getStatusHandler();
    IStatus              commandStatus   = engine_.getLastStatus();
    IStatus              handlerStatus   = statusHandler.getStatus();
    
    if( commandStatus.getSeverity() == IStatus.ERROR &&
        handlerStatus.getSeverity() != IStatus.ERROR )
    {
      // The command returned an error status for the engine to stop,
      // but the status handler did not have report and error.
      // Therefore, we will report this status returned by the command
      // if there is a message to display.  
      String errorMessage = commandStatus.getMessage();
      
      if( errorMessage != null && errorMessage.length() > 0 )
      {
        statusHandler.reportError( commandStatus );
      }
    }
    else if( commandStatus.getSeverity() != IStatus.ERROR &&
             handlerStatus.getSeverity() == IStatus.ERROR )
    {
      // The last command didn't return an error, but the status handler
      // did report and error.  Therefore, we should stop.
      continueExecute = false;   
    }
    
    return continueExecute;
  }
  
  /**
   * The CommandFragmentEngine calls this method when it is peeking forward
   * in the fragments.  When peeking forward the command stack state in the
   * engine is not changes.
   * 
   * 
   * @param fragment the fragment that it is peeking at.
   * @return Indicates whether peeking should stop or not.
   */
  protected boolean peekFragment( CommandFragment fragment )
  {     
    return true;
  }
  
  /**
   * The CommandFragmentEngine calls this method when it is moving forward
   * in the fragments.   When moving forward the command stack state is saved
   * at each fragment is traversed.
   * 
   * @param fragment the fragment that is being traversed.
   * @return indicates if the forward traversal should continue.  
   */
  protected boolean nextFragment( CommandFragment fragment )
  {     
    if( fragment.doNotRunInTransaction() != doNotRunInTransaction_ )
    {
      // The fragment is requesting a different transaction environment than
      // the one that we are in so we must stop and change environments.
      doNotRunInTransaction_ = fragment.doNotRunInTransaction();
      foundStop_             = false;      
    }
    
    return foundStop_;
  }
  
  /**
   * This method is called for each fragment when the command engine is unwinding
   * its stack during an undo operation.
   * 
   * @param fragment  the fragment being undone.
   * @return returns true if the undo process should continue.
   */
  protected boolean undoFragment( CommandFragment fragment )
  {  	  	
    return true;
  }
    
  /**
   * The method executes the CommandFragment commands in the context provided.
   * 
   * @param context the context
   * @return returns the status of executing the commands.
   */
  public IStatus runForwardToNextStop( IRunnableContext context )
  {
    IRunnableWithProgress operation = null;
    
  	doNotRunInTransaction_ = false;
  	
  	do
  	{
  	  // We will stop unless we are changing transaction modes.
      foundStop_ = true;
      
  	  if( doNotRunInTransaction_ )
  	  {
  	    operation = getNoTransactionOperation();
  	  }
  	  else
  	  {
  	    operation = getTransactionOperation();
  	  }
  	  
   	  try
  	  {  	   
  	    if( context == null )
  	    {
  	   	  // We don't have a container yet so just run the operation.
  	   	  operation.run( null );
  	    }
  	    else
  	    {
  	   	  // We have a container where this operation can run and have
  	   	  // its progress displayed.
  	      context.run( false, false, operation );
  	    }
  	  }
  	  catch( InterruptedException exc )
  	  {
  	    //TODO should log these exceptions.
  	    exc.printStackTrace();
  	  }
  	  catch( InvocationTargetException exc )
  	  {
  	    //TODO should log these exceptions.
  	    exc.printStackTrace();
  	  }
  	}  
  	while( !foundStop_ ); 
  	
  	return engine_.getLastStatus();
  }
  
  private IRunnableWithProgress getTransactionOperation()
  {
    IRunnableWithProgress operation = new IRunnableWithProgress()
    {
      public void run( IProgressMonitor monitor )
      {
        environment_.getLog().log(ILog.INFO, "command", 5002, this, "getTransactionOperation", "Start of transaction");
        
        EclipseStatusHandler statusHandler = (EclipseStatusHandler)environment_.getStatusHandler();
        
        statusHandler.resetStatus();
        engine_.moveForwardToNextStop( monitor );
        
        
        environment_.getLog().log(ILog.INFO, "command", 5003, this, "getTransactionOperation", "End of transaction");
      }
    };
    
    return operation;
  }
  
  private IRunnableWithProgress getNoTransactionOperation()
  {
    IRunnableWithProgress operation = new IRunnableWithProgress()
    {
      public void run( IProgressMonitor monitor )
      {
        environment_.getLog().log(ILog.INFO, "command", 5085, this, "getNoTransactionOperation", "Start of NON transaction");
        
        EclipseStatusHandler statusHandler = (EclipseStatusHandler)environment_.getStatusHandler();
        
        statusHandler.resetStatus();
        engine_.moveForwardToNextStop( monitor );
        environment_.getLog().log(ILog.INFO, "command", 5086, this, "getNoTransactionOperation", "End of NON transaction");
      }
    }; 
    
    return operation;
  }
}

Back to the top