Merge pull request #12 from bjornpijnacker/issue-9

Changed all dialogs and popups to use diagramPanel as a parent
This commit is contained in:
Andrew Lalis 2021-02-27 20:36:44 +01:00 committed by GitHub
commit 2ffe085f7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 25 deletions

View File

@ -1,6 +1,8 @@
package nl.andrewlalis.erme.control.actions; package nl.andrewlalis.erme.control.actions;
import lombok.Setter;
import nl.andrewlalis.erme.EntityRelationMappingEditor; import nl.andrewlalis.erme.EntityRelationMappingEditor;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -14,6 +16,9 @@ public class AboutAction extends AbstractAction {
return instance; return instance;
} }
@Setter
private DiagramPanel diagramPanel;
public AboutAction() { public AboutAction() {
super("About"); super("About");
this.putValue(SHORT_DESCRIPTION, "Show some information about this program."); this.putValue(SHORT_DESCRIPTION, "Show some information about this program.");
@ -22,7 +27,7 @@ public class AboutAction extends AbstractAction {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"Entity-Relation Mapping Editor\n" + "Entity-Relation Mapping Editor\n" +
"by Andrew Lalis\n" + "by Andrew Lalis\n" +
"Version " + EntityRelationMappingEditor.VERSION + "\n" + "Version " + EntityRelationMappingEditor.VERSION + "\n" +

View File

@ -1,5 +1,8 @@
package nl.andrewlalis.erme.control.actions; package nl.andrewlalis.erme.control.actions;
import lombok.Setter;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -16,6 +19,9 @@ public class ExitAction extends AbstractAction {
return instance; return instance;
} }
@Setter
private DiagramPanel diagramPanel;
public ExitAction() { public ExitAction() {
super("Exit"); super("Exit");
this.putValue(Action.SHORT_DESCRIPTION, "Exit the program."); this.putValue(Action.SHORT_DESCRIPTION, "Exit the program.");
@ -25,7 +31,7 @@ public class ExitAction extends AbstractAction {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int choice = JOptionPane.showConfirmDialog( int choice = JOptionPane.showConfirmDialog(
(Component) e.getSource(), this.diagramPanel,
"Are you sure you want to quit?\nAll unsaved data will be lost.", "Are you sure you want to quit?\nAll unsaved data will be lost.",
"Confirm Exit", "Confirm Exit",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.OK_CANCEL_OPTION,

View File

@ -34,6 +34,8 @@ public class ExportToImageAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public ExportToImageAction() { public ExportToImageAction() {
super("Export to Image"); super("Export to Image");
@ -45,7 +47,7 @@ public class ExportToImageAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (this.model.getRelations().isEmpty()) { if (this.model.getRelations().isEmpty()) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"Model is empty. Add some relations before exporting to an image.", "Model is empty. Add some relations before exporting to an image.",
"Model Empty", "Model Empty",
JOptionPane.WARNING_MESSAGE JOptionPane.WARNING_MESSAGE
@ -61,7 +63,7 @@ public class ExportToImageAction extends AbstractAction {
if (path != null) { if (path != null) {
fileChooser.setSelectedFile(new File(path)); fileChooser.setSelectedFile(new File(path));
} }
int choice = fileChooser.showSaveDialog((Component) e.getSource()); int choice = fileChooser.showSaveDialog(this.diagramPanel);
if (choice == JFileChooser.APPROVE_OPTION) { if (choice == JFileChooser.APPROVE_OPTION) {
File chosenFile = fileChooser.getSelectedFile(); File chosenFile = fileChooser.getSelectedFile();
if (chosenFile == null || chosenFile.isDirectory()) { if (chosenFile == null || chosenFile.isDirectory()) {
@ -82,7 +84,7 @@ public class ExportToImageAction extends AbstractAction {
ImageIO.write(render, extension, chosenFile); ImageIO.write(render, extension, chosenFile);
prefs.put(LAST_EXPORT_LOCATION_KEY, chosenFile.getAbsolutePath()); prefs.put(LAST_EXPORT_LOCATION_KEY, chosenFile.getAbsolutePath());
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
fileChooser, this.diagramPanel,
"Image export completed in " + String.format("%.4f", durationSeconds) + " seconds.\n" + "Image export completed in " + String.format("%.4f", durationSeconds) + " seconds.\n" +
"Resolution: " + render.getWidth() + "x" + render.getHeight(), "Resolution: " + render.getWidth() + "x" + render.getHeight(),
"Image Export Complete", "Image Export Complete",

View File

@ -1,5 +1,8 @@
package nl.andrewlalis.erme.control.actions; package nl.andrewlalis.erme.control.actions;
import lombok.Setter;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkEvent;
import java.awt.*; import java.awt.*;
@ -24,10 +27,13 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction {
this.modalityType = modalityType; this.modalityType = modalityType;
} }
@Setter
private DiagramPanel diagramPanel;
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog( JDialog dialog = new JDialog(
SwingUtilities.getWindowAncestor((Component) e.getSource()), SwingUtilities.getWindowAncestor(this.diagramPanel),
(String) this.getValue(NAME), (String) this.getValue(NAME),
this.modalityType this.modalityType
); );
@ -54,7 +60,7 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"An error occured:\n" + ex.getMessage(), "An error occured:\n" + ex.getMessage(),
"Error", "Error",
JOptionPane.ERROR_MESSAGE JOptionPane.ERROR_MESSAGE
@ -68,7 +74,7 @@ public abstract class HtmlDocumentViewerAction extends AbstractAction {
dialog.setMaximumSize(new Dimension(600, 800)); dialog.setMaximumSize(new Dimension(600, 800));
dialog.setPreferredSize(new Dimension(600, 800)); dialog.setPreferredSize(new Dimension(600, 800));
dialog.pack(); dialog.pack();
dialog.setLocationRelativeTo(null); dialog.setLocationRelativeTo(this.diagramPanel);
dialog.setVisible(true); dialog.setVisible(true);
} }

View File

@ -49,7 +49,7 @@ public class LoadAction extends AbstractAction {
if (path != null) { if (path != null) {
fileChooser.setSelectedFile(new File(path)); fileChooser.setSelectedFile(new File(path));
} }
int choice = fileChooser.showOpenDialog((Component) e.getSource()); int choice = fileChooser.showOpenDialog(this.diagramPanel);
if (choice == JFileChooser.APPROVE_OPTION) { if (choice == JFileChooser.APPROVE_OPTION) {
File chosenFile = fileChooser.getSelectedFile(); File chosenFile = fileChooser.getSelectedFile();
if (chosenFile == null || chosenFile.isDirectory() || !chosenFile.exists() || !chosenFile.canRead()) { if (chosenFile == null || chosenFile.isDirectory() || !chosenFile.exists() || !chosenFile.canRead()) {

View File

@ -2,6 +2,7 @@ package nl.andrewlalis.erme.control.actions;
import lombok.Setter; import lombok.Setter;
import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
@ -28,6 +29,8 @@ public class SaveAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public SaveAction() { public SaveAction() {
super("Save"); super("Save");
@ -48,7 +51,7 @@ public class SaveAction extends AbstractAction {
if (path != null) { if (path != null) {
fileChooser.setSelectedFile(new File(path)); fileChooser.setSelectedFile(new File(path));
} }
int choice = fileChooser.showSaveDialog((Component) e.getSource()); int choice = fileChooser.showSaveDialog(this.diagramPanel);
if (choice == JFileChooser.APPROVE_OPTION) { if (choice == JFileChooser.APPROVE_OPTION) {
File chosenFile = fileChooser.getSelectedFile(); File chosenFile = fileChooser.getSelectedFile();
if (chosenFile == null || chosenFile.isDirectory()) { if (chosenFile == null || chosenFile.isDirectory()) {

View File

@ -2,6 +2,7 @@ package nl.andrewlalis.erme.control.actions.edits;
import lombok.Setter; import lombok.Setter;
import nl.andrewlalis.erme.model.*; import nl.andrewlalis.erme.model.*;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -24,6 +25,8 @@ public class AddAttributeAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public AddAttributeAction() { public AddAttributeAction() {
super("Add Attribute"); super("Add Attribute");
@ -36,7 +39,7 @@ public class AddAttributeAction extends AbstractAction {
List<Relation> selectedRelations = this.model.getSelectedRelations(); List<Relation> selectedRelations = this.model.getSelectedRelations();
if (selectedRelations.size() != 1) { if (selectedRelations.size() != 1) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"A single relation must be selected to add an attribute.", "A single relation must be selected to add an attribute.",
"Single Relation Required", "Single Relation Required",
JOptionPane.WARNING_MESSAGE JOptionPane.WARNING_MESSAGE
@ -45,11 +48,11 @@ public class AddAttributeAction extends AbstractAction {
} }
Relation r = selectedRelations.get(0); Relation r = selectedRelations.get(0);
Attribute createdAttribute; Attribute createdAttribute;
Component c = (Component) e.getSource(); Component source = this.diagramPanel;
String name = JOptionPane.showInputDialog(c, "Enter the name of the attribute.", "Attribute Name", JOptionPane.PLAIN_MESSAGE); String name = JOptionPane.showInputDialog(source, "Enter the name of the attribute.", "Attribute Name", JOptionPane.PLAIN_MESSAGE);
if (name == null) return; if (name == null) return;
Integer index = (Integer) JOptionPane.showInputDialog( Integer index = (Integer) JOptionPane.showInputDialog(
c, source,
"Select the index to insert this attribute at.", "Select the index to insert this attribute at.",
"Attribute Index", "Attribute Index",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,
@ -59,7 +62,7 @@ public class AddAttributeAction extends AbstractAction {
); );
if (index == null) return; if (index == null) return;
AttributeType type = (AttributeType) JOptionPane.showInputDialog( AttributeType type = (AttributeType) JOptionPane.showInputDialog(
c, source,
"Select the type this attribute is.", "Select the type this attribute is.",
"Attribute Type", "Attribute Type",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,
@ -69,7 +72,7 @@ public class AddAttributeAction extends AbstractAction {
); );
if (type == null) return; if (type == null) return;
boolean shouldUseForeignKey = ((String) JOptionPane.showInputDialog( boolean shouldUseForeignKey = ((String) JOptionPane.showInputDialog(
c, source,
"Is this attribute a foreign key?", "Is this attribute a foreign key?",
"Foreign Key", "Foreign Key",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,
@ -79,11 +82,11 @@ public class AddAttributeAction extends AbstractAction {
)).equalsIgnoreCase("yes"); )).equalsIgnoreCase("yes");
if (shouldUseForeignKey) { if (shouldUseForeignKey) {
if (this.model.getRelations().size() < 2) { 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; return;
} }
Relation fkRelation = (Relation) JOptionPane.showInputDialog( Relation fkRelation = (Relation) JOptionPane.showInputDialog(
c, source,
"Select the relation that this foreign key references.", "Select the relation that this foreign key references.",
"Foreign Key Relation Reference", "Foreign Key Relation Reference",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,
@ -94,11 +97,11 @@ public class AddAttributeAction extends AbstractAction {
if (fkRelation == null) return; if (fkRelation == null) return;
List<Attribute> eligibleAttributes = fkRelation.getAttributes(); List<Attribute> eligibleAttributes = fkRelation.getAttributes();
if (eligibleAttributes.isEmpty()) { 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; return;
} }
Attribute fkAttribute = (Attribute) JOptionPane.showInputDialog( Attribute fkAttribute = (Attribute) JOptionPane.showInputDialog(
c, source,
"Select the attribute that this foreign key references.", "Select the attribute that this foreign key references.",
"Foreign Key Attribute Reference", "Foreign Key Attribute Reference",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,

View File

@ -35,9 +35,8 @@ public class AddRelationAction extends AbstractAction {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Component c = (Component) e.getSource();
String name = JOptionPane.showInputDialog( String name = JOptionPane.showInputDialog(
c, this.diagramPanel,
"Enter the name of the relation.", "Enter the name of the relation.",
"Add Relation", "Add Relation",
JOptionPane.PLAIN_MESSAGE JOptionPane.PLAIN_MESSAGE

View File

@ -4,6 +4,7 @@ import lombok.Setter;
import nl.andrewlalis.erme.model.Attribute; import nl.andrewlalis.erme.model.Attribute;
import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.model.Relation;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -25,6 +26,8 @@ public class RemoveAttributeAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public RemoveAttributeAction() { public RemoveAttributeAction() {
super("Remove Attribute"); super("Remove Attribute");
@ -37,7 +40,7 @@ public class RemoveAttributeAction extends AbstractAction {
List<Relation> selectedRelations = this.model.getSelectedRelations(); List<Relation> selectedRelations = this.model.getSelectedRelations();
if (selectedRelations.size() != 1 || selectedRelations.get(0).getAttributes().isEmpty()) { if (selectedRelations.size() != 1 || selectedRelations.get(0).getAttributes().isEmpty()) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"A single relation with at least one attribute must be selected to remove an attribute.", "A single relation with at least one attribute must be selected to remove an attribute.",
"Single Relation With Attribute Required", "Single Relation With Attribute Required",
JOptionPane.WARNING_MESSAGE JOptionPane.WARNING_MESSAGE
@ -46,7 +49,7 @@ public class RemoveAttributeAction extends AbstractAction {
} }
Relation r = selectedRelations.get(0); Relation r = selectedRelations.get(0);
Attribute attribute = (Attribute) JOptionPane.showInputDialog( Attribute attribute = (Attribute) JOptionPane.showInputDialog(
(Component) e.getSource(), this.diagramPanel,
"Select the attribute to remove.", "Select the attribute to remove.",
"Select Attribute", "Select Attribute",
JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE,

View File

@ -3,6 +3,7 @@ package nl.andrewlalis.erme.control.actions.edits;
import lombok.Setter; import lombok.Setter;
import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.model.Relation;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -22,6 +23,8 @@ public class RemoveRelationAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public RemoveRelationAction() { public RemoveRelationAction() {
super("Remove Relation"); super("Remove Relation");
@ -33,7 +36,7 @@ public class RemoveRelationAction extends AbstractAction {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (this.model.getSelectedRelations().isEmpty()) { if (this.model.getSelectedRelations().isEmpty()) {
JOptionPane.showMessageDialog( JOptionPane.showMessageDialog(
(Component) e.getSource(), this.diagramPanel,
"No relations selected. Select at least one relation to remove.", "No relations selected. Select at least one relation to remove.",
"No Relations Selected", "No Relations Selected",
JOptionPane.WARNING_MESSAGE JOptionPane.WARNING_MESSAGE

View File

@ -122,13 +122,22 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
SaveAction.getInstance().setModel(this.model); SaveAction.getInstance().setModel(this.model);
LoadAction.getInstance().setDiagramPanel(this); LoadAction.getInstance().setDiagramPanel(this);
ExportToImageAction.getInstance().setModel(this.model); ExportToImageAction.getInstance().setModel(this.model);
ExportToImageAction.getInstance().setDiagramPanel(this);
AddRelationAction.getInstance().setModel(this.model); AddRelationAction.getInstance().setModel(this.model);
AddRelationAction.getInstance().setDiagramPanel(this); AddRelationAction.getInstance().setDiagramPanel(this);
RemoveRelationAction.getInstance().setModel(this.model); RemoveRelationAction.getInstance().setModel(this.model);
RemoveRelationAction.getInstance().setDiagramPanel(this);
AddAttributeAction.getInstance().setModel(this.model); AddAttributeAction.getInstance().setModel(this.model);
AddAttributeAction.getInstance().setDiagramPanel(this);
RemoveAttributeAction.getInstance().setModel(this.model); RemoveAttributeAction.getInstance().setModel(this.model);
RemoveAttributeAction.getInstance().setDiagramPanel(this);
LoadSampleModelAction.getInstance().setDiagramPanel(this); LoadSampleModelAction.getInstance().setDiagramPanel(this);
LolcatAction.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) { public static void prepareGraphics(Graphics2D g) {