1 /******************************************************************************* 2 * Copyright (c) 2014 Remain Software, Industrial-TSI and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * Wim Jongman <wim.jongman@remainsoftware.com> - Bug 432892: Eclipse 4 Application does not work after renaming the 13 * project name 14 ******************************************************************************/ 15 package org.eclipse.e4.tools.emf.editor3x; 16 17 import java.util.ArrayList; 18 import java.util.List; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.ltk.core.refactoring.TextChange; 24 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; 25 26 /** 27 * A temporary data storage for Ecipse 4 model refactoring. Use the static 28 * methods to get hold of an instance. 29 * 30 * @author Remain Software - Wim Jongman 31 * 32 */ 33 public class RefactorModel { 34 35 int fIndex = 0; 36 IProject fNewProject; 37 List<String> fNewTexts = new ArrayList<>(); 38 39 IProject fOldProject; 40 41 List<String> fOldTexts = new ArrayList<>(); 42 43 private RefactoringParticipant fParticipant; 44 45 /** 46 * Factory for an empty model. Use the {@link #addTextRename(String, String)} method to add one or more text 47 * renames. 48 * 49 * @param pParticipant 50 * @return the model 51 */ getModel(RefactoringParticipant pParticipant)52 public static RefactorModel getModel(RefactoringParticipant pParticipant) { 53 return new RefactorModel().setRefactorParticipant(pParticipant); 54 } 55 56 /** 57 * Factory for a model with one rename. Use the {@link #addTextRename(String, String)} method to add one or more 58 * text 59 * renames. 60 * 61 * @param pParticipant 62 * @param pOldText 63 * @param pNewText 64 * @return the model. 65 */ getModel(RefactoringParticipant pParticipant, String pOldText, String pNewText)66 public static RefactorModel getModel(RefactoringParticipant pParticipant, 67 String pOldText, String pNewText) { 68 return new RefactorModel().addTextRename(pOldText, pNewText); 69 } 70 71 /** 72 * Factory for a model with one rename and a project rename. The project 73 * rename uses the old and new project because the refactoring framework 74 * expects the already renamed filenames. Use the {@link #addTextRename(String, String)} method to add one or more 75 * text 76 * renames and the old and the new project. 77 * 78 * @param pParticipant 79 * @param pOldText 80 * @param pNewText 81 * @param pOldProject 82 * @param pNewProject 83 * @return the model. 84 */ getModel(RefactoringParticipant pParticipant, String pOldText, String pNewText, IProject pOldProject, IProject pNewProject)85 public static RefactorModel getModel(RefactoringParticipant pParticipant, 86 String pOldText, String pNewText, IProject pOldProject, 87 IProject pNewProject) { 88 return new RefactorModel().addTextRename(pOldText, pNewText) 89 .setProjectRename(pOldProject, pNewProject) 90 .setRefactorParticipant(pParticipant); 91 } 92 93 /** 94 * Adds a text rename to be processed later. For example, if the project 95 * name changes there can be <code>bundlclass://</code> and <code>platform:/plugin</code> changes. 96 * 97 * @param oldText 98 * @param newText 99 * @return the model 100 */ addTextRename(String oldText, String newText)101 public RefactorModel addTextRename(String oldText, String newText) { 102 Assert.isNotNull(oldText); 103 Assert.isNotNull(newText); 104 fOldTexts.add(oldText); 105 fNewTexts.add(newText); 106 return this; 107 } 108 109 /** 110 * When project renaming this returns the new project. This project does not 111 * necessarily exist yet. 112 * 113 * @return the new project or null if it was not set 114 * @see RefactorModel#setProjectRename(IProject, IProject) 115 */ getNewProject()116 public IProject getNewProject() { 117 return fNewProject; 118 } 119 120 /** 121 * @return the current index set by {@link #setIndex(int)} 122 */ getNewTextCurrentIndex()123 public String getNewTextCurrentIndex() { 124 return fNewTexts.get(fIndex); 125 } 126 127 /** 128 * When project renaming this returns the old project. 129 * 130 * @return the new project or null if it was not set 131 * @see RefactorModel#setProjectRename(IProject, IProject) 132 */ getOldProject()133 public IProject getOldProject() { 134 return fOldProject; 135 } 136 137 /** 138 * 139 * @return the old text in the current index. 140 */ getOldTextCurrentIndex()141 public String getOldTextCurrentIndex() { 142 return fOldTexts.get(fIndex); 143 } 144 145 /** 146 * @return the refactoring participant 147 */ getRefactoringParticipant()148 public RefactoringParticipant getRefactoringParticipant() { 149 return fParticipant; 150 } 151 152 /** 153 * @return the number of text renames in this model 154 */ getRenameCount()155 public int getRenameCount() { 156 return fOldTexts.size(); 157 } 158 159 /** 160 * Delegates to the same method of the embedded RefactoringParticipant. 161 * 162 * @param file 163 * @return a TextChange object 164 * @see RefactoringParticipant#getTextChange(Object) 165 */ getTextChange(IFile file)166 public TextChange getTextChange(IFile file) { 167 return fParticipant.getTextChange(file); 168 } 169 170 /** 171 * @return true if this is a projec rename (old and new project are set) 172 */ isProjectRename()173 public boolean isProjectRename() { 174 return fOldProject != null; 175 } 176 177 /** 178 * Sets the current 0-based index. May not be out of bounds. 179 * 180 * @param index 181 * @return the model 182 */ setIndex(int index)183 public RefactorModel setIndex(int index) { 184 Assert.isTrue(index >= 0); 185 Assert.isTrue(index < fOldTexts.size()); 186 fIndex = index; 187 return this; 188 } 189 190 /** 191 * Sets the old and the new project in case of project renaming. 192 * 193 * @param oldProject 194 * @param newProject 195 * @return the model 196 */ 197 public RefactorModel setProjectRename(IProject oldProject, 198 IProject newProject) { 199 Assert.isNotNull(oldProject); 200 Assert.isNotNull(newProject); 201 fOldProject = oldProject; 202 fNewProject = newProject; 203 return this; 204 } 205 206 /** 207 * Sets the RefactoringParticipant. 208 * 209 * @param pParticipant 210 * @return the model 211 * @see RefactoringParticipant 212 */ 213 public RefactorModel setRefactorParticipant( 214 RefactoringParticipant pParticipant) { 215 fParticipant = pParticipant; 216 return this; 217 } 218 } 219