Project

General

Profile

Download (5.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef.palette;
12

    
13
import java.beans.PropertyChangeEvent;
14
import java.beans.PropertyChangeListener;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.Iterator;
18
import java.util.List;
19

    
20
import org.eclipse.jface.resource.ImageDescriptor;
21

    
22
/**
23
 * The model object for a PaletteStack - A stack of tools. A stack should
24
 * contain only tools and should have permissions that are less than or equal to
25
 * its parent.
26
 * 
27
 * @author Whitney Sorenson
28
 * @since 3.0
29
 */
30
public class PaletteStack extends PaletteContainer {
31

    
32
	/**
33
	 * Listens to visibility changes of the children palette entries so that the
34
	 * active entry can be updated if the current active entry is hidden.
35
	 */
36
	private PropertyChangeListener childListener = new PropertyChangeListener() {
37
		public void propertyChange(PropertyChangeEvent evt) {
38
			if (evt.getPropertyName().equals(PaletteEntry.PROPERTY_VISIBLE)
39
					&& evt.getNewValue() == Boolean.FALSE
40
					&& activeEntry == evt.getSource()) {
41
				checkActiveEntry();
42
			}
43
		}
44
	};
45

    
46
	/** Type identifier **/
47
	public static final String PALETTE_TYPE_STACK = "$PaletteStack"; //$NON-NLS-1$
48

    
49
	/** Property name for the active entry **/
50
	public static final String PROPERTY_ACTIVE_ENTRY = "Active Entry"; //$NON-NLS-1$
51

    
52
	private PaletteEntry activeEntry;
53

    
54
	/**
55
	 * Creates a new PaletteStack with the given name, description, and icon.
56
	 * These will be shown only in the customize menu. Any of the given
57
	 * parameter can be <code>null</code>.
58
	 * 
59
	 * @param name
60
	 *            the stack's name
61
	 * @param desc
62
	 *            the stack's description
63
	 * @param icon
64
	 *            an ImageDescriptor for the stack's small icon
65
	 * @see PaletteContainer#PaletteContainer(String, String, ImageDescriptor,
66
	 *      Object)
67
	 */
68
	public PaletteStack(String name, String desc, ImageDescriptor icon) {
69
		super(name, desc, icon, PALETTE_TYPE_STACK);
70
		setUserModificationPermission(PERMISSION_LIMITED_MODIFICATION);
71
	}
72

    
73
	/**
74
	 * Returns true if this type can be a child of this container Only accepts
75
	 * ToolEntry's.
76
	 * 
77
	 * @param type
78
	 *            the type being requested
79
	 * @return true if this can be a child of this container
80
	 */
81
	public boolean acceptsType(Object type) {
82
		if (!type.equals(ToolEntry.PALETTE_TYPE_TOOL))
83
			return false;
84
		return super.acceptsType(type);
85
	}
86

    
87
	/**
88
	 * @see org.eclipse.gef.palette.PaletteContainer#add(int,
89
	 *      org.eclipse.gef.palette.PaletteEntry)
90
	 */
91
	public void add(int index, PaletteEntry entry) {
92
		super.add(index, entry);
93
		checkActiveEntry();
94
	}
95

    
96
	/**
97
	 * @see org.eclipse.gef.palette.PaletteContainer#addAll(java.util.List)
98
	 */
99
	public void addAll(List list) {
100
		super.addAll(list);
101
		checkActiveEntry();
102
		updateListeners(list, true);
103
	}
104

    
105
	/**
106
	 * Checks to make sure the active entry is up-to-date and sets it to the
107
	 * first child if it is <code>null</code>.
108
	 */
109
	private void checkActiveEntry() {
110
		PaletteEntry currEntry = activeEntry;
111
		if (!getChildren().contains(activeEntry))
112
			activeEntry = null;
113
		if (activeEntry == null && getChildren().size() > 0)
114
			activeEntry = (PaletteEntry) getChildren().get(0);
115
		if (activeEntry != null && !activeEntry.isVisible()) {
116
			for (Iterator iterator = getChildren().iterator(); iterator
117
					.hasNext();) {
118
				PaletteEntry child = (PaletteEntry) iterator.next();
119
				if (child.isVisible()) {
120
					activeEntry = child;
121
					break;
122
				}
123
				activeEntry = null;
124
			}
125
		}
126
		listeners.firePropertyChange(PROPERTY_ACTIVE_ENTRY, currEntry,
127
				activeEntry);
128
	}
129

    
130
	/**
131
	 * Returns the PaletteEntry referring to the active entry that should be
132
	 * shown in the palette.
133
	 * 
134
	 * @return active entry to be shown in the palette.
135
	 */
136
	public PaletteEntry getActiveEntry() {
137
		checkActiveEntry();
138
		return activeEntry;
139
	}
140

    
141
	/**
142
	 * @see org.eclipse.gef.palette.PaletteContainer#remove(org.eclipse.gef.palette.PaletteEntry)
143
	 */
144
	public void remove(PaletteEntry entry) {
145
		super.remove(entry);
146
		checkActiveEntry();
147
		updateListeners(Collections.singletonList(entry), false);
148
	}
149

    
150
	/**
151
	 * Sets the "active" child entry to the given PaletteEntry. This entry will
152
	 * be shown on the palette and will be checked in the menu.
153
	 * 
154
	 * @param entry
155
	 *            the entry to show on the palette.
156
	 */
157
	public void setActiveEntry(PaletteEntry entry) {
158
		PaletteEntry oldEntry = activeEntry;
159
		if (activeEntry != null
160
				&& (activeEntry.equals(entry) || !getChildren().contains(entry)))
161
			return;
162
		activeEntry = entry;
163
		listeners.firePropertyChange(PROPERTY_ACTIVE_ENTRY, oldEntry,
164
				activeEntry);
165
	}
166

    
167
	public void add(PaletteEntry entry) {
168
		super.add(entry);
169
		updateListeners(Collections.singletonList(entry), true);
170
	}
171

    
172
	public void setChildren(List list) {
173
		updateListeners(getChildren(), false);
174
		super.setChildren(list);
175
		updateListeners(getChildren(), true);
176
		checkActiveEntry();
177
	}
178

    
179
	/**
180
	 * Either adds or remove the <code>childListener</code> to each palette
181
	 * entry in the collection.
182
	 * 
183
	 * @param entries
184
	 *            a collection of <code>PaletteEntries</code>
185
	 * @param add
186
	 *            true if the lister should be added; false if it should be
187
	 *            removed
188
	 */
189
	private void updateListeners(Collection entries, boolean add) {
190
		for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
191
			PaletteEntry child = (PaletteEntry) iterator.next();
192
			if (add) {
193
				child.addPropertyChangeListener(childListener);
194
			} else {
195
				child.removePropertyChangeListener(childListener);
196
			}
197
		}
198
	}
199

    
200
}
(12-12/18)