001: package org.columba.core.gui.frame;
002:
003: import java.awt.Component;
004: import java.io.File;
005: import java.io.FileInputStream;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010:
011: import javax.swing.BorderFactory;
012: import javax.swing.JComponent;
013: import javax.swing.JPanel;
014: import javax.swing.JPopupMenu;
015:
016: import org.columba.api.gui.frame.IDock;
017: import org.columba.api.gui.frame.IDockable;
018: import org.columba.core.config.DefaultConfigDirectory;
019: import org.columba.core.config.ViewItem;
020: import org.columba.core.gui.docking.XMLPersister;
021: import org.flexdock.docking.Dockable;
022: import org.flexdock.docking.DockingConstants;
023: import org.flexdock.docking.DockingManager;
024: import org.flexdock.docking.DockingPort;
025: import org.flexdock.docking.defaults.DefaultDockingPort;
026: import org.flexdock.docking.state.PersistenceException;
027:
028: public abstract class DockFrameController extends
029: DefaultFrameController implements IDock {
030:
031: private DefaultDockingPort dockingPort = new DefaultDockingPort();
032:
033: private ArrayList<IDockable> list = new ArrayList<IDockable>();
034:
035: private IDockable contextualPanelDockable;
036: private IDockable searchPanelDockable;
037:
038: /**
039: * @param viewItem
040: */
041: public DockFrameController(ViewItem viewItem) {
042: super (viewItem);
043:
044: initComponents();
045:
046: }
047:
048: /**
049: * @param id
050: */
051: public DockFrameController(String id) {
052: super (id);
053:
054: initComponents();
055:
056: }
057:
058: /**
059: *
060: */
061: private void initComponents() {
062:
063: dockingPort = new DefaultDockingPort();
064: dockingPort.setBorder(BorderFactory.createEmptyBorder(5, 5, 5,
065: 5));
066:
067: searchPanelDockable = registerDockable(
068: IDock.DOCKING_VIEW_SEARCH, "Search", getSearchPanel()
069: .getView(), null);
070:
071: }
072:
073: /* (non-Javadoc)
074: * @see org.columba.api.gui.frame.IFrameMediator#getContentPane()
075: */
076: public JPanel getContentPane() {
077: return dockingPort;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.columba.api.gui.frame.IFrameMediator#loadPositions()
082: */
083: public void loadPositions() {
084: super .loadPositions();
085:
086: boolean restoreSuccess = false;
087: // load docking settings from last user session
088: File configDirectory = DefaultConfigDirectory.getInstance()
089: .getCurrentPath();
090: File dockDirectory = new File(configDirectory, "flexdock");
091: if (!dockDirectory.exists())
092: dockDirectory.mkdir();
093: String filename = getId() + ".xml";
094:
095: try {
096: File file = new File(dockDirectory, filename);
097: if (file.exists()) {
098: new XMLPersister().load(new FileInputStream(file),
099: dockingPort);
100: restoreSuccess = true;
101: }
102:
103: } catch (IOException e1) {
104: e1.printStackTrace();
105: } catch (PersistenceException e1) {
106: e1.printStackTrace();
107: }
108:
109: if (!restoreSuccess) {
110: // make sure there is nothing within the root dockingport
111: dockingPort.clear();
112:
113: loadDefaultPosition();
114: }
115:
116: }
117:
118: /* (non-Javadoc)
119: * @see org.columba.api.gui.frame.IFrameMediator#savePositions()
120: */
121: public void savePositions() {
122: super .savePositions();
123:
124: try {
125: File configDirectory = DefaultConfigDirectory.getInstance()
126: .getCurrentPath();
127: File dockDirectory = new File(configDirectory, "flexdock");
128: if (!dockDirectory.exists())
129: dockDirectory.mkdir();
130: String filename = getId() + ".xml";
131: File file = new File(dockDirectory, filename);
132:
133: new XMLPersister().store(new FileOutputStream(file),
134: dockingPort);
135:
136: } catch (IOException e) {
137: e.printStackTrace();
138: } catch (PersistenceException e) {
139: e.printStackTrace();
140: }
141:
142: }
143:
144: /**
145: * @see org.columba.api.gui.frame.IFrameMediator#isInitialized()
146: */
147: /* (non-Javadoc)
148: * @see org.columba.api.gui.frame.IFrameMediator#isInitialized()
149: */
150: public boolean isInitialized() {
151: return false;
152: }
153:
154: /**
155: * @see org.columba.api.gui.frame.IDock#registerDockable(org.columba.api.gui.frame.IDockable)
156: */
157: /* (non-Javadoc)
158: * @see org.columba.api.gui.frame.IDock#registerDockable(org.columba.api.gui.frame.IDockable)
159: */
160: public void registerDockable(IDockable dockable) {
161:
162: list.add(dockable);
163:
164: dockingPort.dock(dockable.getView(), dockable.getId());
165:
166: }
167:
168: /**
169: * @see org.columba.api.gui.frame.IDock#registerDockable(java.lang.String,
170: * java.lang.String, javax.swing.JComponent, javax.swing.JPopupMenu)
171: */
172: /* (non-Javadoc)
173: * @see org.columba.api.gui.frame.IDock#registerDockable(java.lang.String, java.lang.String, javax.swing.JComponent, javax.swing.JPopupMenu)
174: */
175: public IDockable registerDockable(String id, String name,
176: JComponent comp, JPopupMenu popup) {
177: IDockable dockable = new FrameMediatorDockable(id, name, comp,
178: popup);
179:
180: registerDockable(dockable);
181:
182: return dockable;
183: }
184:
185: /**
186: * @see org.columba.api.gui.frame.IDock#getDockableIterator()
187: */
188: /* (non-Javadoc)
189: * @see org.columba.api.gui.frame.IDock#getDockableIterator()
190: */
191: public Iterator<IDockable> getDockableIterator() {
192: return list.iterator();
193: }
194:
195: /**
196: * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
197: * org.columba.api.gui.frame.IDock.REGION, float)
198: */
199: /* (non-Javadoc)
200: * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDock.REGION, float)
201: */
202: public void dock(IDockable dockable, REGION region, float percentage) {
203: String regionString = convertRegion(region);
204:
205: DockingManager.dock(dockable.getView(), dockingPort,
206: regionString, percentage);
207:
208: }
209:
210: /**
211: * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
212: * org.columba.api.gui.frame.IDock.REGION)
213: */
214: /* (non-Javadoc)
215: * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable, org.columba.api.gui.frame.IDock.REGION)
216: */
217: public void dock(IDockable dockable, REGION region) {
218: String regionString = convertRegion(region);
219:
220: DockingManager.dock((Component) dockable.getView(),
221: (DockingPort) dockingPort, regionString);
222: }
223:
224: /**
225: * @see org.columba.api.gui.frame.IDock#dock(org.columba.api.gui.frame.IDockable,
226: * org.columba.api.gui.frame.IDockable,
227: * org.columba.api.gui.frame.IDock.REGION, float)
228: */
229: /* (non-Javadoc)
230: * @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)
231: */
232: public void dock(IDockable dockable, IDockable parentDockable,
233: REGION region, float percentage) {
234: String regionString = convertRegion(region);
235:
236: DockingManager.dock(dockable.getView(), parentDockable
237: .getView(), regionString, percentage);
238:
239: }
240:
241: /**
242: * @see org.columba.api.gui.frame.IDock#setSplitProportion(org.columba.api.gui.frame.IDockable,
243: * float)
244: */
245: /* (non-Javadoc)
246: * @see org.columba.api.gui.frame.IDock#setSplitProportion(org.columba.api.gui.frame.IDockable, float)
247: */
248: public void setSplitProportion(IDockable dockable, float percentage) {
249: DockingManager.setSplitProportion(dockable.getView(),
250: percentage);
251: }
252:
253: // convert region enum to flexdock string
254: /**
255: * @param region
256: * @return regionString
257: */
258: private String convertRegion(REGION region) {
259: String regionString = null;
260: if (region == REGION.CENTER)
261: regionString = DockingConstants.CENTER_REGION;
262: else if (region == REGION.WEST)
263: regionString = DockingConstants.WEST_REGION;
264: else if (region == REGION.EAST)
265: regionString = DockingConstants.EAST_REGION;
266: else if (region == REGION.NORTH)
267: regionString = DockingConstants.NORTH_REGION;
268: else if (region == REGION.SOUTH)
269: regionString = DockingConstants.SOUTH_REGION;
270: else
271: throw new IllegalArgumentException("unsupported region: "
272: + region.toString());
273: return regionString;
274: }
275:
276: /**
277: * Overwrite to specify default docking settings.
278: * <p>
279: * Make use of the <code>dock(..)</code> and
280: * <code>setSplitPropertion(..)</code> methods.
281: * <p>
282: * This method is called in case a formerly persisted state could not be
283: * loaded correctly or no persisted state is available yet.
284: */
285: /**
286: *
287: */
288: public void loadDefaultPosition() {
289: dock(searchPanelDockable, REGION.WEST);
290: dock(contextualPanelDockable, REGION.EAST);
291: }
292:
293: /**
294: * @see org.columba.api.gui.frame.IDock#showDockable(java.lang.String)
295: */
296: public void showDockable(String id) {
297: Dockable dockable = DockingManager.getDockable(id);
298: DockingManager.display(dockable);
299: }
300: }
|