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:
011: /* Generated by Together */
012:
013: package de.uka.ilkd.key.casetool.together.scripts.menuextension;
014:
015: import java.io.File;
016: import java.io.FileWriter;
017: import java.io.IOException;
018: import java.util.Enumeration;
019: import java.util.Vector;
020:
021: import javax.swing.JFileChooser;
022: import javax.swing.JFrame;
023: import javax.swing.JOptionPane;
024:
025: import com.togethersoft.openapi.ide.project.IdeProject;
026: import com.togethersoft.openapi.ide.project.IdeProjectManagerAccess;
027: import com.togethersoft.openapi.ide.window.IdeWindowManager;
028: import com.togethersoft.openapi.rwi.*;
029:
030: import de.uka.ilkd.key.casetool.together.TogetherGFInterface;
031: import de.uka.ilkd.key.casetool.together.TogetherModelClass;
032: import de.uka.ilkd.key.ocl.OCLExport;
033: import de.uka.ilkd.key.ocl.gf.ExportFormatMenu;
034:
035: /** Global KeY Menu interface for exporting all OCL specifications from
036: * a Together project, also includes the option of exporting them in
037: * Natural Language using GF
038: */
039: public class GlobalMenuPoint1_4 implements GlobalMenu {
040:
041: private Vector classes = null;
042: private RwiModel rwiModel = null;
043: private static File lastDirectory = null;
044: private File directory;
045:
046: public String getMenuEntry() {
047: return "Export all OCL specs";
048: }
049:
050: public void run(IdeWindowManager winMan) {
051: classes = new Vector();
052: rwiModel = RwiModelAccess.getModel();
053: // xxx instead of "$model" the constant RwiProperty.MODEL
054: // should be used which however is (erroniously) labeled
055: // deprecated.
056: Enumeration rwiRoots = rwiModel.rootPackages("$model");
057: while (rwiRoots.hasMoreElements()) {
058: RwiPackage rwiPackage = (RwiPackage) rwiRoots.nextElement();
059: processPackage(rwiPackage);
060: }
061: TogetherModelClass[] classArray = new TogetherModelClass[classes
062: .size()];
063: for (int i = 0; i < classArray.length; i++)
064: classArray[i] = (TogetherModelClass) (classes.elementAt(i));
065:
066: if (lastDirectory == null) {
067: IdeProject project = IdeProjectManagerAccess
068: .getProjectManager().getActiveProject();
069: if (project == null) {
070: //no project open
071: JOptionPane.showMessageDialog(new JFrame(),
072: "No project open - " + "Nothing to export!",
073: "Error", JOptionPane.ERROR_MESSAGE);
074: return;
075: }
076: String tprFile = project.getFileName();
077: String projectRoot = tprFile.substring(0, tprFile
078: .lastIndexOf(File.separator));
079: directory = new File(projectRoot);
080: } else {
081: directory = lastDirectory;
082: }
083:
084: JFileChooser jFC = new JFileChooser(directory);
085: // add menu for choosing between OCL and Natural Language output:
086: ExportFormatMenu formatMenu = new ExportFormatMenu();
087: jFC.setAccessory(formatMenu);
088:
089: int saved = jFC.showSaveDialog(new JFrame());
090: if (saved == JFileChooser.APPROVE_OPTION) {
091: int format = formatMenu.getSelection();
092: String chosenName = jFC.getSelectedFile().getName();
093: String outputDir = jFC.getCurrentDirectory().toString();
094:
095: try {
096: if (format == ExportFormatMenu.OCL) {
097: String fullname = outputDir
098: + File.separator
099: + chosenName
100: + (chosenName.endsWith(".ocl") ? ""
101: : ".ocl");
102: File file = new File(fullname);
103: FileWriter output = new FileWriter(file);
104: OCLExport oclExporter = new OCLExport(classArray,
105: output);
106: oclExporter.export();
107: output.close();
108: lastDirectory = jFC.getCurrentDirectory();
109: } else { // Natural Language
110: // first create a temporary OCL file
111: File tempOCL = File.createTempFile(chosenName, "");
112: FileWriter tempOutput = new FileWriter(tempOCL);
113: OCLExport oclExporter = new OCLExport(classArray,
114: tempOutput);
115: oclExporter.export();
116: tempOutput.close();
117:
118: String fullname;
119: String formatString;
120: if (format == ExportFormatMenu.LATEX) {
121: formatString = "latex";
122: fullname = outputDir
123: + File.separator
124: + chosenName
125: + (chosenName.endsWith(".tex") ? ""
126: : ".tex");
127: } else { // default fallback is HTML
128: formatString = "html";
129: fullname = outputDir
130: + File.separator
131: + chosenName
132: + (chosenName.endsWith(".html") ? ""
133: : ".html");
134: }
135: File nl = new File(fullname);
136: TogetherGFInterface gf = new TogetherGFInterface();
137: gf.ocl2nlExport(tempOCL, nl, formatString);
138:
139: tempOCL.delete();
140: lastDirectory = jFC.getCurrentDirectory();
141: }
142: } catch (IOException ioe) {
143: String errorMsg = "Could not export OCL specifications.\n";
144: errorMsg += ioe.toString();
145: JOptionPane.showMessageDialog(new JFrame(), errorMsg,
146: "Oops...", JOptionPane.ERROR_MESSAGE);
147: }
148: }
149: }
150:
151: protected void processPackage(RwiPackage rwiPackage) {
152: // findAssociations(rwiPackage);
153: findClasses(rwiPackage);
154: Enumeration subpackages = rwiPackage.subpackages();
155: while (subpackages.hasMoreElements()) {
156: RwiPackage subpackage = (RwiPackage) subpackages
157: .nextElement();
158: processPackage(subpackage);
159: }
160: }
161:
162: protected void findClasses(RwiPackage rwiPackage) {
163: Enumeration nodes = rwiPackage.nodes();
164: while (nodes.hasMoreElements()) {
165: RwiNode aNode = (RwiNode) nodes.nextElement();
166: if (aNode.getProperty(RwiProperty.SHAPE_TYPE).equals(
167: RwiShapeType.CLASS)) {
168: classes.addElement(new TogetherModelClass(aNode,
169: rwiModel, null));
170: }
171: }
172: }
173:
174: }
|