csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
kchong | ee4b4cf | 2007-05-02 17:49:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2000, 2006 IBM Corporation and others. |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 5 | * which accompanies this distribution, and is available at |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 6 | * http://www.eclipse.org/legal/epl-v10.html |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 7 | * |
| 8 | * Contributors: |
csalter | ce82000 | 2006-01-10 07:53:46 +0000 | [diff] [blame] | 9 | * IBM Corporation - initial API and implementation |
jeffliu | ec1c478 | 2006-05-24 14:16:24 +0000 | [diff] [blame] | 10 | * |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 11 | *******************************************************************************/ |
| 12 | package org.eclipse.wst.xsd.ui.internal.refactor.structure; |
| 13 | |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Arrays; |
| 17 | import java.util.List; |
david_williams | 1ab25b8 | 2006-01-11 03:01:55 +0000 | [diff] [blame] | 18 | |
| 19 | import org.eclipse.core.runtime.Assert; |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 20 | import org.eclipse.core.runtime.CoreException; |
| 21 | import org.eclipse.core.runtime.IProgressMonitor; |
| 22 | import org.eclipse.core.runtime.OperationCanceledException; |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 23 | import org.eclipse.ltk.core.refactoring.Change; |
| 24 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; |
| 25 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; |
| 26 | import org.eclipse.ltk.core.refactoring.participants.ParticipantManager; |
| 27 | import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; |
| 28 | import org.eclipse.ltk.core.refactoring.participants.RenameArguments; |
| 29 | import org.eclipse.ltk.core.refactoring.participants.RenameProcessor; |
| 30 | import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; |
csalter | ce82000 | 2006-01-10 07:53:46 +0000 | [diff] [blame] | 31 | import org.eclipse.wst.xsd.ui.internal.refactor.INameUpdating; |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 32 | import org.eclipse.wst.xsd.ui.internal.refactor.RefactoringMessages; |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 33 | import org.eclipse.xsd.XSDTypeDefinition; |
| 34 | |
| 35 | public class MakeTypeGlobalProcessor extends RenameProcessor implements INameUpdating{ |
| 36 | |
| 37 | private XSDTypeDefinition fTypeComponent; |
| 38 | private String fNewElementName; |
| 39 | |
| 40 | public static final String IDENTIFIER= "org.eclipse.wst.ui.xsd.makeTypeGlobalProcessor"; //$NON-NLS-1$ |
| 41 | |
| 42 | //private QualifiedNameSearchResult fNameSearchResult; |
| 43 | |
| 44 | public MakeTypeGlobalProcessor(XSDTypeDefinition element, String newName) { |
| 45 | fTypeComponent= element; |
| 46 | fNewElementName = newName; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | public XSDTypeDefinition getTypeComponent() { |
| 51 | return fTypeComponent; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | |
| 56 | /* (non-Javadoc) |
| 57 | * @see org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating#canEnableTextUpdating() |
| 58 | */ |
| 59 | public boolean canEnableTextUpdating() { |
| 60 | return true; |
| 61 | } |
david_williams | 47a33d2 | 2005-05-28 01:26:57 +0000 | [diff] [blame] | 62 | |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 63 | protected String[] getAffectedProjectNatures() throws CoreException { |
| 64 | //TODO: find project natures of the files that are going to be refactored |
| 65 | return new String[0]; |
| 66 | } |
| 67 | |
csalter | e2ddc8f | 2005-02-23 14:53:42 +0000 | [diff] [blame] | 68 | protected void loadDerivedParticipants(RefactoringStatus status, |
| 69 | List result, String[] natures, SharableParticipants shared) |
| 70 | throws CoreException { |
| 71 | // TODO: provide a way to load rename participants |
| 72 | } |
| 73 | /* (non-Javadoc) |
| 74 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext) |
| 75 | */ |
| 76 | public RefactoringStatus checkFinalConditions(IProgressMonitor pm, |
| 77 | CheckConditionsContext context) throws CoreException, |
| 78 | OperationCanceledException { |
| 79 | // TODO add code to check final conditions for component rename |
| 80 | return new RefactoringStatus(); |
| 81 | } |
| 82 | /* (non-Javadoc) |
| 83 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor) |
| 84 | */ |
| 85 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm) |
| 86 | throws CoreException, OperationCanceledException { |
| 87 | // TODO add code to check initial conditions for component rename |
| 88 | return new RefactoringStatus(); |
| 89 | } |
| 90 | /* (non-Javadoc) |
| 91 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#createChange(org.eclipse.core.runtime.IProgressMonitor) |
| 92 | */ |
| 93 | public Change createChange(IProgressMonitor pm) throws CoreException, |
| 94 | OperationCanceledException { |
| 95 | // TODO P1 add change creation |
| 96 | // Change[] changes = XSDComponentRenameChange.createChangesFor(this.fNamedComponent, getNewElementName()); |
| 97 | // CompositeChange multiChange = null; |
| 98 | // if(changes.length > 0) |
| 99 | // multiChange = new CompositeChange("XSD component rename participant changes", changes); //$NON-NLS-1$ TODO: externalize string |
| 100 | // return multiChange; |
| 101 | |
| 102 | // computeNameMatches(pm); |
| 103 | // Change[] changes = fNameSearchResult.getAllChanges(); |
| 104 | // return new CompositeChange("XSD file rename participant changes", changes); //TODO: externalize string |
| 105 | pm.beginTask("", 1); //$NON-NLS-1$ |
| 106 | try{ |
| 107 | return new MakeTypeGlobalChange(fTypeComponent, getNewElementName()); |
| 108 | } finally{ |
| 109 | pm.done(); |
| 110 | } |
| 111 | } |
| 112 | /* (non-Javadoc) |
| 113 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getElements() |
| 114 | */ |
| 115 | public Object[] getElements() { |
| 116 | |
| 117 | return new Object[] {fTypeComponent}; |
| 118 | } |
| 119 | /* (non-Javadoc) |
| 120 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getIdentifier() |
| 121 | */ |
| 122 | public String getIdentifier() { |
| 123 | return IDENTIFIER; |
| 124 | } |
| 125 | /* (non-Javadoc) |
| 126 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#getProcessorName() |
| 127 | */ |
| 128 | public String getProcessorName() { |
| 129 | return RefactoringMessages.getFormattedString( |
| 130 | "MakeLocalTypeGlobalRefactoring.name", //$NON-NLS-1$ |
| 131 | new String[]{getNewElementName()}); |
| 132 | |
| 133 | } |
| 134 | /* (non-Javadoc) |
| 135 | * @see org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor#isApplicable() |
| 136 | */ |
| 137 | public boolean isApplicable() throws CoreException { |
| 138 | if (fTypeComponent == null) |
| 139 | return false; |
| 140 | // TODO implement isApplicable logic for the named component, |
| 141 | // verify how it is different from other condition checks |
| 142 | // if (fNamedComponent.isAnonymous()) |
| 143 | // return false; |
| 144 | // if (! Checks.isAvailable(fType)) |
| 145 | // return false; |
| 146 | // if (isSpecialCase(fType)) |
| 147 | // return false; |
| 148 | return true; |
| 149 | } |
| 150 | /* (non-Javadoc) |
| 151 | * @see org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating#checkNewElementName(java.lang.String) |
| 152 | */ |
| 153 | public RefactoringStatus checkNewElementName(String newName){ |
| 154 | Assert.isNotNull(newName, "new name"); //$NON-NLS-1$ |
| 155 | // TODO: implement new name checking |
| 156 | // RefactoringStatus result = Checks.checkTypeName(newName); |
| 157 | // if (Checks.isAlreadyNamed(fType, newName)) |
| 158 | // result.addFatalError(RefactoringCoreMessages.getString("RenameTypeRefactoring.choose_another_name")); //$NON-NLS-1$ |
| 159 | return new RefactoringStatus(); |
| 160 | } |
| 161 | /* (non-Javadoc) |
| 162 | * @see org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating#getNewElement() |
| 163 | */ |
| 164 | public Object getNewElement() throws CoreException { |
| 165 | // TODO implement this method, it's used for updating selection on new element |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | // private void computeNameMatches(IProgressMonitor pm) throws CoreException { |
| 170 | // |
| 171 | // IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
| 172 | // try { |
| 173 | // URL fileURL = Platform.resolve(new URL(fNamedComponent.getSchema().getSchemaLocation())); |
| 174 | // IFile file = workspaceRoot.getFileForLocation(new Path(fileURL.getPath())); |
| 175 | // if (fNameSearchResult == null) |
| 176 | // fNameSearchResult= new QualifiedNameSearchResult(); |
| 177 | // QualifiedNameFinder.process(fNameSearchResult, getNamedComponent().getName(), |
| 178 | // getNewElementName(), |
| 179 | // "*.xsd", file.getProject(), pm); |
| 180 | // } catch (IOException e) { |
| 181 | // // TODO Auto-generated catch block |
| 182 | // e.printStackTrace(); |
| 183 | // } |
| 184 | // } |
| 185 | |
| 186 | public final RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException { |
| 187 | RenameArguments arguments= new RenameArguments(getNewElementName(), true); |
| 188 | String[] natures= getAffectedProjectNatures(); |
| 189 | List result= new ArrayList(); |
| 190 | loadElementParticipants(status, result, arguments, natures, sharedParticipants); |
| 191 | loadDerivedParticipants(status, result, natures, sharedParticipants); |
| 192 | return (RefactoringParticipant[])result.toArray(new RefactoringParticipant[result.size()]); |
| 193 | } |
| 194 | |
| 195 | protected void loadElementParticipants(RefactoringStatus status, List result, RenameArguments arguments, String[] natures, SharableParticipants shared) throws CoreException { |
| 196 | Object[] elements= getElements(); |
| 197 | for (int i= 0; i < elements.length; i++) { |
| 198 | result.addAll(Arrays.asList(ParticipantManager.loadRenameParticipants(status, |
| 199 | this, elements[i], |
| 200 | arguments, natures, shared))); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | public void setNewElementName(String newName) { |
| 206 | |
| 207 | fNewElementName= newName; |
| 208 | } |
| 209 | |
| 210 | public String getNewElementName() { |
| 211 | return fNewElementName; |
| 212 | } |
| 213 | |
| 214 | public String getCurrentElementName() { |
| 215 | // TODO Auto-generated method stub |
| 216 | return fNewElementName; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | } |