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.pentaho.reportdesigner.lib.client.components.Category;
018: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
019:
020: import javax.swing.*;
021: import java.awt.*;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.MouseAdapter;
025: import java.awt.event.MouseEvent;
026: import java.awt.image.BufferedImage;
027: import java.beans.PropertyChangeListener;
028: import java.beans.PropertyChangeSupport;
029:
030: /**
031: * User: Martin
032: * Date: 18.03.2005
033: * Time: 10:46:34
034: */
035: public class ToolWindow {
036: @NotNull
037: public static final String PROPERTY_WINDOW_MODE = "windowMode";
038: @NotNull
039: public static final String PROPERTY_DOCKING_MODE = "dockingMode";
040: @NotNull
041: public static final String PROPERTY_PINNING_MODE = "pinningMode";
042: @NotNull
043: public static final String PROPERTY_SIZE_STATE = "sizeState";
044: @NotNull
045: public static final String PROPERTY_SIZE = "size";
046: @NotNull
047: public static final String PROPERTY_ALIGNMENT = "alignment";
048:
049: public enum Alignment {
050: @NotNull
051: TOP, @NotNull
052: BOTTOM, @NotNull
053: LEFT, @NotNull
054: RIGHT
055: }
056:
057: public enum WindowMode {
058: @NotNull
059: INSIDE, //inside component
060: @NotNull
061: WINDOW
062: //as own window
063: }
064:
065: public enum DockingMode {
066: @NotNull
067: DOCKED, @NotNull
068: FLOATING
069: }
070:
071: public enum PinningMode {
072: @NotNull
073: PINNED, //stays even if we lose focus
074: @NotNull
075: UNPINNED
076: //autohides on focus lost
077: }
078:
079: public enum SizeState {
080: @NotNull
081: MINIMIZED, @NotNull
082: NORMAL
083: }
084:
085: @NotNull
086: private DockingPane dockingPane;
087:
088: @NotNull
089: private Alignment alignment;
090: @NotNull
091: private WindowMode windowMode;
092: @NotNull
093: private DockingMode dockingMode;
094: @NotNull
095: private PinningMode pinningMode;
096: @NotNull
097: private SizeState sizeState;
098:
099: @NotNull
100: private Dimension size;
101:
102: @NotNull
103: private Category<?> category;
104:
105: @NotNull
106: private PropertyChangeSupport propertyChangeSupport;
107:
108: @NotNull
109: private ImageToggleButton toolWindowButton;
110:
111: public ToolWindow(@NotNull
112: Category<?> category) {
113: //noinspection ConstantConditions
114: if (category == null) {
115: throw new IllegalArgumentException(
116: "category must not be null");
117: }
118:
119: alignment = Alignment.LEFT;
120: windowMode = WindowMode.INSIDE;
121: dockingMode = DockingMode.DOCKED;
122: pinningMode = PinningMode.PINNED;
123: sizeState = SizeState.MINIMIZED;
124:
125: size = new Dimension(100, 100);
126:
127: dockingPane = new DockingPane();
128:
129: this .category = category;
130:
131: initToolWindowButton(category);
132:
133: propertyChangeSupport = new PropertyChangeSupport(this );
134: }
135:
136: public void setDockingPane(@NotNull
137: DockingPane dockingPane) {
138: this .dockingPane = dockingPane;
139: }
140:
141: private void initToolWindowButton(@NotNull
142: Category<?> category) {
143: ImageIcon iconSmall = category.getIconSmall();
144: if (iconSmall == null) {
145: iconSmall = new ImageIcon(new BufferedImage(1, 1,
146: BufferedImage.TYPE_INT_ARGB));
147: }
148: toolWindowButton = new ImageToggleButton(iconSmall, category
149: .getTitle());
150: toolWindowButton.setToolTipText(category.getTitle());
151:
152: toolWindowButton.addMouseListener(new MouseAdapter() {
153: public void mouseClicked(@NotNull
154: MouseEvent e) {
155: if (e.isPopupTrigger()) {
156: showPopupMenu(e);
157: }
158: }
159:
160: public void mousePressed(@NotNull
161: MouseEvent e) {
162: if (e.isPopupTrigger()) {
163: showPopupMenu(e);
164: }
165: }
166:
167: public void mouseReleased(@NotNull
168: MouseEvent e) {
169: if (e.isPopupTrigger()) {
170: showPopupMenu(e);
171: }
172: }
173:
174: private void showPopupMenu(@NotNull
175: MouseEvent event) {
176: JPopupMenu popupMenu = new JPopupMenu();
177: JMenuItem menuItemLeft = new JMenuItem(
178: TranslationManager
179: .getInstance()
180: .getTranslation(
181: TranslationManager.COMMON_BUNDLE_PREFIX,
182: "ToolWindow.MenuItem.Left"));
183: menuItemLeft.addActionListener(new ActionListener() {
184: public void actionPerformed(@NotNull
185: ActionEvent e) {
186: setAlignment(Alignment.LEFT);
187: }
188: });
189:
190: JMenuItem menuItemRight = new JMenuItem(
191: TranslationManager
192: .getInstance()
193: .getTranslation(
194: TranslationManager.COMMON_BUNDLE_PREFIX,
195: "ToolWindow.MenuItem.Right"));
196: menuItemRight.addActionListener(new ActionListener() {
197: public void actionPerformed(@NotNull
198: ActionEvent e) {
199: setAlignment(Alignment.RIGHT);
200: }
201: });
202:
203: JMenuItem menuItemTop = new JMenuItem(
204: TranslationManager
205: .getInstance()
206: .getTranslation(
207: TranslationManager.COMMON_BUNDLE_PREFIX,
208: "ToolWindow.MenuItem.Top"));
209: menuItemTop.addActionListener(new ActionListener() {
210: public void actionPerformed(@NotNull
211: ActionEvent e) {
212: setAlignment(Alignment.TOP);
213: }
214: });
215:
216: JMenuItem menuItemBottom = new JMenuItem(
217: TranslationManager
218: .getInstance()
219: .getTranslation(
220: TranslationManager.COMMON_BUNDLE_PREFIX,
221: "ToolWindow.MenuItem.Bottom"));
222: menuItemBottom.addActionListener(new ActionListener() {
223: public void actionPerformed(@NotNull
224: ActionEvent e) {
225: setAlignment(Alignment.BOTTOM);
226: }
227: });
228:
229: JMenuItem menuItemHorizontal = new JMenuItem(
230: TranslationManager
231: .getInstance()
232: .getTranslation(
233: TranslationManager.COMMON_BUNDLE_PREFIX,
234: "ToolWindow.MenuItem.Horizontal"));
235: menuItemHorizontal
236: .addActionListener(new ActionListener() {
237: public void actionPerformed(@NotNull
238: ActionEvent e) {
239: Alignment alignment = getAlignment();
240: dockingPane
241: .setSplitpaneDirection(
242: alignment,
243: DockingPane.SplitpaneDirection.HORIZONTAL);
244: }
245: });
246:
247: JMenuItem menuItemVertical = new JMenuItem(
248: TranslationManager
249: .getInstance()
250: .getTranslation(
251: TranslationManager.COMMON_BUNDLE_PREFIX,
252: "ToolWindow.MenuItem.Vertical"));
253: menuItemVertical
254: .addActionListener(new ActionListener() {
255: public void actionPerformed(@NotNull
256: ActionEvent e) {
257: Alignment alignment = getAlignment();
258: dockingPane
259: .setSplitpaneDirection(
260: alignment,
261: DockingPane.SplitpaneDirection.VERTICAL);
262: }
263: });
264:
265: if (dockingPane == null
266: || dockingPane
267: .getToolWindowCountForSide(getAlignment()) < 2) {
268: menuItemHorizontal.setEnabled(false);
269: menuItemVertical.setEnabled(false);
270: } else {
271: if (dockingPane
272: .getSplitpaneDirection(getAlignment()) == DockingPane.SplitpaneDirection.HORIZONTAL) {
273: menuItemHorizontal.setEnabled(false);
274: } else {
275: menuItemVertical.setEnabled(false);
276: }
277: }
278:
279: switch (getAlignment()) {
280: case TOP:
281: menuItemTop.setEnabled(false);
282: break;
283: case BOTTOM:
284: menuItemBottom.setEnabled(false);
285: break;
286: case LEFT:
287: menuItemLeft.setEnabled(false);
288: break;
289: case RIGHT:
290: menuItemRight.setEnabled(false);
291: break;
292: }
293:
294: popupMenu.add(menuItemLeft);
295: popupMenu.add(menuItemRight);
296: popupMenu.add(menuItemTop);
297: popupMenu.add(menuItemBottom);
298: popupMenu.addSeparator();
299: popupMenu.add(menuItemHorizontal);
300: popupMenu.add(menuItemVertical);
301:
302: popupMenu.show(toolWindowButton, event.getX(), event
303: .getY());
304: }
305: });
306:
307: toolWindowButton.addActionListener(new ActionListener() {
308: public void actionPerformed(@NotNull
309: ActionEvent e) {
310: if (toolWindowButton.isSelected()) {
311: setSizeState(SizeState.NORMAL);
312: } else {
313: setSizeState(SizeState.MINIMIZED);
314: }
315: }
316: });
317: }
318:
319: @NotNull
320: public Alignment getAlignment() {
321: return alignment;
322: }
323:
324: public void setAlignment(@NotNull
325: Alignment alignment) {
326: Alignment alignmentOld = this .alignment;
327: this .alignment = alignment;
328:
329: toolWindowButton.setAlignmentX(alignment);
330:
331: if (alignmentOld != alignment) {
332: propertyChangeSupport.firePropertyChange(
333: PROPERTY_ALIGNMENT, alignmentOld, alignment);
334: }
335: }
336:
337: @NotNull
338: public WindowMode getWindowMode() {
339: return windowMode;
340: }
341:
342: public void setWindowMode(@NotNull
343: WindowMode windowMode) {
344: WindowMode windowModeOld = this .windowMode;
345: this .windowMode = windowMode;
346:
347: if (windowModeOld != windowMode) {
348: propertyChangeSupport.firePropertyChange(
349: PROPERTY_WINDOW_MODE, windowModeOld, windowMode);
350: }
351: }
352:
353: @NotNull
354: public DockingMode getDockingMode() {
355: return dockingMode;
356: }
357:
358: public void setDockingMode(@NotNull
359: DockingMode dockingMode) {
360: DockingMode dockingModeOld = this .dockingMode;
361: this .dockingMode = dockingMode;
362:
363: if (dockingModeOld != dockingMode) {
364: propertyChangeSupport.firePropertyChange(
365: PROPERTY_DOCKING_MODE, dockingModeOld, dockingMode);
366: }
367: }
368:
369: @NotNull
370: public PinningMode getPinningMode() {
371: return pinningMode;
372: }
373:
374: public void setPinningMode(@NotNull
375: PinningMode pinningMode) {
376: PinningMode pinningModeOld = this .pinningMode;
377: this .pinningMode = pinningMode;
378:
379: if (pinningModeOld != pinningMode) {
380: propertyChangeSupport.firePropertyChange(
381: PROPERTY_PINNING_MODE, pinningModeOld, pinningMode);
382: }
383: }
384:
385: @NotNull
386: public SizeState getSizeState() {
387: return sizeState;
388: }
389:
390: public void setSizeState(@NotNull
391: SizeState sizeState) {
392: SizeState sizeStateOld = this .sizeState;
393: this .sizeState = sizeState;
394: toolWindowButton.setSelected(sizeState != SizeState.MINIMIZED);
395:
396: if (sizeStateOld != sizeState) {
397: propertyChangeSupport.firePropertyChange(
398: PROPERTY_SIZE_STATE, sizeStateOld, sizeState);
399: }
400: }
401:
402: @NotNull
403: public Dimension getSize() {
404: return new Dimension(size);
405: }
406:
407: public void setSize(@NotNull
408: Dimension size) {
409: Dimension sizeOld = this .size;
410: this .size = size;
411:
412: if (!sizeOld.equals(size)) {
413: propertyChangeSupport.firePropertyChange(PROPERTY_SIZE,
414: sizeOld, size);
415: }
416: }
417:
418: @NotNull
419: public Category<?> getCategory() {
420: return category;
421: }
422:
423: @NotNull
424: public ImageToggleButton getToolWindowButton() {
425: return toolWindowButton;
426: }
427:
428: public void addPropertyChangeListener(@NotNull
429: PropertyChangeListener listener) {
430: propertyChangeSupport.addPropertyChangeListener(listener);
431: }
432:
433: public void removePropertyChangeListener(@NotNull
434: PropertyChangeListener listener) {
435: propertyChangeSupport.removePropertyChangeListener(listener);
436: }
437:
438: @NotNull
439: public PropertyChangeListener[] getPropertyChangeListeners() {
440: return propertyChangeSupport.getPropertyChangeListeners();
441: }
442:
443: public void addPropertyChangeListener(@NotNull
444: String propertyName, @NotNull
445: PropertyChangeListener listener) {
446: propertyChangeSupport.addPropertyChangeListener(propertyName,
447: listener);
448: }
449:
450: public void removePropertyChangeListener(@NotNull
451: String propertyName, @NotNull
452: PropertyChangeListener listener) {
453: propertyChangeSupport.removePropertyChangeListener(
454: propertyName, listener);
455: }
456:
457: @NotNull
458: public PropertyChangeListener[] getPropertyChangeListeners(@NotNull
459: String propertyName) {
460: return propertyChangeSupport
461: .getPropertyChangeListeners(propertyName);
462: }
463: }
|