972a38ec50dc1c41bcafb68ddf949882d635de5c
[debian/jabref.git] / src / main / java / net / sf / jabref / SidePaneManager.java
1 /*  Copyright (C) 2003-2011 JabRef contributors.
2     This program is free software; you can redistribute it and/or modify
3     it under the terms of the GNU General Public License as published by
4     the Free Software Foundation; either version 2 of the License, or
5     (at your option) any later version.
6
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     GNU General Public License for more details.
11
12     You should have received a copy of the GNU General Public License along
13     with this program; if not, write to the Free Software Foundation, Inc.,
14     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16 package net.sf.jabref;
17
18 import java.util.*;
19
20 import javax.swing.SwingUtilities;
21 import javax.swing.event.ChangeEvent;
22 import javax.swing.event.ChangeListener;
23
24 /**
25  * Manages visibility of SideShowComponents in a given newly constructed
26  * sidePane.
27  * 
28  * @version $Revision$ ($Date$)
29  * 
30  */
31 public class SidePaneManager {
32
33         JabRefFrame frame;
34
35         BasePanel panel;
36
37         SidePane sidep;
38
39         Map<String, SidePaneComponent> components = new LinkedHashMap<String, SidePaneComponent>();
40
41         List<SidePaneComponent> visible = new LinkedList<SidePaneComponent>();
42
43         public SidePaneManager(JabRefFrame frame) {
44                 this.frame = frame;
45                 /*
46                  * Change by Morten Alver 2005.12.04: By postponing the updating of the
47                  * side pane components, we get rid of the annoying latency when
48                  * switching tabs:
49                  */
50                 frame.tabbedPane.addChangeListener(new ChangeListener() {
51                         public void stateChanged(ChangeEvent event) {
52                                 SwingUtilities.invokeLater(new Runnable() {
53                                         public void run() {
54                                                 setActiveBasePanel((BasePanel) SidePaneManager.this.frame.tabbedPane
55                                                         .getSelectedComponent());
56                                         }
57                                 });
58                         }
59                 });
60                 sidep = new SidePane();
61                 sidep.setVisible(false);
62         }
63
64         public SidePane getPanel() {
65                 return sidep;
66         }
67
68         public synchronized boolean hasComponent(String name) {
69                 return (components.get(name) != null);
70         }
71
72         public boolean isComponentVisible(String name) {
73                 Object o = components.get(name);
74                 if (o != null) {
75                         return visible.contains(o);
76                 } else {
77                         return false;
78                 }
79         }
80
81         public synchronized void toggle(String name) {
82                 if (isComponentVisible(name)) {
83                         hide(name);
84                 } else {
85                         show(name);
86                 }
87         }
88
89         public void show(String name) {
90                 Object o = components.get(name);
91                 if (o != null) {
92                         show((SidePaneComponent) o);
93                 } else
94                         System.err.println("Side pane component '" + name + "' unknown.");
95         }
96
97         public void hide(String name) {
98                 Object o = components.get(name);
99                 if (o != null) {
100                         hideComponent((SidePaneComponent) o);
101                 } else
102                         System.err.println("Side pane component '" + name + "' unknown.");
103         }
104
105         public synchronized void register(String name, SidePaneComponent comp) {
106         components.put(name, comp);
107         }
108
109         public synchronized void registerAndShow(String name, SidePaneComponent comp) {
110                 register(name, comp);
111                 show(name);
112         }
113
114         private synchronized void show(SidePaneComponent component) {
115                 if (!visible.contains(component)) {
116                         // Put the new component at the top of the group
117                         visible.add(0, component);
118                         updateView();
119                         component.componentOpening();
120                 }
121         }
122
123     public SidePaneComponent getComponent(String name) {
124         return components.get(name);
125     }
126
127     public synchronized void hideComponent(SidePaneComponent comp) {
128                 if (visible.contains(comp)) {
129                         comp.componentClosing();
130                         visible.remove(comp);
131                         updateView();
132                 }
133         }
134
135     public synchronized void hideComponent(String name) {
136         SidePaneComponent comp = components.get(name);
137         if (comp == null)
138             return;
139         if (visible.contains(comp)) {
140             comp.componentClosing();
141             visible.remove(comp);
142             updateView();
143         }
144     }
145
146         public synchronized void unregisterComponent(String name) {
147             components.remove(name);
148         }
149
150
151
152         /**
153          * Update all side pane components to show information from the given
154          * BasePanel.
155          * 
156          * @param panel
157          */
158         public void setActiveBasePanel(BasePanel panel) {
159         for (String key : components.keySet()) {
160             components.get(key).setActiveBasePanel(panel);
161         }
162         }
163
164         public void updateView() {
165                 sidep.setComponents(visible);
166                 if (visible.size() > 0) {
167                         boolean wasVisible = sidep.isVisible();
168                         sidep.setVisible(true);
169                         if (!wasVisible) {
170                             int width = Globals.prefs.getInt("sidePaneWidth");
171                             if (width > 0)
172                                 frame.contentPane.setDividerLocation(width);
173                             else
174                                 frame.contentPane.setDividerLocation(getPanel().getPreferredSize().width);
175                         }
176                 } else {
177                     if (sidep.isVisible())
178                         Globals.prefs.putInt("sidePaneWidth", frame.contentPane.getDividerLocation());
179                     sidep.setVisible(false);
180                     
181                 }
182         }
183
184         public void revalidate() {
185                 sidep.revalidate();
186                 sidep.repaint();
187         }
188 }