|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.tests.manual; |
| 13 |
|
| 14 |
import java.beans.PropertyChangeListener; |
| 15 |
import java.beans.PropertyChangeSupport; |
| 16 |
|
| 17 |
import org.eclipse.core.databinding.DataBindingContext; |
| 18 |
import org.eclipse.core.databinding.beans.BeansObservables; |
| 19 |
import org.eclipse.core.databinding.observable.Realm; |
| 20 |
import org.eclipse.core.databinding.observable.value.IObservableValue; |
| 21 |
import org.eclipse.core.runtime.Assert; |
| 22 |
import org.eclipse.core.runtime.CoreException; |
| 23 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 24 |
import org.eclipse.core.runtime.IStatus; |
| 25 |
import org.eclipse.core.runtime.Status; |
| 26 |
import org.eclipse.core.runtime.SubMonitor; |
| 27 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
| 28 |
import org.eclipse.jface.dialogs.MessageDialog; |
| 29 |
import org.eclipse.jface.layout.GridDataFactory; |
| 30 |
import org.eclipse.jface.layout.GridLayoutFactory; |
| 31 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 32 |
import org.eclipse.jface.window.IShellProvider; |
| 33 |
import org.eclipse.swt.SWT; |
| 34 |
import org.eclipse.swt.events.DisposeEvent; |
| 35 |
import org.eclipse.swt.events.DisposeListener; |
| 36 |
import org.eclipse.swt.widgets.Button; |
| 37 |
import org.eclipse.swt.widgets.Composite; |
| 38 |
import org.eclipse.swt.widgets.Group; |
| 39 |
import org.eclipse.swt.widgets.Label; |
| 40 |
import org.eclipse.swt.widgets.Text; |
| 41 |
import org.eclipse.ui.IEditorInput; |
| 42 |
import org.eclipse.ui.IEditorSite; |
| 43 |
import org.eclipse.ui.IFileEditorInput; |
| 44 |
import org.eclipse.ui.ISaveablePart; |
| 45 |
import org.eclipse.ui.ISaveablesSource; |
| 46 |
import org.eclipse.ui.PartInitException; |
| 47 |
import org.eclipse.ui.PlatformUI; |
| 48 |
import org.eclipse.ui.Saveable; |
| 49 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 50 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
| 51 |
import org.eclipse.ui.internal.progress.WorkbenchSiteProgressService; |
| 52 |
import org.eclipse.ui.part.EditorPart; |
| 53 |
import org.eclipse.ui.progress.IJobRunnable; |
| 54 |
import org.eclipse.ui.progress.IWorkbenchSiteProgressService; |
| 55 |
|
| 56 |
/** |
| 57 |
* @since 3.3 |
| 58 |
* |
| 59 |
*/ |
| 60 |
public class TestBackgroundSaveEditor extends EditorPart implements |
| 61 |
ISaveablesSource { |
| 62 |
|
| 63 |
public class MySaveable extends Saveable { |
| 64 |
|
| 65 |
PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); |
| 66 |
|
| 67 |
private boolean dirty; |
| 68 |
|
| 69 |
public boolean supportsBackgroundSave() { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
public void doSave(IProgressMonitor monitor) throws CoreException { |
| 74 |
SubMonitor subMonitor = SubMonitor.convert(monitor, 2); |
| 75 |
IJobRunnable runnable = doSave(subMonitor.newChild(1), getSite()); |
| 76 |
if (runnable!=null) { |
| 77 |
runnable.run(subMonitor.newChild(1)); |
| 78 |
} |
| 79 |
monitor.done(); |
| 80 |
} |
| 81 |
|
| 82 |
public IJobRunnable doSave(IProgressMonitor monitor, |
| 83 |
IShellProvider shellProvider) throws CoreException { |
| 84 |
monitor.beginTask("Saving in the foreground", |
| 85 |
data.foregroundSaveTime); |
| 86 |
data.setOutput(""); |
| 87 |
for (int i = 0; i < data.foregroundSaveTime; i++) { |
| 88 |
try { |
| 89 |
Thread.sleep(1000); |
| 90 |
} catch (InterruptedException e) { |
| 91 |
Thread.currentThread().interrupt(); |
| 92 |
} |
| 93 |
data.setOutput(data.getInput().substring(0, |
| 94 |
Math.min(i, data.getInput().length()))); |
| 95 |
monitor.worked(1); |
| 96 |
} |
| 97 |
if (data.throwExceptionInForeground) { |
| 98 |
throw new CoreException(new Status(IStatus.ERROR, |
| 99 |
"org.eclipse.ui.tests", |
| 100 |
"Saving in the foreground failed")); |
| 101 |
} |
| 102 |
monitor.done(); |
| 103 |
if (!data.saveInBackground) { |
| 104 |
data.setOutput(data.getInput()); |
| 105 |
setDirty(false); |
| 106 |
return null; |
| 107 |
} |
| 108 |
WorkbenchSiteProgressService progressService = (WorkbenchSiteProgressService) getSite() |
| 109 |
.getAdapter(IWorkbenchSiteProgressService.class); |
| 110 |
IJobRunnable result = new IJobRunnable() { |
| 111 |
public IStatus run(IProgressMonitor monitor) { |
| 112 |
monitor.beginTask("Saving in the background", |
| 113 |
data.backgroundSaveTime); |
| 114 |
for (int i = 0; i < data.backgroundSaveTime; i++) { |
| 115 |
try { |
| 116 |
Thread.sleep(1000); |
| 117 |
} catch (InterruptedException e) { |
| 118 |
Thread.currentThread().interrupt(); |
| 119 |
} |
| 120 |
data.setOutput(data.getInput().substring( |
| 121 |
0, |
| 122 |
Math.min(i + data.foregroundSaveTime, data |
| 123 |
.getInput().length()))); |
| 124 |
monitor.worked(1); |
| 125 |
} |
| 126 |
if (data.throwExceptionInBackground) { |
| 127 |
return new Status(IStatus.ERROR, |
| 128 |
"org.eclipse.ui.tests", |
| 129 |
"Saving in the background failed"); |
| 130 |
} |
| 131 |
data.setOutput(data.getInput()); |
| 132 |
setDirty(false); |
| 133 |
monitor.done(); |
| 134 |
return Status.OK_STATUS; |
| 135 |
} |
| 136 |
}; |
| 137 |
return result; |
| 138 |
} |
| 139 |
|
| 140 |
public boolean equals(Object object) { |
| 141 |
return this == object; |
| 142 |
} |
| 143 |
|
| 144 |
public ImageDescriptor getImageDescriptor() { |
| 145 |
return input.getImageDescriptor(); |
| 146 |
} |
| 147 |
|
| 148 |
public String getName() { |
| 149 |
return input.getName(); |
| 150 |
} |
| 151 |
|
| 152 |
public String getToolTipText() { |
| 153 |
return input.getToolTipText(); |
| 154 |
} |
| 155 |
|
| 156 |
public int hashCode() { |
| 157 |
return System.identityHashCode(this); |
| 158 |
} |
| 159 |
|
| 160 |
public boolean isDirty() { |
| 161 |
return dirty; |
| 162 |
} |
| 163 |
|
| 164 |
public void setDirty(boolean dirty) { |
| 165 |
firePropertyChange("dirty", new Boolean(this.dirty), new Boolean( |
| 166 |
this.dirty = dirty)); |
| 167 |
getSite().getShell().getDisplay().syncExec(new Runnable(){ |
| 168 |
public void run() { |
| 169 |
TestBackgroundSaveEditor.this |
| 170 |
.firePropertyChange(ISaveablePart.PROP_DIRTY); |
| 171 |
}}); |
| 172 |
} |
| 173 |
|
| 174 |
public void addPropertyChangeListener(String propertyName, |
| 175 |
PropertyChangeListener listener) { |
| 176 |
changeSupport.addPropertyChangeListener(propertyName, listener); |
| 177 |
} |
| 178 |
|
| 179 |
void firePropertyChange(String propertyName, Object oldValue, |
| 180 |
Object newValue) { |
| 181 |
changeSupport.firePropertyChange(propertyName, oldValue, newValue); |
| 182 |
} |
| 183 |
|
| 184 |
public void removePropertyChangeListener(String propertyName, |
| 185 |
PropertyChangeListener listener) { |
| 186 |
changeSupport.removePropertyChangeListener(propertyName, listener); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
private MySaveable mySaveable; |
| 191 |
private Text inputText; |
| 192 |
private IEditorInput input; |
| 193 |
private Composite myComposite; |
| 194 |
|
| 195 |
public TestBackgroundSaveEditor() { |
| 196 |
mySaveable = new MySaveable(); |
| 197 |
} |
| 198 |
|
| 199 |
public void createPartControl(Composite parent) { |
| 200 |
myComposite = parent; |
| 201 |
Realm realm = SWTObservables.getRealm(parent.getDisplay()); |
| 202 |
final DataBindingContext dbc = new DataBindingContext(realm); |
| 203 |
parent.addDisposeListener(new DisposeListener() { |
| 204 |
public void widgetDisposed(DisposeEvent e) { |
| 205 |
dbc.dispose(); |
| 206 |
} |
| 207 |
}); |
| 208 |
|
| 209 |
final IObservableValue inputObservable = BeansObservables.observeValue( |
| 210 |
realm, data, "input"); |
| 211 |
final IObservableValue outputObservable = BeansObservables |
| 212 |
.observeValue(realm, data, "output"); |
| 213 |
|
| 214 |
createInputGroup(parent, dbc, inputObservable); |
| 215 |
createOptionsGroup(parent, realm, dbc, inputObservable, |
| 216 |
outputObservable); |
| 217 |
createOutputGroup(parent, dbc, outputObservable); |
| 218 |
|
| 219 |
GridLayoutFactory.swtDefaults().numColumns(3).equalWidth(true) |
| 220 |
.generateLayout(parent); |
| 221 |
} |
| 222 |
|
| 223 |
private void createOutputGroup(Composite parent, |
| 224 |
final DataBindingContext dbc, |
| 225 |
final IObservableValue outputObservable) { |
| 226 |
Group outputGroup = new Group(parent, SWT.NONE); |
| 227 |
outputGroup.setText("Output"); |
| 228 |
Text outputText = new Text(outputGroup, SWT.BORDER | SWT.READ_ONLY |
| 229 |
| SWT.MULTI); |
| 230 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(outputText); |
| 231 |
dbc.bindValue(SWTObservables.observeText(outputText, SWT.NONE), |
| 232 |
outputObservable, null); |
| 233 |
GridLayoutFactory.swtDefaults().generateLayout(outputGroup); |
| 234 |
} |
| 235 |
|
| 236 |
private void createOptionsGroup(Composite parent, Realm realm, |
| 237 |
final DataBindingContext dbc, |
| 238 |
final IObservableValue inputObservable, |
| 239 |
final IObservableValue outputObservable) { |
| 240 |
Group optionsGroup = new Group(parent, SWT.NONE); |
| 241 |
optionsGroup.setText("Options"); |
| 242 |
|
| 243 |
Button dirtyButton = new Button(optionsGroup, SWT.CHECK); |
| 244 |
new Label(optionsGroup, SWT.NONE).setText("Editor is dirty"); |
| 245 |
IObservableValue dirtyObservable = BeansObservables.observeValue(realm, |
| 246 |
mySaveable, "dirty"); |
| 247 |
dbc.bindValue(SWTObservables.observeSelection(dirtyButton), |
| 248 |
dirtyObservable, null); |
| 249 |
// IObservableValue inputAndOutputDiffer = new ComputedValue(realm) { |
| 250 |
// protected Object calculate() { |
| 251 |
// return new Boolean(!Util.equals(inputObservable.getValue(), |
| 252 |
// outputObservable.getValue())); |
| 253 |
// } |
| 254 |
// }; |
| 255 |
// dbc.bindValue(dirtyObservable, inputAndOutputDiffer, null); |
| 256 |
|
| 257 |
Button saveInBackgroundButton = new Button(optionsGroup, SWT.CHECK); |
| 258 |
new Label(optionsGroup, SWT.NONE) |
| 259 |
.setText("Do part of the save in the background"); |
| 260 |
dbc.bindValue(SWTObservables.observeSelection(saveInBackgroundButton), |
| 261 |
BeansObservables.observeValue(realm, data, "saveInBackground"), |
| 262 |
null); |
| 263 |
|
| 264 |
Button foregroundExceptionButton = new Button(optionsGroup, SWT.CHECK); |
| 265 |
new Label(optionsGroup, SWT.NONE) |
| 266 |
.setText("Throw exception while saving in the foreground"); |
| 267 |
dbc.bindValue(SWTObservables |
| 268 |
.observeSelection(foregroundExceptionButton), BeansObservables |
| 269 |
.observeValue(realm, data, "throwExceptionInForeground"), null); |
| 270 |
|
| 271 |
Button backgroundExceptionButton = new Button(optionsGroup, SWT.CHECK); |
| 272 |
new Label(optionsGroup, SWT.NONE) |
| 273 |
.setText("Throw exception while saving in the background"); |
| 274 |
dbc.bindValue(SWTObservables |
| 275 |
.observeSelection(backgroundExceptionButton), BeansObservables |
| 276 |
.observeValue(realm, data, "throwExceptionInBackground"), null); |
| 277 |
|
| 278 |
new Label(optionsGroup, SWT.NONE).setText("Foreground save time:"); |
| 279 |
Text optionsForegroundTime = new Text(optionsGroup, SWT.BORDER); |
| 280 |
dbc.bindValue(SWTObservables.observeText(optionsForegroundTime, |
| 281 |
SWT.Modify), BeansObservables.observeValue(realm, data, |
| 282 |
"foregroundSaveTime"), null); |
| 283 |
|
| 284 |
new Label(optionsGroup, SWT.NONE).setText("Background save time:"); |
| 285 |
Text optionsBackgroundTime = new Text(optionsGroup, SWT.BORDER); |
| 286 |
dbc.bindValue(SWTObservables.observeText(optionsBackgroundTime, |
| 287 |
SWT.Modify), BeansObservables.observeValue(realm, data, |
| 288 |
"backgroundSaveTime"), null); |
| 289 |
|
| 290 |
GridLayoutFactory.swtDefaults().numColumns(2).generateLayout( |
| 291 |
optionsGroup); |
| 292 |
} |
| 293 |
|
| 294 |
private void createInputGroup(Composite parent, |
| 295 |
final DataBindingContext dbc, final IObservableValue inputObservable) { |
| 296 |
Group inputGroup = new Group(parent, SWT.NONE); |
| 297 |
inputGroup.setText("Input"); |
| 298 |
|
| 299 |
inputText = new Text(inputGroup, SWT.BORDER | SWT.MULTI); |
| 300 |
dbc.bindValue(SWTObservables.observeText(inputText, SWT.Modify), |
| 301 |
inputObservable, null); |
| 302 |
|
| 303 |
GridLayoutFactory.swtDefaults().generateLayout(inputGroup); |
| 304 |
} |
| 305 |
|
| 306 |
public void doSave(IProgressMonitor monitor) { |
| 307 |
try { |
| 308 |
mySaveable.doSave(monitor); |
| 309 |
} catch (CoreException e) { |
| 310 |
String title = "Save failed"; |
| 311 |
WorkbenchPlugin.log(title, new Status(IStatus.WARNING, |
| 312 |
PlatformUI.PLUGIN_ID, 0, title, e)); |
| 313 |
MessageDialog.openError(getSite().getShell(), |
| 314 |
WorkbenchMessages.Error, title + ':' + e.getMessage()); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
public void doSaveAs() { |
| 319 |
Assert.isTrue(false, "Should not be called"); |
| 320 |
} |
| 321 |
|
| 322 |
public void init(IEditorSite site, IEditorInput input) |
| 323 |
throws PartInitException { |
| 324 |
if (!(input instanceof IFileEditorInput)) |
| 325 |
throw new PartInitException( |
| 326 |
"Invalid Input: Must be IFileEditorInput"); |
| 327 |
setSite(site); |
| 328 |
setInput(input); |
| 329 |
this.input = input; |
| 330 |
} |
| 331 |
|
| 332 |
public boolean isDirty() { |
| 333 |
return mySaveable.isDirty(); |
| 334 |
} |
| 335 |
|
| 336 |
public boolean isSaveAsAllowed() { |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
public void setFocus() { |
| 341 |
inputText.setFocus(); |
| 342 |
} |
| 343 |
|
| 344 |
public static class Data { |
| 345 |
PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); |
| 346 |
public String input; |
| 347 |
public String output; |
| 348 |
public String buffer; |
| 349 |
public boolean saveInBackground; |
| 350 |
public boolean throwExceptionInForeground; |
| 351 |
public boolean throwExceptionInBackground; |
| 352 |
public int foregroundSaveTime; |
| 353 |
public int backgroundSaveTime; |
| 354 |
|
| 355 |
public String getOutput() { |
| 356 |
return output; |
| 357 |
} |
| 358 |
|
| 359 |
public void setOutput(String output) { |
| 360 |
firePropertyChange("output", this.output, this.output = output); |
| 361 |
} |
| 362 |
|
| 363 |
public void addPropertyChangeListener(String propertyName, |
| 364 |
PropertyChangeListener listener) { |
| 365 |
changeSupport.addPropertyChangeListener(propertyName, listener); |
| 366 |
} |
| 367 |
|
| 368 |
void firePropertyChange(String propertyName, Object oldValue, |
| 369 |
Object newValue) { |
| 370 |
changeSupport.firePropertyChange(propertyName, oldValue, newValue); |
| 371 |
} |
| 372 |
|
| 373 |
public void removePropertyChangeListener(String propertyName, |
| 374 |
PropertyChangeListener listener) { |
| 375 |
changeSupport.removePropertyChangeListener(propertyName, listener); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* @return Returns the input. |
| 380 |
*/ |
| 381 |
public String getInput() { |
| 382 |
return input; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* @param input |
| 387 |
* The input to set. |
| 388 |
*/ |
| 389 |
public void setInput(String input) { |
| 390 |
this.input = input; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* @return Returns the buffer. |
| 395 |
*/ |
| 396 |
public String getBuffer() { |
| 397 |
return buffer; |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* @param buffer |
| 402 |
* The buffer to set. |
| 403 |
*/ |
| 404 |
public void setBuffer(String buffer) { |
| 405 |
this.buffer = buffer; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* @return Returns the saveInBackground. |
| 410 |
*/ |
| 411 |
public boolean isSaveInBackground() { |
| 412 |
return saveInBackground; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* @param saveInBackground |
| 417 |
* The saveInBackground to set. |
| 418 |
*/ |
| 419 |
public void setSaveInBackground(boolean saveInBackground) { |
| 420 |
this.saveInBackground = saveInBackground; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* @return Returns the throwExceptionInForeground. |
| 425 |
*/ |
| 426 |
public boolean isThrowExceptionInForeground() { |
| 427 |
return throwExceptionInForeground; |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* @param throwExceptionInForeground |
| 432 |
* The throwExceptionInForeground to set. |
| 433 |
*/ |
| 434 |
public void setThrowExceptionInForeground( |
| 435 |
boolean throwExceptionInForeground) { |
| 436 |
this.throwExceptionInForeground = throwExceptionInForeground; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* @return Returns the throwExceptionInBackground. |
| 441 |
*/ |
| 442 |
public boolean isThrowExceptionInBackground() { |
| 443 |
return throwExceptionInBackground; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* @param throwExceptionInBackground |
| 448 |
* The throwExceptionInBackground to set. |
| 449 |
*/ |
| 450 |
public void setThrowExceptionInBackground( |
| 451 |
boolean throwExceptionInBackground) { |
| 452 |
this.throwExceptionInBackground = throwExceptionInBackground; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* @return Returns the foregroundSaveTime. |
| 457 |
*/ |
| 458 |
public int getForegroundSaveTime() { |
| 459 |
return foregroundSaveTime; |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* @param foregroundSaveTime |
| 464 |
* The foregroundSaveTime to set. |
| 465 |
*/ |
| 466 |
public void setForegroundSaveTime(int foregroundSaveTime) { |
| 467 |
this.foregroundSaveTime = foregroundSaveTime; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* @return Returns the backgroundSaveTime. |
| 472 |
*/ |
| 473 |
public int getBackgroundSaveTime() { |
| 474 |
return backgroundSaveTime; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* @param backgroundSaveTime |
| 479 |
* The backgroundSaveTime to set. |
| 480 |
*/ |
| 481 |
public void setBackgroundSaveTime(int backgroundSaveTime) { |
| 482 |
this.backgroundSaveTime = backgroundSaveTime; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
private Data data = new Data(); |
| 487 |
|
| 488 |
public Saveable[] getActiveSaveables() { |
| 489 |
return new Saveable[] { mySaveable }; |
| 490 |
} |
| 491 |
|
| 492 |
public Saveable[] getSaveables() { |
| 493 |
return new Saveable[] { mySaveable }; |
| 494 |
} |
| 495 |
|
| 496 |
} |