001: /**
002: *
003: */package model;
004:
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import logging.LoggingPlugin;
010: import newprocess.Element;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IFolder;
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.ResourcesPlugin;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.ILog;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.gef.EditPart;
022: import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
023: import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart;
024: import org.eclipse.gmf.runtime.notation.Diagram;
025: import org.eclipse.gmf.runtime.notation.Node;
026:
027: /**
028: * @author sh
029: *
030: */
031: public class ModelUtil {
032:
033: /**
034: * Helper method to return the locations of the given files.
035: *
036: * @param files a list of files
037: * @return modelList a list containing the location of all files
038: */
039: private static List<String> getModelFilePaths(List<IFile> files) {
040: List<String> modelList = new ArrayList<String>();
041: for (Iterator<IFile> iterator = files.iterator(); iterator
042: .hasNext();) {
043: IFile file = (IFile) iterator.next();
044: String path = file.getLocation().toString();
045: modelList.add(path);
046: }
047: return modelList;
048: }
049:
050: /**
051: * get all Meta Model Files (*.ecore) in the workspace.
052: * Just opened projects are considered.
053: *
054: * @return modelList a list containing all Meta Model Files of the workspace
055: */
056: public static List<String> getMetaModelFiles() {
057: List<IFile> modelFiles = scanWorkspace("ecore");
058: return getModelFilePaths(modelFiles);
059: }
060:
061: /**
062: * Scans the whole workspace for files with the given file extension.
063: * Just opened projects are considered.
064: *
065: * @param fileExtension the file ending of the wanted files
066: * @return matches a list containing all found files
067: */
068: public static List<IFile> scanWorkspace(String fileExtension) {
069: try {
070: List<IFile> matches = new ArrayList<IFile>();
071:
072: // scan the whole workspace for opened projects
073: IProject[] projects = ResourcesPlugin.getWorkspace()
074: .getRoot().getProjects();
075: for (IProject project : projects) {
076:
077: // just search if if the current project is open
078: if (!project.isAccessible())
079: continue;
080:
081: // get the content of the current project and scan for the wanted files
082: IResource[] members = project.members();
083: for (IResource resource : members) {
084:
085: if (resource instanceof IFile) {
086: if (((IFile) resource).getFileExtension() != null
087: && ((IFile) resource)
088: .getFileExtension().equals(
089: fileExtension)) {
090: matches.add((IFile) resource);
091: }
092: }
093:
094: // scan recursivly all subfolders
095: else if (resource instanceof IFolder)
096: matches.addAll(search(((IFolder) resource),
097: fileExtension));
098: }
099: }
100:
101: return matches;
102: } catch (IllegalStateException e1) {
103: String message = " You are not running under Eclipse but "
104: + "instead under just a standard java application";
105: ILog logger = LoggingPlugin.getDefault().getLog();
106: String symName = LoggingPlugin.getDefault().getBundle()
107: .getSymbolicName();
108: logger.log(new Status(IStatus.ERROR, symName, 0, message,
109: e1));
110: throw new RuntimeException(message, e1);
111: // TODO: finding the Meta Model without running under Eclipse.
112: } catch (CoreException e) {
113: return null;
114: }
115: }
116:
117: /**
118: * Searches for files with the given file extension starting at
119: * the given folder.
120: *
121: * @param folder the folder to start
122: * @param ending the file extension of the file to search
123: * @return matches a list containing all found files
124: */
125: public static List<IFile> search(IFolder folder, String ending) {
126: List<IFile> matches = new ArrayList<IFile>();
127: if (folder != null) {
128: IResource[] members;
129: try {
130: members = folder.members();
131: } catch (CoreException e) {
132: String message = e.getMessage();
133: ILog logger = LoggingPlugin.getDefault().getLog();
134: String symName = LoggingPlugin.getDefault().getBundle()
135: .getSymbolicName();
136: logger.log(new Status(IStatus.ERROR, symName, 0,
137: message, e));
138: throw new RuntimeException(e);
139: }
140:
141: for (int i = 0; i < members.length; i++) {
142: IResource resource = members[i];
143: if (resource instanceof IFile) {
144: if (((IFile) resource).getFileExtension() != null
145: && ((IFile) resource).getFileExtension()
146: .equals(ending)) {
147: matches.add((IFile) resource);
148: }
149: } else if (resource instanceof IFolder) {
150: matches.addAll(search((IFolder) resource, ending));
151: }
152: }
153: }
154: return matches;
155: }
156:
157: /**
158: * get all Model Files (*.concept) in the workspace.
159: * Just opened projects are considered.
160: *
161: * @return modelList a list containing all Model Files of the workspace
162: */
163: public static List<String> getModelFiles() {
164: List<IFile> modelFiles = scanWorkspace("concept");
165: return getModelFilePaths(modelFiles);
166: }
167:
168: /**
169: * get the implementation property of a model element.
170: *
171: * @param editPart the EditPart containing the
172: * implementation property
173: * @return implementation the implementation property
174: */
175: public static String getModelImplProperty(EditPart editPart) {
176: String implementation = null;
177: if (editPart instanceof DiagramEditPart)
178: implementation = ((newprocess.Process) ((Diagram) editPart
179: .getModel()).getElement()).getImplementation();
180: else if (editPart instanceof ShapeNodeEditPart)
181: implementation = ((Element) ((Node) editPart.getModel())
182: .getElement()).getImplementation();
183: return implementation;
184: }
185: }
|