comparison dwtx/core/commands/Category.d @ 3:6518c18a01f7

eclipse.core package without osgi dependencies
author Frank Benoit <benoit@tionex.de>
date Wed, 26 Mar 2008 00:57:19 +0100
parents
children ea8ff534f622
comparison
equal deleted inserted replaced
2:a012107a911c 3:6518c18a01f7
1 /*******************************************************************************
2 * Copyright (c) 2005, 2006 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.core.commands.Category;
14
15 import tango.util.collection.ArraySeq;
16
17 import dwtx.core.commands.common.NamedHandleObject;
18 import dwtx.core.internal.commands.util.Util;
19 import dwtx.core.commands.ICategoryListener;
20 import dwtx.core.commands.CategoryEvent;
21
22 import dwt.dwthelper.utils;
23 import tango.text.convert.Format;
24
25 /**
26 * <p>
27 * A logical group for a set of commands. A command belongs to exactly one
28 * category. The category has no functional effect, but may be used in graphical
29 * tools that want to group the set of commands somehow.
30 * </p>
31 *
32 * @since 3.1
33 */
34 public final class Category : NamedHandleObject {
35
36 /**
37 * A collection of objects listening to changes to this category. This
38 * collection is <code>null</code> if there are no listeners.
39 */
40 private ArraySeq!(ICategoryListener) categoryListeners;
41
42 /**
43 * Constructs a new instance of <code>Category</code> based on the given
44 * identifier. When a category is first constructed, it is undefined.
45 * Category should only be constructed by the <code>CommandManager</code>
46 * to ensure that identifier remain unique.
47 *
48 * @param id
49 * The identifier for the category. This value must not be
50 * <code>null</code>, and must be unique amongst all
51 * categories.
52 */
53 this(String id) {
54 super(id);
55 }
56
57 /**
58 * Adds a listener to this category that will be notified when this
59 * category's state changes.
60 *
61 * @param categoryListener
62 * The listener to be added; must not be <code>null</code>.
63 */
64 public final void addCategoryListener(
65 ICategoryListener categoryListener) {
66 if (categoryListener is null) {
67 throw new NullPointerException();
68 }
69 if (categoryListeners is null) {
70 categoryListeners = new ArraySeq!(ICategoryListener);
71 }
72 if (!categoryListeners.contains(categoryListener)) {
73 categoryListeners.append(categoryListener);
74 }
75 }
76
77 /**
78 * <p>
79 * Defines this category by giving it a name, and possibly a description as
80 * well. The defined property automatically becomes <code>true</code>.
81 * </p>
82 * <p>
83 * Notification is sent to all listeners that something has changed.
84 * </p>
85 *
86 * @param name
87 * The name of this command; must not be <code>null</code>.
88 * @param description
89 * The description for this command; may be <code>null</code>.
90 */
91 public final void define(String name, String description) {
92 if (name is null) {
93 throw new NullPointerException(
94 "The name of a command cannot be null"); //$NON-NLS-1$
95 }
96
97 bool definedChanged = !this.defined;
98 this.defined = true;
99
100 bool nameChanged = !Util.equals(this.name, name);
101 this.name = name;
102
103 bool descriptionChanged = !Util.equals(this.description,
104 description);
105 this.description = description;
106
107 fireCategoryChanged(new CategoryEvent(this, definedChanged,
108 descriptionChanged, nameChanged));
109 }
110
111 /**
112 * Notifies the listeners for this category that it has changed in some way.
113 *
114 * @param categoryEvent
115 * The event to send to all of the listener; must not be
116 * <code>null</code>.
117 */
118 private final void fireCategoryChanged(CategoryEvent categoryEvent) {
119 if (categoryEvent is null) {
120 throw new NullPointerException();
121 }
122 if (categoryListeners !is null) {
123 foreach( listener; categoryListeners ){
124 listener.categoryChanged(categoryEvent);
125 }
126 }
127 }
128
129 /**
130 * Removes a listener from this category.
131 *
132 * @param categoryListener
133 * The listener to be removed; must not be <code>null</code>.
134 *
135 */
136 public final void removeCategoryListener(
137 ICategoryListener categoryListener) {
138 if (categoryListener is null) {
139 throw new NullPointerException();
140 }
141
142 if (categoryListeners !is null) {
143 categoryListeners.remove(categoryListener);
144 }
145 }
146
147 /*
148 * (non-Javadoc)
149 *
150 * @see dwtx.core.commands.common.HandleObject#toString()
151 */
152 public String toString() {
153 if (string is null) {
154 string = Format( "Category({},{},{},{})", id, name, description, defined );
155 }
156 return string;
157 }
158
159 /*
160 * (non-Javadoc)
161 *
162 * @see dwtx.core.commands.common.HandleObject#undefine()
163 */
164 public void undefine() {
165 string = null;
166
167 final bool definedChanged = defined;
168 defined = false;
169
170 final bool nameChanged = name !is null;
171 name = null;
172
173 final bool descriptionChanged = description !is null;
174 description = null;
175
176 fireCategoryChanged(new CategoryEvent(this, definedChanged,
177 descriptionChanged, nameChanged));
178 }
179
180 }