001: package net.sf.mockcreator.plugin.popup.actions;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.PrintWriter;
005:
006: import net.sf.mockcreator.codegeneration.MockBuilder;
007: import net.sf.mockcreator.codegeneration.MockDescriptor;
008: import net.sf.mockcreator.codegeneration.Saver;
009: import net.sf.mockcreator.plugin.CollectProjectClassPath;
010: import net.sf.mockcreator.plugin.MockCreatorPropertyPage;
011:
012: import org.eclipse.core.resources.IResource;
013: import org.eclipse.core.runtime.NullProgressMonitor;
014: import org.eclipse.jdt.core.IClassFile;
015: import org.eclipse.jdt.core.ICompilationUnit;
016: import org.eclipse.jdt.core.IJavaElement;
017: import org.eclipse.jdt.core.IJavaProject;
018: import org.eclipse.jface.action.IAction;
019: import org.eclipse.jface.dialogs.MessageDialog;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.ui.IObjectActionDelegate;
023: import org.eclipse.ui.IWorkbenchPart;
024:
025: public class MockAction implements IObjectActionDelegate {
026:
027: private IWorkbenchPart part;
028: private ISelection selection;
029:
030: public MockAction() {
031: super ();
032: }
033:
034: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
035: this .part = targetPart;
036: }
037:
038: public void run(IAction action) {
039: if (!(selection instanceof StructuredSelection))
040: return;
041:
042: StructuredSelection ss = (StructuredSelection) selection;
043: Object o = ss.getFirstElement();
044:
045: if (o instanceof IClassFile) {
046: mockJavaElement((IClassFile) o);
047: }
048:
049: if (o instanceof ICompilationUnit) {
050: ICompilationUnit icu = (ICompilationUnit) o;
051: mockJavaElement(icu);
052: }
053: }
054:
055: public void selectionChanged(IAction action, ISelection selection) {
056: this .selection = selection;
057: }
058:
059: private void mockJavaElement(IJavaElement icf) {
060: IJavaElement parent = icf.getParent();
061:
062: String pkg = null;
063: String clz = null;
064:
065: if (parent != null) {
066: pkg = parent.getElementName();
067: } else {
068: pkg = "";
069: }
070:
071: clz = icf.getElementName();
072: int dot = clz.lastIndexOf('.');
073: if (dot >= 0) {
074: clz = clz.substring(0, dot);
075: }
076:
077: String full = (pkg.length() == 0 ? "" : pkg + ".") + clz;
078: mockInProject(icf.getJavaProject(), full);
079: }
080:
081: private void mockInProject(IJavaProject prj, String full) {
082: System.out.println("Mocking: " + full);
083:
084: try {
085: String[] cp = CollectProjectClassPath.collect(prj);
086:
087: MockDescriptor md = new MockDescriptor(full, cp);
088:
089: String outputPackage = MockCreatorPropertyPage
090: .getTargetPackage(prj);
091: System.out.println("cfg op:[" + outputPackage + "]");
092: if (!"*".equals(outputPackage)) {
093: int starIdx = outputPackage.indexOf('*');
094: if (starIdx >= 0) {
095: outputPackage = outputPackage.substring(0, starIdx)
096: + md.getSourcePackageName()
097: + outputPackage.substring(starIdx + 1);
098: }
099: md.setTargetPackageName(outputPackage);
100: }
101:
102: String outputDir = MockCreatorPropertyPage
103: .getOutputDir(prj);
104: System.out.println("cfg od:[" + outputDir + "]");
105: outputDir = prj.getProject().getLocation()
106: .addTrailingSeparator()
107: + outputDir;
108: System.out.println("new op:[" + outputDir + "]");
109: md.setOutputPath(outputDir);
110:
111: String file = MockCreatorPropertyPage.getFile(prj);
112: if (file != null && file.trim().length() != 0) {
113: file = prj.getProject().getLocation()
114: .addTrailingSeparator()
115: + file;
116: try {
117: new FileUpdater().updateFile(file, full);
118: } catch (Exception ex) {
119: String msg = exceptionToMessage(ex);
120: MessageDialog.openError(part.getSite().getShell(),
121: "MockCreator",
122: "failed to update mocked classes file "
123: + file + "\n\n" + msg);
124: }
125: }
126:
127: Saver.saveMock(md, MockBuilder.createMock(md));
128: prj.getCorrespondingResource()
129: .refreshLocal(IResource.DEPTH_INFINITE,
130: new NullProgressMonitor());
131: } catch (Exception e) {
132: String msg = exceptionToMessage(e);
133: MessageDialog.openError(part.getSite().getShell(),
134: "MockCreator", msg);
135: }
136: }
137:
138: private String exceptionToMessage(Exception e) {
139: ByteArrayOutputStream baos = new ByteArrayOutputStream();
140: PrintWriter w = new PrintWriter(baos);
141: e.printStackTrace(w);
142: w.close();
143: String msg = baos.toString();
144: return msg;
145: }
146: }
|