From c8a80c8d63cf146fd69f5217838f50347678e09e Mon Sep 17 00:00:00 2001 From: Viola Date: Sat, 11 Apr 2020 13:29:16 +0100 Subject: [PATCH] Issue31 add progress bar (#44) * apply multiple sterotypes at same time. Only works if all elements in the selection are the same. * if selection has classes and associations selected at the same time validate rule for only the object that had the click. * apply same stereotype for all elements selected * fix update stereotype when selection multiple objects. * when user selects more than one object do not apply constraints * added new Fixed menu - now suggests stereotypes only when 1 class is selected or all selected classes have the same super class. * fixed action behavior in context menu of the model explorer * fixed context menu when selecting stereotypes from model explorer * progressBar first version * progress bar when calling diagram verification * progresspanel - WIP * fixes - wip * progresbar with threads. Now user can cancel the request. * fixed bug in case of exception or response null * added message when user cancel the request. * added progress bar to export to json after user selects the filename and file directory * first close the progess bar then show the dialog * added progress bar when exporting to gufo * removed duplcated code. * bug fixed when selecting an invalid endpoint * bug fixed when handling model verification response * added progress dialog to the functions that connects to the server. Improved the way the progress dialog works. * improved in how the loading box closes. * Fixed null pointer when canceling the model export Co-authored-by: Victor Viola Co-authored-by: Tiago Prince Sales --- .../DiagramVerificationAction.java | 95 +++++-- .../vp/controllers/ExportToGUFOAction.java | 107 ++++++-- .../vp/controllers/ModelExportAction.java | 163 +++++++----- .../controllers/ModelVerificationAction.java | 103 +++++-- .../ontouml/vp/utils/OntoUMLServerUtils.java | 73 ++--- .../inf/ontouml/vp/utils/ServerRequest.java | 18 ++ .../unibz/inf/ontouml/vp/utils/ViewUtils.java | 251 ++++++++++-------- .../inf/ontouml/vp/views/ProgressPanel.java | 62 +++++ 8 files changed, 604 insertions(+), 268 deletions(-) create mode 100644 src/main/java/it/unibz/inf/ontouml/vp/utils/ServerRequest.java create mode 100644 src/main/java/it/unibz/inf/ontouml/vp/views/ProgressPanel.java diff --git a/src/main/java/it/unibz/inf/ontouml/vp/controllers/DiagramVerificationAction.java b/src/main/java/it/unibz/inf/ontouml/vp/controllers/DiagramVerificationAction.java index f30a3f24..dd14de17 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/controllers/DiagramVerificationAction.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/controllers/DiagramVerificationAction.java @@ -1,17 +1,20 @@ package it.unibz.inf.ontouml.vp.controllers; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.awt.Component; import com.vp.plugin.ApplicationManager; import com.vp.plugin.action.VPAction; import com.vp.plugin.action.VPActionController; import com.vp.plugin.diagram.IClassDiagramUIModel; import com.vp.plugin.diagram.IDiagramUIModel; +import com.vp.plugin.view.IDialog; +import com.vp.plugin.view.IDialogHandler; import it.unibz.inf.ontouml.vp.model.ModelElement; import it.unibz.inf.ontouml.vp.utils.OntoUMLServerUtils; +import it.unibz.inf.ontouml.vp.utils.ServerRequest; import it.unibz.inf.ontouml.vp.utils.ViewUtils; +import it.unibz.inf.ontouml.vp.views.ProgressPanel; /** * Implementation of toolbar button action responsible for performing diagram verification. @@ -19,6 +22,12 @@ */ public class DiagramVerificationAction implements VPActionController { + private ProgressPanel progressPanel; + private ProgressDialog loading; + private IDialog mainDialog; + DiagramVerificationRequest request; + Thread thread; + /** * * Performs OntoUML diagram verification. @@ -33,25 +42,14 @@ public void performAction(VPAction action) { ViewUtils.simpleDialog("Diagram Verification", "Please open a diagram before running this command."); return; } - - ExecutorService executor = Executors.newFixedThreadPool(10); - executor.execute(new Runnable() { - @Override - public void run() { - try { - ViewUtils.clearLog(ViewUtils.SCOPE_PLUGIN); - final String response = OntoUMLServerUtils.requestModelVerification(ModelElement.generateModel(true)); + request = new DiagramVerificationRequest(); - if (response != null) { - ViewUtils.logDiagramVerificationResponse(response); - } - } catch (Exception e) { - e.printStackTrace(); - } - } + loading = new ProgressDialog(); + ApplicationManager.instance().getViewManager().showDialog(loading); - }); + Thread thread = new Thread(request); + thread.start(); } /** @@ -72,8 +70,67 @@ private static boolean hasOpenedClassDiagram() { for (IDiagramUIModel diagram : diagramArray) if (diagram instanceof IClassDiagramUIModel && diagram.isOpened()) return true; - + return false; } + protected class ProgressDialog implements IDialogHandler { + + @Override + public Component getComponent() { + progressPanel = new ProgressPanel(request); + return progressPanel; + } + + @Override + public void prepare(IDialog dialog) { + mainDialog = dialog; + mainDialog.setTitle("Verification Service"); + mainDialog.setModal(false); + mainDialog.setResizable(false); + dialog.setSize(progressPanel.getWidth(), progressPanel.getHeight() + 20); + progressPanel.setContainerDialog(mainDialog); + } + + @Override + public void shown() { + } + + @Override + public boolean canClosed() { + mainDialog.close(); + return true; + } + + } + + public class DiagramVerificationRequest extends ServerRequest { + + @Override + public void run() { + while (keepRunning()) { + try { + final String response = OntoUMLServerUtils.requestModelVerification(ModelElement.generateModel(true), loading); + + if (keepRunning()) { + if (response != null) { + mainDialog.close(); + request.doStop(); + ViewUtils.logDiagramVerificationResponse(response); + } else { + loading.canClosed(); + request.doStop(); + } + } else { + loading.canClosed(); + request.doStop(); + ViewUtils.cleanAndShowMessage("Request cancelled by the user."); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } } \ No newline at end of file diff --git a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ExportToGUFOAction.java b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ExportToGUFOAction.java index 96ea4240..a6535182 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ExportToGUFOAction.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ExportToGUFOAction.java @@ -1,23 +1,27 @@ package it.unibz.inf.ontouml.vp.controllers; +import java.awt.Component; import java.awt.FileDialog; import java.awt.Frame; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.stream.Collectors; import com.vp.plugin.ApplicationManager; import com.vp.plugin.action.VPAction; import com.vp.plugin.action.VPActionController; +import com.vp.plugin.view.IDialog; +import com.vp.plugin.view.IDialogHandler; import it.unibz.inf.ontouml.vp.model.ModelElement; import it.unibz.inf.ontouml.vp.utils.Configurations; import it.unibz.inf.ontouml.vp.utils.OntoUMLServerUtils; import it.unibz.inf.ontouml.vp.utils.ProjectConfigurations; +import it.unibz.inf.ontouml.vp.utils.ServerRequest; +import it.unibz.inf.ontouml.vp.utils.ViewUtils; +import it.unibz.inf.ontouml.vp.views.ProgressPanel; /** * Implementation of toolbar button Export to gUFO. @@ -28,33 +32,25 @@ */ public class ExportToGUFOAction implements VPActionController { + private ProgressPanel progressPanel; + private ProgressDialog loading; + private IDialog mainDialog; + ExportToGUFORequest request; + @Override public void performAction(VPAction action) { - ExecutorService executor = Executors.newFixedThreadPool(1); - executor.execute(new Runnable() { + request = new ExportToGUFORequest(); - @Override - public void run() { - try { - final BufferedReader gufo = OntoUMLServerUtils - .transformToGUFO(ModelElement.generateModel(true), - "http://api.ontouml.org/", "turtle", "name"); - if(gufo != null) { - saveFile(gufo); - } - } catch (Exception e) { - // ViewUtils.exportToGUFOIssueDialog("Some error occurred while exporting the model"); - e.printStackTrace(); - } - } + loading = new ProgressDialog(); + ApplicationManager.instance().getViewManager().showDialog(loading); - }); + Thread thread = new Thread(request); + thread.start(); } /** - * Called when the menu containing the button is accessed allowing for action - * manipulation, such as enable/disable or selecting the button. + * Called when the menu containing the button is accessed allowing for action manipulation, such as enable/disable or selecting the button. * * OBS: DOES NOT apply to this class. */ @@ -65,23 +61,20 @@ public void update(VPAction action) { private void saveFile(BufferedReader buffer) throws IOException { final Configurations configs = Configurations.getInstance(); final ProjectConfigurations projectConfigurations = configs.getProjectConfigurations(); - final FileDialog fd = new FileDialog((Frame) ApplicationManager.instance().getViewManager().getRootFrame(), - "Choose destination", FileDialog.SAVE); + final FileDialog fd = new FileDialog((Frame) ApplicationManager.instance().getViewManager().getRootFrame(), "Choose destination", FileDialog.SAVE); String suggestedFolderPath = projectConfigurations.getExportGUFOFolderPath(); String suggestedFileName = projectConfigurations.getExportGUFOFilename(); - if(suggestedFileName.isEmpty()){ + if (suggestedFileName.isEmpty()) { String projectName = ApplicationManager.instance().getProjectManager().getProject().getName(); - suggestedFileName = projectName+".ttl"; + suggestedFileName = projectName + ".ttl"; } fd.setDirectory(suggestedFolderPath); fd.setFile(suggestedFileName); fd.setVisible(true); - - if (fd.getDirectory() != null && fd.getFile() != null) { final String fileDirectory = fd.getDirectory(); final String fileName = !fd.getFile().endsWith(".ttl") ? fd.getFile() + ".ttl" : fd.getFile(); @@ -94,4 +87,64 @@ private void saveFile(BufferedReader buffer) throws IOException { } } + public class ProgressDialog implements IDialogHandler { + + @Override + public Component getComponent() { + progressPanel = new ProgressPanel(request); + return progressPanel; + } + + @Override + public void prepare(IDialog dialog) { + mainDialog = dialog; + mainDialog.setTitle("Export to GUFO"); + mainDialog.setModal(false); + mainDialog.setResizable(false); + dialog.setSize(progressPanel.getWidth(), progressPanel.getHeight() + 20); + progressPanel.setContainerDialog(mainDialog); + } + + @Override + public void shown() { + } + + @Override + public boolean canClosed() { + mainDialog.close(); + return true; + } + } + + public class ExportToGUFORequest extends ServerRequest { + + @Override + public void run() { + while (keepRunning()) { + try { + final BufferedReader gufo = OntoUMLServerUtils.transformToGUFO(ModelElement.generateModel(true), "http://api.ontouml.org/", "turtle", "name", loading); + + if (keepRunning()) { + if (gufo != null) { + saveFile(gufo); + ViewUtils.cleanAndShowMessage("Model exported successfully."); + request.doStop(); + } else { + loading.canClosed(); + request.doStop(); + ViewUtils.cleanAndShowMessage("Unable to transform to GUFO. Please check your model."); + } + } else { + loading.canClosed(); + request.doStop(); + ViewUtils.cleanAndShowMessage("Request cancelled by the user."); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + } \ No newline at end of file diff --git a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelExportAction.java b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelExportAction.java index c28c90f9..03cd00bc 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelExportAction.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelExportAction.java @@ -3,10 +3,13 @@ import com.vp.plugin.ApplicationManager; import com.vp.plugin.action.VPAction; import com.vp.plugin.action.VPActionController; +import com.vp.plugin.view.IDialog; +import com.vp.plugin.view.IDialogHandler; import it.unibz.inf.ontouml.vp.model.ModelElement; import it.unibz.inf.ontouml.vp.utils.Configurations; import it.unibz.inf.ontouml.vp.utils.ProjectConfigurations; import it.unibz.inf.ontouml.vp.utils.ViewUtils; +import it.unibz.inf.ontouml.vp.views.ProgressPanel; import java.awt.*; import java.io.IOException; @@ -14,73 +17,109 @@ import java.nio.file.Paths; /** - * * Implementation toolbar button action responsible for exporting OntoUML model * in JSON (according to OntoUML Schema). - * - * @author Claudenir Fonseca * + * @author Claudenir Fonseca */ public class ModelExportAction implements VPActionController { - /** - * - * Performs model export in JSON format. - * - * @param action - * - */ - @Override - public void performAction(VPAction action) { - final Configurations configs = Configurations.getInstance(); - final ProjectConfigurations projectConfigurations = configs.getProjectConfigurations(); - - FileDialog fd = new FileDialog((Frame) ApplicationManager.instance().getViewManager().getRootFrame(), - "Choose destination", FileDialog.SAVE); - - String suggestedFolderPath = projectConfigurations.getExportFolderPath(); - String suggestedFileName = projectConfigurations.getExportFilename(); - - if(suggestedFileName.isEmpty()){ - String projectName = ApplicationManager.instance().getProjectManager().getProject().getName(); - suggestedFileName = projectName+".json"; - } - - fd.setDirectory(suggestedFolderPath); - fd.setFile(suggestedFileName); - fd.setVisible(true); - - String fileDirectory = fd.getDirectory(); - String fileName = fd.getFile(); - - if(!fileName.endsWith(".json")) - fileName+=".json"; - - if (fileDirectory != null) { - try { - final String jsonModel = ModelElement.generateModel(true); - Files.write(Paths.get(fileDirectory, fileName), jsonModel.getBytes()); - projectConfigurations.setExportFolderPath(fileDirectory); - projectConfigurations.setExportFilename(fileName); - configs.save(); - } catch (IOException e) { - ViewUtils.log( - "Export Failed. Please submit your Visual Paradigm's log and the time of the error our developers.", - ViewUtils.SCOPE_PLUGIN); - e.printStackTrace(); - } - } - - } - - /** - * Called when the menu containing the button is accessed allowing for action - * manipulation, such as enable/disable or selecting the button. - * - * OBS: DOES NOT apply to this class. - */ - @Override - public void update(VPAction action) { - } + private ProgressPanel progressPanel; + private ProgressDialog loading; + private IDialog mainDialog; + + Thread thread; + + /** + * Performs model export in JSON format. + * + * @param action + */ + @Override + public void performAction(VPAction action) { + final Configurations configs = Configurations.getInstance(); + final ProjectConfigurations projectConfigurations = configs.getProjectConfigurations(); + + FileDialog fd = new FileDialog((Frame) ApplicationManager.instance().getViewManager().getRootFrame(), + "Choose destination", FileDialog.SAVE); + + String suggestedFolderPath = projectConfigurations.getExportFolderPath(); + String suggestedFileName = projectConfigurations.getExportFilename(); + + if (suggestedFileName.isEmpty()) { + String projectName = ApplicationManager.instance().getProjectManager().getProject().getName(); + suggestedFileName = projectName + ".json"; + } + + fd.setDirectory(suggestedFolderPath); + fd.setFile(suggestedFileName); + fd.setVisible(true); + + String fileDirectory = fd.getDirectory(); + + if (fileDirectory != null) { + + loading = new ProgressDialog(); + ApplicationManager.instance().getViewManager().showDialog(loading); + + try { + String fileName = fd.getFile(); + + if (!fileName.endsWith(".json")) + fileName += ".json"; + + final String jsonModel = ModelElement.generateModel(true); + Files.write(Paths.get(fileDirectory, fileName), jsonModel.getBytes()); + projectConfigurations.setExportFolderPath(fileDirectory); + projectConfigurations.setExportFilename(fileName); + configs.save(); + ViewUtils.cleanAndShowMessage("Model exported successfully."); + } catch (IOException e) { + ViewUtils.cleanAndShowMessage("Model export failed."); + e.printStackTrace(); + } + + mainDialog.close(); + } + + } + + /** + * Called when the menu containing the button is accessed allowing for action + * manipulation, such as enable/disable or selecting the button. + *

+ * OBS: DOES NOT apply to this class. + */ + @Override + public void update(VPAction action) { + } + + protected class ProgressDialog implements IDialogHandler { + + @Override + public Component getComponent() { + progressPanel = new ProgressPanel("Building model..."); + return progressPanel; + } + + @Override + public void prepare(IDialog dialog) { + mainDialog = dialog; + mainDialog.setTitle("Export to JSON"); + mainDialog.setModal(false); + mainDialog.setResizable(false); + dialog.setSize(progressPanel.getWidth(), progressPanel.getHeight() + 20); + progressPanel.setContainerDialog(mainDialog); + } + + @Override + public void shown() { + } + + @Override + public boolean canClosed() { + return false; + } + } } \ No newline at end of file diff --git a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelVerificationAction.java b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelVerificationAction.java index a7b629ac..07155fb4 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelVerificationAction.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/controllers/ModelVerificationAction.java @@ -1,16 +1,21 @@ package it.unibz.inf.ontouml.vp.controllers; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.awt.Component; + +import com.vp.plugin.ApplicationManager; import com.vp.plugin.action.VPAction; import com.vp.plugin.action.VPActionController; +import com.vp.plugin.view.IDialog; +import com.vp.plugin.view.IDialogHandler; + import it.unibz.inf.ontouml.vp.model.ModelElement; import it.unibz.inf.ontouml.vp.utils.OntoUMLServerUtils; +import it.unibz.inf.ontouml.vp.utils.ServerRequest; import it.unibz.inf.ontouml.vp.utils.ViewUtils; +import it.unibz.inf.ontouml.vp.views.ProgressPanel; /** - * Implementation of toolbar button action responsible for performing model - * verification. + * Implementation of toolbar button action responsible for performing model verification. * * @author Victor Viola * @author Claudenir Fonseca @@ -18,6 +23,11 @@ */ public class ModelVerificationAction implements VPActionController { + private ProgressPanel progressPanel; + private ProgressDialog loading; + private IDialog mainDialog; + ModelVerificationRequest request; + /** * * Performs OntoUML model verification. @@ -28,30 +38,17 @@ public class ModelVerificationAction implements VPActionController { @Override public void performAction(VPAction action) { - ExecutorService executor = Executors.newFixedThreadPool(10); - executor.execute(new Runnable() { + request = new ModelVerificationRequest(); - @Override - public void run() { - try { - ViewUtils.clearLog(ViewUtils.SCOPE_PLUGIN); - final String response = OntoUMLServerUtils - .requestModelVerification(ModelElement.generateModel(true)); - - if(response != null){ - ViewUtils.logVerificationResponse(response); - } - } catch (Exception e) { - e.printStackTrace(); - } - } + loading = new ProgressDialog(); + ApplicationManager.instance().getViewManager().showDialog(loading); - }); + Thread thread = new Thread(request); + thread.start(); } /** - * Called when the menu containing the button is accessed allowing for action - * manipulation, such as enable/disable or selecting the button. + * Called when the menu containing the button is accessed allowing for action manipulation, such as enable/disable or selecting the button. * * OBS: DOES NOT apply to this class. */ @@ -59,4 +56,64 @@ public void run() { public void update(VPAction action) { } + protected class ProgressDialog implements IDialogHandler { + + @Override + public Component getComponent() { + progressPanel = new ProgressPanel(request); + return progressPanel; + } + + @Override + public void prepare(IDialog dialog) { + mainDialog = dialog; + mainDialog.setTitle("Verification Service"); + mainDialog.setModal(false); + mainDialog.setResizable(false); + dialog.setSize(progressPanel.getWidth(), progressPanel.getHeight() + 20); + progressPanel.setContainerDialog(mainDialog); + } + + @Override + public void shown() { + } + + @Override + public boolean canClosed() { + mainDialog.close(); + return true; + } + + } + + public class ModelVerificationRequest extends ServerRequest { + + @Override + public void run() { + while (keepRunning()) { + try { + final String response = OntoUMLServerUtils.requestModelVerification(ModelElement.generateModel(true), loading); + + if (keepRunning()) { + if (response != null) { + loading.canClosed(); + request.doStop(); + ViewUtils.logVerificationResponse(response); + } else { + loading.canClosed(); + request.doStop(); + } + } else { + loading.canClosed(); + request.doStop(); + ViewUtils.cleanAndShowMessage("Request cancelled by the user."); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + } \ No newline at end of file diff --git a/src/main/java/it/unibz/inf/ontouml/vp/utils/OntoUMLServerUtils.java b/src/main/java/it/unibz/inf/ontouml/vp/utils/OntoUMLServerUtils.java index ec6444f3..e51831f7 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/utils/OntoUMLServerUtils.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/utils/OntoUMLServerUtils.java @@ -14,11 +14,11 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.vp.plugin.view.IDialogHandler; /** * - * Class responsible for making requests to the OntoUML Server based on standard - * end points and configured server URL. + * Class responsible for making requests to the OntoUML Server based on standard end points and configured server URL. * * @author Claudenir Fonseca * @author Victor Viola @@ -34,8 +34,7 @@ public class OntoUMLServerUtils { private static final String USER_MESSAGE_UNKNOWN_ERROR_REQUEST = "Error sending model verification to the server."; private static final String USER_MESSAGE_UNKNOWN_ERROR_RESPONSE = "Error receiving model verification response."; - public static BufferedReader transformToGUFO(String model, String baseIRI, String format, String uriFormatBy) - throws Exception { + public static BufferedReader transformToGUFO(String model, String baseIRI, String format, String uriFormatBy, IDialogHandler loading) throws Exception { final JsonObject optionsObj = new JsonObject(); optionsObj.addProperty("baseIRI", baseIRI); optionsObj.addProperty("format", format); @@ -58,41 +57,42 @@ public static BufferedReader transformToGUFO(String model, String baseIRI, Strin url = ProjectConfigurations.DEFAULT_SERVER_URL + TRANSFORM_GUFO_SERVICE_ENDPOINT; } + loading.shown(); + try { final HttpURLConnection request = request(url, body); - final BufferedReader responseReader = request.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST - ? new BufferedReader(new InputStreamReader(request.getInputStream())) + final BufferedReader responseReader = request.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? new BufferedReader(new InputStreamReader(request.getInputStream())) : new BufferedReader(new InputStreamReader(request.getErrorStream())); + + loading.canClosed(); switch (request.getResponseCode()) { case HttpURLConnection.HTTP_OK: - if(!request.getContentType().equals("text/html")) { + if (!request.getContentType().equals("text/html")) { return responseReader; } else { - if(ViewUtils.exportToGUFOIssueDialogWithOption("Server not found.", HttpURLConnection.HTTP_NOT_FOUND)) - return transformToGUFO(model, baseIRI, format, uriFormatBy); + if (ViewUtils.exportToGUFOIssueDialogWithOption("Server not found.", HttpURLConnection.HTTP_NOT_FOUND)) + return transformToGUFO(model, baseIRI, format, uriFormatBy, loading); System.out.println(responseReader.lines().collect(Collectors.joining())); new Exception("Server not found.").printStackTrace(); return null; } case HttpURLConnection.HTTP_BAD_REQUEST: - ViewUtils.exportToGUFOIssueDialog("Unable to transform model due to unexpected error." - + "\nPlease check the model for nay syntactical errors."); + ViewUtils.exportToGUFOIssueDialog("Unable to transform the model due to an unexpected error." + "\nPlease check the model for any syntactical errors."); System.out.println(responseReader.lines().collect(Collectors.joining())); - new Exception("Unable to transform model due to unexpected error." - + "\nPlease check the model for nay syntactical errors.").printStackTrace(); + new Exception("Unable to transform the model due to an unexpected error." + "\nPlease check the model for any syntactical errors.").printStackTrace(); return null; case HttpURLConnection.HTTP_NOT_FOUND: if (ViewUtils.exportToGUFOIssueDialogWithOption("Server not found.", HttpURLConnection.HTTP_NOT_FOUND)) - return transformToGUFO(model, baseIRI, format, uriFormatBy); + return transformToGUFO(model, baseIRI, format, uriFormatBy, loading); System.out.println(responseReader.lines().collect(Collectors.joining())); new Exception("Server not found.").printStackTrace(); return null; case HttpURLConnection.HTTP_INTERNAL_ERROR: if (ViewUtils.exportToGUFOIssueDialogWithOption("Server error.", HttpURLConnection.HTTP_INTERNAL_ERROR)) - return transformToGUFO(model, baseIRI, format, uriFormatBy); + return transformToGUFO(model, baseIRI, format, uriFormatBy, loading); System.out.println(responseReader.lines().collect(Collectors.joining())); new Exception("Server error.").printStackTrace(); @@ -111,8 +111,7 @@ public static BufferedReader transformToGUFO(String model, String baseIRI, Strin return null; } - public static String requestModelVerification(String serializedModel) { - + public static String requestModelVerification(String serializedModel, IDialogHandler loading) { final ProjectConfigurations configurations = Configurations.getInstance().getProjectConfigurations(); final String url; @@ -121,41 +120,46 @@ public static String requestModelVerification(String serializedModel) { } else { url = ProjectConfigurations.DEFAULT_SERVER_URL + VERIFICATION_SERVICE_ENDPOINT; } + + loading.shown(); try { final HttpURLConnection request = request(url, serializedModel); final StringBuilder response = new StringBuilder(); - final BufferedReader reader = request.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST - ? new BufferedReader(new InputStreamReader(request.getInputStream())) - : new BufferedReader(new InputStreamReader(request.getErrorStream())); + final BufferedReader reader = request.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST ? new BufferedReader(new InputStreamReader(request.getInputStream())) : new BufferedReader( + new InputStreamReader(request.getErrorStream())); String line = null; + while ((line = reader.readLine()) != null) { response.append(line.trim()); } + reader.close(); - + + loading.canClosed(); + switch (request.getResponseCode()) { case HttpURLConnection.HTTP_OK: - if(!request.getContentType().equals("text/html")) { + if (!request.getContentType().equals("text/html")) { return response.toString(); } else { - if(ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_NOT_FOUND, HttpURLConnection.HTTP_NOT_FOUND)) - return requestModelVerification(serializedModel); + if (ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_NOT_FOUND, HttpURLConnection.HTTP_NOT_FOUND)) + return requestModelVerification(serializedModel, loading); } case HttpURLConnection.HTTP_BAD_REQUEST: ViewUtils.verificationFailedDialog(USER_MESSAGE_BAD_REQUEST); return null; case HttpURLConnection.HTTP_NOT_FOUND: - if(ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_NOT_FOUND, HttpURLConnection.HTTP_NOT_FOUND)) - return requestModelVerification(serializedModel); - + if (ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_NOT_FOUND, HttpURLConnection.HTTP_NOT_FOUND)) + return requestModelVerification(serializedModel, loading); + return null; case HttpURLConnection.HTTP_INTERNAL_ERROR: - if(ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_INTERNAL_ERROR, HttpURLConnection.HTTP_INTERNAL_ERROR)) - return requestModelVerification(serializedModel); - + if (ViewUtils.verificationFailedDialogWithOption(USER_MESSAGE_INTERNAL_ERROR, HttpURLConnection.HTTP_INTERNAL_ERROR)) + return requestModelVerification(serializedModel, loading); + return null; default: ViewUtils.verificationFailedDialog(USER_MESSAGE_UNKNOWN_ERROR_RESPONSE); @@ -163,12 +167,15 @@ public static String requestModelVerification(String serializedModel) { } } catch (SocketException e) { + loading.canClosed(); ViewUtils.verificationFailedDialog(USER_MESSAGE_NOT_FOUND); e.printStackTrace(); } catch (IOException e) { + loading.canClosed(); ViewUtils.verificationFailedDialog(USER_MESSAGE_UNKNOWN_ERROR_RESPONSE); e.printStackTrace(); } catch (Exception e) { + loading.canClosed(); ViewUtils.verificationFailedDialog(USER_MESSAGE_UNKNOWN_ERROR_REQUEST); e.printStackTrace(); } @@ -179,15 +186,15 @@ public static String requestModelVerification(String serializedModel) { private static HttpURLConnection request(String urlString, String body) throws MalformedURLException, IOException { final URL url = new URL(urlString); final HttpURLConnection request = (HttpURLConnection) url.openConnection(); - + request.setRequestMethod("POST"); request.setRequestProperty("Content-Type", "application/json"); request.setReadTimeout(60000); request.setDoOutput(true); - + final OutputStream requestStream = request.getOutputStream(); final byte[] requestBody = body.getBytes(); - + requestStream.write(requestBody, 0, requestBody.length); requestStream.flush(); requestStream.close(); diff --git a/src/main/java/it/unibz/inf/ontouml/vp/utils/ServerRequest.java b/src/main/java/it/unibz/inf/ontouml/vp/utils/ServerRequest.java new file mode 100644 index 00000000..c2bc3eec --- /dev/null +++ b/src/main/java/it/unibz/inf/ontouml/vp/utils/ServerRequest.java @@ -0,0 +1,18 @@ +package it.unibz.inf.ontouml.vp.utils; + +public class ServerRequest implements Runnable { + + private boolean doStop = false; + + public synchronized void doStop() { + this.doStop = true; + } + + protected synchronized boolean keepRunning() { + return this.doStop == false; + } + + @Override + public void run() {} + +} diff --git a/src/main/java/it/unibz/inf/ontouml/vp/utils/ViewUtils.java b/src/main/java/it/unibz/inf/ontouml/vp/utils/ViewUtils.java index 719ee88d..a8bebff1 100644 --- a/src/main/java/it/unibz/inf/ontouml/vp/utils/ViewUtils.java +++ b/src/main/java/it/unibz/inf/ontouml/vp/utils/ViewUtils.java @@ -1,16 +1,22 @@ package it.unibz.inf.ontouml.vp.utils; +import java.awt.FileDialog; +import java.awt.Frame; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; import java.net.HttpURLConnection; +import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Iterator; +import java.util.stream.Collectors; import javax.swing.ImageIcon; import javax.swing.JList; @@ -18,6 +24,7 @@ import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -35,8 +42,7 @@ /** * - * Class responsible for facilitating display of messages on Visual Paradigm's - * log. + * Class responsible for facilitating display of messages on Visual Paradigm's log. * * @author Claudenir Fonseca * @author Victor Viola @@ -56,7 +62,7 @@ public class ViewUtils { public static final String NAVIGATION_LOGO_FILENAME = "navigation.png"; public static final String MORE_HORIZ_LOGO = "more_horiz"; public static final String MORE_HORIZ_LOGO_FILENAME = "more_horiz.png"; - + public static void log(String message) { ApplicationManager.instance().getViewManager().showMessage(timestamp() + message); } @@ -76,11 +82,23 @@ public static void simpleLog(String message, String scope) { public static void clearLog(String scope) { ApplicationManager.instance().getViewManager().clearMessages(scope); } - + public static void simpleDialog(String title, String message) { ApplicationManager.instance().getViewManager().showConfirmDialog(null, message, title, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } + public static void cleanAndShowMessage(String message) { + + ApplicationManager.instance().getViewManager().removeMessagePaneComponent(OntoUMLPlugin.PLUGIN_ID); + + ArrayList messageList = new ArrayList(); + messageList.add(timestamp() + message); + JList list = new JList<>(messageList.toArray()); + JScrollPane parentContainer = new JScrollPane(list); + ApplicationManager.instance().getViewManager().showMessagePaneComponent(OntoUMLPlugin.PLUGIN_ID, ViewUtils.SCOPE_PLUGIN, parentContainer); + + } + private static String timestamp() { return "[" + (new Timestamp(System.currentTimeMillis())) + "] "; } @@ -90,24 +108,21 @@ public static String getFilePath(String imageName) { final File pluginDir = ApplicationManager.instance().getPluginInfo(OntoUMLPlugin.PLUGIN_ID).getPluginDir(); switch (imageName) { - case SIMPLE_LOGO: - return Paths.get(pluginDir.getAbsolutePath(), "icons", "logo", SIMPLE_LOGO_FILENAME).toFile() - .getAbsolutePath(); - case NAVIGATION_LOGO: - return Paths.get(pluginDir.getAbsolutePath(), "icons", NAVIGATION_LOGO_FILENAME).toFile() - .getAbsolutePath(); - case MORE_HORIZ_LOGO: - return Paths.get(pluginDir.getAbsolutePath(), "icons", MORE_HORIZ_LOGO_FILENAME).toFile() - .getAbsolutePath(); - default: - return null; + case SIMPLE_LOGO: + return Paths.get(pluginDir.getAbsolutePath(), "icons", "logo", SIMPLE_LOGO_FILENAME).toFile().getAbsolutePath(); + case NAVIGATION_LOGO: + return Paths.get(pluginDir.getAbsolutePath(), "icons", NAVIGATION_LOGO_FILENAME).toFile().getAbsolutePath(); + case MORE_HORIZ_LOGO: + return Paths.get(pluginDir.getAbsolutePath(), "icons", MORE_HORIZ_LOGO_FILENAME).toFile().getAbsolutePath(); + default: + return null; } } public static void logDiagramVerificationResponse(String responseMessage) { ArrayList errorList = new ArrayList(); ArrayList idModelElementList = new ArrayList(); - + try { JsonArray response = (JsonArray) new JsonParser().parse(responseMessage).getAsJsonArray(); @@ -115,27 +130,26 @@ public static void logDiagramVerificationResponse(String responseMessage) { final String diagramName = getCurrentClassDiagramName(); verificationDiagramConcludedDialog(errorCount, diagramName); - - if(errorCount==0) + + if (errorCount == 0) errorList.add("No issues were found in diagram \"" + diagramName + "\"."); - for (JsonElement elem : response) { final JsonObject error = elem.getAsJsonObject(); final String id = error.getAsJsonObject("source").get("id").getAsString(); - + if (isElementInCurrentDiagram(id)) { final String errorMessage = error.get("severity").getAsString() + ":" + " " + error.get("title").getAsString() + " " + error.get("description").getAsString(); errorList.add(timestamp() + errorMessage); idModelElementList.add(id); } } - + JList list = new JList<>(errorList.toArray()); ContextMenuListener listener = new ContextMenuListener(idModelElementList, list); list.addMouseListener(listener); list.addMouseMotionListener(listener); - + JScrollPane parentContainer = new JScrollPane(list); ApplicationManager.instance().getViewManager().showMessagePaneComponent(OntoUMLPlugin.PLUGIN_ID, SCOPE_PLUGIN, parentContainer); @@ -143,7 +157,7 @@ public static void logDiagramVerificationResponse(String responseMessage) { verificationServerErrorDialog(responseMessage); } } - + public static void logVerificationResponse(String responseMessage) { ArrayList errorList = new ArrayList(); ArrayList idModelElementList = new ArrayList(); @@ -153,85 +167,85 @@ public static void logVerificationResponse(String responseMessage) { verificationConcludedDialog(errorCount); - if(errorCount==0) + if (errorCount == 0) errorList.add("No issues were found in your project."); - + for (JsonElement elem : response) { final JsonObject error = elem.getAsJsonObject(); final String id = error.getAsJsonObject("source").get("id").getAsString(); - final String errorMessage = error.get("severity").getAsString() + ":" + " " - + error.get("title").getAsString() + " " + error.get("description").getAsString(); + final String errorMessage = error.get("severity").getAsString() + ":" + " " + error.get("title").getAsString() + " " + error.get("description").getAsString(); errorList.add(timestamp() + errorMessage); idModelElementList.add(id); } - + JList list = new JList<>(errorList.toArray()); ContextMenuListener listener = new ContextMenuListener(idModelElementList, list); list.addMouseListener(listener); list.addMouseMotionListener(listener); - + JScrollPane parentContainer = new JScrollPane(list); ApplicationManager.instance().getViewManager().showMessagePaneComponent(OntoUMLPlugin.PLUGIN_ID, SCOPE_PLUGIN, parentContainer); - + } catch (JsonSyntaxException e) { verificationServerErrorDialog(responseMessage); } } public static void verificationServerErrorDialog(String userMessage) { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, userMessage, "Verification Service", - JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager.instance().getViewManager() + .showConfirmDialog(null, userMessage, "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } public static void verificationConcludedDialog(int nIssues) { if (nIssues > 0) { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "Issues found in your project: " + nIssues +".\n" + - "For details, click on the \"Show Message\" icon on the bottom right corner of the app.", - "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, "Issues found in your project: " + nIssues + ".\n" + "For details, click on the \"Show Message\" icon on the bottom right corner of the app.", + "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } else { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "No issues were found in your project.", "Verification Service", - JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, "No issues were found in your project.", "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, + new ImageIcon(getFilePath(SIMPLE_LOGO))); } } - + public static void verificationDiagramConcludedDialog(int nIssues, String diagramName) { if (nIssues > 0) { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "Issues found in diagram \"" + diagramName + "\": " + nIssues + ".\n" + - "For details, click on the \"Show Message\" icon on the bottom right corner of the app.", - "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, + "Issues found in diagram \"" + diagramName + "\": " + nIssues + ".\n" + "For details, click on the \"Show Message\" icon on the bottom right corner of the app.", + "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } else { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "No issues were found in diagram \"" + diagramName + "\".\n" + - "Other issues may still exist in your project.", - "Verification Service", - JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, "No issues were found in diagram \"" + diagramName + "\".\n" + "Other issues may still exist in your project.", "Verification Service", + JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } } public static void verificationFailedDialog(String msg) { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, msg, "Verification Service", - JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager.instance().getViewManager() + .showConfirmDialog(null, msg, "Verification Service", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } public static boolean verificationFailedDialogWithOption(String msg, int httpCode) { final ProjectConfigurations configurations = Configurations.getInstance().getProjectConfigurations(); - if (configurations.isCustomServerEnabled() && (httpCode == HttpURLConnection.HTTP_NOT_FOUND - || httpCode == HttpURLConnection.HTTP_INTERNAL_ERROR)) { + if (configurations.isCustomServerEnabled() && (httpCode == HttpURLConnection.HTTP_NOT_FOUND || httpCode == HttpURLConnection.HTTP_INTERNAL_ERROR)) { - int option = ApplicationManager.instance().getViewManager().showConfirmDialog(null, - msg + "\nDo you want to retry using the default server?", "Verification Service", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + int option = ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, msg + "\nDo you want to retry using the default server?", "Verification Service", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, + new ImageIcon(getFilePath(SIMPLE_LOGO))); if (option == JOptionPane.OK_OPTION) { configurations.setCustomServerEnabled(false); @@ -247,20 +261,19 @@ public static boolean verificationFailedDialogWithOption(String msg, int httpCod } public static void exportToGUFOIssueDialog(String msg) { - ApplicationManager.instance().getViewManager().showConfirmDialog(null, msg, "Export to gUFO", - JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); + ApplicationManager.instance().getViewManager().showConfirmDialog(null, msg, "Export to gUFO", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } public static boolean exportToGUFOIssueDialogWithOption(String msg, int httpCode) { final ProjectConfigurations configurations = Configurations.getInstance().getProjectConfigurations(); - if (configurations.isCustomServerEnabled() && (httpCode == HttpURLConnection.HTTP_NOT_FOUND - || httpCode == HttpURLConnection.HTTP_INTERNAL_ERROR)) { + if (configurations.isCustomServerEnabled() && (httpCode == HttpURLConnection.HTTP_NOT_FOUND || httpCode == HttpURLConnection.HTTP_INTERNAL_ERROR)) { - int option = ApplicationManager.instance().getViewManager().showConfirmDialog(null, - msg + "\nDo you want to retry using the default server?", "Export to gUFO", - JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + int option = ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, msg + "\nDo you want to retry using the default server?", "Export to gUFO", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, + new ImageIcon(getFilePath(SIMPLE_LOGO))); if (option == JOptionPane.OK_OPTION) { configurations.setCustomServerEnabled(false); @@ -276,20 +289,52 @@ public static boolean exportToGUFOIssueDialogWithOption(String msg, int httpCode } public static int smartPaintEnableDialog() { - return ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "Smart Paint is disabled. Do you want to enable this feature?\n" - + "Warning: this feature will affect all diagrams within the project.", - "Smart Paint", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + return ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, "Smart Paint is disabled. Do you want to enable this feature?\n" + "Warning: this feature will affect all diagrams within the project.", "Smart Paint", + JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } public static int smartPaintConfirmationDialog() { - return ApplicationManager.instance().getViewManager().showConfirmDialog(null, - "Warning: this feature will affect all diagrams within the project.\n" + "Do you want to proceed?", - "Smart Paint", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, - new ImageIcon(getFilePath(SIMPLE_LOGO))); + return ApplicationManager + .instance() + .getViewManager() + .showConfirmDialog(null, "Warning: this feature will affect all diagrams within the project.\n" + "Do you want to proceed?", "Smart Paint", JOptionPane.YES_NO_OPTION, + JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getFilePath(SIMPLE_LOGO))); } - + + public static void saveFile(BufferedReader buffer) throws IOException { + final Configurations configs = Configurations.getInstance(); + final ProjectConfigurations projectConfigurations = configs.getProjectConfigurations(); + final FileDialog fd = new FileDialog((Frame) ApplicationManager.instance().getViewManager().getRootFrame(), "Choose destination", FileDialog.SAVE); + + String suggestedFolderPath = projectConfigurations.getExportGUFOFolderPath(); + String suggestedFileName = projectConfigurations.getExportGUFOFilename(); + + if (suggestedFileName.isEmpty()) { + String projectName = ApplicationManager.instance().getProjectManager().getProject().getName(); + suggestedFileName = projectName + ".ttl"; + } + + fd.setDirectory(suggestedFolderPath); + fd.setFile(suggestedFileName); + fd.setVisible(true); + + if (fd.getDirectory() != null && fd.getFile() != null) { + final String fileDirectory = fd.getDirectory(); + final String fileName = !fd.getFile().endsWith(".ttl") ? fd.getFile() + ".ttl" : fd.getFile(); + final String output = buffer.lines().collect(Collectors.joining("\n")); + + Files.write(Paths.get(fileDirectory, fileName), output.getBytes()); + projectConfigurations.setExportGUFOFolderPath(fileDirectory); + projectConfigurations.setExportGUFOFilename(fileName); + configs.save(); + } + + ViewUtils.cleanAndShowMessage("File saved successfuly."); + } + public static String getCurrentClassDiagramName() { final IDiagramUIModel[] diagramArray = ApplicationManager.instance().getProjectManager().getProject().toDiagramArray(); @@ -303,7 +348,7 @@ public static String getCurrentClassDiagramName() { return null; } - + public static String getCurrentClassDiagramId() { final IDiagramUIModel[] diagramArray = ApplicationManager.instance().getProjectManager().getProject().toDiagramArray(); @@ -317,26 +362,25 @@ public static String getCurrentClassDiagramId() { return null; } - + public static IDiagramUIModel getCurrentClassDiagram() { return ApplicationManager.instance().getProjectManager().getProject().getDiagramById(getCurrentClassDiagramId()); } - + public static boolean isElementInCurrentDiagram(String id) { - - if(getCurrentClassDiagram() == null) + + if (getCurrentClassDiagram() == null) return false; - - - for(IDiagramElement element : getCurrentClassDiagram().toDiagramElementArray()){ - if(element.getModelElement().getId().equals(id)) + + for (IDiagramElement element : getCurrentClassDiagram().toDiagramElementArray()) { + if (element.getModelElement().getId().equals(id)) return true; } return false; } - + public static int errorCountInCurrentDiagram(String responseMessage) { int errorCount = 0; @@ -350,10 +394,10 @@ public static int errorCountInCurrentDiagram(String responseMessage) { } catch (JsonSyntaxException e) { return 0; } - + return errorCount; } - + public static boolean isElementInAnyDiagram(String elementId) { final IDiagramUIModel[] diagramArray = ApplicationManager.instance().getProjectManager().getProject().toDiagramArray(); @@ -389,11 +433,11 @@ public static void highlightDiagramElement(String elementId) { IDiagramElement diagramElement = null; // Checks if the active diagram contains the element - if(diagramManager.getActiveDiagram() != null) { + if (diagramManager.getActiveDiagram() != null) { final Iterator iter = diagramManager.getActiveDiagram().diagramElementIterator(); while (iter.hasNext()) { final IDiagramElement current = (IDiagramElement) iter.next(); - if(current.getModelElement().equals(element)){ + if (current.getModelElement().equals(element)) { diagramElement = current; continue; } @@ -401,18 +445,17 @@ public static void highlightDiagramElement(String elementId) { } // In case the active diagram does not contain it, get the master view or the first diagram element for that element - if(diagramElement == null) { - if(element.getMasterView() != null) { + if (diagramElement == null) { + if (element.getMasterView() != null) { diagramElement = element.getMasterView(); - } - else { + } else { final IDiagramElement[] diagramElements = element.getDiagramElements(); diagramElement = diagramElements != null ? diagramElements[0] : null; } } // Highlights the diagram element if it is not null - if(diagramElement != null) { + if (diagramElement != null) { diagramManager.highlight(diagramElement); } } @@ -423,13 +466,13 @@ final class ContextMenu extends JPopupMenu { private JMenuItem takeMeThere; private JMenuItem openSpec; private ActionListener menuListener; - - public ContextMenu(){ - + + public ContextMenu() { + } public ContextMenu(String idModelElement) { - + menuListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -455,8 +498,8 @@ public void actionPerformed(ActionEvent e) { add(takeMeThere); add(openSpec); } - - public void disableItem(String item){ + + public void disableItem(String item) { switch (item) { case "Take me there!": takeMeThere.setEnabled(false); diff --git a/src/main/java/it/unibz/inf/ontouml/vp/views/ProgressPanel.java b/src/main/java/it/unibz/inf/ontouml/vp/views/ProgressPanel.java new file mode 100644 index 00000000..f2349a4c --- /dev/null +++ b/src/main/java/it/unibz/inf/ontouml/vp/views/ProgressPanel.java @@ -0,0 +1,62 @@ +package it.unibz.inf.ontouml.vp.views; + +import it.unibz.inf.ontouml.vp.utils.ServerRequest; +import it.unibz.inf.ontouml.vp.utils.ViewUtils; + +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JProgressBar; + +import com.vp.plugin.view.IDialog; + +public class ProgressPanel extends JPanel { + + private static final long serialVersionUID = 1L; + + private JLabel label; + private JProgressBar progressBar; + private JButton btnCancel; + private IDialog _dialog; + + public ProgressPanel(String text) { + setSize(new Dimension(200, 100)); + + label = new JLabel(); + label.setText(text); + + progressBar = new JProgressBar(); + progressBar.setIndeterminate(true); + + GridBagConstraints constraints = new GridBagConstraints(); + constraints.anchor = GridBagConstraints.CENTER; + + add(label, constraints); + add(progressBar, constraints); + } + + public ProgressPanel(ServerRequest request) { + this("Contacting Server..."); + + btnCancel = new JButton("Cancel"); + btnCancel.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + request.doStop(); + _dialog.close(); + ViewUtils.cleanAndShowMessage("Request cancelled by the user."); + } + }); + + add(btnCancel); + } + + public void setContainerDialog(IDialog dialog) { + this._dialog = dialog; + } +} \ No newline at end of file