001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.refactoring;
011:
012: import org.eclipse.core.filebuffers.FileBuffers;
013: import org.eclipse.core.filebuffers.ITextFileBuffer;
014: import org.eclipse.core.filebuffers.ITextFileBufferManager;
015: import org.eclipse.core.filebuffers.LocationKind;
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IPath;
021: import org.eclipse.core.runtime.IProgressMonitor;
022: import org.eclipse.core.runtime.Path;
023: import org.eclipse.jdt.core.IJavaElement;
024: import org.eclipse.jface.text.IDocument;
025: import org.eclipse.ltk.core.refactoring.Change;
026: import org.eclipse.pde.core.build.IBuild;
027: import org.eclipse.pde.core.build.IBuildEntry;
028: import org.eclipse.pde.internal.core.text.build.BuildModel;
029: import org.eclipse.pde.internal.core.text.build.PropertiesTextChangeListener;
030: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
031: import org.eclipse.text.edits.MultiTextEdit;
032: import org.eclipse.text.edits.TextEdit;
033:
034: public class BuildPropertiesChange {
035:
036: public static Change createRenameChange(IFile file,
037: Object[] affectedElements, String[] newNames,
038: IProgressMonitor monitor) throws CoreException {
039: ITextFileBufferManager manager = FileBuffers
040: .getTextFileBufferManager();
041: try {
042: manager.connect(file.getFullPath(), LocationKind.NORMALIZE,
043: monitor);
044: ITextFileBuffer buffer = manager.getTextFileBuffer(file
045: .getFullPath(), LocationKind.NORMALIZE);
046:
047: IDocument document = buffer.getDocument();
048:
049: try {
050: BuildModel model = new BuildModel(document, false);
051: model.load();
052: if (!model.isLoaded())
053: return null;
054: PropertiesTextChangeListener listener = new PropertiesTextChangeListener(
055: document);
056: model.addModelChangedListener(listener);
057:
058: IBuild build = model.getBuild();
059: IBuildEntry[] entries = build.getBuildEntries();
060: for (int i = 0; i < affectedElements.length; i++) {
061: if (affectedElements[i] instanceof IJavaElement)
062: continue;
063: IResource res = (IResource) affectedElements[i];
064: // if resource instanceof IProject, then the project is being renamed and there is no action to do in the build.properties for the resource// if resource instanceof IProject, then the project is being renamed and there is no action to do in the build.properties for the resource
065: if (res instanceof IProject)
066: continue;
067: for (int j = 0; j < entries.length; j++) {
068: addBuildEntryEdit(entries[j], res, newNames[i]);
069: }
070: }
071:
072: TextEdit[] operations = listener.getTextOperations();
073: if (operations.length > 0) {
074: MoveFromChange change = new MoveFromChange("", file); //$NON-NLS-1$
075: MultiTextEdit edit = new MultiTextEdit();
076: edit.addChildren(operations);
077: change.setEdit(edit);
078: PDEModelUtility.setChangeTextType(change, file);
079: return change;
080: }
081: } catch (CoreException e) {
082: return null;
083: }
084: return null;
085: } finally {
086: manager.disconnect(file.getFullPath(),
087: LocationKind.NORMALIZE, monitor);
088: }
089: }
090:
091: private static void addBuildEntryEdit(IBuildEntry entry,
092: IResource res, String string) {
093: IPath resPath = res.getProjectRelativePath();
094: String[] tokens = entry.getTokens();
095: for (int i = 0; i < tokens.length; i++) {
096: if (resPath.isPrefixOf(new Path(tokens[i]))) {
097: try {
098: entry.renameToken(tokens[i], string
099: .concat(tokens[i].substring(resPath
100: .toString().length())));
101: } catch (CoreException e) {
102: }
103: }
104: }
105: }
106:
107: }
|