From 06a6a0704f16f6b624e92ac05738e6729c0bbfb9 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Sat, 27 Feb 2021 20:32:00 +0100 Subject: [PATCH] changed all dialogs and popups to use diagramPanel as a parent This makes them spawn at the center of the diagramPanel, instead of the center of the primary display of the computer --- .../erme/control/actions/AboutAction.java | 7 +++++- .../erme/control/actions/ExitAction.java | 8 ++++++- .../control/actions/ExportToImageAction.java | 8 ++++--- .../actions/HtmlDocumentViewerAction.java | 12 +++++++--- .../erme/control/actions/LoadAction.java | 2 +- .../erme/control/actions/SaveAction.java | 5 +++- .../actions/edits/AddAttributeAction.java | 23 +++++++++++-------- .../actions/edits/AddRelationAction.java | 3 +-- .../actions/edits/RemoveAttributeAction.java | 7 ++++-- .../actions/edits/RemoveRelationAction.java | 5 +++- .../andrewlalis/erme/view/DiagramPanel.java | 9 ++++++++ 11 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/AboutAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/AboutAction.java index 333023f..87fbce5 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/AboutAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/AboutAction.java @@ -1,6 +1,8 @@ package nl.andrewlalis.erme.control.actions; +import lombok.Setter; import nl.andrewlalis.erme.EntityRelationMappingEditor; +import nl.andrewlalis.erme.view.DiagramPanel; import javax.swing.*; import java.awt.*; @@ -14,6 +16,9 @@ public class AboutAction extends AbstractAction { return instance; } + @Setter + private DiagramPanel diagramPanel; + public AboutAction() { super("About"); this.putValue(SHORT_DESCRIPTION, "Show some information about this program."); @@ -22,7 +27,7 @@ public class AboutAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "Entity-Relation Mapping Editor\n" + "by Andrew Lalis\n" + "Version " + EntityRelationMappingEditor.VERSION + "\n" + diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExitAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExitAction.java index fdeb888..42ecead 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExitAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExitAction.java @@ -1,5 +1,8 @@ package nl.andrewlalis.erme.control.actions; +import lombok.Setter; +import nl.andrewlalis.erme.view.DiagramPanel; + import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -16,6 +19,9 @@ public class ExitAction extends AbstractAction { return instance; } + @Setter + private DiagramPanel diagramPanel; + public ExitAction() { super("Exit"); this.putValue(Action.SHORT_DESCRIPTION, "Exit the program."); @@ -25,7 +31,7 @@ public class ExitAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { int choice = JOptionPane.showConfirmDialog( - (Component) e.getSource(), + this.diagramPanel, "Are you sure you want to quit?\nAll unsaved data will be lost.", "Confirm Exit", JOptionPane.OK_CANCEL_OPTION, diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java index a040a3d..9531830 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/ExportToImageAction.java @@ -34,6 +34,8 @@ public class ExportToImageAction extends AbstractAction { @Setter private MappingModel model; + @Setter + private DiagramPanel diagramPanel; public ExportToImageAction() { super("Export to Image"); @@ -45,7 +47,7 @@ public class ExportToImageAction extends AbstractAction { public void actionPerformed(ActionEvent e) { if (this.model.getRelations().isEmpty()) { JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "Model is empty. Add some relations before exporting to an image.", "Model Empty", JOptionPane.WARNING_MESSAGE @@ -61,7 +63,7 @@ public class ExportToImageAction extends AbstractAction { if (path != null) { fileChooser.setSelectedFile(new File(path)); } - int choice = fileChooser.showSaveDialog((Component) e.getSource()); + int choice = fileChooser.showSaveDialog(this.diagramPanel); if (choice == JFileChooser.APPROVE_OPTION) { File chosenFile = fileChooser.getSelectedFile(); if (chosenFile == null || chosenFile.isDirectory()) { @@ -82,7 +84,7 @@ public class ExportToImageAction extends AbstractAction { ImageIO.write(render, extension, chosenFile); prefs.put(LAST_EXPORT_LOCATION_KEY, chosenFile.getAbsolutePath()); JOptionPane.showMessageDialog( - fileChooser, + this.diagramPanel, "Image export completed in " + String.format("%.4f", durationSeconds) + " seconds.\n" + "Resolution: " + render.getWidth() + "x" + render.getHeight(), "Image Export Complete", diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/HtmlDocumentViewerAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/HtmlDocumentViewerAction.java index da5ad90..b23dcf9 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/HtmlDocumentViewerAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/HtmlDocumentViewerAction.java @@ -1,5 +1,8 @@ package nl.andrewlalis.erme.control.actions; +import lombok.Setter; +import nl.andrewlalis.erme.view.DiagramPanel; + import javax.swing.*; import javax.swing.event.HyperlinkEvent; import java.awt.*; @@ -24,10 +27,13 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction { this.modalityType = modalityType; } + @Setter + private DiagramPanel diagramPanel; + @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog( - SwingUtilities.getWindowAncestor((Component) e.getSource()), + SwingUtilities.getWindowAncestor(this.diagramPanel), (String) this.getValue(NAME), this.modalityType ); @@ -54,7 +60,7 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction { } catch (IOException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "An error occured:\n" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE @@ -68,7 +74,7 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction { dialog.setMaximumSize(new Dimension(600, 800)); dialog.setPreferredSize(new Dimension(600, 800)); dialog.pack(); - dialog.setLocationRelativeTo(null); + dialog.setLocationRelativeTo(this.diagramPanel); dialog.setVisible(true); } diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/LoadAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/LoadAction.java index 5cf2859..1e85b14 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/LoadAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/LoadAction.java @@ -49,7 +49,7 @@ public class LoadAction extends AbstractAction { if (path != null) { fileChooser.setSelectedFile(new File(path)); } - int choice = fileChooser.showOpenDialog((Component) e.getSource()); + int choice = fileChooser.showOpenDialog(this.diagramPanel); if (choice == JFileChooser.APPROVE_OPTION) { File chosenFile = fileChooser.getSelectedFile(); if (chosenFile == null || chosenFile.isDirectory() || !chosenFile.exists() || !chosenFile.canRead()) { diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/SaveAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/SaveAction.java index 9bd0d44..2310d55 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/SaveAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/SaveAction.java @@ -2,6 +2,7 @@ package nl.andrewlalis.erme.control.actions; import lombok.Setter; import nl.andrewlalis.erme.model.MappingModel; +import nl.andrewlalis.erme.view.DiagramPanel; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; @@ -28,6 +29,8 @@ public class SaveAction extends AbstractAction { @Setter private MappingModel model; + @Setter + private DiagramPanel diagramPanel; public SaveAction() { super("Save"); @@ -48,7 +51,7 @@ public class SaveAction extends AbstractAction { if (path != null) { fileChooser.setSelectedFile(new File(path)); } - int choice = fileChooser.showSaveDialog((Component) e.getSource()); + int choice = fileChooser.showSaveDialog(this.diagramPanel); if (choice == JFileChooser.APPROVE_OPTION) { File chosenFile = fileChooser.getSelectedFile(); if (chosenFile == null || chosenFile.isDirectory()) { diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddAttributeAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddAttributeAction.java index 59ee032..df59ec9 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddAttributeAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddAttributeAction.java @@ -2,6 +2,7 @@ package nl.andrewlalis.erme.control.actions.edits; import lombok.Setter; import nl.andrewlalis.erme.model.*; +import nl.andrewlalis.erme.view.DiagramPanel; import javax.swing.*; import java.awt.*; @@ -24,6 +25,8 @@ public class AddAttributeAction extends AbstractAction { @Setter private MappingModel model; + @Setter + private DiagramPanel diagramPanel; public AddAttributeAction() { super("Add Attribute"); @@ -36,7 +39,7 @@ public class AddAttributeAction extends AbstractAction { List selectedRelations = this.model.getSelectedRelations(); if (selectedRelations.size() != 1) { JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "A single relation must be selected to add an attribute.", "Single Relation Required", JOptionPane.WARNING_MESSAGE @@ -45,11 +48,11 @@ public class AddAttributeAction extends AbstractAction { } Relation r = selectedRelations.get(0); Attribute createdAttribute; - Component c = (Component) e.getSource(); - String name = JOptionPane.showInputDialog(c, "Enter the name of the attribute.", "Attribute Name", JOptionPane.PLAIN_MESSAGE); + Component source = this.diagramPanel; + String name = JOptionPane.showInputDialog(source, "Enter the name of the attribute.", "Attribute Name", JOptionPane.PLAIN_MESSAGE); if (name == null) return; Integer index = (Integer) JOptionPane.showInputDialog( - c, + source, "Select the index to insert this attribute at.", "Attribute Index", JOptionPane.PLAIN_MESSAGE, @@ -59,7 +62,7 @@ public class AddAttributeAction extends AbstractAction { ); if (index == null) return; AttributeType type = (AttributeType) JOptionPane.showInputDialog( - c, + source, "Select the type this attribute is.", "Attribute Type", JOptionPane.PLAIN_MESSAGE, @@ -69,7 +72,7 @@ public class AddAttributeAction extends AbstractAction { ); if (type == null) return; boolean shouldUseForeignKey = ((String) JOptionPane.showInputDialog( - c, + source, "Is this attribute a foreign key?", "Foreign Key", JOptionPane.PLAIN_MESSAGE, @@ -79,11 +82,11 @@ public class AddAttributeAction extends AbstractAction { )).equalsIgnoreCase("yes"); if (shouldUseForeignKey) { if (this.model.getRelations().size() < 2) { - JOptionPane.showMessageDialog(c, "There should be at least 2 relations present in the model.", "Not Enough Relations", JOptionPane.WARNING_MESSAGE); + JOptionPane.showMessageDialog(source, "There should be at least 2 relations present in the model.", "Not Enough Relations", JOptionPane.WARNING_MESSAGE); return; } Relation fkRelation = (Relation) JOptionPane.showInputDialog( - c, + source, "Select the relation that this foreign key references.", "Foreign Key Relation Reference", JOptionPane.PLAIN_MESSAGE, @@ -94,11 +97,11 @@ public class AddAttributeAction extends AbstractAction { if (fkRelation == null) return; List eligibleAttributes = fkRelation.getAttributes(); if (eligibleAttributes.isEmpty()) { - JOptionPane.showMessageDialog(c, "There are no referencable attributes in the selected relation.", "No Referencable Attributes", JOptionPane.WARNING_MESSAGE); + JOptionPane.showMessageDialog(source, "There are no referencable attributes in the selected relation.", "No Referencable Attributes", JOptionPane.WARNING_MESSAGE); return; } Attribute fkAttribute = (Attribute) JOptionPane.showInputDialog( - c, + source, "Select the attribute that this foreign key references.", "Foreign Key Attribute Reference", JOptionPane.PLAIN_MESSAGE, diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java index 5dfd813..36563a5 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java @@ -35,9 +35,8 @@ public class AddRelationAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { - Component c = (Component) e.getSource(); String name = JOptionPane.showInputDialog( - c, + this.diagramPanel, "Enter the name of the relation.", "Add Relation", JOptionPane.PLAIN_MESSAGE diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveAttributeAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveAttributeAction.java index 227aff9..d1fbb4b 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveAttributeAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveAttributeAction.java @@ -4,6 +4,7 @@ import lombok.Setter; import nl.andrewlalis.erme.model.Attribute; import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.Relation; +import nl.andrewlalis.erme.view.DiagramPanel; import javax.swing.*; import java.awt.*; @@ -25,6 +26,8 @@ public class RemoveAttributeAction extends AbstractAction { @Setter private MappingModel model; + @Setter + private DiagramPanel diagramPanel; public RemoveAttributeAction() { super("Remove Attribute"); @@ -37,7 +40,7 @@ public class RemoveAttributeAction extends AbstractAction { List selectedRelations = this.model.getSelectedRelations(); if (selectedRelations.size() != 1 || selectedRelations.get(0).getAttributes().isEmpty()) { JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "A single relation with at least one attribute must be selected to remove an attribute.", "Single Relation With Attribute Required", JOptionPane.WARNING_MESSAGE @@ -46,7 +49,7 @@ public class RemoveAttributeAction extends AbstractAction { } Relation r = selectedRelations.get(0); Attribute attribute = (Attribute) JOptionPane.showInputDialog( - (Component) e.getSource(), + this.diagramPanel, "Select the attribute to remove.", "Select Attribute", JOptionPane.PLAIN_MESSAGE, diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveRelationAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveRelationAction.java index 5a24136..321093f 100644 --- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveRelationAction.java +++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/RemoveRelationAction.java @@ -3,6 +3,7 @@ package nl.andrewlalis.erme.control.actions.edits; import lombok.Setter; import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.Relation; +import nl.andrewlalis.erme.view.DiagramPanel; import javax.swing.*; import java.awt.*; @@ -22,6 +23,8 @@ public class RemoveRelationAction extends AbstractAction { @Setter private MappingModel model; + @Setter + private DiagramPanel diagramPanel; public RemoveRelationAction() { super("Remove Relation"); @@ -33,7 +36,7 @@ public class RemoveRelationAction extends AbstractAction { public void actionPerformed(ActionEvent e) { if (this.model.getSelectedRelations().isEmpty()) { JOptionPane.showMessageDialog( - (Component) e.getSource(), + this.diagramPanel, "No relations selected. Select at least one relation to remove.", "No Relations Selected", JOptionPane.WARNING_MESSAGE diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java index 54bc65e..3eb46e2 100644 --- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java +++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java @@ -122,13 +122,22 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { SaveAction.getInstance().setModel(this.model); LoadAction.getInstance().setDiagramPanel(this); ExportToImageAction.getInstance().setModel(this.model); + ExportToImageAction.getInstance().setDiagramPanel(this); AddRelationAction.getInstance().setModel(this.model); AddRelationAction.getInstance().setDiagramPanel(this); RemoveRelationAction.getInstance().setModel(this.model); + RemoveRelationAction.getInstance().setDiagramPanel(this); AddAttributeAction.getInstance().setModel(this.model); + AddAttributeAction.getInstance().setDiagramPanel(this); RemoveAttributeAction.getInstance().setModel(this.model); + RemoveAttributeAction.getInstance().setDiagramPanel(this); LoadSampleModelAction.getInstance().setDiagramPanel(this); LolcatAction.getInstance().setDiagramPanel(this); + AboutAction.getInstance().setDiagramPanel(this); + ExitAction.getInstance().setDiagramPanel(this); + InstructionsAction.getInstance().setDiagramPanel(this); + MappingAlgorithmHelpAction.getInstance().setDiagramPanel(this); + SaveAction.getInstance().setDiagramPanel(this); } public static void prepareGraphics(Graphics2D g) {