|
| 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.ViewManager; |
| 13 | +import com.vp.plugin.action.VPAction; |
| 14 | +import com.vp.plugin.action.VPActionController; |
| 15 | +import com.vp.plugin.diagram.IConnectorUIModel; |
| 16 | +import com.vp.plugin.diagram.IDiagramElement; |
| 17 | +import com.vp.plugin.diagram.IDiagramUIModel; |
| 18 | +import com.vp.plugin.model.IModelElement; |
| 19 | +import com.vp.plugin.model.IRelationship; |
| 20 | + |
| 21 | +import plugins.plantUML.export.ClassDiagramExporter; |
| 22 | +import plugins.plantUML.export.UseCaseDiagramExporter; |
| 23 | + |
| 24 | +public class PlantUMLExportController implements VPActionController { |
| 25 | + |
| 26 | + public void performAction(VPAction action) { |
| 27 | + // Get the view manager and the parent component for the modal dialog |
| 28 | +// ViewManager viewManager = ApplicationManager.instance().getViewManager(); |
| 29 | +// Component parentFrame = viewManager.getRootFrame(); |
| 30 | +// |
| 31 | +// // Popup a file chooser for choosing the output file |
| 32 | +// JFileChooser fileChooser = viewManager.createJFileChooser(); |
| 33 | +// fileChooser.setFileFilter(new FileFilter() { |
| 34 | +// |
| 35 | +// public String getDescription() { |
| 36 | +// return "*.txt, *.puml"; |
| 37 | +// } |
| 38 | +// |
| 39 | +// public boolean accept(File file) { |
| 40 | +// return file.isDirectory() || |
| 41 | +// file.getName().toLowerCase().endsWith(".txt") || |
| 42 | +// file.getName().toLowerCase().endsWith(".puml"); |
| 43 | +// } |
| 44 | +// |
| 45 | +// }); |
| 46 | +// |
| 47 | +// // Show save dialog and get the selected file |
| 48 | +// int userSelection = fileChooser.showSaveDialog(parentFrame); |
| 49 | +// if (userSelection == JFileChooser.APPROVE_OPTION) { |
| 50 | +// File file = fileChooser.getSelectedFile(); |
| 51 | +// String fileName = file.getName().toLowerCase(); |
| 52 | +// |
| 53 | +// // Ensure the file has either .txt or .puml extension |
| 54 | +// if (!fileName.endsWith(".txt") && !fileName.endsWith(".puml")) { |
| 55 | +// // Default to .txt if no extension is provided |
| 56 | +// file = new File(file.getAbsolutePath() + ".txt"); |
| 57 | +// } |
| 58 | +// |
| 59 | +// if (file != null && !file.isDirectory()) { |
| 60 | +// String messageContent = "Test message"; // Fixed test message |
| 61 | +// |
| 62 | +// // Write the test message to the file |
| 63 | +// try (FileWriter writer = new FileWriter(file)) { |
| 64 | +// writer.write(messageContent); |
| 65 | +// viewManager.showMessageDialog(parentFrame, "Diagram exported to " + file.getAbsolutePath()); |
| 66 | +// } catch (IOException e) { |
| 67 | +// viewManager.showMessageDialog(parentFrame, "Error writing to file: " + e.getMessage()); |
| 68 | +// } |
| 69 | +// } |
| 70 | +// } |
| 71 | + |
| 72 | + IDiagramUIModel activeDiagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram(); |
| 73 | + String diagramType = activeDiagram.getType(); |
| 74 | + |
| 75 | + switch(diagramType) { |
| 76 | + case "ClassDiagram": |
| 77 | + ClassDiagramExporter cde = new ClassDiagramExporter(); |
| 78 | + cde.extract(activeDiagram); |
| 79 | + break; |
| 80 | + case "UseCaseDiagram": |
| 81 | + UseCaseDiagramExporter ucde = new UseCaseDiagramExporter(); |
| 82 | + ucde.extract(activeDiagram); |
| 83 | + break; |
| 84 | + default: |
| 85 | + ApplicationManager.instance().getViewManager().showMessage("TYPE " + diagramType + " not yet implemented"); |
| 86 | + IDiagramElement[] allElements = activeDiagram.toDiagramElementArray(); |
| 87 | + |
| 88 | + for (IDiagramElement diagramElement : allElements){ |
| 89 | + ApplicationManager.instance().getViewManager().showMessage("NAME:"); |
| 90 | + ApplicationManager.instance().getViewManager().showMessage(diagramElement.getModelElement().getName()); |
| 91 | + ApplicationManager.instance().getViewManager().showMessage("DESCRIPTION:"); |
| 92 | + ApplicationManager.instance().getViewManager().showMessage(diagramElement.getModelElement().getDescription()); // getDocumentation is deprecated |
| 93 | + ApplicationManager.instance().getViewManager().showMessage("TYPE:"); |
| 94 | + ApplicationManager.instance().getViewManager().showMessage(diagramElement.getModelElement().getModelType()); |
| 95 | + } |
| 96 | + IDiagramElement[] connectors = activeDiagram.toDiagramElementArray(); |
| 97 | + for (IDiagramElement connector : connectors) { |
| 98 | + IModelElement connectorModel = connector.getModelElement(); |
| 99 | + if (connectorModel instanceof IRelationship) { |
| 100 | + |
| 101 | + IRelationship relationship = (IRelationship) connectorModel; |
| 102 | + String id = relationship.getId(); |
| 103 | + IModelElement source = relationship.getFrom(); |
| 104 | + IModelElement target = relationship.getTo(); |
| 105 | + |
| 106 | + String sourceAlias = source.getName(); |
| 107 | + String targetAlias = target.getName(); |
| 108 | + |
| 109 | + IConnectorUIModel connection = (IConnectorUIModel) connector; |
| 110 | + ApplicationManager.instance().getViewManager().showMessage("connector of type " + connector.getModelElement().getModelType() + " from: " + sourceAlias + " to " + targetAlias); |
| 111 | + ApplicationManager.instance().getViewManager().showMessage(connector.getModelElement().getModelType()); |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + public void update(VPAction action) { |
| 122 | + // No update behavior needed for this action |
| 123 | + } |
| 124 | +} |
0 commit comments