01: /**
02: * L2FProd.com Common Components 7.3 License.
03: *
04: * Copyright 2005-2007 L2FProd.com
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package com.l2fprod.common.swing;
18:
19: import java.beans.PropertyChangeEvent;
20: import java.beans.PropertyChangeListener;
21:
22: /**
23: * This class is used to create a multiple-exclusion scope for a set of
24: * JTaskPaneGroups. Creating a set of JTaskPaneGroups with the same
25: * GroupOfTaskPaneGroup object means that expanding one of those JTaskPaneGroups
26: * will collapse all other JTaskPaneGroups in the group.
27: *
28: * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
29: */
30: public class GroupOfTaskPaneGroup implements PropertyChangeListener {
31:
32: private JTaskPaneGroup selection;
33:
34: /**
35: * Adds a <code>JTaskPaneGroup</code> to this group.
36: *
37: * @param taskpaneGroup
38: */
39: public void add(JTaskPaneGroup taskpaneGroup) {
40: register(taskpaneGroup);
41: // if we have no selection
42: if (selection == null) {
43: // and if the taskpane is expanded
44: if (taskpaneGroup.isExpanded()) {
45: // then the selection becomes the taskpane
46: selection = taskpaneGroup;
47: }
48: } else {
49: // we have a selection, so this taskpane must be collapsed
50: taskpaneGroup.setExpanded(false);
51: }
52:
53: maybeUpdateSelection(taskpaneGroup);
54: }
55:
56: /**
57: * Removes a <code>JTaskPaneGroup</code> from this group.
58: *
59: * @param taskpaneGroup
60: */
61: public void remove(JTaskPaneGroup taskpaneGroup) {
62: unregister(taskpaneGroup);
63: // if we are removing the currently selection taskpane, reset the selection
64: // to "null" otherwise the taskpane may get modified by us later.
65: if (selection == taskpaneGroup) {
66: selection = null;
67: }
68: }
69:
70: public void propertyChange(PropertyChangeEvent event) {
71: // if a taskpane gets expanded, collapse the previously expanded one if any
72: JTaskPaneGroup taskpaneGroup = (JTaskPaneGroup) event
73: .getSource();
74: maybeUpdateSelection(taskpaneGroup);
75: }
76:
77: private void maybeUpdateSelection(JTaskPaneGroup taskpaneGroup) {
78: if (selection != taskpaneGroup && taskpaneGroup.isExpanded()) {
79: if (selection != null) {
80: selection.setExpanded(false);
81: }
82: selection = taskpaneGroup;
83: }
84: }
85:
86: private void register(JTaskPaneGroup taskpaneGroup) {
87: taskpaneGroup.addPropertyChangeListener(
88: JTaskPaneGroup.EXPANDED_CHANGED_KEY, this );
89: }
90:
91: private void unregister(JTaskPaneGroup taskpaneGroup) {
92: taskpaneGroup.removePropertyChangeListener(
93: JTaskPaneGroup.EXPANDED_CHANGED_KEY, this);
94: }
95:
96: }
|