But all person displaying related information into the View and add a panel for the...
[Persons_Comparator.git] / src / MainWindowsView.java
index 065c90d8050f0deb9203ce7733bb26b49b64c029..85470ff4636bc67730f5ca54eff5d8e34cc38af9 100644 (file)
@@ -1,26 +1,28 @@
 import javax.swing.*;
+import javax.swing.text.DefaultEditorKit;
 import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
 import java.util.ArrayList;
 
 public class MainWindowsView extends JFrame {
-    MainWindowsView(String title) {
+
+    MainWindowsView(String title, JPanel panel) {
         //Create and set up the window.
         setTitle(title);
-        setSize(300, 300);
+        setSize(panel.getPreferredSize());
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-        JPanel panel = new PersonView();
+        //Create menu
+        JMenuBar menuBar = createMenuBar();
+        setJMenuBar(menuBar);
 
-        //Get all Swing/AWT primitive components in the views and add them to the panel.
+        //Get all Swing/AWT JPanel in the views and add them to the main panel.
         ArrayList<Component> components = new ArrayList<>();
         for (int i = 0; i < panel.getComponentCount(); i++) {
-            if ((panel.getComponent(i) instanceof Container)) {
-                Container subContainer = (Container) panel.getComponent(i);
-                for (int j = 0; j < subContainer.getComponentCount(); j++) {
-                    components.add(subContainer.getComponent(j));
-                }
-            }
+            components.add(panel.getComponent(i));
         }
         for (Component component : components) {
             panel.add(component);
@@ -29,6 +31,46 @@ public class MainWindowsView extends JFrame {
         setContentPane(panel);
     }
 
+    /**
+     * Create menu that support cut/copy/paste.
+     */
+    private JMenuBar createMenuBar() {
+        JMenuItem menuItem;
+        JMenuBar menuBar = new JMenuBar();
+
+        JMenu fileMenu = new JMenu("File");
+
+        menuItem = new JMenuItem("Exit");
+        menuItem.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                System.exit(0);
+            }
+        });
+        fileMenu.add(menuItem);
+
+        JMenu editMenu = new JMenu("Edit");
+        editMenu.setMnemonic(KeyEvent.VK_E);
+
+        menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
+        menuItem.setText("Cut");
+        menuItem.setMnemonic(KeyEvent.VK_T);
+        editMenu.add(menuItem);
+
+        menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
+        menuItem.setText("Copy");
+        menuItem.setMnemonic(KeyEvent.VK_C);
+        editMenu.add(menuItem);
+
+        menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
+        menuItem.setText("Paste");
+        menuItem.setMnemonic(KeyEvent.VK_P);
+        editMenu.add(menuItem);
+
+        menuBar.add(fileMenu);
+        menuBar.add(editMenu);
+        return menuBar;
+    }
+
     /**
      * Show the GUI. For thread safety,
      * this method should be invoked from the
@@ -40,5 +82,4 @@ public class MainWindowsView extends JFrame {
         //this.pack();
         this.setVisible(true);
     }
-
-}
+}
\ No newline at end of file