|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2004 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 |
package org.eclipse.compare; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.util.Arrays; |
| 15 |
|
| 16 |
import org.eclipse.swt.SWT; |
| 17 |
import org.eclipse.swt.events.SelectionAdapter; |
| 18 |
import org.eclipse.swt.events.SelectionEvent; |
| 19 |
import org.eclipse.swt.graphics.Image; |
| 20 |
import org.eclipse.swt.widgets.Button; |
| 21 |
import org.eclipse.swt.widgets.Composite; |
| 22 |
import org.eclipse.swt.widgets.Control; |
| 23 |
import org.eclipse.swt.widgets.Shell; |
| 24 |
|
| 25 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 26 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 27 |
import org.eclipse.jface.util.Assert; |
| 28 |
import org.eclipse.jface.viewers.*; |
| 29 |
import org.eclipse.jface.window.Window; |
| 30 |
import org.eclipse.jface.wizard.IWizard; |
| 31 |
import org.eclipse.jface.wizard.WizardDialog; |
| 32 |
|
| 33 |
import org.eclipse.core.resources.*; |
| 34 |
import org.eclipse.core.runtime.*; |
| 35 |
|
| 36 |
import org.eclipse.ui.IEditorPart; |
| 37 |
|
| 38 |
import org.eclipse.compare.internal.*; |
| 39 |
import org.eclipse.compare.internal.patch.PatchMessages; |
| 40 |
import org.eclipse.compare.internal.patch.PatchWizard; |
| 41 |
|
| 42 |
|
| 43 |
public class CompareWithPatchAction extends BaseCompareAction { |
| 44 |
|
| 45 |
private PatchDialogSettings dialogSettings; //$NON-NLS-1$ |
| 46 |
|
| 47 |
static class PatchWizardDialog extends WizardDialog { |
| 48 |
|
| 49 |
PatchWizardDialog(Shell parent, IWizard wizard) { |
| 50 |
super(parent, wizard); |
| 51 |
|
| 52 |
setShellStyle(getShellStyle() | SWT.RESIZE); |
| 53 |
setMinimumPageSize(700, 500); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
protected boolean isEnabled(ISelection selection) { |
| 58 |
return Utilities.getResources(selection).length == 1; |
| 59 |
} |
| 60 |
|
| 61 |
/* (non-Javadoc) |
| 62 |
* @see org.eclipse.compare.internal.BaseCompareAction#run(org.eclipse.jface.viewers.ISelection) |
| 63 |
*/ |
| 64 |
protected void run(ISelection selection) { |
| 65 |
PatchWizard wizard= new PatchWizard(selection); |
| 66 |
wizard.setDialogSettings(dialogSettings); |
| 67 |
if (areAllEditorsSaved()) { |
| 68 |
PatchWizardDialog dialog= new PatchWizardDialog(CompareUIPlugin.getShell(), wizard); |
| 69 |
dialog.open(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
private boolean areAllEditorsSaved(){ |
| 74 |
if (CompareUIPlugin.getDirtyEditors().length == 0) |
| 75 |
return true; |
| 76 |
if (! saveAllDirtyEditors()) |
| 77 |
return false; |
| 78 |
Shell shell= CompareUIPlugin.getShell(); |
| 79 |
try { |
| 80 |
// Save isn't cancelable. |
| 81 |
IWorkspace workspace= ResourcesPlugin.getWorkspace(); |
| 82 |
IWorkspaceDescription description= workspace.getDescription(); |
| 83 |
boolean autoBuild= description.isAutoBuilding(); |
| 84 |
description.setAutoBuilding(false); |
| 85 |
workspace.setDescription(description); |
| 86 |
try { |
| 87 |
new ProgressMonitorDialog(shell).run(false, false, createRunnable()); |
| 88 |
} finally { |
| 89 |
description.setAutoBuilding(autoBuild); |
| 90 |
workspace.setDescription(description); |
| 91 |
} |
| 92 |
return true; |
| 93 |
} catch (InvocationTargetException e) { |
| 94 |
ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception); |
| 95 |
return false; |
| 96 |
} catch (CoreException e) { |
| 97 |
ExceptionHandler.handle(e, shell, PatchMessages.PatchAction_ExceptionTitle, PatchMessages.PatchAction_Exception); |
| 98 |
return false; |
| 99 |
} catch (InterruptedException e) { |
| 100 |
Assert.isTrue(false); // Can't happen. Operation isn't cancelable. |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private IRunnableWithProgress createRunnable() { |
| 106 |
return new IRunnableWithProgress() { |
| 107 |
public void run(IProgressMonitor pm) { |
| 108 |
IEditorPart[] editorsToSave= CompareUIPlugin.getDirtyEditors(); |
| 109 |
pm.beginTask(PatchMessages.PatchAction_SavingDirtyEditorsTask, editorsToSave.length); |
| 110 |
for (int i= 0; i < editorsToSave.length; i++) { |
| 111 |
editorsToSave[i].doSave(new SubProgressMonitor(pm, 1)); |
| 112 |
pm.worked(1); |
| 113 |
} |
| 114 |
pm.done(); |
| 115 |
} |
| 116 |
}; |
| 117 |
} |
| 118 |
|
| 119 |
private boolean saveAllDirtyEditors() { |
| 120 |
if (ComparePreferencePage.getSaveAllEditors()) //must save everything |
| 121 |
return true; |
| 122 |
ListDialog dialog= new ListDialog(CompareUIPlugin.getShell()) { |
| 123 |
protected Control createDialogArea(Composite parent) { |
| 124 |
Composite result= (Composite) super.createDialogArea(parent); |
| 125 |
final Button check= new Button(result, SWT.CHECK); |
| 126 |
check.setText(PatchMessages.PatchAction_AlwaysSaveQuestion); |
| 127 |
check.setSelection(ComparePreferencePage.getSaveAllEditors()); |
| 128 |
check.addSelectionListener( |
| 129 |
new SelectionAdapter() { |
| 130 |
public void widgetSelected(SelectionEvent e) { |
| 131 |
ComparePreferencePage.setSaveAllEditors(check.getSelection()); |
| 132 |
} |
| 133 |
} |
| 134 |
); |
| 135 |
applyDialogFont(result); |
| 136 |
return result; |
| 137 |
} |
| 138 |
}; |
| 139 |
dialog.setTitle(PatchMessages.PatchAction_SaveAllQuestion); |
| 140 |
dialog.setAddCancelButton(true); |
| 141 |
dialog.setLabelProvider(createDialogLabelProvider()); |
| 142 |
dialog.setMessage(PatchMessages.PatchAction_SaveAllDescription); |
| 143 |
dialog.setContentProvider(new ListContentProvider()); |
| 144 |
dialog.setInput(Arrays.asList(CompareUIPlugin.getDirtyEditors())); |
| 145 |
return dialog.open() == Window.OK; |
| 146 |
} |
| 147 |
|
| 148 |
private ILabelProvider createDialogLabelProvider() { |
| 149 |
return new LabelProvider() { |
| 150 |
public Image getImage(Object element) { |
| 151 |
return ((IEditorPart) element).getTitleImage(); |
| 152 |
} |
| 153 |
public String getText(Object element) { |
| 154 |
return ((IEditorPart) element).getTitle(); |
| 155 |
} |
| 156 |
}; |
| 157 |
} |
| 158 |
|
| 159 |
public void setDialogSettings(PatchDialogSettings config) { |
| 160 |
this.dialogSettings = config; |
| 161 |
} |
| 162 |
} |