Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b0724a53db9f6d312c102ed0e0b940c58298638 (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
/*******************************************************************************
 * Copyright (c) 2003, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060504   119296 pmoogk@ca.ibm.com - Peter Moogk
 * 20060822   154750 pmoogk@ca.ibm.com - Peter Moogk
 * 20150113	  457332 jgwest@ca.ibm.com - Jonathan West, TimedWSDLSelectionConditionCommand/TimedOperation classes blocks automated tests with confirmation dialog box
 *******************************************************************************/
package org.eclipse.wst.command.internal.env.ui.common;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.commands.operations.IUndoContext;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.command.internal.env.ui.EnvironmentUIMessages;

public class TimedOperation implements IUndoableOperation
{
  private AbstractOperation operation;
  private int               timeout;
  private IProgressMonitor  tempMonitor;
  private IAdaptable        tempInfo;
  private IStatus           returnStatus;
  private boolean           operationComplete;
  private String            timeOutMessage;
  
  private boolean headless = false;
  
  public TimedOperation( AbstractOperation operation, int timeout, String timeOutMessage )
  {
    this.operation      = operation;
    this.timeout        = timeout;
    this.timeOutMessage = timeOutMessage;
  }

  public IStatus execute(IProgressMonitor monitor, IAdaptable info) 
  {
    Thread  executeThread = new Thread( new OperationRunnable() );
    
    returnStatus      = Status.OK_STATUS;
    tempMonitor       = monitor;
    tempInfo          = info;
    operationComplete = false;
    executeThread.start();
    
    synchronized( operation )
    {
      while( !operationComplete )
      {
        try
        {
          operation.wait(timeout);
        }
        catch( InterruptedException exc )
        {
          String message     = exc.getMessage();
          Status errorStatus = new Status( IStatus.ERROR,"id", 0, message == null ? "" : message, exc );
          executeThread.interrupt();
          return errorStatus;
        }
        
        if( !operationComplete )
        {
        	
          Status errorStatus = new Status( IStatus.ERROR,"id", 0, EnvironmentUIMessages.MSG_ERROR_OPERATION_TIMED_OUT, null);

          boolean waitMore;
          if(!headless) 
          {
	        // We timed out, since the execution thread hasn't set operationComplete to true.
	        Shell   shell       = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
	        waitMore    = MessageDialog.openConfirm(shell, EnvironmentUIMessages.MSG_ERROR_OPERATION_TIMED_OUT, timeOutMessage );
          } else 
          {
        	// If we are running headless, don't pop-up a message dialog; we should just automatically stop waiting after timeout. 
        	waitMore = false;
          }
          
          if( !waitMore )
          {
            executeThread.interrupt();
            operationComplete = true;
            return errorStatus;
          }
        }
      }
    }
    
    // We completed successfully.  Therefore return the status set in the forked
    // thread.
    return returnStatus;
  }

  private class OperationRunnable implements Runnable
  {
    public void run()
    {
      try
      {
        returnStatus = operation.execute(tempMonitor, tempInfo);
      }
      catch( Throwable exc )
      {
        String message = exc.getMessage();
        
        returnStatus = new Status( IStatus.ERROR,"id", 0, message == null ? "" : message, exc );
      }
      finally
      {
        synchronized( operation )
        {
          operationComplete = true;
          operation.notify();
        }        
      }
    }
  }
  
  public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException
  {
    return operation.redo(monitor, info);
  }

  public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException
  {
    return operation.undo(monitor, info);
  }
  public IUndoContext[] getContexts()
  {
    return operation.getContexts();
  }

  public boolean hasContext(IUndoContext context)
  {
    return operation.hasContext(context);
  }

  public void addContext(IUndoContext context)
  {
    operation.addContext(context);
  }

  public boolean canExecute()
  {
    return operation.canExecute();
  }

  public boolean canRedo()
  {
    return operation.canRedo();
  }

  public boolean canUndo()
  {
    return operation.canUndo();
  }

  public void dispose()
  {
    operation.dispose();
  }

  public String getLabel()
  {
    return operation.getLabel();
  }

  public void removeContext(IUndoContext context)
  {
    operation.removeContext(context);
  }

  public void setLabel(String name)
  {
    operation.setLabel(name);
  }
  
  public void setHeadless(boolean headless) 
  {
	this.headless = headless;
  }
}

Back to the top