KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006 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.ITextFileBuffer;
15 import org.eclipse.core.filebuffers.ITextFileBufferManager;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.pde.core.build.IBuild;
27 import org.eclipse.pde.core.build.IBuildEntry;
28 import org.eclipse.pde.internal.core.text.build.BuildModel;
29 import org.eclipse.pde.internal.core.text.build.PropertiesTextChangeListener;
30 import org.eclipse.text.edits.MultiTextEdit;
31 import org.eclipse.text.edits.TextEdit;
32
33 public class BuildPropertiesChange {
34     
35     public static Change createRenameChange(IFile file, Object JavaDoc[] affectedElements, String JavaDoc[] newNames, IProgressMonitor monitor)
36     throws CoreException {
37         ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
38         try {
39             manager.connect(file.getFullPath(), monitor);
40             ITextFileBuffer buffer = manager.getTextFileBuffer(file.getFullPath());
41             
42             IDocument document = buffer.getDocument();
43             
44             try {
45                 BuildModel model = new BuildModel(document,false);
46                 model.load();
47                 if (!model.isLoaded())
48                     return null;
49                 PropertiesTextChangeListener listener = new PropertiesTextChangeListener(document);
50                 model.addModelChangedListener(listener);
51                 
52                 IBuild build = model.getBuild();
53                 IBuildEntry[] entries = build.getBuildEntries();
54                 for (int i = 0; i < affectedElements.length; i++) {
55                     if (affectedElements[i] instanceof IJavaElement)
56                         continue;
57                     IResource res = (IResource)affectedElements[i];
58                     // 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
59
if (res instanceof IProject)
60                         continue;
61                     for (int j = 0; j < entries.length; j++) {
62                         addBuildEntryEdit(entries[j], res, newNames[i]);
63                     }
64                 }
65                 
66                 TextEdit[] operations = listener.getTextOperations();
67                 if (operations.length > 0) {
68                     MoveFromChange change = new MoveFromChange("", file); //$NON-NLS-1$
69
MultiTextEdit edit = new MultiTextEdit();
70                     edit.addChildren(operations);
71                     change.setEdit(edit);
72                     return change;
73                 }
74             } catch (CoreException e) {
75                 return null;
76             }
77             return null;
78         } finally {
79             manager.disconnect(file.getFullPath(), monitor);
80         }
81     }
82
83     private static void addBuildEntryEdit(IBuildEntry entry, IResource res, String JavaDoc string) {
84         IPath resPath = res.getProjectRelativePath();
85         String JavaDoc[] tokens = entry.getTokens();
86         for (int i = 0; i < tokens.length; i++) {
87             if (resPath.isPrefixOf(new Path(tokens[i]))) {
88                 try {
89                     entry.renameToken(tokens[i], string.concat(tokens[i].substring(resPath.toString().length())));
90                 } catch (CoreException e) {
91                 }
92             }
93         }
94     }
95
96 }
97
Popular Tags