| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.application; |
| 16 | |
| 17 | import java.io.File; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.apache.commons.logging.Log; |
| 21 | import org.flexdock.docking.DockableFactory; |
| 22 | import org.flexdock.docking.DockingManager; |
| 23 | import org.flexdock.docking.drag.effects.DragPreview; |
| 24 | import org.flexdock.event.EventManager; |
| 25 | import org.flexdock.perspective.PerspectiveFactory; |
| 26 | import org.flexdock.perspective.PerspectiveManager; |
| 27 | import org.flexdock.perspective.persist.FilePersistenceHandler; |
| 28 | import org.flexdock.view.Viewport; |
| 29 | import org.jdesktop.application.ApplicationContext; |
| 30 | import org.jdesktop.application.LocalStorage; |
| 31 | |
| 32 | import net.sourceforge.hivegui.docking.EmptyableViewport; |
| 33 | import net.sourceforge.hivegui.docking.ViewportFactory; |
| 34 | import net.sourceforge.hivegui.menu.MenuFactory; |
| 35 | |
| 36 | public class FlexdockApplicationInitializer |
| 37 | implements ApplicationInitializer |
| 38 | { |
| 39 | static final public String DEFAULT_PERSPECTIVE = "DEFAULT_PERSPECTIVE"; |
| 40 | |
| 41 | public void setContextHolder(ApplicationContextHolder holder) |
| 42 | { |
| 43 | _context = holder.getContext(); |
| 44 | } |
| 45 | |
| 46 | public void initialize(HiveGuiApplicationMain application, String[] args) |
| 47 | { |
| 48 | _application = application; |
| 49 | } |
| 50 | |
| 51 | public void startup() |
| 52 | { |
| 53 | // Initialize flexdock |
| 54 | initDocking(); |
| 55 | loadLayout(); |
| 56 | // Fix for Flexdock bug if drop on forbidden port |
| 57 | EventManager.addListener(new HiveGuiDockingListener()); |
| 58 | // Now we can set actual function of EmptyableViewport |
| 59 | if (_mainPort instanceof EmptyableViewport) |
| 60 | { |
| 61 | EmptyableViewport.setInitDone(); |
| 62 | } |
| 63 | // Initialize main frame |
| 64 | initMainFrame(); |
| 65 | } |
| 66 | |
| 67 | protected void initDocking() |
| 68 | { |
| 69 | DockingManager.setDockableFactory(_dockableFactory); |
| 70 | for (DockingStrategyContribution contrib: _strategies) |
| 71 | { |
| 72 | DockingManager.setDockingStrategy( contrib.getViewportClass(), |
| 73 | contrib.getStrategy()); |
| 74 | } |
| 75 | DockingManager.setDragPreview(_dragPreview); |
| 76 | //TODO: add support for floating and minimization |
| 77 | DockingManager.setFloatingEnabled(false); |
| 78 | DockingManager.setSingleTabsAllowed(_allowSingleTabs); |
| 79 | DockingManager.setAutoPersist(false); |
| 80 | |
| 81 | PerspectiveManager.setFactory(_perspectiveFactory); |
| 82 | LocalStorage storage = _context.getLocalStorage(); |
| 83 | File dockingFile = new File(storage.getDirectory(), getDockingStateFile()); |
| 84 | PerspectiveManager.setPersistenceHandler(new FilePersistenceHandler(dockingFile)); |
| 85 | PerspectiveManager.getInstance().setCurrentPerspective(getDefaultPerspectiveId(), true); |
| 86 | } |
| 87 | |
| 88 | protected String getDefaultPerspectiveId() |
| 89 | { |
| 90 | return DEFAULT_PERSPECTIVE; |
| 91 | } |
| 92 | |
| 93 | // CSOFF: IllegalCatchCheck |
| 94 | protected void loadLayout() |
| 95 | { |
| 96 | try |
| 97 | { |
| 98 | //#### Should check return |
| 99 | DockingManager.loadLayoutModel(); |
| 100 | } |
| 101 | catch (Exception e) |
| 102 | { |
| 103 | //#### Try to recreate perspective from scratch (in case of a |
| 104 | // problem with docking.xml file |
| 105 | _logger.error("loadLayout", e); |
| 106 | } |
| 107 | // Create the main docking port |
| 108 | _mainPort = _portFactory.createViewport(); |
| 109 | DockingManager.restoreLayout(); |
| 110 | } |
| 111 | // CSON: IllegalCatchCheck |
| 112 | |
| 113 | protected void initMainFrame() |
| 114 | { |
| 115 | // Build and install Menu |
| 116 | _application.getMainFrame().setJMenuBar(_menuFactory.getMenuBar()); |
| 117 | // Finally, show the main frame |
| 118 | _application.show(_mainPort); |
| 119 | } |
| 120 | |
| 121 | public void ready() |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | public void shutdown() |
| 126 | { |
| 127 | saveLayout(); |
| 128 | } |
| 129 | |
| 130 | // CSOFF: IllegalCatchCheck |
| 131 | protected void saveLayout() |
| 132 | { |
| 133 | try |
| 134 | { |
| 135 | DockingManager.storeLayoutModel(); |
| 136 | } |
| 137 | catch (Exception e) |
| 138 | { |
| 139 | _logger.error("saveLayout", e); |
| 140 | } |
| 141 | } |
| 142 | // CSON: IllegalCatchCheck |
| 143 | |
| 144 | protected String getDockingStateFile() |
| 145 | { |
| 146 | return "docking.xml"; |
| 147 | } |
| 148 | |
| 149 | public boolean isAllowSingleTabs() |
| 150 | { |
| 151 | return _allowSingleTabs; |
| 152 | } |
| 153 | |
| 154 | public void setAllowSingleTabs(boolean allowSingleTabs) |
| 155 | { |
| 156 | _allowSingleTabs = allowSingleTabs; |
| 157 | } |
| 158 | |
| 159 | public DockableFactory getDockableFactory() |
| 160 | { |
| 161 | return _dockableFactory; |
| 162 | } |
| 163 | |
| 164 | public void setDockableFactory(DockableFactory dockableFactory) |
| 165 | { |
| 166 | _dockableFactory = dockableFactory; |
| 167 | } |
| 168 | |
| 169 | public DragPreview getDragPreview() |
| 170 | { |
| 171 | return _dragPreview; |
| 172 | } |
| 173 | |
| 174 | public void setDragPreview(DragPreview dragPreview) |
| 175 | { |
| 176 | _dragPreview = dragPreview; |
| 177 | } |
| 178 | |
| 179 | public Log getLogger() |
| 180 | { |
| 181 | return _logger; |
| 182 | } |
| 183 | |
| 184 | public void setLogger(Log logger) |
| 185 | { |
| 186 | _logger = logger; |
| 187 | } |
| 188 | |
| 189 | public MenuFactory getMenuFactory() |
| 190 | { |
| 191 | return _menuFactory; |
| 192 | } |
| 193 | |
| 194 | public void setMenuFactory(MenuFactory menuFactory) |
| 195 | { |
| 196 | _menuFactory = menuFactory; |
| 197 | } |
| 198 | |
| 199 | public PerspectiveFactory getPerspectiveFactory() |
| 200 | { |
| 201 | return _perspectiveFactory; |
| 202 | } |
| 203 | |
| 204 | public void setPerspectiveFactory(PerspectiveFactory perspectiveFactory) |
| 205 | { |
| 206 | _perspectiveFactory = perspectiveFactory; |
| 207 | } |
| 208 | |
| 209 | public ViewportFactory getPortFactory() |
| 210 | { |
| 211 | return _portFactory; |
| 212 | } |
| 213 | |
| 214 | public void setPortFactory(ViewportFactory portFactory) |
| 215 | { |
| 216 | _portFactory = portFactory; |
| 217 | } |
| 218 | |
| 219 | public List<DockingStrategyContribution> getStrategies() |
| 220 | { |
| 221 | return _strategies; |
| 222 | } |
| 223 | |
| 224 | public void setStrategies(List<DockingStrategyContribution> strategies) |
| 225 | { |
| 226 | _strategies = strategies; |
| 227 | } |
| 228 | |
| 229 | protected Log _logger; |
| 230 | protected ApplicationContext _context; |
| 231 | protected MenuFactory _menuFactory; |
| 232 | protected DockableFactory _dockableFactory; |
| 233 | protected List<DockingStrategyContribution> _strategies; |
| 234 | protected ViewportFactory _portFactory; |
| 235 | protected PerspectiveFactory _perspectiveFactory; |
| 236 | protected DragPreview _dragPreview; |
| 237 | protected boolean _allowSingleTabs; |
| 238 | protected HiveGuiApplicationMain _application; |
| 239 | protected Viewport _mainPort; |
| 240 | } |