Browse Source

fixes #72: fixed positioning for JOption/JMessagePanes + 1 forgotten popUp

I. Dix 6 years ago
parent
commit
9839805b85
3 changed files with 36 additions and 46 deletions
  1. 15 23
      src/ui/view/AlgorithmMenu.java
  2. 4 4
      src/ui/view/DisplayedInformationPopUp.java
  3. 17 19
      src/ui/view/GUI.java

+ 15 - 23
src/ui/view/AlgorithmMenu.java

@@ -1,6 +1,13 @@
 package ui.view;
 
-import java.awt.Color;
+import api.CpsAlgorithm;
+import ui.controller.Control;
+import ui.model.Model;
+
+import javax.swing.*;
+import javax.tools.JavaCompiler;
+import javax.tools.ToolProvider;
+import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.BufferedReader;
@@ -10,18 +17,6 @@ import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.HashMap;
 
-import javax.swing.JFileChooser;
-import javax.swing.JFrame;
-import javax.swing.JMenu;
-import javax.swing.JMenuItem;
-import javax.swing.JOptionPane;
-import javax.tools.JavaCompiler;
-import javax.tools.ToolProvider;
-
-import api.CpsAlgorithm;
-import ui.controller.Control;
-import ui.model.Model;
-
 /**
  * This Class represents the Menu, where you can select the Algorithms.
  * 
@@ -32,15 +27,13 @@ public class AlgorithmMenu extends JMenu {
 	private static final long serialVersionUID = 1L;
 
 	JMenuItem algoFolderButton = new JMenuItem("Select Algorithm Folder");
-	private JMenu mnSelectAlgorithm = new JMenu("Select Algorithm");
-	private HashMap<String, File> algosHash = new HashMap<>();
-	private JMenuItem noneItem = new JMenuItem("none");
-
 	// root Directory
 	File root = null;
-
 	Model model;
 	Control controller;
+    private JMenu mnSelectAlgorithm = new JMenu("Select Algorithm");
+    private HashMap<String, File> algosHash = new HashMap<>();
+    private JMenuItem noneItem = new JMenuItem("none");
 
 	/**
 	 * Constructor.
@@ -50,8 +43,8 @@ public class AlgorithmMenu extends JMenu {
 	 * @param cont
 	 *            the Controller
 	 */
-	public AlgorithmMenu(Model mod, Control cont) {
-		super();
+    public AlgorithmMenu(Model mod, Control cont, GUI parentGui) {
+        super();
 		// Init Stuff
 		this.model = mod;
 		this.controller = cont;
@@ -67,12 +60,11 @@ public class AlgorithmMenu extends JMenu {
 
 			private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
 				JFileChooser fileChooser = new JFileChooser();
-				JFrame test = new JFrame();
 				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 				fileChooser.setAcceptAllFileFilterUsed(false);
 
-				if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
-					// empty everything and reset the selected algorithm
+                if (fileChooser.showOpenDialog(parentGui.getFrmCyberPhysical()) == JFileChooser.APPROVE_OPTION) {
+                    // empty everything and reset the selected algorithm
 					controller.setAlgorithm(null);
 					mnSelectAlgorithm.removeAll();
 					mnSelectAlgorithm.add(noneItem);

+ 4 - 4
src/ui/view/DisplayedInformationPopUp.java

@@ -30,13 +30,13 @@ public class DisplayedInformationPopUp extends JDialog {
 	 * @param canvas
 	 *            the Canvas
 	 */
-	public DisplayedInformationPopUp(MyCanvas canvas, JPanel update, Control cont) {
-		super((java.awt.Frame) null, true);
+    public DisplayedInformationPopUp(MyCanvas canvas, JPanel update, Control cont, JFrame parentFrame) {
+        super((java.awt.Frame) null, true);
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
 		this.setTitle("Edit Displayed Informations");
 		setBounds(100, 100, 400, 276);
-		setLocationRelativeTo(this.getParent());
-		getContentPane().setLayout(new BorderLayout());
+        setLocationRelativeTo(parentFrame);
+        getContentPane().setLayout(new BorderLayout());
 		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
 		getContentPane().add(contentPanel, BorderLayout.CENTER);
 		contentPanel.setLayout(null);

+ 17 - 19
src/ui/view/GUI.java

@@ -229,7 +229,7 @@ public class GUI implements CategoryListener {
         control.initListener(this);
         controller.setCanvas(canvas);
         model.setConsole(console);
-        algorithmMenu = new AlgorithmMenu(model, control);
+        algorithmMenu = new AlgorithmMenu(model, control, this);
         initialize();
         updateCategories(model.getCategories());
         updCon = new UpdateController(model, controller);
@@ -526,7 +526,7 @@ public class GUI implements CategoryListener {
                     }
                 } catch (HeadlessException | JsonParseException | UnsupportedFlavorException | IOException e1) {
                     JLabel message = new JLabel("The Clipboard information cannot be pasted into Application.");
-                    JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
+                    JOptionPane.showMessageDialog(frmCyberPhysical, message, "", JOptionPane.ERROR_MESSAGE);
                 }
 
             }
@@ -608,7 +608,7 @@ public class GUI implements CategoryListener {
         mnNewMenuEdit.add(mntmFindReplace);
         mntmEditShowedInformation.addActionListener(actionEvent -> {
             try {
-                DisplayedInformationPopUp dialog = new DisplayedInformationPopUp(canvas, contentPane, controller);
+                DisplayedInformationPopUp dialog = new DisplayedInformationPopUp(canvas, contentPane, controller, frmCyberPhysical);
                 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                 dialog.setVisible(true);
             } catch (Exception ex) {
@@ -1438,14 +1438,14 @@ public class GUI implements CategoryListener {
                 switch (selectedOption) {
 
                     case "Category":
-                        String catName = JOptionPane.showInputDialog(Languages.getLanguage()[56]);
+                        String catName = JOptionPane.showInputDialog(frmCyberPhysical, Languages.getLanguage()[56]);
                         if (catName.length() != 0) {
                             controller.addCategory(catName);
                         }
                         break;
                     case "Object":
                         if (selectedNode == null) {
-                            JOptionPane.showMessageDialog(new JFrame(),
+                            JOptionPane.showMessageDialog(frmCyberPhysical,
                                     Languages.getLanguage()[57] + selectedOption + ".");
                         }
                         if (selectedNode != null && selectedNode.getLevel() == 1) {
@@ -1566,7 +1566,7 @@ public class GUI implements CategoryListener {
                 try {
                     switch (depthOfNode) {
                         case 1:
-                            int dialogResult = JOptionPane.showConfirmDialog(null, eraseCategory + nodeName + "?",
+                            int dialogResult = JOptionPane.showConfirmDialog(frmCyberPhysical, eraseCategory + nodeName + "?",
                                     warningText, JOptionPane.YES_NO_OPTION);
                             if (dialogResult == JOptionPane.YES_OPTION) {
                                 controller.deleteCategory(nodeName);
@@ -1578,13 +1578,13 @@ public class GUI implements CategoryListener {
                             break;
 
                         default:
-                            JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
+                            JOptionPane.showMessageDialog(frmCyberPhysical, selectObjBeforeErase);
                     }
                 } catch (Exception e2) {
                 }
 
             } else {
-                JOptionPane.showMessageDialog(new JFrame(), selectObjBeforeErase);
+                JOptionPane.showMessageDialog(frmCyberPhysical, selectObjBeforeErase);
             }
             tree.repaint();
         });
@@ -1595,7 +1595,7 @@ public class GUI implements CategoryListener {
         mntmNew.addActionListener(actionEvent -> {
             if (model.getObjectsOnCanvas().size() != 0) {
                 int newWarning = JOptionPane.YES_NO_OPTION;
-                int dialogForNewWarning = JOptionPane.showConfirmDialog(null, saveBeforeNew, warningText,
+                int dialogForNewWarning = JOptionPane.showConfirmDialog(frmCyberPhysical, saveBeforeNew, warningText,
                         newWarning);
                 if (dialogForNewWarning == JOptionPane.YES_OPTION) {
                     mntmSave.doClick();
@@ -1623,11 +1623,10 @@ public class GUI implements CategoryListener {
 
             private void menuFileExitActionPerformed() {
                 JFileChooser fileChooser = new JFileChooser();
-                JFrame test = new JFrame();
                 FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
                 fileChooser.setFileFilter(holonFilter);
 
-                if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
+                if (fileChooser.showOpenDialog(frmCyberPhysical) == JFileChooser.APPROVE_OPTION) {
                     File file = fileChooser.getSelectedFile();
 
                     try {
@@ -1641,7 +1640,7 @@ public class GUI implements CategoryListener {
                     } catch (IOException | ArchiveException e) {
                         e.printStackTrace();
                         JLabel message = new JLabel("The savefile is corrupt and cannot be opened.");
-                        JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
+                        JOptionPane.showMessageDialog(frmCyberPhysical, message, "", JOptionPane.ERROR_MESSAGE);
                     }
                 }
             }
@@ -1655,11 +1654,10 @@ public class GUI implements CategoryListener {
 
             private void menuSaveActionPerformed() {
                 JFileChooser fileChooser = new JFileChooser();
-                JFrame test = new JFrame();
                 FileNameExtensionFilter holonFilter = new FileNameExtensionFilter("Holon Save File(*.holon)", "holon");
                 fileChooser.setFileFilter(holonFilter);
 
-                if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
+                if (fileChooser.showSaveDialog(frmCyberPhysical) == JFileChooser.APPROVE_OPTION) {
                     String file = fileChooser.getSelectedFile().getPath();
                     if (fileChooser.getFileFilter().equals(holonFilter)) {
                         if (!file.contains("."))
@@ -1672,7 +1670,7 @@ public class GUI implements CategoryListener {
                         JLabel message = new JLabel(
                                 "Are you sure to use the extension \"" + suffix + "\" instead of \".holon\"?");
 
-                        int response = JOptionPane.showOptionDialog(null, message, "", JOptionPane.DEFAULT_OPTION,
+                        int response = JOptionPane.showOptionDialog(frmCyberPhysical, message, "", JOptionPane.DEFAULT_OPTION,
                                 JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
 
                         if (response == 0)
@@ -1833,7 +1831,7 @@ public class GUI implements CategoryListener {
 
         if (nrOfOldSaves > 0) {
             int dialogButton = JOptionPane.YES_NO_OPTION;
-            int dialogResult = JOptionPane.showConfirmDialog(null, "Old autosave file was found, should it be loaded?",
+            int dialogResult = JOptionPane.showConfirmDialog(frmCyberPhysical, "Old autosave file was found, should it be loaded?",
                     warningText, dialogButton);
             if (dialogResult == JOptionPane.YES_OPTION) {
                 if (dest.exists()) {
@@ -1918,13 +1916,13 @@ public class GUI implements CategoryListener {
      */
     private void addObjectAction(String objType, DefaultMutableTreeNode selectedNode) {
         if (selectedNode == null) {
-            JOptionPane.showMessageDialog(new JFrame(),
+            JOptionPane.showMessageDialog(frmCyberPhysical,
                     "Please select a Category first before adding " + objType + ".");
         }
         // if selected node is a directory for Categories
         else {
             if (selectedNode.getLevel() == 1) {
-                String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
+                String objname = JOptionPane.showInputDialog(frmCyberPhysical, "Please enter a Name for the " + objType);
                 Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
 
                 if (objname.length() != 0) {
@@ -1940,7 +1938,7 @@ public class GUI implements CategoryListener {
 
                 }
             } else {
-                JOptionPane.showMessageDialog(new JFrame(),
+                JOptionPane.showMessageDialog(frmCyberPhysical,
                         "Objects can not be added to Objects. Please select a Category.");
             }
         }