|
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-53
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; |
| 26 |
import org.eclipse.hyades.test.ui.editor.extension.EditorExtensionMessages; |
| 27 |
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigator; |
| 22 |
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigatorMessages; |
28 |
import org.eclipse.hyades.test.ui.internal.navigator.TestNavigatorMessages; |
|
|
29 |
import org.eclipse.hyades.test.ui.internal.navigator.refactoring.RenameRefactoring; |
| 30 |
import org.eclipse.hyades.test.ui.navigator.CMNNamedElementProxyNode; |
| 31 |
import org.eclipse.hyades.test.ui.navigator.IFileProxyManager; |
| 23 |
import org.eclipse.hyades.test.ui.navigator.IProxyNode; |
32 |
import org.eclipse.hyades.test.ui.navigator.IProxyNode; |
|
|
33 |
import org.eclipse.hyades.test.ui.navigator.IReferencerProxyNode; |
| 24 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer; |
34 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer; |
|
|
35 |
import org.eclipse.hyades.test.ui.navigator.actions.IProxyNodeRenamer2; |
| 25 |
import org.eclipse.hyades.test.ui.navigator.actions.RenamerUIStatus; |
36 |
import org.eclipse.hyades.test.ui.navigator.actions.RenamerUIStatus; |
| 26 |
import org.eclipse.hyades.ui.internal.navigator.TreeNavigator; |
37 |
import org.eclipse.hyades.ui.internal.navigator.TreeNavigator; |
| 27 |
import org.eclipse.hyades.ui.util.IDisposable; |
38 |
import org.eclipse.hyades.ui.util.IDisposable; |
| 28 |
import org.eclipse.jface.action.Action; |
39 |
import org.eclipse.jface.action.Action; |
|
|
40 |
import org.eclipse.jface.dialogs.Dialog; |
| 29 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
41 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 30 |
import org.eclipse.jface.viewers.IStructuredSelection; |
42 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 31 |
import org.eclipse.jface.viewers.StructuredSelection; |
43 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
|
44 |
import org.eclipse.ltk.ui.refactoring.RefactoringWizard; |
| 45 |
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; |
| 46 |
import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; |
| 32 |
import org.eclipse.swt.SWT; |
47 |
import org.eclipse.swt.SWT; |
| 33 |
import org.eclipse.swt.custom.TreeEditor; |
48 |
import org.eclipse.swt.custom.TreeEditor; |
| 34 |
import org.eclipse.swt.events.FocusAdapter; |
49 |
import org.eclipse.swt.events.FocusAdapter; |
| 35 |
import org.eclipse.swt.events.FocusEvent; |
50 |
import org.eclipse.swt.events.FocusEvent; |
|
|
51 |
import org.eclipse.swt.events.ModifyEvent; |
| 52 |
import org.eclipse.swt.events.ModifyListener; |
| 36 |
import org.eclipse.swt.graphics.Point; |
53 |
import org.eclipse.swt.graphics.Point; |
|
|
54 |
import org.eclipse.swt.layout.GridData; |
| 55 |
import org.eclipse.swt.layout.GridLayout; |
| 37 |
import org.eclipse.swt.widgets.Composite; |
56 |
import org.eclipse.swt.widgets.Composite; |
| 38 |
import org.eclipse.swt.widgets.Display; |
57 |
import org.eclipse.swt.widgets.Display; |
| 39 |
import org.eclipse.swt.widgets.Event; |
58 |
import org.eclipse.swt.widgets.Event; |
|
|
59 |
import org.eclipse.swt.widgets.Label; |
| 40 |
import org.eclipse.swt.widgets.Listener; |
60 |
import org.eclipse.swt.widgets.Listener; |
|
|
61 |
import org.eclipse.swt.widgets.Shell; |
| 41 |
import org.eclipse.swt.widgets.Text; |
62 |
import org.eclipse.swt.widgets.Text; |
| 42 |
import org.eclipse.swt.widgets.Tree; |
63 |
import org.eclipse.swt.widgets.Tree; |
| 43 |
import org.eclipse.swt.widgets.TreeItem; |
64 |
import org.eclipse.swt.widgets.TreeItem; |
|
|
65 |
import org.eclipse.ui.PlatformUI; |
| 44 |
import org.eclipse.ui.actions.TextActionHandler; |
66 |
import org.eclipse.ui.actions.TextActionHandler; |
| 45 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
67 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
| 46 |
|
68 |
|
| 47 |
/** |
69 |
/** |
| 48 |
* @author Jerome Gout |
70 |
* @author Jerome Gout |
| 49 |
* @author Paul E. Slauenwhite |
71 |
* @author Paul E. Slauenwhite |
| 50 |
* @version March, 2007 |
72 |
* @version March 12, 2008 |
| 51 |
* @since 3.3 |
73 |
* @since 3.3 |
| 52 |
*/ |
74 |
*/ |
| 53 |
public class RenameAction extends Action implements IDisposable { |
75 |
public class RenameAction extends Action implements IDisposable { |
|
Lines 60-65
Link Here
|
| 60 |
protected TreeNavigator navigator; |
82 |
protected TreeNavigator navigator; |
| 61 |
protected IContainer renamedResource; |
83 |
protected IContainer renamedResource; |
| 62 |
|
84 |
|
|
|
85 |
private class RenameRefactoringWizard extends RefactoringWizard { |
| 86 |
private RenameRefactoringNewNameInputPage page; |
| 87 |
|
| 88 |
private RenameRefactoringWizard(RenameRefactoring refactoring, String name) { |
| 89 |
super(refactoring, DIALOG_BASED_USER_INTERFACE/* | NONE*/); |
| 90 |
this.page = new RenameRefactoringNewNameInputPage(refactoring, name); |
| 91 |
} |
| 92 |
|
| 93 |
protected void addUserInputPages() { |
| 94 |
addPage(page); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
class RenameRefactoringNewNameInputPage extends UserInputWizardPage { |
| 99 |
private RenameRefactoring refactoring; |
| 100 |
private Text nameText; |
| 101 |
private String oldName; |
| 102 |
private String newName; |
| 103 |
|
| 104 |
public RenameRefactoringNewNameInputPage(RenameRefactoring refactoring, String oldName) { |
| 105 |
super(""); //$NON-NLS-1$ |
| 106 |
this.oldName = oldName; |
| 107 |
this.refactoring = refactoring; |
| 108 |
} |
| 109 |
|
| 110 |
public void createControl(Composite parent) { |
| 111 |
Composite result= new Composite(parent, SWT.NONE); |
| 112 |
setControl(result); |
| 113 |
result.setLayout(new GridLayout(2, false)); |
| 114 |
|
| 115 |
Label label= new Label(result, SWT.NONE); |
| 116 |
label.setText(ActionMessages.RENAME_NEW_NAME_LABEL); |
| 117 |
nameText = new Text(result, SWT.SINGLE | SWT.BORDER); |
| 118 |
nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); |
| 119 |
nameText.setText(oldName); |
| 120 |
nameText.addModifyListener(new ModifyListener() { |
| 121 |
public void modifyText(ModifyEvent e) { |
| 122 |
newName = nameText.getText(); |
| 123 |
refactoring.setNewName(newName); |
| 124 |
setPageComplete(isPageComplete()); |
| 125 |
} |
| 126 |
}); |
| 127 |
nameText.selectAll(); |
| 128 |
Dialog.applyDialogFont(result); |
| 129 |
} |
| 130 |
|
| 131 |
public boolean isPageComplete() { |
| 132 |
// first check if name is not empty and have changed |
| 133 |
boolean basicCondition = newName != null && newName.length() > 0 && !newName.equals(oldName); |
| 134 |
if (!basicCondition) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
// then check if target is ok |
| 138 |
if (selection == null || selection.isEmpty()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
Object objSelected = selection.getFirstElement(); |
| 142 |
if (objSelected instanceof IContainer) { |
| 143 |
// check if destination exists in case of a container |
| 144 |
IContainer destContainer = getIContainer((IContainer)objSelected, newName); |
| 145 |
return (!destContainer.exists()); |
| 146 |
} else if (objSelected instanceof IProxyNode) { |
| 147 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 148 |
IResource res = proxy.getUnderlyingResource(); |
| 149 |
if (res instanceof IFile) { |
| 150 |
IFile file = (IFile)res; |
| 151 |
String extension = file.getFileExtension(); |
| 152 |
String destName = newName; |
| 153 |
if (extension != null) { |
| 154 |
destName = newName+'.'+extension; |
| 155 |
} |
| 156 |
// check if physical element already exists |
| 157 |
IFile destFile = ((IFile)res).getParent().getFile(new Path(destName)); |
| 158 |
if (destFile.exists()) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
// check if logical element exists |
| 162 |
IResource [] allDirElems = null; |
| 163 |
try { |
| 164 |
allDirElems = file.getParent().members(); |
| 165 |
} catch (CoreException e) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
IFileProxyManager proxManager = null; |
| 169 |
if(PlatformUI.getWorkbench() != null) { |
| 170 |
TestNavigator nav = TestNavigator.getTestNavigator(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
| 171 |
if (nav != null) { |
| 172 |
proxManager = nav.getCurrentFileProxyManager(); |
| 173 |
} |
| 174 |
} |
| 175 |
if ((allDirElems != null) && (proxManager != null)) { |
| 176 |
for (int i=0; i<allDirElems.length; i++) { |
| 177 |
IResource resloc = allDirElems[i]; |
| 178 |
if (resloc instanceof IFile) { |
| 179 |
IProxyNode prox = proxManager.getProxy((IFile)resloc, null); |
| 180 |
String resName = null; |
| 181 |
if ((prox != null) && (prox instanceof CMNNamedElementProxyNode)) { |
| 182 |
resName = ((CMNNamedElementProxyNode)prox).getName(); |
| 183 |
if (newName.equals(resName)) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
return true; |
| 190 |
} else { |
| 191 |
return true; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private IContainer getIContainer(IContainer container,String newName) { |
| 200 |
if(container.getFullPath().segmentCount() > 1) { |
| 201 |
return container.getParent().getFolder(new Path(newName)); |
| 202 |
} else { |
| 203 |
return ResourcesPlugin.getWorkspace().getRoot().getProject(newName); |
| 204 |
} |
| 205 |
} |
| 63 |
public RenameAction(TreeNavigator treeNav) { |
206 |
public RenameAction(TreeNavigator treeNav) { |
| 64 |
super(TestNavigatorMessages.RenameAction_ActionName); |
207 |
super(TestNavigatorMessages.RenameAction_ActionName); |
| 65 |
this.navigator = treeNav; |
208 |
this.navigator = treeNav; |
|
Lines 82-97
Link Here
|
| 82 |
return true; |
225 |
return true; |
| 83 |
} else if (objSelected instanceof IProxyNode) { |
226 |
} else if (objSelected instanceof IProxyNode) { |
| 84 |
IProxyNode proxy = (IProxyNode) objSelected; |
227 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 85 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
228 |
IProxyNodeRenamer2 renamer2 = (IProxyNodeRenamer2)proxy.getAdapter(IProxyNodeRenamer2.class); |
| 86 |
if (renamer != null) { |
229 |
if(renamer2 != null) { |
| 87 |
return renamer.isApplicableFor(); |
230 |
return true; |
| 88 |
} else { |
|
|
| 89 |
//- this means that there is no renamer for this proxy |
| 90 |
return false; |
| 91 |
} |
231 |
} |
| 92 |
} else { |
232 |
oldActionValidation(proxy); |
| 93 |
return false; |
233 |
} |
|
|
234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @param proxy |
| 239 |
* @return |
| 240 |
* @deprecated |
| 241 |
*/ |
| 242 |
private boolean oldActionValidation(IProxyNode proxy) { |
| 243 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
| 244 |
if (renamer != null) { |
| 245 |
return renamer.isApplicableFor(); |
| 94 |
} |
246 |
} |
|
|
247 |
return false; |
| 95 |
} |
248 |
} |
| 96 |
|
249 |
|
| 97 |
/** |
250 |
/** |
|
Lines 111-117
Link Here
|
| 111 |
* Create the text editor widget. |
264 |
* Create the text editor widget. |
| 112 |
* This code has been imported from RenameResourceAction |
265 |
* This code has been imported from RenameResourceAction |
| 113 |
* @param renamer |
266 |
* @param renamer |
| 114 |
* |
267 |
* @deprecated |
| 115 |
*/ |
268 |
*/ |
| 116 |
private void createTextEditor(final IProxyNodeRenamer renamer) { |
269 |
private void createTextEditor(final IProxyNodeRenamer renamer) { |
| 117 |
//- Create text editor parent. This draws a nice bounding rect. |
270 |
//- Create text editor parent. This draws a nice bounding rect. |
|
Lines 165-170
Link Here
|
| 165 |
textActionHandler.addText(textEditor); |
318 |
textActionHandler.addText(textEditor); |
| 166 |
} |
319 |
} |
| 167 |
|
320 |
|
|
|
321 |
/** |
| 322 |
* |
| 323 |
* @param renamer |
| 324 |
* @param newName |
| 325 |
* @deprecated |
| 326 |
*/ |
| 168 |
protected void performRename(final IProxyNodeRenamer renamer, final String newName) { |
327 |
protected void performRename(final IProxyNodeRenamer renamer, final String newName) { |
| 169 |
Runnable query = new Runnable() { |
328 |
Runnable query = new Runnable() { |
| 170 |
public void run() { |
329 |
public void run() { |
|
Lines 242-247
Link Here
|
| 242 |
* This code is inspired from RenameResourceAction |
401 |
* This code is inspired from RenameResourceAction |
| 243 |
* @param oldName current value of the selected item |
402 |
* @param oldName current value of the selected item |
| 244 |
* @param renamer |
403 |
* @param renamer |
|
|
404 |
* @deprecated |
| 245 |
*/ |
405 |
*/ |
| 246 |
private void getNewNameInline(String oldName, IProxyNodeRenamer renamer) { |
406 |
private void getNewNameInline(String oldName, IProxyNodeRenamer renamer) { |
| 247 |
//- Make sure text editor is created only once. |
407 |
//- Make sure text editor is created only once. |
|
Lines 265-292
Link Here
|
| 265 |
Object objSelected = selection.getFirstElement(); |
425 |
Object objSelected = selection.getFirstElement(); |
| 266 |
if (objSelected instanceof IContainer) { |
426 |
if (objSelected instanceof IContainer) { |
| 267 |
renamedResource = (IContainer)objSelected; |
427 |
renamedResource = (IContainer)objSelected; |
| 268 |
//- reuse the inline editor to rename a project or folder node |
428 |
preformContainerRefactoringRename(renamedResource); |
| 269 |
getNewNameInline(((IContainer)objSelected).getName(), null); |
|
|
| 270 |
} else if (objSelected instanceof IProxyNode) { |
429 |
} else if (objSelected instanceof IProxyNode) { |
| 271 |
IProxyNode proxy = (IProxyNode) objSelected; |
430 |
IProxyNode proxy = (IProxyNode) objSelected; |
| 272 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
431 |
IProxyNodeRenamer2 renamer2 = (IProxyNodeRenamer2)proxy.getAdapter(IProxyNodeRenamer2.class); |
| 273 |
if (renamer != null) { |
432 |
if(renamer2 != null) { |
| 274 |
RenamerUIStatus status = renamer.performUserInteraction(proxy.getText()); |
433 |
performProxyRefactoringRename(proxy); |
| 275 |
switch (status.getStatus()) { |
434 |
} else { |
| 276 |
case RenamerUIStatus.OK : |
435 |
IReferencerProxyNode ref = (IReferencerProxyNode)proxy.getAdapter(IReferencerProxyNode.class); |
| 277 |
performRename(renamer, status.getNewName()); |
436 |
if(ref != null) { |
| 278 |
break; |
437 |
performProxyRefactoringRename(proxy); |
| 279 |
case RenamerUIStatus.CANCEL : |
438 |
} else { |
| 280 |
//- nothing to do |
439 |
performOldRename(proxy); |
| 281 |
break; |
|
|
| 282 |
case RenamerUIStatus.INLINE_EDITOR : |
| 283 |
getNewNameInline(proxy.getText(), renamer); |
| 284 |
break; |
| 285 |
} |
440 |
} |
| 286 |
} |
441 |
} |
| 287 |
} |
442 |
} |
| 288 |
} |
443 |
} |
| 289 |
|
444 |
|
|
|
445 |
/** |
| 446 |
* @param proxy |
| 447 |
* @deprecated |
| 448 |
*/ |
| 449 |
private void performOldRename(IProxyNode proxy) { |
| 450 |
//- this code will be removed in 5.0 |
| 451 |
IProxyNodeRenamer renamer = (IProxyNodeRenamer) proxy.getAdapter(IProxyNodeRenamer.class); |
| 452 |
if (renamer != null) { |
| 453 |
RenamerUIStatus status = renamer.performUserInteraction(proxy.getText()); |
| 454 |
switch (status.getStatus()) { |
| 455 |
case RenamerUIStatus.OK : |
| 456 |
performRename(renamer, status.getNewName()); |
| 457 |
break; |
| 458 |
case RenamerUIStatus.CANCEL : |
| 459 |
//- nothing to do |
| 460 |
break; |
| 461 |
case RenamerUIStatus.INLINE_EDITOR : |
| 462 |
getNewNameInline(proxy.getText(), renamer); |
| 463 |
break; |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
private void preformContainerRefactoringRename(IContainer container) { |
| 469 |
RenameRefactoring refactoring = new RenameRefactoring(container); |
| 470 |
performRefactoringRename(refactoring, container.getName()); |
| 471 |
} |
| 472 |
|
| 473 |
private void performProxyRefactoringRename(IProxyNode proxy) { |
| 474 |
RenameRefactoring refactoring = new RenameRefactoring(proxy); |
| 475 |
performRefactoringRename(refactoring, proxy.getText()); |
| 476 |
} |
| 477 |
|
| 478 |
private void performRefactoringRename(final RenameRefactoring refactoring, final String oldName) { |
| 479 |
Shell shell = Display.getCurrent().getActiveShell(); |
| 480 |
if (! canActivate(shell)) |
| 481 |
return; |
| 482 |
RefactoringWizard wizard = new RenameRefactoringWizard(refactoring, oldName); |
| 483 |
wizard.setDefaultPageTitle(ActionMessages.RENAME_ACTION_NAME); |
| 484 |
wizard.setNeedsProgressMonitor(true); |
| 485 |
RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard); |
| 486 |
try { |
| 487 |
op.run(shell , ""); //$NON-NLS-1$ |
| 488 |
} catch (InterruptedException e) { |
| 489 |
UiPlugin.logError(e); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
private boolean canActivate(Shell shell) { |
| 494 |
return EditorUtil.saveEditors(shell); |
| 495 |
} |
| 496 |
|
| 290 |
public void selectionChanged(IStructuredSelection structuredSelection) { |
497 |
public void selectionChanged(IStructuredSelection structuredSelection) { |
| 291 |
selection = structuredSelection; |
498 |
selection = structuredSelection; |
| 292 |
} |
499 |
} |