001: /**
002: *
003: */package core;
004:
005: import gui.actions.ActionUtil;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import logging.LoggingPlugin;
012: import model.ModelUtil;
013: import model.PropertyUtil;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.runtime.ILog;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.gef.EditPart;
022: import org.eclipse.jdt.core.ICompilationUnit;
023: import org.eclipse.jdt.core.IJavaElement;
024: import org.eclipse.jdt.core.IJavaProject;
025: import org.eclipse.jdt.core.IPackageFragment;
026: import org.eclipse.jdt.core.IPackageFragmentRoot;
027: import org.eclipse.jdt.core.JavaCore;
028: import org.eclipse.jdt.core.JavaModelException;
029: import org.eclipse.jdt.core.dom.FieldDeclaration;
030: import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
031: import org.eclipse.jdt.internal.core.PackageFragmentRoot;
032: import org.eclipse.jface.dialogs.MessageDialog;
033:
034: import diagram.section.EnvEntry;
035:
036: /**
037: * @author sh
038: *
039: */
040: public class Util {
041:
042: /**
043: * Converts the path of a Class File to the full qualified Java Class File Notation.
044: * For Example (src/org/osbl/process/sh.java) to (org.osbl.process.sh).
045: *
046: * @param path the path to the Class File
047: * @param className the Name of the Class File
048: * @return
049: */
050: public static String implementation(IPath path, String className) {
051: path = path.removeLastSegments(1);
052: path = path.append(className);
053: path = path.removeFirstSegments(1);
054: String implementation = path.toString().replace('/', '.');
055: int ih = implementation.lastIndexOf('.');
056: if (ih > -1)
057: return implementation.substring(0, ih);
058: return null;
059: }
060:
061: /**
062: * get all Compilation Units in the workspace.
063: * Just opened projects are considered.
064: *
065: * @return cuList a list containing all compilation units of the workspace
066: */
067: public static List<ICompilationUnit> getICompilationUnits() {
068: List<IFile> javaFiles = ModelUtil.scanWorkspace("java");
069:
070: List<ICompilationUnit> cuList = new ArrayList<ICompilationUnit>();
071: for (Iterator iterator = javaFiles.iterator(); iterator
072: .hasNext();) {
073: IFile file = (IFile) iterator.next();
074: IProject project = file.getProject();
075:
076: IJavaProject javaProject = JavaCore.create(project);
077: try {
078: IPackageFragmentRoot roots[] = javaProject
079: .getPackageFragmentRoots();
080: for (int i = 0; i < roots.length; i++) {
081: IPackageFragmentRoot packageFragmentRoot = roots[i];
082: if (packageFragmentRoot instanceof JarPackageFragmentRoot)
083: continue;
084:
085: PackageFragmentRoot root = (PackageFragmentRoot) packageFragmentRoot;
086: IJavaElement childs[] = root.getChildren();
087: for (int j = 0; j < childs.length; j++) {
088: IJavaElement javaElement = childs[j];
089: if (javaElement instanceof IPackageFragment) {
090: IPackageFragment frag = (IPackageFragment) javaElement;
091: ICompilationUnit[] units = frag
092: .getCompilationUnits();
093: for (int k = 0; k < units.length; k++) {
094: ICompilationUnit compilationUnit = units[k];
095: cuList.add(compilationUnit);
096: }
097: } else if (javaElement instanceof ICompilationUnit) {
098: cuList.add((ICompilationUnit) javaElement);
099: }
100: }
101: }
102: } catch (JavaModelException e) {
103: String message = e.getMessage();
104: ILog logger = LoggingPlugin.getDefault().getLog();
105: String symName = LoggingPlugin.getDefault().getBundle()
106: .getSymbolicName();
107: logger.log(new Status(IStatus.ERROR, symName, 0,
108: message, e));
109: throw new RuntimeException(e);
110: }
111: }
112: return cuList;
113: }
114:
115: /**
116: * helper method to get the implementation of the
117: * given compilation unit.
118: *
119: * @param unit the compilation unit
120: * @return the implementation string of the compilation unit
121: */
122: public static String getImplementation(ICompilationUnit unit) {
123: String implementation = null;
124: IJavaElement parent = unit.getParent();
125: if (parent instanceof IPackageFragment) {
126: IPackageFragment packageFragment = (IPackageFragment) parent;
127: String packageName = packageFragment.getElementName();
128:
129: implementation = packagename(unit.getElementName());
130: if (packageName != null && !packageName.equals("")) {
131: implementation = packageName + "."
132: + packagename(unit.getElementName());
133: }
134: } else if (parent instanceof IPackageFragmentRoot) {
135: IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) parent;
136: String packageName = packageFragmentRoot.getElementName();
137:
138: implementation = packagename(unit.getElementName());
139: if (packageName != null && !packageName.equals("")) {
140: implementation = packageName + "."
141: + packagename(unit.getElementName());
142: }
143: }
144: return implementation;
145: }
146:
147: /**
148: * Helper method to remove the source code for given environment entry.
149: * The source code means the member variable, setter and getter methods.
150: *
151: * @param editPart the EditPart of the model containing the
152: * environment entry
153: * @param envEntry the environment entry whichs source code should be deleted
154: */
155: public static void removeEnvEntrySource(EditPart editPart,
156: EnvEntry envEntry) {
157: // get the implementation value
158: String implementation = PropertyUtil
159: .getImplementation(editPart);
160: if (implementation == null || implementation.equals(""))
161: return;
162:
163: // get the compilation unit
164: ICompilationUnit target = null;
165: List<ICompilationUnit> units = getICompilationUnits();
166: for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator
167: .hasNext();) {
168: ICompilationUnit unit = (ICompilationUnit) iterator.next();
169: if (getImplementation(unit).equals(implementation)) {
170: // the compilation unit has been found
171: // check if the member variable is available
172: FieldDeclarationParser fieldParser = new FieldDeclarationParser();
173: FieldDeclaration fDecl = fieldParser
174: .parseFieldDeclarations(unit, envEntry);
175:
176: // if the member variable has been found, we set it as target
177: if (fDecl != null)
178: target = unit;
179: }
180: }
181:
182: // return if no source is available
183: if (target == null)
184: return;
185:
186: // ask if source code should be deleted
187: boolean answer = MessageDialog.openQuestion(ActionUtil
188: .getShell(), "Delete source code",
189: "also delete source code ?");
190:
191: // if the user has accepted and a underlying java source file is available
192: // delete the environment entry source
193: if (answer) {
194: RefactorInfo info = new RefactorInfo();
195: info.setUnit(target);
196: info.setNewVarName(envEntry.getName());
197: info.setNewName(envEntry.getType());
198:
199: RemoveFieldDeclaration removeField = new RemoveFieldDeclaration();
200: removeField.removeFieldDeclarations(info);
201: }
202: }
203:
204: /**
205: * Helper method to extract the packagename
206: * of the given full qualified class name.
207: *
208: * @param className the full qualified class name
209: * @return the package of the full qualified class name
210: */
211: public static String packagename(String className) {
212: int i = className.lastIndexOf('.');
213: if (i > -1)
214: return className.substring(0, i);
215: else
216: return null;
217: }
218:
219: /**
220: * Helper method to extract the class name
221: * of the given full qualified class name.
222: *
223: * @param className the full qualified class name
224: * @return the class name of the full qualified name
225: */
226: public static String classname(String className) {
227: int i = className.lastIndexOf('.');
228: if (i > -1)
229: return className.substring(i + 1);
230: else
231: return className;
232: }
233:
234: /**
235: * Helper method to get the class name with the
236: * java extension.
237: *
238: * @param className the class name
239: * @return the class file name
240: */
241: public static String classFileName(String className) {
242: return classname(className) + ".java";
243: }
244:
245: /**
246: * Helper method to get the class path with the '/' notation.
247: *
248: * @param packageName the full qualified class name with '.' notation.
249: * @return
250: */
251: public static String packageStructure(String packageName) {
252: return packagename(packageName).replace('.', '/') + '/';
253: }
254: }
|