Project

General

Profile

Download (11.4 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.PropertyChangeListener;
14
import java.beans.PropertyChangeSupport;
15

    
16
import org.eclipse.jface.resource.ImageDescriptor;
17

    
18
/**
19
 * Root class (statically) for the palette model.
20
 * 
21
 * @author Pratik Shah
22
 */
23
public class PaletteEntry {
24

    
25
	/**
26
	 * Property name for the entry's small icon
27
	 */
28
	public static final String PROPERTY_SMALL_ICON = "Small Icon"; //$NON-NLS-1$
29

    
30
	/**
31
	 * Property name for the entry's type
32
	 */
33
	public static final String PROPERTY_TYPE = "Type"; //$NON-NLS-1$
34

    
35
	/**
36
	 * Property name for the entry's large icon
37
	 */
38
	public static final String PROPERTY_LARGE_ICON = "Large Icon"; //$NON-NLS-1$
39

    
40
	/**
41
	 * Property name for the entry's label (name)
42
	 */
43
	public static final String PROPERTY_LABEL = "Name"; //$NON-NLS-1$
44

    
45
	/**
46
	 * Property name for the entry's description
47
	 */
48
	public static final String PROPERTY_DESCRIPTION = "Description"; //$NON-NLS-1$
49

    
50
	/**
51
	 * Property name for the entry's hidden status
52
	 */
53
	public static final String PROPERTY_VISIBLE = "Visible"; //$NON-NLS-1$
54

    
55
	/**
56
	 * Property name for the entry's default staus
57
	 */
58
	public static final String PROPERTY_DEFAULT = "Default"; //$NON-NLS-1$
59

    
60
	/**
61
	 * Property name for the entry's parent
62
	 */
63
	public static final String PROPERTY_PARENT = "Parent"; //$NON-NLS-1$
64

    
65
	/**
66
	 * Type unknown
67
	 */
68
	public static final String PALETTE_TYPE_UNKNOWN = "Palette_type_Unknown";//$NON-NLS-1$
69

    
70
	/**
71
	 * No changes can be made to a PaletteEntry with this permission level.
72
	 */
73
	public static final int PERMISSION_NO_MODIFICATION = 1;
74

    
75
	/**
76
	 * Entries with this permission level can only be hidden/shown.
77
	 */
78
	public static final int PERMISSION_HIDE_ONLY = 3;
79

    
80
	/**
81
	 * Any property of entries with this level of permission can be changed;
82
	 * however, they cannot be deleted from the palette. The children
83
	 * PaletteContainers with this permission level can be reordered within that
84
	 * container (however, cross-container moving is not allowed).
85
	 * 
86
	 */
87
	public static final int PERMISSION_LIMITED_MODIFICATION = 7;
88

    
89
	/**
90
	 * All modifications allowed.
91
	 */
92
	public static final int PERMISSION_FULL_MODIFICATION = 15;
93

    
94
	/**
95
	 * PropertyChangeSupport
96
	 */
97
	protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);
98

    
99
	private PaletteContainer parent;
100
	private String label, id;
101
	private String shortDescription;
102
	private ImageDescriptor iconSmall;
103
	private ImageDescriptor iconLarge;
104
	private boolean visible = true;
105
	private int permission = PERMISSION_FULL_MODIFICATION;
106
	private Object type = PaletteEntry.PALETTE_TYPE_UNKNOWN;
107

    
108
	/**
109
	 * Constructor
110
	 * <p>
111
	 * Any parameter can be <code>null</code>
112
	 * </p>
113
	 * 
114
	 * @param label
115
	 *            The entry's name
116
	 * @param shortDescription
117
	 *            The entry's description
118
	 */
119
	public PaletteEntry(String label, String shortDescription) {
120
		this(label, shortDescription, null, null, null);
121
	}
122

    
123
	/**
124
	 * Constructor
125
	 * <p>
126
	 * Any parameter can be <code>null</code>
127
	 * </p>
128
	 * 
129
	 * @param label
130
	 *            The entry's name
131
	 * @param shortDescription
132
	 *            The entry's description
133
	 * @param type
134
	 *            The entry's type
135
	 */
136
	public PaletteEntry(String label, String shortDescription, Object type) {
137
		this(label, shortDescription, null, null, type);
138
	}
139

    
140
	/**
141
	 * Constructor
142
	 * <p>
143
	 * Any parameter can be <code>null</code>
144
	 * </p>
145
	 * 
146
	 * @param label
147
	 *            The entry's name
148
	 * @param shortDescription
149
	 *            The entry's description
150
	 * @param iconSmall
151
	 *            The small icon to represent this entry
152
	 * @param iconLarge
153
	 *            The large icon to represent this entry
154
	 */
155
	public PaletteEntry(String label, String shortDescription,
156
			ImageDescriptor iconSmall, ImageDescriptor iconLarge) {
157
		this(label, shortDescription, iconSmall, iconLarge, null);
158
	}
159

    
160
	/**
161
	 * Constructor
162
	 * <p>
163
	 * Any parameter can be <code>null</code>
164
	 * </p>
165
	 * 
166
	 * @param label
167
	 *            The entry's name
168
	 * @param shortDescription
169
	 *            The entry's description
170
	 * @param iconSmall
171
	 *            The small icon to represent this entry
172
	 * @param iconLarge
173
	 *            The large icon to represent this entry
174
	 * @param type
175
	 *            The entry's type
176
	 */
177
	public PaletteEntry(String label, String shortDescription,
178
			ImageDescriptor iconSmall, ImageDescriptor iconLarge, Object type) {
179
		this(label, shortDescription, iconSmall, iconLarge, type, null);
180
	}
181

    
182
	/**
183
	 * Constructor
184
	 * <p>
185
	 * Any parameter can be <code>null</code>
186
	 * </p>
187
	 * 
188
	 * @param label
189
	 *            The entry's name
190
	 * @param shortDescription
191
	 *            The entry's description
192
	 * @param smallIcon
193
	 *            The small icon to represent this entry
194
	 * @param largeIcon
195
	 *            The large icon to represent this entry
196
	 * @param type
197
	 *            The entry's type
198
	 * @param id
199
	 *            The entry's id (preferrably unique)
200
	 */
201
	public PaletteEntry(String label, String shortDescription,
202
			ImageDescriptor smallIcon, ImageDescriptor largeIcon, Object type,
203
			String id) {
204
		setLabel(label);
205
		setDescription(shortDescription);
206
		setSmallIcon(smallIcon);
207
		setLargeIcon(largeIcon);
208
		setType(type);
209
		setId(id);
210
	}
211

    
212
	/**
213
	 * A listener can only be added once. Adding it more than once will do
214
	 * nothing.
215
	 * 
216
	 * @param listener
217
	 *            the PropertyChangeListener that is to be notified of changes
218
	 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
219
	 */
220
	public void addPropertyChangeListener(PropertyChangeListener listener) {
221
		listeners.removePropertyChangeListener(listener);
222
		listeners.addPropertyChangeListener(listener);
223
	}
224

    
225
	/**
226
	 * @return a short desecription describing this entry.
227
	 */
228
	public String getDescription() {
229
		return shortDescription;
230
	}
231

    
232
	/**
233
	 * Returns the id. If no ID has been set (or it is <code>null</code>), an
234
	 * empty String will be returned.
235
	 * 
236
	 * @return String id
237
	 */
238
	public String getId() {
239
		if (id == null)
240
			return ""; //$NON-NLS-1$
241
		return id;
242
	}
243

    
244
	/**
245
	 * @return the label for this entry.
246
	 */
247
	public String getLabel() {
248
		return label;
249
	}
250

    
251
	/**
252
	 * @return a large icon representing this entry.
253
	 */
254
	public ImageDescriptor getLargeIcon() {
255
		return iconLarge;
256
	}
257

    
258
	/**
259
	 * @return the parent container of this entry
260
	 */
261
	public PaletteContainer getParent() {
262
		return parent;
263
	}
264

    
265
	/**
266
	 * @return a small icon representing the entry.
267
	 */
268
	public ImageDescriptor getSmallIcon() {
269
		return iconSmall;
270
	}
271

    
272
	/**
273
	 * @return the type of this entry. Useful for different interpretations of
274
	 *         the palette model.
275
	 */
276
	public Object getType() {
277
		return type;
278
	}
279

    
280
	/**
281
	 * Returned values are from amongst the following:
282
	 * <UL>
283
	 * <LI>PERMISSION_NO_MODIFICATION</LI>
284
	 * <LI>PERMISSION_HIDE_ONLY</LI>
285
	 * <LI>PERMISSION_LIMITED_MODIFICATION</LI>
286
	 * <LI>PERMISSION_FULL_MODIFICATION</LI>
287
	 * </UL>
288
	 * 
289
	 * @return the permission level for this entry.
290
	 * @see #setUserModificationPermission(int)
291
	 */
292
	public int getUserModificationPermission() {
293
		return permission;
294
	}
295

    
296
	/**
297
	 * @return whether or not this entry is visible. An entry that is not
298
	 *         visible is not shown on the palette.
299
	 */
300
	public boolean isVisible() {
301
		return visible;
302
	}
303

    
304
	/**
305
	 * @param listener
306
	 *            the PropertyChangeListener that is not to be notified anymore
307
	 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
308
	 */
309
	public void removePropertyChangeListener(PropertyChangeListener listener) {
310
		listeners.removePropertyChangeListener(listener);
311
	}
312

    
313
	/**
314
	 * Mutator method for description
315
	 * 
316
	 * @param s
317
	 *            The new description
318
	 */
319
	public void setDescription(String s) {
320
		if (s == null && shortDescription == null) {
321
			return;
322
		}
323

    
324
		if (s == null || !s.equals(shortDescription)) {
325
			String oldDescrption = shortDescription;
326
			shortDescription = s;
327
			listeners.firePropertyChange(PROPERTY_DESCRIPTION, oldDescrption,
328
					shortDescription);
329
		}
330
	}
331

    
332
	/**
333
	 * Sets the id. Can be <code>null</code>.
334
	 * 
335
	 * @param id
336
	 *            The new id to be set
337
	 */
338
	public void setId(String id) {
339
		this.id = id;
340
	}
341

    
342
	/**
343
	 * Mutator method for label
344
	 * 
345
	 * @param s
346
	 *            The new name
347
	 */
348
	public void setLabel(String s) {
349
		if (s == null && label == null) {
350
			return;
351
		}
352

    
353
		if (s == null || !s.equals(label)) {
354
			String oldLabel = label;
355
			label = s;
356
			listeners.firePropertyChange(PROPERTY_LABEL, oldLabel, label);
357
		}
358
	}
359

    
360
	/**
361
	 * Mutator method for large icon
362
	 * 
363
	 * @param icon
364
	 *            The large icon to represent this entry
365
	 */
366
	public void setLargeIcon(ImageDescriptor icon) {
367
		if (icon != iconLarge) {
368
			ImageDescriptor oldIcon = iconLarge;
369
			iconLarge = icon;
370
			listeners.firePropertyChange(PROPERTY_LARGE_ICON, oldIcon,
371
					iconLarge);
372
		}
373
	}
374

    
375
	/**
376
	 * Sets the parent of this entry
377
	 * 
378
	 * @param newParent
379
	 *            The parent PaletteContainer
380
	 */
381
	public void setParent(PaletteContainer newParent) {
382
		if (parent != newParent) {
383
			PaletteContainer oldParent = parent;
384
			parent = newParent;
385
			listeners.firePropertyChange(PROPERTY_PARENT, oldParent, parent);
386
		}
387
	}
388

    
389
	/**
390
	 * Mutator method for small icon
391
	 * 
392
	 * @param icon
393
	 *            The new small icon to represent this entry
394
	 */
395
	public void setSmallIcon(ImageDescriptor icon) {
396
		if (icon != iconSmall) {
397
			ImageDescriptor oldIcon = iconSmall;
398
			iconSmall = icon;
399
			listeners.firePropertyChange(PROPERTY_SMALL_ICON, oldIcon, icon);
400
		}
401
	}
402

    
403
	/**
404
	 * Mutator method for type
405
	 * 
406
	 * @param newType
407
	 *            The new type
408
	 */
409
	public void setType(Object newType) {
410
		if (newType == null && type == null) {
411
			return;
412
		}
413

    
414
		if (type == null || !type.equals(newType)) {
415
			Object oldType = type;
416
			type = newType;
417
			listeners.firePropertyChange(PROPERTY_TYPE, oldType, type);
418
		}
419
	}
420

    
421
	/**
422
	 * Permissions are not checked before making modifications. Clients should
423
	 * check the permission before invoking a modification. Sub-classes may
424
	 * extend the set of permissions. Current set has:
425
	 * <UL>
426
	 * <LI>PERMISSION_NO_MODIFICATION</LI>
427
	 * <LI>PERMISSION_HIDE_ONLY</LI>
428
	 * <LI>PERMISSION_LIMITED_MODIFICATION</LI>
429
	 * <LI>PERMISSION_FULL_MODIFICATION</LI>
430
	 * </UL>
431
	 * Default is <code>PERMISSION_FULL_MODIFICATION</code>
432
	 * 
433
	 * @param permission
434
	 *            One of the above-specified permission levels
435
	 */
436
	public void setUserModificationPermission(int permission) {
437
		this.permission = permission;
438
	}
439

    
440
	/**
441
	 * Makes this entry visible or invisible. An invisible entry does not show
442
	 * up on the palette.
443
	 * 
444
	 * @param newVal
445
	 *            The new boolean indicating whether the entry is visible or not
446
	 */
447
	public void setVisible(boolean newVal) {
448
		if (newVal != visible) {
449
			visible = newVal;
450
			listeners.firePropertyChange(PROPERTY_VISIBLE, !visible, visible);
451
		}
452
	}
453

    
454
	/**
455
	 * @see java.lang.Object#toString()
456
	 */
457
	public String toString() {
458
		return "Palette Entry (" + (label != null ? label : "") //$NON-NLS-2$//$NON-NLS-1$
459
				+ ")"; //$NON-NLS-1$
460
	}
461

    
462
}
(7-7/18)