| 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.docking; |
| 16 | |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import javax.swing.event.ChangeEvent; |
| 20 | import javax.swing.event.ChangeListener; |
| 21 | |
| 22 | import org.flexdock.docking.Dockable; |
| 23 | import org.flexdock.docking.DockingManager; |
| 24 | import org.flexdock.docking.DockingPort; |
| 25 | import org.flexdock.docking.defaults.DefaultDockingStrategy; |
| 26 | import org.flexdock.docking.drag.DragOperation; |
| 27 | import org.flexdock.view.View; |
| 28 | import org.flexdock.view.Viewport; |
| 29 | |
| 30 | public class EmptyableViewportDockingStrategy |
| 31 | extends DefaultDockingStrategy |
| 32 | implements ViewportFactory |
| 33 | { |
| 34 | @Override protected DockingPort createDockingPortImpl(DockingPort parent) |
| 35 | { |
| 36 | return createViewport(); |
| 37 | } |
| 38 | |
| 39 | public Viewport createViewport() |
| 40 | { |
| 41 | EmptyableViewport port = createViewportImpl(); |
| 42 | EmptyableViewChangeListener listener = new EmptyableViewChangeListener(); |
| 43 | port.setListener(listener); |
| 44 | listener.setViewport(port); |
| 45 | return port; |
| 46 | } |
| 47 | |
| 48 | protected EmptyableViewport createViewportImpl() |
| 49 | { |
| 50 | return new EmptyableViewport(); |
| 51 | } |
| 52 | |
| 53 | @SuppressWarnings("unchecked") |
| 54 | @Override public boolean dock( Dockable dockable, |
| 55 | DockingPort port, |
| 56 | String region, |
| 57 | DragOperation operation) |
| 58 | { |
| 59 | try |
| 60 | { |
| 61 | _disableListener++; |
| 62 | boolean result = super.dock(dockable, port, region, operation); |
| 63 | EmptyableViewport viewport = (EmptyableViewport) port; |
| 64 | if ( result |
| 65 | && viewport.isEmptyablePort() |
| 66 | && CENTER_REGION.equals(region)) |
| 67 | { |
| 68 | if ( dockable != getEmptyView() |
| 69 | && viewport.getViewset().contains(getEmptyView())) |
| 70 | { |
| 71 | super.undock(getEmptyView()); |
| 72 | } |
| 73 | // Push board area selection event |
| 74 | viewChanged((View) dockable); |
| 75 | } |
| 76 | if (result && CENTER_REGION.equals(region)) |
| 77 | { |
| 78 | // Workaround to flexdock bug in showing the right tabText in some |
| 79 | // circumstances. Force change of tab text for all dockables in port |
| 80 | Set<View> views = viewport.getViewset(); |
| 81 | for (View view: views) |
| 82 | { |
| 83 | String tabText = view.getTabText(); |
| 84 | view.setTabText(""); |
| 85 | view.setTabText(tabText); |
| 86 | } |
| 87 | } |
| 88 | return result; |
| 89 | } |
| 90 | finally |
| 91 | { |
| 92 | _disableListener--; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override public boolean undock(Dockable dockable) |
| 97 | { |
| 98 | try |
| 99 | { |
| 100 | boolean mustCallViewChanged = false; |
| 101 | _disableListener++; |
| 102 | if (dockable == null) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | View view = (View) dockable; |
| 107 | EmptyableViewport port = (EmptyableViewport) view.getDockingPort(); |
| 108 | if (port != null && port.isEmptyablePort()) |
| 109 | { |
| 110 | if (port.getViewset().size() == 1) |
| 111 | { |
| 112 | super.dock(getEmptyView(), port, DockingManager.CENTER_REGION); |
| 113 | // Push board area selection event |
| 114 | viewChanged(null); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | mustCallViewChanged = true; |
| 119 | } |
| 120 | } |
| 121 | boolean result = super.undock(dockable); |
| 122 | if (mustCallViewChanged) |
| 123 | { |
| 124 | // Need to call viewChanged with the new view |
| 125 | viewChanged((View) port.getDockable(CENTER_REGION)); |
| 126 | } |
| 127 | return result; |
| 128 | } |
| 129 | finally |
| 130 | { |
| 131 | _disableListener--; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | protected void viewChanged(View view) |
| 136 | { |
| 137 | } |
| 138 | |
| 139 | final protected Dockable getEmptyView() |
| 140 | { |
| 141 | if (_emptyView == null) |
| 142 | { |
| 143 | _emptyView = DockingManager.getDockableFactory().getDockable( |
| 144 | EmptyableViewport.EMPTY_VIEW_ID); |
| 145 | } |
| 146 | return _emptyView; |
| 147 | } |
| 148 | |
| 149 | protected class EmptyableViewChangeListener implements ChangeListener |
| 150 | { |
| 151 | public void setViewport(EmptyableViewport port) |
| 152 | { |
| 153 | _port = port; |
| 154 | } |
| 155 | |
| 156 | public void stateChanged(ChangeEvent evt) |
| 157 | { |
| 158 | if (_disableListener == 0 && _port.isEmptyablePort()) |
| 159 | { |
| 160 | View view = (View) _port.getDockable(CENTER_REGION); |
| 161 | viewChanged(view); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private EmptyableViewport _port; |
| 166 | } |
| 167 | |
| 168 | protected Dockable _emptyView = null; |
| 169 | protected int _disableListener = 0; |
| 170 | } |