]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/SidebarController.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / gui / SidebarController.java
1 package tim.prune.gui;
2
3 import java.awt.Component;
4
5 import javax.swing.JSplitPane;
6 import javax.swing.SwingUtilities;
7
8 /**
9  * Class to control the showing and hiding of the sidebars
10  * (left panel, right panel and profile display)
11  */
12 public class SidebarController
13 {
14         /** array of hideable components */
15         private Component[] _components = null;
16         /** array of splitter panes */
17         private JSplitPane[] _splitters = null;
18         /** array of splitter positions */
19         private int[] _positions = null;
20
21
22         /**
23          * Constructor
24          * @param inComponents array of components to hide/show
25          * @param inSplitters array of splitter panes
26          */
27         public SidebarController(Component[] inComponents, JSplitPane[] inSplitters)
28         {
29                 _components = inComponents;
30                 _splitters = inSplitters;
31                 _positions = new int[inSplitters.length];
32         }
33
34         /**
35          * Toggle full screen mode on or off
36          */
37         public void toggle()
38         {
39                 if (_components != null && _components.length > 0)
40                 {
41                         boolean visible = _components[0].isVisible();
42                         if (visible) {
43                                 // Store divider locations
44                                 for (int i=0; i<_components.length; i++) {
45                                         _positions[i] = _splitters[i].getDividerLocation();
46                                 }
47                         }
48                         // Set visibility of components
49                         for (int i=0; i<_components.length; i++) {
50                                 _components[i].setVisible(!visible);
51                         }
52                         // Restore divider locations
53                         for (int i=0; i<_components.length; i++) {
54                                 if (!visible) {
55                                         _splitters[i].setDividerLocation(_positions[i]);
56                                 }
57                         }
58                         // Hiding of panels has to occur in separate thread to update properly
59                         if (visible) SwingUtilities.invokeLater(new Runnable() {
60                                 public void run() {
61                                         for (int i=0; i<_components.length; i++) {
62                                                 _splitters[i].setDividerLocation(i==0?0.0:1.0);
63                                                 _splitters[i].invalidate();
64                                         }
65                                 }
66                         });
67                 }
68         }
69 }