001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.components.docking;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018:
019: import javax.swing.*;
020: import java.beans.PropertyChangeEvent;
021: import java.beans.PropertyChangeListener;
022: import java.util.ArrayList;
023:
024: /**
025: * User: Martin
026: * Date: 18.03.2005
027: * Time: 10:43:01
028: */
029: public class DockingPane extends JPanel {
030:
031: public enum SplitpaneDirection {
032: @NotNull
033: HORIZONTAL, @NotNull
034: VERTICAL
035: }
036:
037: public enum Split {
038: @NotNull
039: TOP, @NotNull
040: BOTTOM, @NotNull
041: LEFT, @NotNull
042: RIGHT
043: }
044:
045: @NotNull
046: private ArrayList<ToolWindow> toolWindows;
047:
048: @NotNull
049: private JComponent centerComponent;
050:
051: @NotNull
052: private JPanel[] buttonBorders = new JPanel[4];
053:
054: @NotNull
055: @SuppressWarnings({"unchecked"})
056: private ArrayList<ToolWindow>[] toolWindowsAtSide = (ArrayList<ToolWindow>[]) new ArrayList[4];
057:
058: @NotNull
059: private BorderComponent[] dockedComponents = new BorderComponent[4];
060:
061: @SuppressWarnings({"MismatchedReadAndWriteOfArray"})
062: @NotNull
063: private BorderComponent[] floatingComponents = new BorderComponent[4];
064:
065: public DockingPane() {
066: setLayout(new DockingPaneLayout());
067:
068: centerComponent = new JPanel();
069: toolWindows = new ArrayList<ToolWindow>();
070:
071: buttonBorders[0] = new JPanel();
072: BoxLayout boxLayout0 = new BoxLayout(buttonBorders[0],
073: BoxLayout.X_AXIS);
074: buttonBorders[0].setLayout(boxLayout0);
075:
076: buttonBorders[1] = new JPanel();
077: BoxLayout boxLayout1 = new BoxLayout(buttonBorders[1],
078: BoxLayout.X_AXIS);
079: buttonBorders[1].setLayout(boxLayout1);
080:
081: buttonBorders[2] = new JPanel();
082: BoxLayout boxLayout2 = new BoxLayout(buttonBorders[2],
083: BoxLayout.Y_AXIS);
084: buttonBorders[2].setLayout(boxLayout2);
085:
086: buttonBorders[3] = new JPanel();
087: BoxLayout boxLayout3 = new BoxLayout(buttonBorders[3],
088: BoxLayout.Y_AXIS);
089: buttonBorders[3].setLayout(boxLayout3);
090:
091: for (JPanel buttonBorder : buttonBorders) {
092: buttonBorder.setOpaque(true);
093: }
094:
095: setBackground(buttonBorders[0].getBackground());
096:
097: dockedComponents[0] = new BorderComponent(Split.TOP,
098: SplitpaneDirection.HORIZONTAL, this );
099: dockedComponents[1] = new BorderComponent(Split.BOTTOM,
100: SplitpaneDirection.HORIZONTAL, this );
101: dockedComponents[2] = new BorderComponent(Split.LEFT,
102: SplitpaneDirection.VERTICAL, this );
103: dockedComponents[3] = new BorderComponent(Split.RIGHT,
104: SplitpaneDirection.VERTICAL, this );
105:
106: floatingComponents[0] = new BorderComponent(Split.TOP,
107: SplitpaneDirection.HORIZONTAL, this );
108: floatingComponents[1] = new BorderComponent(Split.BOTTOM,
109: SplitpaneDirection.HORIZONTAL, this );
110: floatingComponents[2] = new BorderComponent(Split.LEFT,
111: SplitpaneDirection.VERTICAL, this );
112: floatingComponents[3] = new BorderComponent(Split.RIGHT,
113: SplitpaneDirection.VERTICAL, this );
114:
115: toolWindowsAtSide[0] = new ArrayList<ToolWindow>();
116: toolWindowsAtSide[1] = new ArrayList<ToolWindow>();
117: toolWindowsAtSide[2] = new ArrayList<ToolWindow>();
118: toolWindowsAtSide[3] = new ArrayList<ToolWindow>();
119:
120: for (JPanel buttonBorder : buttonBorders) {
121: add(buttonBorder);
122: }
123: }
124:
125: public void addToolWindow(@NotNull
126: ToolWindow toolWindow) {
127: toolWindows.add(toolWindow);
128: toolWindow.setDockingPane(this );
129: toolWindow.setSizeState(ToolWindow.SizeState.MINIMIZED);
130:
131: toolWindowsAtSide[indexForAlignment(toolWindow.getAlignment())]
132: .add(toolWindow);
133:
134: buttonBorders[indexForAlignment(toolWindow.getAlignment())]
135: .add(toolWindow.getToolWindowButton());
136:
137: for (BorderComponent dockedComponent : dockedComponents) {
138: add(dockedComponent);
139: }
140:
141: toolWindow.addPropertyChangeListener(
142: ToolWindow.PROPERTY_ALIGNMENT,
143: new PropertyChangeListener() {
144: public void propertyChange(@NotNull
145: PropertyChangeEvent evt) {
146: ToolWindow toolWindow = (ToolWindow) evt
147: .getSource();
148: ToolWindow.Alignment alignmentOld = (ToolWindow.Alignment) evt
149: .getOldValue();
150: ToolWindow.Alignment alignmentNew = (ToolWindow.Alignment) evt
151: .getNewValue();
152:
153: if (toolWindow.getDockingMode() == ToolWindow.DockingMode.DOCKED) {
154: for (ArrayList<ToolWindow> arrayList : toolWindowsAtSide) {
155: arrayList.remove(toolWindow);
156: }
157:
158: toolWindowsAtSide[indexForAlignment(alignmentNew)]
159: .add(toolWindow);
160:
161: rebuildDockedComponent(alignmentOld);
162: rebuildDockedComponent(alignmentNew);
163: } else if (toolWindow.getDockingMode() == ToolWindow.DockingMode.FLOATING) {
164: //TODO
165: }
166: }
167:
168: });
169:
170: toolWindow.addPropertyChangeListener(
171: ToolWindow.PROPERTY_WINDOW_MODE,
172: new PropertyChangeListener() {
173: public void propertyChange(@NotNull
174: PropertyChangeEvent evt) {
175: //TODO
176: }
177: });
178:
179: toolWindow.addPropertyChangeListener(
180: ToolWindow.PROPERTY_DOCKING_MODE,
181: new PropertyChangeListener() {
182: public void propertyChange(@NotNull
183: PropertyChangeEvent evt) {
184: //TODO
185: }
186: });
187:
188: toolWindow.addPropertyChangeListener(
189: ToolWindow.PROPERTY_PINNING_MODE,
190: new PropertyChangeListener() {
191: public void propertyChange(@NotNull
192: PropertyChangeEvent evt) {
193: //TODO
194: }
195: });
196:
197: toolWindow.addPropertyChangeListener(
198: ToolWindow.PROPERTY_SIZE_STATE,
199: new PropertyChangeListener() {
200: public void propertyChange(@NotNull
201: PropertyChangeEvent evt) {
202: ToolWindow toolWindow = (ToolWindow) evt
203: .getSource();
204: rebuildDockedComponent(toolWindow
205: .getAlignment());
206: }
207: });
208:
209: toolWindow.addPropertyChangeListener(ToolWindow.PROPERTY_SIZE,
210: new PropertyChangeListener() {
211: public void propertyChange(@NotNull
212: PropertyChangeEvent evt) {
213: revalidate();
214: repaint();
215: }
216: });
217: }
218:
219: public int getToolWindowCountForSide(@NotNull
220: ToolWindow.Alignment alignment) {
221: return toolWindowsAtSide[indexForAlignment(alignment)].size();
222: }
223:
224: private int indexForAlignment(@NotNull
225: ToolWindow.Alignment alignment) {
226: switch (alignment) {
227: case TOP:
228: return 0;
229: case BOTTOM:
230: return 1;
231: case LEFT:
232: return 2;
233: case RIGHT:
234: return 3;
235: }
236: return 0;
237: }
238:
239: private void rebuildDockedComponent(@NotNull
240: ToolWindow.Alignment alignment) {
241: JPanel buttonBorder = buttonBorders[indexForAlignment(alignment)];
242: buttonBorder.removeAll();
243:
244: dockedComponents[indexForAlignment(alignment)]
245: .clearToolWindows();
246:
247: ArrayList<ToolWindow> arrayList = toolWindowsAtSide[indexForAlignment(alignment)];
248: for (ToolWindow toolWindow : arrayList) {
249: buttonBorder.add(toolWindow.getToolWindowButton());
250: if (toolWindow.getSizeState() == ToolWindow.SizeState.NORMAL) {
251: dockedComponents[indexForAlignment(alignment)]
252: .add(toolWindow);
253: }
254: }
255:
256: revalidate();
257: repaint();
258: }
259:
260: public int getMaxSize(@NotNull
261: ToolWindow.Alignment alignment) {
262: if (alignment == ToolWindow.Alignment.LEFT) {
263: return getWidth()
264: - (buttonBorders[2].getWidth()
265: + buttonBorders[3].getWidth() + dockedComponents[3]
266: .getWidth());
267: } else if (alignment == ToolWindow.Alignment.RIGHT) {
268: return getWidth()
269: - (buttonBorders[2].getWidth()
270: + buttonBorders[3].getWidth() + dockedComponents[2]
271: .getWidth());
272: } else if (alignment == ToolWindow.Alignment.TOP) {
273: return getHeight()
274: - (buttonBorders[0].getHeight()
275: + buttonBorders[1].getHeight() + dockedComponents[1]
276: .getHeight());
277: } else if (alignment == ToolWindow.Alignment.BOTTOM) {
278: return getHeight()
279: - (buttonBorders[0].getHeight()
280: + buttonBorders[1].getHeight() + dockedComponents[0]
281: .getHeight());
282: }
283:
284: return 100;
285: }
286:
287: @Nullable
288: public ToolWindow getToolWindow(@NotNull
289: String key) {
290: for (ToolWindow toolWindow : toolWindows) {
291: if (toolWindow.getCategory().getKey().equals(key)) {
292: return toolWindow;
293: }
294: }
295:
296: return null;
297: }
298:
299: @NotNull
300: public ToolWindow[] getToolWindows() {
301: return toolWindows.toArray(new ToolWindow[toolWindows.size()]);
302: }
303:
304: @NotNull
305: public JComponent getCenterComponent() {
306: return centerComponent;
307: }
308:
309: public void setCenterComponent(@NotNull
310: JComponent centerComponent) {
311: remove(this .centerComponent);
312: this .centerComponent = centerComponent;
313: add(centerComponent);
314:
315: revalidate();
316: repaint();
317: }
318:
319: public void setSplitpaneDirection(@NotNull
320: ToolWindow.Alignment alignment, @NotNull
321: SplitpaneDirection splitpaneDirection) {
322: dockedComponents[indexForAlignment(alignment)]
323: .setSplitpaneDirection(splitpaneDirection);
324: }
325:
326: @NotNull
327: public SplitpaneDirection getSplitpaneDirection(@NotNull
328: ToolWindow.Alignment alignment) {
329: return dockedComponents[indexForAlignment(alignment)]
330: .getSplitpaneDirection();
331: }
332:
333: @NotNull
334: public JPanel[] getButtonBorders() {
335: return buttonBorders;
336: }
337:
338: @NotNull
339: public BorderComponent[] getDockedComponents() {
340: return dockedComponents;
341: }
342:
343: }
|