001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010: package de.uka.ilkd.key.casetool.together.scripts.patternmechanism;
011:
012: import java.io.*;
013: import java.util.LinkedList;
014:
015: import com.togethersoft.openapi.ide.IdeContext;
016: import com.togethersoft.openapi.ide.command.*;
017: import com.togethersoft.openapi.ide.diagram.IdeDiagramManagerAccess;
018: import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
019: import com.togethersoft.openapi.ide.message.IdeMessageType;
020: import com.togethersoft.openapi.rwi.*;
021: import com.togethersoft.openapi.vfs.VirtualFile;
022: import com.togethersoft.openapi.vfs.VirtualFileManagerAccess;
023:
024: import de.uka.ilkd.key.casetool.patternimplementor.PatternMechanism;
025: import de.uka.ilkd.key.casetool.patternimplementor.PatternMechanismUI;
026: import de.uka.ilkd.key.casetool.patternimplementor.SourceCode;
027: import de.uka.ilkd.key.casetool.together.TogetherOCLSimplInterface;
028: import de.uka.ilkd.key.casetool.together.keydebugclassloader.KeyScript;
029:
030: public class KeYPatternMechanism extends KeyScript {
031:
032: private IdeCommandGroup myGroup; // command group
033: private IdeCommandItem myItem2; // command item
034:
035: /**
036: * Runs this module.
037: *
038: * @param context
039: * the IdeContext instance containing the selection information
040: * at the moment the module was called.
041: */
042: public void run1(IdeContext context) {
043: // printing message into message pane
044: IdeMessageManagerAccess.printMessage(
045: IdeMessageType.INFORMATION, "KeY module : started");
046:
047: // menu creation
048: createTheMenu();
049:
050: // printing final message
051: IdeMessageManagerAccess.printMessage(
052: IdeMessageType.INFORMATION, "KeY module: finished");
053: }
054:
055: /**
056: * This method is called while Together is loading.
057: */
058: public void autorun1() {
059: // creation menu in processs of Together loading (startup).
060: createTheMenu();
061: }
062:
063: /**
064: * Creates popup menu (adding menus and command in popup menu)
065: */
066: private void createTheMenu() {
067: // getting IdeCommandManager object
068: IdeCommandManager cman = IdeCommandManagerAccess
069: .getCommandManager();
070:
071: // create a new command group which can be used as a container
072: // for other command items
073: this .myGroup = cman.createGroup("IDOfTheGroup", // identifier
074:
075: // constraint object witch defines constraints for the command
076: // items
077: new IdeCommandConstraints("context = element, "
078: + "shapeType=" + RwiShapeType.CLASS_DIAGRAM
079: + ", " +
080: //"shapetype=" + RwiShapeType.CLASS + ", " +
081: "location=popupMenu"),
082:
083: /*
084: * ... shapeType=Class,
085: * shapeType="+RwiShapeType.CLASS_DIAGRAM+", location...
086: */
087:
088: // Now, the constraints mechanism works so, that it will run
089: // checkStatus method (below) only, if the shapetype of the
090: // element
091: // equals the specified value "Class" ( for classes and
092: // interfaces ).
093: // listener object for receiving command events from items and
094: // groups
095: new IdeCommandCheckListener() {
096:
097: public void checkStatus(IdeCommandEvent event) {
098: // getting the element(s) under cursor
099: // getting context
100: IdeContext context = event.getElementContext();
101:
102: // getting array of selected elements from context
103: RwiElement[] selectedRwiElements = context
104: .getRwiElements();
105:
106: // getting selected element
107: RwiElement theElement = selectedRwiElements[0];
108:
109: // set group text
110: event.getCommandItem().setText(
111: "KeY Design Pattern");
112: }
113: });
114:
115: // create second item
116: // Note: the "placeAfter" parameter, there we specify the ID of the
117: // myItem
118: myItem2 = cman.createItem("IDOfTheItem2",
119: new IdeCommandConstraints(
120: "context = element, parent=IDOfTheGroup" /*
121: * , placeAfter =
122: * IDOfTheItem
123: */
124: + ", location=popupMenu"),
125: new IdeCommandAdapter() {
126:
127: // The item is always visible, so
128: // we use the empty checkStatus method.
129: // the user managed to invoke the command
130: public void actionPerformed(IdeCommandEvent event) {
131: // making message pane visible
132: IdeMessageManagerAccess.getMessageManager()
133: .setPaneVisible(true);
134:
135: // printing message
136: IdeMessageManagerAccess.printMessage(
137: IdeMessageType.INFORMATION,
138: "Debug : Insert Design Pattern");
139:
140: PatternMechanism pm = new PatternMechanism();
141: new PatternMechanismUI(pm);
142:
143: //doSomeWork("Hello");
144: //doSomeWork("Tjohoo");
145: if (pm.getImplementation() != null) {
146: saveSourceCode(pm.getImplementation());
147: }
148: }
149: });
150: myItem2.setText("Insert Design Pattern"); // set item text
151:
152: // making message pane visible
153: IdeMessageManagerAccess.getMessageManager()
154: .setPaneVisible(true);
155:
156: // printing message
157: IdeMessageManagerAccess
158: .printMessage(
159: IdeMessageType.INFORMATION,
160: "KeY module: new popup submenu for classes/interfaces was successfully created.");
161: }
162:
163: public void saveSourceCode(SourceCode sc) {
164: final int numberOfClasses = sc.nofClasses();
165: //Added by Daniel
166: LinkedList newClasses = new LinkedList();
167: //
168: for (int i = 0; i < numberOfClasses; i++) {
169: SourceCode tmp = sc.getClass(i);
170:
171: String codeStr = tmp.toText();
172:
173: //SourceCode$ClassDelimiter::name
174: String filename = tmp.getClassName();
175:
176: //Added by Daniel
177: //filename has the form: "<class_name>.java"
178: newClasses
179: .add(filename.substring(0, filename.indexOf(".")));
180: //
181:
182: /*RwiDiagram activeDiagram;
183: RwiPackage subpackage;
184: RwiNode node;
185: String path;*/
186: String absolutefilename = getCurrentPath(filename);
187:
188: saveToFile(codeStr, absolutefilename);
189: }
190:
191: //Added by Daniel
192: //(new TogetherOCLSimplInterface()).simplifyConstraints(newClasses);
193: //
194: }
195:
196: /**
197: *
198: * getCurrentPath("hello") returns "/path/to/hello.java"
199: * getCurrentPath("world.java") returns "/path/to/world.java"
200: *
201: * @param filename
202: * @return returns a complete path to a file that should be (or are) in the active diagram.
203: */
204: private String getCurrentPath(String filename) {
205: RwiDiagram activeDiagram = IdeDiagramManagerAccess
206: .getDiagramManager().getActiveDiagram().getRwiDiagram();
207:
208: RwiPackage subpackage = activeDiagram.getContainingPackage();
209:
210: RwiNode node;
211:
212: /*
213: * if(subpackage.canCreateNodeByPattern(RwiLanguage.JAVA,
214: * RwiPattern.DEFAULT_CLASS)) { }
215: */
216:
217: // http://i12www.ira.uka.de/~klebanov/doc/together-api/com/togethersoft/openapi/rwi/RwiPackage.html#createNodeByPattern(java.lang.String,%20java.lang.String)
218: node = subpackage.createNodeByPattern(RwiLanguage.JAVA,
219: RwiPattern.DEFAULT_CLASS);
220: node.setProperty(RwiProperty.NAME, filename);
221: String absolutefilename = node.getProperty(RwiProperty.FILE);
222: // node.delete() is safe because:
223: // if the file exists then (the created node has another name)
224: // else (the file didn't exist before, then it's ok to delete since it is empty)
225: node.delete();
226: //----------------------------
227: String path = absolutefilename.substring(0, absolutefilename
228: .lastIndexOf(File.separatorChar) + 1);
229: //String realFilename =
230: // absolutefilename.substring(absolutefilename.lastIndexOf(File.separatorChar)+1,absolutefilename.lastIndexOf('.'));
231: if (filename.indexOf(".java") != -1) {
232: filename = filename.substring(0, filename.indexOf(".java"));
233: }
234: absolutefilename = path + filename + ".java";
235: return absolutefilename;
236: }
237:
238: private void saveToFile(String codeStr, String absolutefilename) {
239: try {
240: File outFile = new File(absolutefilename);
241: /**
242: * If file already exists, place the contents of the existing file
243: * in the end of the new file with '//' before each line.
244: */
245: if (outFile.exists()) {
246:
247: //System.err.println("File exists!!! - se till att all kod
248: // finns kvar");
249: /*
250: * throw new Exception("File " + absolutefilename + " exists!");
251: */
252: BufferedReader in = new BufferedReader(new FileReader(
253: outFile));
254: String oldContents = new String();
255: while (in.ready()) {
256: String tmp = "// " + in.readLine() + "\n";
257: oldContents = oldContents + tmp;
258: }
259: in.close();
260: codeStr = codeStr + "\n" + oldContents;
261: }
262: PrintWriter out = new PrintWriter(new BufferedWriter(
263: new FileWriter(outFile)));
264:
265: out.println(codeStr);
266: out.flush();
267: out.close();
268: } catch (Exception e) {
269: e.printStackTrace();
270: }
271:
272: VirtualFile vf = VirtualFileManagerAccess
273: .getVirtualFileManager().getVirtualFile(
274: absolutefilename);
275: try {
276: VirtualFileManagerAccess.getVirtualFileManager()
277: .externalUpdate(vf, true);
278: } catch (IOException e1) {
279: // TODO Auto-generated catch block
280: e1.printStackTrace();
281: }
282: }
283: }
|