KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > refactoring > CreateHeaderChangeOperation


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.refactoring;
12
13 import org.eclipse.core.filebuffers.FileBuffers;
14 import org.eclipse.core.filebuffers.LocationKind;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRunnable;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.ltk.core.refactoring.TextFileChange;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.ibundle.IBundle;
26 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
27 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
28 import org.eclipse.pde.internal.core.text.bundle.BundleSymbolicNameHeader;
29 import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener;
30 import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader;
31 import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
32 import org.eclipse.pde.internal.core.text.bundle.FragmentHostHeader;
33 import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
34 import org.eclipse.pde.internal.core.text.bundle.RequireBundleHeader;
35 import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject;
36 import org.eclipse.text.edits.MalformedTreeException;
37 import org.eclipse.text.edits.MultiTextEdit;
38 import org.eclipse.text.edits.TextEdit;
39
40 public class CreateHeaderChangeOperation implements IWorkspaceRunnable {
41     
42     private IPluginModelBase fBase;
43     private String JavaDoc fHeaderKey;
44     private String JavaDoc fOldValue;
45     private String JavaDoc fNewValue;
46     private TextFileChange fChange;
47
48     public CreateHeaderChangeOperation(IPluginModelBase base, String JavaDoc headerKey, String JavaDoc oldValue, String JavaDoc newValue ) {
49         fBase = base;
50         fHeaderKey = headerKey;
51         fOldValue = oldValue;
52         fNewValue = newValue;
53     }
54     
55     public void run(IProgressMonitor monitor) throws CoreException {
56         try {
57             IResource res = fBase.getUnderlyingResource();
58             if (res == null)
59                 return;
60             IProject proj = res.getProject();
61             IFile file = proj.getFile("META-INF/MANIFEST.MF"); //$NON-NLS-1$
62
if (file.exists())
63                 fChange = updateBundleHeader(file, monitor);
64         } finally {
65             monitor.done();
66         }
67     }
68     
69     public TextFileChange getChange() {
70         return fChange;
71     }
72     
73     protected TextFileChange updateBundleHeader(IFile manifest, IProgressMonitor monitor) throws CoreException {
74         monitor.beginTask("", 2); //$NON-NLS-1$
75
try {
76             IBundle bundle = BundleManifestChange.getBundle(manifest, new SubProgressMonitor(monitor, 1));
77             if (bundle != null) {
78                 BundleTextChangeListener listener = createListener(bundle);
79                 if (listener != null) {
80                     IManifestHeader mHeader = bundle.getManifestHeader(fHeaderKey);
81                     if (mHeader instanceof BundleSymbolicNameHeader) {
82                         ((BundleSymbolicNameHeader)mHeader).setId(fNewValue);
83                     } else if (mHeader instanceof RequireBundleHeader) {
84                         RequireBundleHeader header = (RequireBundleHeader)mHeader;
85                         RequireBundleObject bundles[] = header.getRequiredBundles();
86                         for (int i = 0; i < bundles.length; i++) {
87                             if (bundles[i].getId().equals(fOldValue))
88                                 bundles[i].setId(fNewValue);
89                         }
90                     } else if (mHeader instanceof FragmentHostHeader) {
91                         ((FragmentHostHeader)mHeader).setHostId(fNewValue);
92                     } else if (mHeader instanceof ExportPackageHeader) {
93                         ExportPackageObject[] packages = ((ExportPackageHeader)mHeader).getPackages();
94                         for (int i = 0; i < packages.length; i++) {
95                             PackageFriend[] friends = packages[i].getFriends();
96                             for (int j = 0; j < friends.length; j++) {
97                                 if (friends[j].getName().equals(fOldValue)) {
98                                     packages[i].removeFriend(friends[j]);
99                                     packages[i].addFriend(new PackageFriend(packages[i], fNewValue));
100                                 }
101                             }
102                         }
103                     }
104                     
105                     return getTextChange(listener, manifest);
106                 }
107             }
108         } catch (MalformedTreeException e) {
109         } catch (CoreException e) {
110         } catch (BadLocationException e) {
111         } finally {
112             FileBuffers.getTextFileBufferManager().disconnect(manifest.getFullPath(), LocationKind.NORMALIZE, new SubProgressMonitor(monitor, 1));
113             monitor.done();
114         }
115         return null;
116     }
117     
118     private BundleTextChangeListener createListener(IBundle bundle) {
119         if (bundle != null) {
120             BundleTextChangeListener listener = new BundleTextChangeListener(((BundleModel)bundle.getModel()).getDocument());
121             bundle.getModel().addModelChangedListener(listener);
122             return listener;
123         }
124         return null;
125     }
126     
127     private TextFileChange getTextChange(BundleTextChangeListener listener, IFile manifest) {
128         TextEdit[] edits = listener.getTextOperations();
129         if (edits.length == 0)
130             return null;
131         MultiTextEdit edit = new MultiTextEdit();
132         edit.addChildren(edits);
133         TextFileChange change = new TextFileChange("", manifest); //$NON-NLS-1$
134
change.setEdit(edit);
135         return change;
136     }
137 }
138
Popular Tags