001: /*******************************************************************************
002: * Copyright (c) 2007 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.LocationKind;
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.IWorkspaceRunnable;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021: import org.eclipse.jface.text.BadLocationException;
022: import org.eclipse.ltk.core.refactoring.TextFileChange;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.core.ibundle.IBundle;
025: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
026: import org.eclipse.pde.internal.core.text.bundle.BundleModel;
027: import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader;
028: import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener;
029: import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader;
030: import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
031: import org.eclipse.pde.internal.core.text.bundle.FragmentHostHeader;
032: import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
033: import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader;
034: import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject;
035: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
036: import org.eclipse.text.edits.MalformedTreeException;
037: import org.eclipse.text.edits.MultiTextEdit;
038: import org.eclipse.text.edits.TextEdit;
039:
040: public class CreateHeaderChangeOperation implements IWorkspaceRunnable {
041:
042: private IPluginModelBase fBase;
043: private String fHeaderKey;
044: private String fOldValue;
045: private String fNewValue;
046: private TextFileChange fChange;
047:
048: public CreateHeaderChangeOperation(IPluginModelBase base,
049: String headerKey, String oldValue, String newValue) {
050: fBase = base;
051: fHeaderKey = headerKey;
052: fOldValue = oldValue;
053: fNewValue = newValue;
054: }
055:
056: public void run(IProgressMonitor monitor) throws CoreException {
057: try {
058: IResource res = fBase.getUnderlyingResource();
059: if (res == null)
060: return;
061: IProject proj = res.getProject();
062: IFile file = proj.getFile("META-INF/MANIFEST.MF"); //$NON-NLS-1$
063: if (file.exists())
064: fChange = updateBundleHeader(file, monitor);
065: } finally {
066: monitor.done();
067: }
068: }
069:
070: public TextFileChange getChange() {
071: return fChange;
072: }
073:
074: protected TextFileChange updateBundleHeader(IFile manifest,
075: IProgressMonitor monitor) throws CoreException {
076: monitor.beginTask("", 2); //$NON-NLS-1$
077: try {
078: IBundle bundle = BundleManifestChange.getBundle(manifest,
079: new SubProgressMonitor(monitor, 1));
080: if (bundle != null) {
081: BundleTextChangeListener listener = createListener(bundle);
082: if (listener != null) {
083: IManifestHeader mHeader = bundle
084: .getManifestHeader(fHeaderKey);
085: if (mHeader instanceof BundleSymbolicNameHeader) {
086: ((BundleSymbolicNameHeader) mHeader)
087: .setId(fNewValue);
088: } else if (mHeader instanceof RequireBundleHeader) {
089: RequireBundleHeader header = (RequireBundleHeader) mHeader;
090: RequireBundleObject bundles[] = header
091: .getRequiredBundles();
092: for (int i = 0; i < bundles.length; i++) {
093: if (bundles[i].getId().equals(fOldValue))
094: bundles[i].setId(fNewValue);
095: }
096: } else if (mHeader instanceof FragmentHostHeader) {
097: ((FragmentHostHeader) mHeader)
098: .setHostId(fNewValue);
099: } else if (mHeader instanceof ExportPackageHeader) {
100: ExportPackageObject[] packages = ((ExportPackageHeader) mHeader)
101: .getPackages();
102: for (int i = 0; i < packages.length; i++) {
103: PackageFriend[] friends = packages[i]
104: .getFriends();
105: for (int j = 0; j < friends.length; j++) {
106: if (friends[j].getName().equals(
107: fOldValue)) {
108: packages[i]
109: .removeFriend(friends[j]);
110: packages[i]
111: .addFriend(new PackageFriend(
112: packages[i],
113: fNewValue));
114: }
115: }
116: }
117: }
118:
119: return getTextChange(listener, manifest);
120: }
121: }
122: } catch (MalformedTreeException e) {
123: } catch (CoreException e) {
124: } catch (BadLocationException e) {
125: } finally {
126: FileBuffers.getTextFileBufferManager().disconnect(
127: manifest.getFullPath(), LocationKind.NORMALIZE,
128: new SubProgressMonitor(monitor, 1));
129: monitor.done();
130: }
131: return null;
132: }
133:
134: private BundleTextChangeListener createListener(IBundle bundle) {
135: if (bundle != null) {
136: BundleTextChangeListener listener = new BundleTextChangeListener(
137: ((BundleModel) bundle.getModel()).getDocument());
138: bundle.getModel().addModelChangedListener(listener);
139: return listener;
140: }
141: return null;
142: }
143:
144: private TextFileChange getTextChange(
145: BundleTextChangeListener listener, IFile manifest) {
146: TextEdit[] edits = listener.getTextOperations();
147: if (edits.length == 0)
148: return null;
149: MultiTextEdit edit = new MultiTextEdit();
150: edit.addChildren(edits);
151: TextFileChange change = new TextFileChange("", manifest); //$NON-NLS-1$
152: change.setEdit(edit);
153: PDEModelUtility.setChangeTextType(change, manifest);
154: return change;
155: }
156: }
|