|
| 1 | +package plugins.plantUML.actions; |
| 2 | + |
| 3 | +import java.awt.Component; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileWriter; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import javax.swing.JFileChooser; |
| 9 | +import javax.swing.filechooser.FileFilter; |
| 10 | + |
| 11 | +import com.vp.plugin.ApplicationManager; |
| 12 | +import com.vp.plugin.ProjectManager; |
| 13 | +import com.vp.plugin.ViewManager; |
| 14 | +import com.vp.plugin.action.VPAction; |
| 15 | +import com.vp.plugin.action.VPActionController; |
| 16 | +import com.vp.plugin.model.IModelElement; |
| 17 | +import com.vp.plugin.model.IStep; |
| 18 | +import com.vp.plugin.model.IStepContainer; |
| 19 | +import com.vp.plugin.model.IUseCase; |
| 20 | +import com.vp.plugin.model.factory.IModelElementFactory; |
| 21 | + |
| 22 | +public class PlantUMLActionController implements VPActionController{ |
| 23 | + |
| 24 | + public void performAction(VPAction action) { |
| 25 | + // get the view manager and the parent component for modal the dialog. |
| 26 | + ViewManager viewManager = ApplicationManager.instance().getViewManager(); |
| 27 | + Component parentFrame = viewManager.getRootFrame(); |
| 28 | + |
| 29 | + // popup a file chooser for choosing the output file |
| 30 | + JFileChooser fileChooser = viewManager.createJFileChooser(); |
| 31 | + fileChooser.setFileFilter(new FileFilter() { |
| 32 | + |
| 33 | + public String getDescription() { |
| 34 | + return "*.htm"; |
| 35 | + } |
| 36 | + |
| 37 | + public boolean accept(File file) { |
| 38 | + return file.isDirectory() || file.getName().toLowerCase().endsWith(".htm"); |
| 39 | + } |
| 40 | + |
| 41 | + }); |
| 42 | + fileChooser.showSaveDialog(parentFrame); |
| 43 | + File file = fileChooser.getSelectedFile(); |
| 44 | + |
| 45 | + if (file!=null && !file.isDirectory()) { |
| 46 | + String htmlContent = ""; |
| 47 | + String result = ""; |
| 48 | + |
| 49 | + // Retrieve all use cases from project |
| 50 | + ProjectManager projectManager = ApplicationManager.instance().getProjectManager(); |
| 51 | + IModelElement[] models = projectManager.getProject().toModelElementArray(IModelElementFactory.MODEL_TYPE_USE_CASE); |
| 52 | + |
| 53 | + // Retrieve an HTML string of flow of events info from every use case |
| 54 | + for (int i = 0; i < models.length; i++) { |
| 55 | + IModelElement modelElement = models[i]; |
| 56 | + IUseCase useCase = (IUseCase)modelElement; |
| 57 | + htmlContent += generate(useCase); |
| 58 | + } |
| 59 | + |
| 60 | + // write to file |
| 61 | + try { |
| 62 | + FileWriter writer = new FileWriter(fileChooser.getSelectedFile()); |
| 63 | + System.out.println(file.getAbsolutePath()); |
| 64 | + writer.write(htmlContent); |
| 65 | + writer.close(); |
| 66 | + result = "Success! HTML generated to "+file.getAbsolutePath(); |
| 67 | + } catch (IOException e) { |
| 68 | + } |
| 69 | + |
| 70 | + // show the generation result |
| 71 | + viewManager.showMessageDialog(parentFrame, result); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public String generate(IUseCase useCase) { |
| 76 | + String content = ""; |
| 77 | + |
| 78 | + // Retrieve flow of events sets from use case. Each IStepContainer is a set of flow of events |
| 79 | + IStepContainer[] stepContainers = useCase.toStepContainerArray(); |
| 80 | + for (int i = 0; i < stepContainers.length; i++) { |
| 81 | + IStepContainer stepContainer = stepContainers[i]; |
| 82 | + |
| 83 | + // Print the name of use case and flow of events to HTML string |
| 84 | + content += "<table border=\"1\" width=\"500\"><tr><th>" + useCase.getName() + " - " + stepContainer.getName() + "</th></tr>"; |
| 85 | + |
| 86 | + // Print the flow of events content to HTML string |
| 87 | + IStep[] stepArray = stepContainer.toStepArray(); |
| 88 | + for (int j = 0; j < stepArray.length; j++) { |
| 89 | + IStep step = stepArray[j]; |
| 90 | + content += "<tr><td>"+ (j+1) + ". " + step.getName()+"</td></tr>"; |
| 91 | + } |
| 92 | + content += "</table><br>"; |
| 93 | + } |
| 94 | + |
| 95 | + return content; |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + public void update(VPAction action) { |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments