Browse Source

Add button when no drawing are loaded.

Holt59 6 years ago
parent
commit
ee241b963a
1 changed files with 60 additions and 40 deletions
  1. 60
    40
      src/main/org/insa/graphics/MainWindow.java

+ 60
- 40
src/main/org/insa/graphics/MainWindow.java View File

@@ -22,6 +22,8 @@ import java.util.List;
22 22
 import java.util.prefs.Preferences;
23 23
 
24 24
 import javax.swing.BorderFactory;
25
+import javax.swing.Box;
26
+import javax.swing.BoxLayout;
25 27
 import javax.swing.JButton;
26 28
 import javax.swing.JFileChooser;
27 29
 import javax.swing.JFrame;
@@ -155,7 +157,7 @@ public class MainWindow extends JFrame {
155 157
         // Create drawing and action listeners...
156 158
         this.basicDrawing = new BasicDrawing();
157 159
         this.mapViewDrawing = new MapViewDrawing();
158
-        this.drawing = this.basicDrawing;
160
+        this.drawing = basicDrawing;
159 161
 
160 162
         // Createa palettes
161 163
         this.basicPalette = new BasicGraphPalette();
@@ -297,7 +299,53 @@ public class MainWindow extends JFrame {
297 299
         this.baf.addAction(currentThread);
298 300
 
299 301
         // Click adapter
300
-        setJMenuBar(createMenuBar());
302
+        ActionListener openMapActionListener = new ActionListener() {
303
+            @Override
304
+            public void actionPerformed(ActionEvent e) {
305
+                JFileChooser chooser = new JFileChooser();
306
+                FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files",
307
+                        "mapgr");
308
+                File mapFolder = new File(
309
+                        preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
310
+                if (!mapFolder.exists()) {
311
+                    mapFolder = new File(System.getProperty("user.dir"));
312
+                }
313
+                chooser.setCurrentDirectory(mapFolder);
314
+                chooser.setFileFilter(filter);
315
+                if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
316
+                    graphFilePath = chooser.getSelectedFile().getAbsolutePath();
317
+
318
+                    // Check...
319
+                    if (chooser.getSelectedFile().exists()) {
320
+                        preferences.put(DEFAULT_MAP_FOLDER_KEY,
321
+                                chooser.getSelectedFile().getParent());
322
+                    }
323
+
324
+                    DataInputStream stream;
325
+                    try {
326
+                        stream = new DataInputStream(new BufferedInputStream(
327
+                                new FileInputStream(chooser.getSelectedFile())));
328
+                    }
329
+                    catch (IOException e1) {
330
+                        JOptionPane.showMessageDialog(MainWindow.this,
331
+                                "Cannot open the selected file.");
332
+                        return;
333
+                    }
334
+                    loadGraph(new BinaryGraphReader(stream));
335
+                }
336
+            }
337
+        };
338
+
339
+        setJMenuBar(createMenuBar(openMapActionListener));
340
+
341
+        JPanel openPanel = new JPanel();
342
+        openPanel.setLayout(new BoxLayout(openPanel, BoxLayout.PAGE_AXIS));
343
+        JButton openButton = new JButton("Open Map... ");
344
+        openButton.setAlignmentX(Component.CENTER_ALIGNMENT);
345
+        openButton.addActionListener(openMapActionListener);
346
+        openPanel.add(Box.createVerticalGlue());
347
+        openPanel.add(openButton);
348
+        openPanel.add(Box.createVerticalGlue());
301 349
 
302 350
         addWindowListener(new WindowAdapter() {
303 351
             public void windowClosing(WindowEvent e) {
@@ -351,7 +399,7 @@ public class MainWindow extends JFrame {
351 399
         mainPanel.setDividerSize(5);
352 400
 
353 401
         mainPanel.setBackground(Color.WHITE);
354
-        mainPanel.setLeftComponent((Component) this.drawing);
402
+        mainPanel.setLeftComponent(openPanel);
355 403
         mainPanel.setRightComponent(rightComponent);
356 404
         this.add(mainPanel, BorderLayout.CENTER);
357 405
 
@@ -431,9 +479,16 @@ public class MainWindow extends JFrame {
431 479
      * Draw the stored graph on the drawing.
432 480
      */
433 481
     private void drawGraph(Class<? extends Drawing> newClass, GraphPalette palette) {
482
+
434 483
         // Save old divider location
435 484
         int oldLocation = mainPanel.getDividerLocation();
436 485
 
486
+        // Set drawing if not set
487
+        if (!(mainPanel.getLeftComponent() instanceof Drawing)) {
488
+            mainPanel.setLeftComponent((Component) this.drawing);
489
+            mainPanel.setDividerLocation(oldLocation);
490
+        }
491
+
437 492
         boolean isNewGraph = newClass == null;
438 493
         boolean isMapView = (isNewGraph && drawing == mapViewDrawing)
439 494
                 || (!isNewGraph && newClass.equals(MapViewDrawing.class));
@@ -587,47 +642,12 @@ public class MainWindow extends JFrame {
587 642
         mainPanel.setDividerLocation(dividerLocation);
588 643
     }
589 644
 
590
-    private JMenuBar createMenuBar() {
645
+    private JMenuBar createMenuBar(ActionListener openMapActionListener) {
591 646
 
592 647
         // Open Map item...
593 648
         JMenuItem openMapItem = new JMenuItem("Open Map... ", KeyEvent.VK_O);
594 649
         openMapItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK));
595
-        openMapItem.addActionListener(baf.createBlockingAction(new ActionListener() {
596
-            @Override
597
-            public void actionPerformed(ActionEvent e) {
598
-                JFileChooser chooser = new JFileChooser();
599
-                FileNameExtensionFilter filter = new FileNameExtensionFilter("Graph files",
600
-                        "mapgr");
601
-                File mapFolder = new File(
602
-                        preferences.get(DEFAULT_MAP_FOLDER_KEY, DEFAULT_MAP_FOLDER_INSA));
603
-                if (!mapFolder.exists()) {
604
-                    mapFolder = new File(System.getProperty("user.dir"));
605
-                }
606
-                chooser.setCurrentDirectory(mapFolder);
607
-                chooser.setFileFilter(filter);
608
-                if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
609
-                    graphFilePath = chooser.getSelectedFile().getAbsolutePath();
610
-
611
-                    // Check...
612
-                    if (chooser.getSelectedFile().exists()) {
613
-                        preferences.put(DEFAULT_MAP_FOLDER_KEY,
614
-                                chooser.getSelectedFile().getParent());
615
-                    }
616
-
617
-                    DataInputStream stream;
618
-                    try {
619
-                        stream = new DataInputStream(new BufferedInputStream(
620
-                                new FileInputStream(chooser.getSelectedFile())));
621
-                    }
622
-                    catch (IOException e1) {
623
-                        JOptionPane.showMessageDialog(MainWindow.this,
624
-                                "Cannot open the selected file.");
625
-                        return;
626
-                    }
627
-                    loadGraph(new BinaryGraphReader(stream));
628
-                }
629
-            }
630
-        }));
650
+        openMapItem.addActionListener(baf.createBlockingAction(openMapActionListener));
631 651
 
632 652
         // Open Path item...
633 653
         JMenuItem openPathItem = new JMenuItem("Open Path... ", KeyEvent.VK_P);

Loading…
Cancel
Save