Index: src/org/jbpm/gd/jpdl/refactoring/MoveProcessHandler.java =================================================================== --- src/org/jbpm/gd/jpdl/refactoring/MoveProcessHandler.java (revision 24193) +++ src/org/jbpm/gd/jpdl/refactoring/MoveProcessHandler.java (working copy) @@ -1,15 +1,40 @@ package org.jbpm.gd.jpdl.refactoring; -import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.resources.IResource; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; -public class MoveProcessHandler extends AbstractHandler { +public class MoveProcessHandler extends AbstractProcessHandler { @Override - public Object execute(ExecutionEvent arg0) throws ExecutionException { - System.out.println("hello, move world"); + public Object execute(ExecutionEvent event) throws ExecutionException { + Shell activeShell= HandlerUtil.getActiveShell(event); + ISelection sel= HandlerUtil.getCurrentSelection(event); + if (sel instanceof IStructuredSelection) { + IResource resource= getCurrentResource((IStructuredSelection) sel); + if (resource != null) { + MoveProcessWizard refactoringWizard= new MoveProcessWizard(resource); + RefactoringWizardOpenOperation op= new RefactoringWizardOpenOperation(refactoringWizard); + try { + op.run(activeShell, "Move Process"); + } catch (InterruptedException e) { + // do nothing + } + } + } return null; } + private IResource getCurrentResource(IStructuredSelection sel) { + IResource[] resources= getSelectedResources(sel); + if (resources.length == 1) { + return resources[0]; + } + return null; + } } Index: src/org/jbpm/gd/jpdl/refactoring/MoveProcessWizard.java =================================================================== --- src/org/jbpm/gd/jpdl/refactoring/MoveProcessWizard.java (revision 0) +++ src/org/jbpm/gd/jpdl/refactoring/MoveProcessWizard.java (revision 0) @@ -0,0 +1,140 @@ +package org.jbpm.gd.jpdl.refactoring; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerFilter; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring; +import org.eclipse.ltk.ui.refactoring.RefactoringWizard; +import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.ui.model.BaseWorkbenchContentProvider; +import org.eclipse.ui.model.WorkbenchLabelProvider; +import org.eclipse.ui.model.WorkbenchViewerComparator; + +public class MoveProcessWizard extends RefactoringWizard { + + public MoveProcessWizard(IResource resource) { + super(new MoveRefactoring(new MoveProcessProcessor(resource)), DIALOG_BASED_USER_INTERFACE); + setDefaultPageTitle("Move Process"); + setWindowTitle("Move Process"); + } + + protected void addUserInputPages() { + MoveProcessProcessor processor= (MoveProcessProcessor) getRefactoring().getAdapter(MoveProcessProcessor.class); + addPage(new MoveProcessRefactoringConfigurationPage(processor)); + } + + private static class MoveProcessRefactoringConfigurationPage extends UserInputWizardPage { + + private final MoveProcessProcessor refactoringProcessor; + private TreeViewer destinationField; + + public MoveProcessRefactoringConfigurationPage(MoveProcessProcessor processor) { + super("MoveResourcesRefactoringConfigurationPage"); + refactoringProcessor= processor; + } + + public void createControl(Composite parent) { + initializeDialogUnits(parent); + Composite composite= new Composite(parent, SWT.NONE); + composite.setLayout(new GridLayout(2, false)); + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + composite.setFont(parent.getFont()); + Label label= new Label(composite, SWT.NONE); + IResource[] resourcesToMove= refactoringProcessor.getResourcesToMove(); + label.setText("&Choose destination for " + refactoringProcessor.getProcessToMove() + " :"); + label.setLayoutData(new GridData()); + destinationField= new TreeViewer(composite, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); + GridData gd= new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1); + gd.widthHint= convertWidthInCharsToPixels(40); + gd.heightHint= convertHeightInCharsToPixels(15); + destinationField.getTree().setLayoutData(gd); + destinationField.setLabelProvider(new WorkbenchLabelProvider()); + destinationField.setContentProvider(new BaseWorkbenchContentProvider()); + destinationField.setComparator(new WorkbenchViewerComparator()); + destinationField.setInput(ResourcesPlugin.getWorkspace()); + destinationField.addFilter(new ViewerFilter() { + public boolean select(Viewer viewer, Object parentElement, Object element) { + if (element instanceof IProject) { + IProject project= (IProject) element; + return project.isAccessible(); + } else if (element instanceof IFolder) { + return true; + } + return false; + } + }); + destinationField.addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + validatePage(); + } + }); + if (resourcesToMove.length > 0) { + destinationField.setSelection(new StructuredSelection(resourcesToMove[0].getParent())); + } + setPageComplete(false); + setControl(composite); + } + + public void setVisible(boolean visible) { + if (visible) { + destinationField.getTree().setFocus(); + if (getErrorMessage() != null) { + setErrorMessage(null); + } + + } + super.setVisible(visible); + } + + private final void validatePage() { + RefactoringStatus status; + + IStructuredSelection selection= (IStructuredSelection) destinationField.getSelection(); + Object firstElement= selection.getFirstElement(); + if (firstElement instanceof IContainer) { + status= refactoringProcessor.validateDestination((IContainer) firstElement); + + } else { + status= new RefactoringStatus(); + status.addError("Select a process."); + } + setPageComplete(status); + } + + protected boolean performFinish() { + initializeRefactoring(); + storeSettings(); + return super.performFinish(); + } + + public IWizardPage getNextPage() { + initializeRefactoring(); + storeSettings(); + return super.getNextPage(); + } + + private void storeSettings() { + } + + private void initializeRefactoring() { + IContainer container= (IContainer) ((IStructuredSelection) destinationField.getSelection()).getFirstElement(); + refactoringProcessor.setDestination(container); + } + } +} \ No newline at end of file Index: src/org/jbpm/gd/jpdl/refactoring/MoveProcessProcessor.java =================================================================== --- src/org/jbpm/gd/jpdl/refactoring/MoveProcessProcessor.java (revision 0) +++ src/org/jbpm/gd/jpdl/refactoring/MoveProcessProcessor.java (revision 0) @@ -0,0 +1,215 @@ +package org.jbpm.gd.jpdl.refactoring; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IResourceStatus; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; +import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CompositeChange; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; +import org.eclipse.ltk.core.refactoring.participants.MoveProcessor; +import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; +import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker; +import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; +import org.eclipse.ltk.core.refactoring.resource.MoveResourceChange; + +public class MoveProcessProcessor extends MoveProcessor { + + private final IResource[] resourcesToMove; + private IContainer destination; + + public MoveProcessProcessor(IResource resource) { + IResource jpdlResource = null, gpdResource = null; + if (resource == null || !resource.exists()) { + throw new IllegalArgumentException("resource must not be null and must exist"); + } + if (isJpdlFile(resource)) { + jpdlResource = resource; + gpdResource = getGpdResource(resource); + } else if (isGpdFile(resource)) { + jpdlResource = getJpdlResource(resource); + gpdResource = resource; + } + if (jpdlResource != null && jpdlResource.exists() && gpdResource != null && gpdResource.exists()) { + resourcesToMove = new IResource[] { jpdlResource, gpdResource }; + } else { + throw new IllegalArgumentException("both jpdlFile and gpdFile must not be null and must exist"); + } + } + + private String getProcessName(IResource resource) { + String name = resource.getName(); + if (name.startsWith(".") && name.endsWith(".gpd.xml")) { + return name.substring(1, name.indexOf(".gpd.xml")); + } else if (name.endsWith(".jpdl.xml")) { + return name.substring(0, name.indexOf(".jpdl.xml")); + } else { + throw new IllegalArgumentException("resource must be either a jpdlFile or gpdFile"); + } + } + + private boolean isJpdlFile(IResource resource) { + return resource.getName().endsWith(".jpdl.xml"); + } + + private boolean isGpdFile(IResource resource) { + String name = resource.getName(); + return name.startsWith(".") && name.endsWith(".gpd.xml"); + } + + private IResource getGpdResource(IResource resource) { + if (!resource.getName().endsWith(".jpdl.xml")) { + throw new IllegalArgumentException("gpd resource can only be obtained for a jpdl file"); + } + return resource.getParent().getFile(new Path("." + getProcessName(resource) + ".gpd.xml")); + } + + private IResource getJpdlResource(IResource resource) { + if (!resource.getName().endsWith(".gpd.xml")) { + throw new IllegalArgumentException("jpdl resource can only be obtained for a gpd file"); + } + return resource.getParent().getFile(new Path(getProcessName(resource) + ".jpdl.xml")); + } + + public void setDestination(IContainer destination) { + Assert.isNotNull(destination); + this.destination= destination; + } + + public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { + IStatus result = null; + if (!resourcesToMove[0].isSynchronized(IResource.DEPTH_INFINITE)) { + result = new Status ( + IStatus.ERROR, + ResourcesPlugin.PI_RESOURCES, + IResourceStatus.OUT_OF_SYNC_LOCAL, + "Resource " + resourcesToMove[0].getName() + "is out of sync with file system", + null); + } + if (!resourcesToMove[1].isSynchronized(IResource.DEPTH_INFINITE)) { + IStatus temp = new Status ( + IStatus.ERROR, + ResourcesPlugin.PI_RESOURCES, + IResourceStatus.OUT_OF_SYNC_LOCAL, + "Resource " + resourcesToMove[1].getName() + "is out of sync with file system", + null); + if (result == null) { + result = temp; + } else { + MultiStatus multi = new MultiStatus( + ResourcesPlugin.PI_RESOURCES, + IResourceStatus.OUT_OF_SYNC_LOCAL, + "both resources are out of sync with file system", + null); + multi.add(result); + multi.add(temp); + result = multi; + } + } + if (result == null) { + result = Status.OK_STATUS; + } + return RefactoringStatus.create(result); + } + + public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException { + pm.beginTask("", 1); + try { + RefactoringStatus status= validateDestination(destination); + if (status.hasFatalError()) { + return status; + } + ResourceChangeChecker checker= (ResourceChangeChecker) context.getChecker(ResourceChangeChecker.class); + IResourceChangeDescriptionFactory deltaFactory= checker.getDeltaFactory(); + + for (int i= 0; i < resourcesToMove.length; i++) { + IResource resource= resourcesToMove[i]; + IResource newResource= destination.findMember(resource.getName()); + if (newResource != null) { + status.addWarning("'" + resourcesToMove[i].getFullPath().toString() + "' already exist. It will be replaced."); + deltaFactory.delete(newResource); + } + deltaFactory.move(resourcesToMove[i], destination.getFullPath()); + } + return status; + } finally { + pm.done(); + } + } + + public RefactoringStatus validateDestination(IContainer destination) { + Assert.isNotNull(destination, "container is null"); + if (destination instanceof IWorkspaceRoot) + return RefactoringStatus.createFatalErrorStatus("Invalid parent"); + + if (!destination.exists()) { + return RefactoringStatus.createFatalErrorStatus("Destination does not exist"); + } + return new RefactoringStatus(); + } + + public Change createChange(IProgressMonitor pm) throws CoreException { + pm.beginTask("", resourcesToMove.length); + try { + CompositeChange compositeChange= new CompositeChange("process move"); + compositeChange.markAsSynthetic(); + for (int i= 0; i < resourcesToMove.length; i++) { + MoveResourceChange moveChange= new MoveResourceChange(resourcesToMove[i], destination); + compositeChange.add(moveChange); + } + return compositeChange; + } finally { + pm.done(); + } + } + + public IResource[] getResourcesToMove() { + return resourcesToMove; + } + + public String getProcessToMove() { + return getProcessName(resourcesToMove[0]); + } + + public Object[] getElements() { + return resourcesToMove; + } + + public String getIdentifier() { + return "org.jbpm.gd.jpdl.refactoring.moveProcessProcessor"; + } + + public String getProcessorName() { + return "Move Process"; + } + + public boolean isApplicable() { + for (int i= 0; i < resourcesToMove.length; i++) { + if (!canMove(resourcesToMove[i])) { + return false; + } + } + return true; + } + + private static boolean canMove(IResource res) { + return (res instanceof IFile || res instanceof IFolder) && res.exists() && !res.isPhantom(); + } + + public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants shared) throws CoreException { + return new RefactoringParticipant[0]; + } + +} \ No newline at end of file Index: plugin.xml =================================================================== --- plugin.xml (revision 24193) +++ plugin.xml (working copy) @@ -1600,7 +1600,7 @@ -