KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > frame > DockFrameController


1 package org.columba.core.gui.frame;
2
3 import java.awt.Component JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import javax.swing.BorderFactory JavaDoc;
12 import javax.swing.JComponent JavaDoc;
13 import javax.swing.JPanel JavaDoc;
14 import javax.swing.JPopupMenu JavaDoc;
15
16 import org.columba.api.gui.frame.IDock;
17 import org.columba.api.gui.frame.IDockable;
18 import org.columba.core.config.DefaultConfigDirectory;
19 import org.columba.core.config.ViewItem;
20 import org.columba.core.gui.docking.XMLPersister;
21 import org.flexdock.docking.Dockable;
22 import org.flexdock.docking.DockingConstants;
23 import org.flexdock.docking.DockingManager;
24 import org.flexdock.docking.DockingPort;
25 import org.flexdock.docking.defaults.DefaultDockingPort;
26 import org.flexdock.docking.state.PersistenceException;
27
28 public abstract class DockFrameController extends DefaultFrameController
29         implements IDock {
30
31     private DefaultDockingPort dockingPort = new DefaultDockingPort();
32
33     private ArrayList JavaDoc<IDockable> list = new ArrayList JavaDoc<IDockable>();
34
35     private IDockable contextualPanelDockable;
36     private IDockable searchPanelDockable;
37
38     /**
39      * @param viewItem
40      */

41     public DockFrameController(ViewItem viewItem) {
42         super(viewItem);
43
44         initComponents();
45
46     }
47
48     /**
49      * @param id
50      */

51     public DockFrameController(String JavaDoc id) {
52         super(id);
53
54         initComponents();
55
56     }
57
58
59     /**
60      *
61      */

62     private void initComponents() {
63
64         dockingPort = new DefaultDockingPort();
65         dockingPort.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
66
67         searchPanelDockable = registerDockable(IDock.DOCKING_VIEW_SEARCH, "Search", getSearchPanel().getView(), null);
68
69     }
70
71     /* (non-Javadoc)
72      * @see org.columba.api.gui.frame.IFrameMediator#getContentPane()
73      */

74     public JPanel JavaDoc getContentPane() {
75         return dockingPort;
76     }
77
78     /* (non-Javadoc)
79      * @see org.columba.api.gui.frame.IFrameMediator#loadPositions()
80      */

81     public void loadPositions() {
82         super.loadPositions();
83
84         boolean restoreSuccess = false;
85         // load docking settings from last user session
86
File JavaDoc configDirectory = DefaultConfigDirectory.getInstance().getCurrentPath();
87         File JavaDoc dockDirectory = new File JavaDoc(configDirectory, "flexdock");
88         if (!dockDirectory.exists())
89             dockDirectory.mkdir();
90         String JavaDoc filename = getId() + ".xml";
91
92         try {
93             File JavaDoc file = new File JavaDoc(dockDirectory, filename);
94             if (file.exists()) {
95                 new XMLPersister().load(new FileInputStream JavaDoc(file), dockingPort);
96                 restoreSuccess = true;
97             }
98
99         } catch (IOException JavaDoc e1) {
100             e1.printStackTrace();
101         } catch (PersistenceException e1) {
102             e1.printStackTrace();
103         }
104
105         if (!restoreSuccess) {
106             // make sure there is nothing within the root dockingport
107
dockingPort.clear();
108
109             loadDefaultPosition();
110         }
111
112     }
113
114     /* (non-Javadoc)
115      * @see org.columba.api.gui.frame.IFrameMediator#savePositions()
116      */

117     public void savePositions() {
118         super.savePositions();
119
120         try {
121             File JavaDoc configDirectory = DefaultConfigDirectory.getInstance().getCurrentPath();
122             File JavaDoc dockDirectory = new File JavaDoc(configDirectory, "flexdock");
123             if (!dockDirectory.exists())
124                 dockDirectory.mkdir();
125             String JavaDoc filename = getId() + ".xml";
126             File JavaDoc file = new File JavaDoc(dockDirectory, filename);
127
128             new XMLPersister().store(new FileOutputStream JavaDoc(file), dockingPort);
129
130         } catch (IOException JavaDoc e) {
131             e.printStackTrace();
132         } catch (PersistenceException e) {
133             e.printStackTrace();
134         }
135
136     }
137
138     /**
139      * @see org.columba.api.gui.frame.IFrameMediator#isInitialized()
140      */

141     /* (non-Javadoc)
142      * @see org.columba.api.gui.frame.IFrameMediator#isInitialized()
143      */

144     public boolean isInitialized() {
145         return false;
146     }
147
148     /**
149      * @see org.columba.api.gui.frame.IDock#registerDockable(org.columba.api.gui.frame.IDockable)
150      */

151     /* (non-Javadoc)
152      * @see org.columba.api.gui.frame.IDock#registerDockable(org.columba.api.gui.frame.IDockable)
153      */

154     public void registerDockable(IDockable dockable) {
155
156         list.add(dockable);
157
158         dockingPort.dock(dockable.getView(), dockable.getId());
159
160     }
161
162     /**
163      * @see org.columba.api.gui.frame.IDock#registerDockable(java.lang.String,
164      * java.lang.String, javax.swing.JComponent, javax.swing.JPopupMenu)
165      */

166     /* (non-Javadoc)
167      * @see org.columba.api.gui.frame.IDock#registerDockable(java.lang.String, java.lang.String, javax.swing.JComponent, javax.swing.JPopupMenu)
168      */

169     public IDockable registerDockable(String JavaDoc id, String JavaDoc name, JComponent JavaDoc comp,
170             JPopupMenu JavaDoc popup) {
171         IDockable dockable = new FrameMediatorDockable(id, name, comp, popup);
172
173         registerDockable(dockable);
174
175         return dockable;
176     }
177
178     /**
179      * @see org.columba.api.gui.frame.IDock#getDockableIterator()
180      */

181     /* (non-Javadoc)
182      * @see org.columba.api.gui.frame.IDock#getDockableIterator()
183      */

184     public Iterator JavaDoc<IDockable> getDockableIterator() {
185         return list.iterator();
186     }
187
188     /**
189      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
190      * org.columba.api.gui.frame.IDock.REGION, float)
191      */

192     /* (non-Javadoc)
193      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDock.REGION, float)
194      */

195     public void dock(IDockable dockable, REGION region, float percentage) {
196         String JavaDoc regionString = convertRegion(region);
197
198         DockingManager.dock(dockable.getView(), dockingPort, regionString,
199                 percentage);
200
201     }
202
203     /**
204      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
205      * org.columba.api.gui.frame.IDock.REGION)
206      */

207     /* (non-Javadoc)
208      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDock.REGION)
209      */

210     public void dock(IDockable dockable, REGION region) {
211         String JavaDoc regionString = convertRegion(region);
212
213         DockingManager.dock((Component JavaDoc) dockable.getView(),
214                 (DockingPort) dockingPort, regionString);
215     }
216
217     /**
218      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
219      * org.columba.api.gui.frame.IDockable,
220      * org.columba.api.gui.frame.IDock.REGION, float)
221      */

222     /* (non-Javadoc)
223      * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDock.REGION, float)
224      */

225     public void dock(IDockable dockable, IDockable parentDockable,
226             REGION region, float percentage) {
227         String JavaDoc regionString = convertRegion(region);
228
229         DockingManager.dock(dockable.getView(), parentDockable.getView(),
230                 regionString, percentage);
231
232     }
233
234     /**
235      * @see org.columba.api.gui.frame.IDock#setSplitProportion(org.columba.api.gui.frame.IDockable,
236      * float)
237      */

238     /* (non-Javadoc)
239      * @see org.columba.api.gui.frame.IDock#setSplitProportion(org.columba.api.gui.frame.IDockable, float)
240      */

241     public void setSplitProportion(IDockable dockable, float percentage) {
242         DockingManager.setSplitProportion(dockable.getView(), percentage);
243     }
244
245     // convert region enum to flexdock string
246
/**
247      * @param region
248      * @return regionString
249      */

250     private String JavaDoc convertRegion(REGION region) {
251         String JavaDoc regionString = null;
252         if (region == REGION.CENTER)
253             regionString = DockingConstants.CENTER_REGION;
254         else if (region == REGION.WEST)
255             regionString = DockingConstants.WEST_REGION;
256         else if (region == REGION.EAST)
257             regionString = DockingConstants.EAST_REGION;
258         else if (region == REGION.NORTH)
259             regionString = DockingConstants.NORTH_REGION;
260         else if (region == REGION.SOUTH)
261             regionString = DockingConstants.SOUTH_REGION;
262         else
263             throw new IllegalArgumentException JavaDoc("unsupported region: "
264                     + region.toString());
265         return regionString;
266     }
267
268     /**
269      * Overwrite to specify default docking settings.
270      * <p>
271      * Make use of the <code>dock(..)</code> and
272      * <code>setSplitPropertion(..)</code> methods.
273      * <p>
274      * This method is called in case a formerly persisted state could not be
275      * loaded correctly or no persisted state is available yet.
276      */

277     /**
278      *
279      */

280     public void loadDefaultPosition() {
281         dock(searchPanelDockable, REGION.WEST);
282         dock(contextualPanelDockable, REGION.EAST);
283     }
284
285
286     /**
287      * @see org.columba.api.gui.frame.IDock#showDockable(java.lang.String)
288      */

289     public void showDockable(String JavaDoc id) {
290         Dockable dockable = DockingManager.getDockable(id);
291         DockingManager.display(dockable);
292     }
293 }
294
Popular Tags