From 5e6b965a06e5dc0fea136a4dd49bb6bcb3a96626 Mon Sep 17 00:00:00 2001
From: Minh Hiep Pham <minh.pham@telecom-paristech.fr>
Date: Tue, 9 Apr 2019 15:03:51 +0200
Subject: [PATCH] Updated cpu node help

---
 src/main/java/ui/MainGUI.java                |   4 +
 src/main/java/ui/window/JDialogCPUNode.java  |  29 ++---
 src/main/java/ui/window/JFrameHelp.java      |   1 +
 src/main/java/ui/window/TGComponentHelp.java | 112 +++++++++++++++++++
 4 files changed, 128 insertions(+), 18 deletions(-)
 create mode 100644 src/main/java/ui/window/TGComponentHelp.java

diff --git a/src/main/java/ui/MainGUI.java b/src/main/java/ui/MainGUI.java
index 9c3a94f697..fef7fd3ded 100644
--- a/src/main/java/ui/MainGUI.java
+++ b/src/main/java/ui/MainGUI.java
@@ -712,6 +712,10 @@ public class MainGUI implements ActionListener, WindowListener, KeyListener, Per
         return helpManager;
     }
 
+    public void setHelpManager(HelpManager hm) {
+        helpManager = hm;
+    }
+
     public void showIntegratedHelp() {
         if (helpManager != null) {
             openHelpFrame(helpManager);
diff --git a/src/main/java/ui/window/JDialogCPUNode.java b/src/main/java/ui/window/JDialogCPUNode.java
index 500d67241a..a385a031e9 100644
--- a/src/main/java/ui/window/JDialogCPUNode.java
+++ b/src/main/java/ui/window/JDialogCPUNode.java
@@ -103,7 +103,7 @@ public class JDialogCPUNode extends JDialogBase implements ActionListener  {
     //issue 183
     List<JButton>   buttons = new ArrayList<>();
     List<HelpEntry> helpEntries;
-    JFrameHWNodeHelp cpuHelp;
+    TGComponentHelp cpuHelp;
 
     /* Creates new form  */
     public JDialogCPUNode(MainGUI _mgui, Frame _frame, String _title, TMLArchiCPUNode _node, ArchUnitMEC _MECType,
@@ -132,33 +132,20 @@ public class JDialogCPUNode extends JDialogBase implements ActionListener  {
 
     //issue 183
     private void buttonClick(JButton but, HelpEntry he) {
-        //setModalityType(ModalityType.MODELESS);
         but.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 if(cpuHelp == null ) {
-                    cpuHelp = new JFrameHWNodeHelp(mgui,"Help",he);
-                    cpuHelp.setLocationRelativeTo(but);
+                    cpuHelp = new TGComponentHelp(mgui, he);
+                    cpuHelp.setLocationHelpWindow(but);
                 }else{
                     if(!cpuHelp.isVisible()) {
-                        cpuHelp = new JFrameHWNodeHelp(mgui,"Help",he);
-                        cpuHelp.setLocationRelativeTo(but);
+                        cpuHelp = new TGComponentHelp(mgui, he);
+                        cpuHelp.setLocationHelpWindow(but);
                     }else{
                         cpuHelp.setVisible(false);
                     }
                 }
-
-                if(cpuHelp != null) {
-                    cpuHelp.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close");
-                    cpuHelp.getRootPane().getActionMap().put("close", new AbstractAction() {
-                        @Override
-                        public void actionPerformed(ActionEvent e) {
-                            if(!cpuHelp.isVisible())
-                                dispose();
-                            cpuHelp.setVisible(false);
-                        }
-                    });
-                }
             }
         });
     }
@@ -593,12 +580,18 @@ public class JDialogCPUNode extends JDialogBase implements ActionListener  {
         regularClose = true;
         MECType = ArchUnitMEC.Types.get( MECTypeCB.getSelectedIndex() );
         dispose();
+        if ((cpuHelp != null) && cpuHelp.isVisible()) {
+            cpuHelp.setVisible(false);
+        }
     }
 
     public void cancelDialog() {
 
         //TraceManager.addDev("Cancel dialog");
         dispose();
+        if ((cpuHelp != null) && cpuHelp.isVisible()) {
+            cpuHelp.setVisible(false);
+        }
     }
 
     public boolean isRegularClose() {
diff --git a/src/main/java/ui/window/JFrameHelp.java b/src/main/java/ui/window/JFrameHelp.java
index 446c62f886..afbb63ad2a 100644
--- a/src/main/java/ui/window/JFrameHelp.java
+++ b/src/main/java/ui/window/JFrameHelp.java
@@ -83,6 +83,7 @@ public	class JFrameHelp extends JFrame implements ActionListener {
         visitedEntries = new Vector<>();
 
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
         Container framePanel = getContentPane();
         framePanel.setLayout(new BorderLayout());
         Font f = new Font("Courrier", Font.BOLD, 12);
diff --git a/src/main/java/ui/window/TGComponentHelp.java b/src/main/java/ui/window/TGComponentHelp.java
new file mode 100644
index 0000000000..5124ea9a9d
--- /dev/null
+++ b/src/main/java/ui/window/TGComponentHelp.java
@@ -0,0 +1,112 @@
+package ui.window;
+
+import help.HelpEntry;
+import help.HelpManager;
+import myutil.TraceManager;
+import ui.MainGUI;
+import ui.util.IconManager;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+public class TGComponentHelp extends JDialog implements ActionListener {
+
+    MainGUI mainGUI;
+    HelpEntry helpEntry;
+    HelpManager helpManager;
+    JButton helpBut;
+    JEditorPane pane;
+
+
+    public TGComponentHelp(MainGUI _mgui, HelpEntry _he) {
+        mainGUI = _mgui;
+        helpEntry = _he;
+
+        getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "close");
+        getRootPane().getActionMap().put("close", new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                dispose();
+            }
+        });
+        setUndecorated(true);
+        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
+        Container framePanel = getContentPane();
+        framePanel.setLayout(new BorderLayout());
+        Font f = new Font("Courrier", Font.BOLD, 12);
+
+        JPanel helpPanel = new JPanel();
+        helpPanel.setLayout(new BorderLayout());
+        pane = new JEditorPane("text/html;charset=UTF-8", "");
+        pane.setEditable(false);
+        pane.setText(helpEntry.getHTMLContent());
+
+        JScrollPane jsp = new JScrollPane(pane);
+        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+        helpPanel.add(jsp, BorderLayout.CENTER);
+
+        framePanel.add(helpPanel, BorderLayout.CENTER);
+
+        helpBut = new JButton("Help", IconManager.imgic32);
+
+        helpManager = new HelpManager();
+        if(helpManager.loadEntries()) {
+            mainGUI.setHelpManager(helpManager);
+        }
+
+        helpBut.addActionListener(this);
+
+        JPanel jp = new JPanel();
+        jp.add(helpBut);
+        framePanel.add(jp, BorderLayout.SOUTH);
+
+        setSize(300, 200);
+
+        setVisible(true);
+
+    }
+
+    @Override
+    public void actionPerformed(ActionEvent e) {
+        if(e.getSource() == helpBut) {
+            mainGUI.openHelpFrame(helpEntry);
+        }
+    }
+
+    public void setLocationHelpWindow(Component c) {
+        // target location
+        int dx, dy;
+        // target GC
+        GraphicsDevice gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
+        Rectangle gcBounds;
+
+        Dimension windowSize = getSize();
+        //TraceManager.addDev("window size ==> height = " + windowSize.height + " width = " + windowSize.width);
+
+        gcBounds = gc.getDefaultConfiguration().getBounds();
+
+        //TraceManager.addDev("gc bounds ==> (x,y) = " + "(" + gcBounds.x + "," + gcBounds.y + ")" + " gc bound width = " + gcBounds.width + " gc " +
+        //       "bound height = " + gcBounds.height);
+
+        Dimension compSize = c.getSize();
+
+        Point compLocation = c.getLocationOnScreen();
+        //TraceManager.addDev("button location ==> (x,y) = " + "(" + compLocation.x + "," + compLocation.y + ")" );
+
+        dx = compLocation.x + compSize.width;
+        dy = compLocation.y + compSize.height;
+
+        if (dy + windowSize.height > gcBounds.y + gcBounds.height) {
+            dy = gcBounds.y + gcBounds.height - windowSize.height;
+        }
+
+        if (compLocation.x + windowSize.width > gcBounds.width + gcBounds.x) {
+            dx = compLocation.x - windowSize.width;
+        }
+        //TraceManager.addDev("help window location ==> (x,y) = " + "(" + dx+ "," + dy + ")" );
+        setLocation(dx, dy);
+    }
+}
\ No newline at end of file
-- 
GitLab