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.awt.Component; |
18 | import java.awt.Insets; |
19 | import java.util.Set; |
20 | |
21 | import javax.swing.JTabbedPane; |
22 | import javax.swing.UIManager; |
23 | import javax.swing.event.ChangeListener; |
24 | |
25 | import org.flexdock.docking.activation.ActiveDockableListener; |
26 | import org.flexdock.docking.event.TabbedDragListener; |
27 | import org.flexdock.docking.event.hierarchy.DockingPortTracker; |
28 | import org.flexdock.util.LookAndFeelSettings; |
29 | import org.flexdock.view.View; |
30 | import org.flexdock.view.Viewport; |
31 | |
32 | // Special viewport that must be used in lieu of Viewport (everywhere) in order |
33 | // to allow one (or more) empty region. |
34 | // This port accepts in its center only views which content implement a special |
35 | // marker interface. Those special views are accepted no where else. |
36 | public class EmptyableViewport extends Viewport |
37 | { |
38 | static public final String EMPTY_VIEW_ID = "EMPTY_VIEW"; |
39 | |
40 | public EmptyableViewport() |
41 | { |
42 | this(EmptyableViewMarker.class); |
43 | } |
44 | |
45 | public EmptyableViewport(Class<?> viewMarker) |
46 | { |
47 | _viewMarker = viewMarker; |
48 | } |
49 | |
50 | public boolean isEmptyablePort() |
51 | { |
52 | return isMarkedView((View) getComponent(CENTER_REGION)); |
53 | } |
54 | |
55 | protected boolean isMarkedView(View view) |
56 | { |
57 | if (view != null && view.getContentPane() != null) |
58 | { |
59 | return _viewMarker.isAssignableFrom(view.getContentPane().getClass()); |
60 | } |
61 | else |
62 | { |
63 | return false; |
64 | } |
65 | } |
66 | |
67 | @Override protected JTabbedPane createTabbedPane() |
68 | { |
69 | Insets oldInsets = UIManager.getInsets(LookAndFeelSettings.TAB_PANE_BORDER_INSETS); |
70 | int tabPlacement = getInitTabPlacement(); |
71 | int edgeInset = LookAndFeelSettings.getTabEdgeInset(tabPlacement); |
72 | |
73 | Insets newInsets = new Insets(0, 2, 0, 2); |
74 | switch (tabPlacement) |
75 | { |
76 | case JTabbedPane.LEFT: |
77 | newInsets.left = edgeInset >= 0 ? edgeInset : oldInsets.left; |
78 | break; |
79 | |
80 | case JTabbedPane.BOTTOM: |
81 | newInsets.bottom = edgeInset >= 0 ? edgeInset : oldInsets.bottom; |
82 | break; |
83 | |
84 | case JTabbedPane.RIGHT: |
85 | newInsets.right = edgeInset >= 0 ? edgeInset : oldInsets.right; |
86 | break; |
87 | |
88 | case JTabbedPane.TOP: |
89 | default: |
90 | newInsets.top = edgeInset >= 0 ? edgeInset : oldInsets.top; |
91 | break; |
92 | } |
93 | |
94 | UIManager.put(LookAndFeelSettings.TAB_PANE_BORDER_INSETS, newInsets); |
95 | JTabbedPane pane = new JTabbedPane(); |
96 | pane.setTabPlacement(tabPlacement); |
97 | UIManager.put(LookAndFeelSettings.TAB_PANE_BORDER_INSETS, oldInsets); |
98 | |
99 | TabbedDragListener tdl = new TabbedDragListener(); |
100 | pane.addMouseListener(tdl); |
101 | pane.addMouseMotionListener(tdl); |
102 | |
103 | pane.addChangeListener(ActiveDockableListener.getInstance()); |
104 | |
105 | if (_listener != null) |
106 | { |
107 | // Listen to changes of current selection in tab |
108 | pane.addChangeListener(_listener); |
109 | } |
110 | return pane; |
111 | } |
112 | |
113 | @Override public boolean isDockingAllowed(Component comp, String region) |
114 | { |
115 | boolean allow = super.isDockingAllowed(comp, region); |
116 | if (allow && isInitDone()) |
117 | { |
118 | // Special check for board drawing areas |
119 | allow = isEmptyablePort() && CENTER_REGION.equals(region); |
120 | if (!isMarkedView((View) comp)) |
121 | { |
122 | allow = !allow; |
123 | } |
124 | } |
125 | return allow; |
126 | } |
127 | |
128 | static public void setInitDone() |
129 | { |
130 | _initDone = true; |
131 | } |
132 | |
133 | static protected boolean isInitDone() |
134 | { |
135 | return _initDone; |
136 | } |
137 | |
138 | @SuppressWarnings("unchecked") |
139 | static public EmptyableViewport getEmptyablePort() |
140 | { |
141 | Set<EmptyableViewport> ports = DockingPortTracker.getDockingPorts(); |
142 | for (EmptyableViewport port: ports) |
143 | { |
144 | if (port.isEmptyablePort()) |
145 | { |
146 | return port; |
147 | } |
148 | } |
149 | return null; |
150 | } |
151 | |
152 | public ChangeListener getListener() |
153 | { |
154 | return _listener; |
155 | } |
156 | |
157 | public void setListener(ChangeListener listener) |
158 | { |
159 | _listener = listener; |
160 | } |
161 | |
162 | protected final Class<?> _viewMarker; |
163 | protected ChangeListener _listener; |
164 | static private boolean _initDone = false; |
165 | } |