|
Lines 1-5
Link Here
|
| 1 |
/********************************************************************** |
1 |
/********************************************************************** |
| 2 |
* Copyright (c) 2005, 2007 IBM Corporation and others. |
2 |
* Copyright (c) 2005, 2008 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 14-43
Link Here
|
| 14 |
import java.lang.reflect.InvocationTargetException; |
14 |
import java.lang.reflect.InvocationTargetException; |
| 15 |
|
15 |
|
| 16 |
import org.eclipse.core.resources.IContainer; |
16 |
import org.eclipse.core.resources.IContainer; |
|
|
17 |
import org.eclipse.core.resources.IFile; |
| 17 |
import org.eclipse.core.resources.IResource; |
18 |
import org.eclipse.core.resources.IResource; |
|
|
19 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 18 |
import org.eclipse.core.runtime.CoreException; |
20 |
import org.eclipse.core.runtime.CoreException; |
| 19 |
import org.eclipse.core.runtime.IPath; |
21 |
import org.eclipse.core.runtime.IPath; |
| 20 |
import org.eclipse.core.runtime.IProgressMonitor; |
22 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
|
23 |
import org.eclipse.core.runtime.Path; |
| 21 |
import org.eclipse.core.runtime.SubProgressMonitor; |
24 |
import org.eclipse.core.runtime.SubProgressMonitor; |
|
|
25 |
import org.eclipse.hyades.test.ui.UiPlugin; |
| 22 |
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigatorMessages; |
26 |
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigatorMessages; |
|
|
27 |
import org.eclipse.hyades.test.ui.internal.navigator.refactoring.RenameRefactoring; |
| 23 |
import org.eclipse.hyades.test.ui.navigator.IProxyNode; |
28 |
import org.eclipse.hyades.test.ui.navigator.IProxyNode; |
|
|
29 |
import org.eclipse.hyades.test.ui.navigator.IReferencerProxyNode; |
| 24 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer; |
30 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer; |
|
|
31 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer2; |
| 25 |
import org.eclipse.hyades.test.ui.navigator.actions.RenamerUIStatus; |
32 |
import org.eclipse.hyades.test.ui.navigator.actions.RenamerUIStatus; |
| 26 |
import org.eclipse.hyades.ui.internal.navigator.TreeNavigator; |
33 |
import org.eclipse.hyades.ui.internal.navigator.TreeNavigator; |
| 27 |
import org.eclipse.hyades.ui.util.IDisposable; |
34 |
import org.eclipse.hyades.ui.util.IDisposable; |
| 28 |
import org.eclipse.jface.action.Action; |
35 |
import org.eclipse.jface.action.Action; |
|
|
36 |
import org.eclipse.jface.dialogs.Dialog; |
| 29 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
37 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
38 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 31 |
import org.eclipse.jface.viewers.StructuredSelection; |
39 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
|
40 |
import org.eclipse.ltk.ui.refactoring.RefactoringWizard; |
| 41 |
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; |
| 42 |
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; |
| 32 |
import org.eclipse.swt.SWT; |
43 |
import org.eclipse.swt.SWT; |
| 33 |
import org.eclipse.swt.custom.TreeEditor; |
44 |
import org.eclipse.swt.custom.TreeEditor; |
| 34 |
import org.eclipse.swt.events.FocusAdapter; |
45 |
import org.eclipse.swt.events.FocusAdapter; |
| 35 |
import org.eclipse.swt.events.FocusEvent; |
46 |
import org.eclipse.swt.events.FocusEvent; |
|
|
47 |
import org.eclipse.swt.events.ModifyEvent; |
| 48 |
import org.eclipse.swt.events.ModifyListener; |
| 36 |
import org.eclipse.swt.graphics.Point; |
49 |
import org.eclipse.swt.graphics.Point; |
|
|
50 |
import org.eclipse.swt.layout.GridData; |
| 51 |
import org.eclipse.swt.layout.GridLayout; |
| 37 |
import org.eclipse.swt.widgets.Composite; |
52 |
import org.eclipse.swt.widgets.Composite; |
| 38 |
import org.eclipse.swt.widgets.Display; |
53 |
import org.eclipse.swt.widgets.Display; |
| 39 |
import org.eclipse.swt.widgets.Event; |
54 |
import org.eclipse.swt.widgets.Event; |
|
|
55 |
import org.eclipse.swt.widgets.Label; |
| 40 |
import org.eclipse.swt.widgets.Listener; |
56 |
import org.eclipse.swt.widgets.Listener; |
|
|
57 |
import org.eclipse.swt.widgets.Shell; |
| 41 |
import org.eclipse.swt.widgets.Text; |
58 |
import org.eclipse.swt.widgets.Text; |
| 42 |
import org.eclipse.swt.widgets.Tree; |
59 |
import org.eclipse.swt.widgets.Tree; |
| 43 |
import org.eclipse.swt.widgets.TreeItem; |
60 |
import org.eclipse.swt.widgets.TreeItem; |
|
Lines 47-53
Link Here
|
| 47 |
/** |
64 |
/** |
| 48 |
* @author Jerome Gout |
65 |
* @author Jerome Gout |
| 49 |
* @author Paul E. Slauenwhite |
66 |
* @author Paul E. Slauenwhite |
| 50 |
* @version March, 2007 |
67 |
* @version March 12, 2008 |
| 51 |
* @since 3.3 |
68 |
* @since 3.3 |
| 52 |
*/ |
69 |
*/ |
| 53 |
public class RenameAction extends Action implements IDisposable { |
70 |
public class RenameAction extends Action implements IDisposable { |
|
Lines 60-65
Link Here
|
| 60 |
protected TreeNavigator navigator; |
77 |
protected TreeNavigator navigator; |
| 61 |
protected IContainer renamedResource; |
78 |
protected IContainer renamedResource; |
| 62 |
|
79 |
|
|
|
80 |
private class RenameRefactoringWizard extends RefactoringWizard { |
| 81 |
private RenameRefactoringNewNameInputPage page; |
| 82 |
|
| 83 |
private RenameRefactoringWizard(RenameRefactoring refactoring, String name) { |
| 84 |
super(refactoring, DIALOG_BASED_USER_INTERFACE/* | NONE*/); |
| 85 |
this.page = new RenameRefactoringNewNameInputPage(refactoring, name); |
| 86 |
} |
| 87 |
|
| 88 |
protected void addUserInputPages() { |
| 89 |
addPage(page); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
class RenameRefactoringNewNameInputPage extends UserInputWizardPage { |
| 94 |
private RenameRefactoring refactoring; |
| 95 |
private Text nameText; |
| 96 |
private String oldName; |
| 97 |
private String newName; |
| 98 |
|
| 99 |
public RenameRefactoringNewNameInputPage(RenameRefactoring refactoring, String oldName) { |
| 100 |
super(""); //$NON-NLS-1$ |
| 101 |
this.oldName = oldName; |
| 102 |
this.refactoring = refactoring; |
| 103 |
} |
| 104 |
|
| 105 |
public void createControl(Composite parent) { |
| 106 |
Composite result= new Composite(parent, SWT.NONE); |
| 107 |
setControl(result); |
| 108 |
result.setLayout(new GridLayout(2, false)); |
| 109 |
|
| 110 |
Label label= new Label(result, SWT.NONE); |
| 111 |
label.setText(ActionMessages.RENAME_NEW_NAME_LABEL); |
| 112 |
nameText = new Text(result, SWT.SINGLE | SWT.BORDER); |
| 113 |
nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 114 |
nameText.setText(oldName); |
| 115 |
nameText.addModifyListener(new ModifyListener() { |
| 116 |
public void modifyText(ModifyEvent e) { |
| 117 |
newName = nameText.getText(); |
| 118 |
refactoring.setNewName(newName); |
| 119 |
setPageComplete(isPageComplete()); |
| 120 |
} |
| 121 |
}); |
| 122 |
nameText.selectAll(); |
| 123 |
Dialog.applyDialogFont(result); |
| 124 |
} |
| 125 |
|
| 126 |
public boolean isPageComplete() { |
| 127 |
// first check if name is not empty and have changed |
| 128 |
boolean basicCondition = newName != null && newName.length() > 0 && !newName.equals(oldName); |
| 129 |
if (!basicCondition) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
// then check if target is ok |
| 133 |
if (selection == null || selection.isEmpty()) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
Object objSelected = selection.getFirstElement(); |
| 137 |
if (objSelected instanceof IContainer) { |
| 138 |
// check if destination exists in case of a container |
| 139 |
IContainer destContainer = getIContainer((IContainer)objSelected, newName); |
| 140 |
return !(destContainer.exists()); |
| 141 |
} else if (objSelected instanceof IProxyNode) { |
| 142 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 143 |
IResource res = proxy.getUnderlyingResource(); |
| 144 |
if (res instanceof IFile) { |
| 145 |
IFile file = (IFile)res; |
| 146 |
String extension = file.getFileExtension(); |
| 147 |
String destName = newName; |
| 148 |
if (extension != null) { |
| 149 |
destName = newName+'.'+extension; |
| 150 |
} |
| 151 |
IFile destFile = ((IFile)res).getParent().getFile(new Path(destName)); |
| 152 |
return (!destFile.exists()); |
| 153 |
} |
| 154 |
} |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
private IContainer getIContainer(IContainer container,String newName) { |
| 160 |
if(container.getFullPath().segmentCount() > 1) { |
| 161 |
return container.getParent().getFolder(new Path(newName)); |
| 162 |
} else { |
| 163 |
return ResourcesPlugin.getWorkspace().getRoot().getProject(newName); |
| 164 |
} |
| 165 |
} |
| 63 |
public RenameAction(TreeNavigator treeNav) { |
166 |
public RenameAction(TreeNavigator treeNav) { |
| 64 |
super(TestNavigatorMessages.RenameAction_ActionName); |
167 |
super(TestNavigatorMessages.RenameAction_ActionName); |
| 65 |
this.navigator = treeNav; |
168 |
this.navigator = treeNav; |
|
Lines 82-97
Link Here
|
| 82 |
return true; |
185 |
return true; |
| 83 |
} else if (objSelected instanceof IProxyNode) { |
186 |
} else if (objSelected instanceof IProxyNode) { |
| 84 |
IProxyNode proxy = (IProxyNode) objSelected; |
187 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 85 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
188 |
IProxyNodeRenamer2 renamer2 = (IProxyNodeRenamer2)proxy.getAdapter(IProxyNodeRenamer2.class); |
| 86 |
if (renamer != null) { |
189 |
if(renamer2 != null) { |
| 87 |
return renamer.isApplicableFor(); |
190 |
return true; |
| 88 |
} else { |
|
|
| 89 |
//- this means that there is no renamer for this proxy |
| 90 |
return false; |
| 91 |
} |
191 |
} |
| 92 |
} else { |
192 |
oldActionValidation(proxy); |
| 93 |
return false; |
193 |
} |
|
|
194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @param proxy |
| 199 |
* @return |
| 200 |
* @deprecated |
| 201 |
*/ |
| 202 |
private boolean oldActionValidation(IProxyNode proxy) { |
| 203 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
| 204 |
if (renamer != null) { |
| 205 |
return renamer.isApplicableFor(); |
| 94 |
} |
206 |
} |
|
|
207 |
return false; |
| 95 |
} |
208 |
} |
| 96 |
|
209 |
|
| 97 |
/** |
210 |
/** |
|
Lines 111-117
Link Here
|
| 111 |
* Create the text editor widget. |
224 |
* Create the text editor widget. |
| 112 |
* This code has been imported from RenameResourceAction |
225 |
* This code has been imported from RenameResourceAction |
| 113 |
* @param renamer |
226 |
* @param renamer |
| 114 |
* |
227 |
* @deprecated |
| 115 |
*/ |
228 |
*/ |
| 116 |
private void createTextEditor(final IProxyNodeRenamer renamer) { |
229 |
private void createTextEditor(final IProxyNodeRenamer renamer) { |
| 117 |
//- Create text editor parent. This draws a nice bounding rect. |
230 |
//- Create text editor parent. This draws a nice bounding rect. |
|
Lines 165-170
Link Here
|
| 165 |
textActionHandler.addText(textEditor); |
278 |
textActionHandler.addText(textEditor); |
| 166 |
} |
279 |
} |
| 167 |
|
280 |
|
|
|
281 |
/** |
| 282 |
* |
| 283 |
* @param renamer |
| 284 |
* @param newName |
| 285 |
* @deprecated |
| 286 |
*/ |
| 168 |
protected void performRename(final IProxyNodeRenamer renamer, final String newName) { |
287 |
protected void performRename(final IProxyNodeRenamer renamer, final String newName) { |
| 169 |
Runnable query = new Runnable() { |
288 |
Runnable query = new Runnable() { |
| 170 |
public void run() { |
289 |
public void run() { |
|
Lines 242-247
Link Here
|
| 242 |
* This code is inspired from RenameResourceAction |
361 |
* This code is inspired from RenameResourceAction |
| 243 |
* @param oldName current value of the selected item |
362 |
* @param oldName current value of the selected item |
| 244 |
* @param renamer |
363 |
* @param renamer |
|
|
364 |
* @deprecated |
| 245 |
*/ |
365 |
*/ |
| 246 |
private void getNewNameInline(String oldName, IProxyNodeRenamer renamer) { |
366 |
private void getNewNameInline(String oldName, IProxyNodeRenamer renamer) { |
| 247 |
//- Make sure text editor is created only once. |
367 |
//- Make sure text editor is created only once. |
|
Lines 265-292
Link Here
|
| 265 |
Object objSelected = selection.getFirstElement(); |
385 |
Object objSelected = selection.getFirstElement(); |
| 266 |
if (objSelected instanceof IContainer) { |
386 |
if (objSelected instanceof IContainer) { |
| 267 |
renamedResource = (IContainer)objSelected; |
387 |
renamedResource = (IContainer)objSelected; |
| 268 |
//- reuse the inline editor to rename a project or folder node |
388 |
preformContainerRefactoringRename(renamedResource); |
| 269 |
getNewNameInline(((IContainer)objSelected).getName(), null); |
|
|
| 270 |
} else if (objSelected instanceof IProxyNode) { |
389 |
} else if (objSelected instanceof IProxyNode) { |
| 271 |
IProxyNode proxy = (IProxyNode) objSelected; |
390 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 272 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
391 |
IProxyNodeRenamer2 renamer2 = (IProxyNodeRenamer2)proxy.getAdapter(IProxyNodeRenamer2.class); |
| 273 |
if (renamer != null) { |
392 |
if(renamer2 != null) { |
| 274 |
RenamerUIStatus status = renamer.performUserInteraction(proxy.getText()); |
393 |
performProxyRefactoringRename(proxy); |
| 275 |
switch (status.getStatus()) { |
394 |
} else { |
| 276 |
case RenamerUIStatus.OK : |
395 |
IReferencerProxyNode ref = (IReferencerProxyNode)proxy.getAdapter(IReferencerProxyNode.class); |
| 277 |
performRename(renamer, status.getNewName()); |
396 |
if(ref != null) { |
| 278 |
break; |
397 |
performProxyRefactoringRename(proxy); |
| 279 |
case RenamerUIStatus.CANCEL : |
398 |
} else { |
| 280 |
//- nothing to do |
399 |
performOldRename(proxy); |
| 281 |
break; |
|
|
| 282 |
case RenamerUIStatus.INLINE_EDITOR : |
| 283 |
getNewNameInline(proxy.getText(), renamer); |
| 284 |
break; |
| 285 |
} |
400 |
} |
| 286 |
} |
401 |
} |
| 287 |
} |
402 |
} |
| 288 |
} |
403 |
} |
| 289 |
|
404 |
|
|
|
405 |
/** |
| 406 |
* @param proxy |
| 407 |
* @deprecated |
| 408 |
*/ |
| 409 |
private void performOldRename(IProxyNode proxy) { |
| 410 |
//- this code will be removed in 5.0 |
| 411 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
| 412 |
if (renamer != null) { |
| 413 |
RenamerUIStatus status = renamer.performUserInteraction(proxy.getText()); |
| 414 |
switch (status.getStatus()) { |
| 415 |
case RenamerUIStatus.OK : |
| 416 |
performRename(renamer, status.getNewName()); |
| 417 |
break; |
| 418 |
case RenamerUIStatus.CANCEL : |
| 419 |
//- nothing to do |
| 420 |
break; |
| 421 |
case RenamerUIStatus.INLINE_EDITOR : |
| 422 |
getNewNameInline(proxy.getText(), renamer); |
| 423 |
break; |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
private void preformContainerRefactoringRename(IContainer container) { |
| 429 |
RenameRefactoring refactoring = new RenameRefactoring(container); |
| 430 |
performRefactoringRename(refactoring, container.getName()); |
| 431 |
} |
| 432 |
|
| 433 |
private void performProxyRefactoringRename(IProxyNode proxy) { |
| 434 |
RenameRefactoring refactoring = new RenameRefactoring(proxy); |
| 435 |
performRefactoringRename(refactoring, proxy.getText()); |
| 436 |
} |
| 437 |
|
| 438 |
private void performRefactoringRename(final RenameRefactoring refactoring, final String oldName) { |
| 439 |
Shell shell = Display.getCurrent().getActiveShell(); |
| 440 |
if (! canActivate(shell)) |
| 441 |
return; |
| 442 |
RefactoringWizard wizard = new RenameRefactoringWizard(refactoring, oldName); |
| 443 |
wizard.setDefaultPageTitle(ActionMessages.RENAME_ACTION_NAME); |
| 444 |
wizard.setNeedsProgressMonitor(true); |
| 445 |
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard); |
| 446 |
try { |
| 447 |
op.run(shell , ""); //$NON-NLS-1$ |
| 448 |
} catch (InterruptedException e) { |
| 449 |
UiPlugin.logError(e); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
private boolean canActivate(Shell shell) { |
| 454 |
return EditorUtil.saveEditors(shell); |
| 455 |
} |
| 456 |
|
| 290 |
public void selectionChanged(IStructuredSelection structuredSelection) { |
457 |
public void selectionChanged(IStructuredSelection structuredSelection) { |
| 291 |
selection = structuredSelection; |
458 |
selection = structuredSelection; |
| 292 |
} |
459 |
} |