1 11 package org.eclipse.core.commands; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.commands.common.NamedHandleObject; 18 import org.eclipse.core.internal.commands.util.Util; 19 20 29 public final class Category extends NamedHandleObject { 30 31 35 private Collection categoryListeners; 36 37 48 Category(final String id) { 49 super(id); 50 } 51 52 59 public final void addCategoryListener( 60 final ICategoryListener categoryListener) { 61 if (categoryListener == null) { 62 throw new NullPointerException (); 63 } 64 if (categoryListeners == null) { 65 categoryListeners = new ArrayList (); 66 } 67 if (!categoryListeners.contains(categoryListener)) { 68 categoryListeners.add(categoryListener); 69 } 70 } 71 72 86 public final void define(final String name, final String description) { 87 if (name == null) { 88 throw new NullPointerException ( 89 "The name of a command cannot be null"); } 91 92 final boolean definedChanged = !this.defined; 93 this.defined = true; 94 95 final boolean nameChanged = !Util.equals(this.name, name); 96 this.name = name; 97 98 final boolean descriptionChanged = !Util.equals(this.description, 99 description); 100 this.description = description; 101 102 fireCategoryChanged(new CategoryEvent(this, definedChanged, 103 descriptionChanged, nameChanged)); 104 } 105 106 113 private final void fireCategoryChanged(final CategoryEvent categoryEvent) { 114 if (categoryEvent == null) { 115 throw new NullPointerException (); 116 } 117 if (categoryListeners != null) { 118 final Iterator listenerItr = categoryListeners.iterator(); 119 while (listenerItr.hasNext()) { 120 final ICategoryListener listener = (ICategoryListener) listenerItr 121 .next(); 122 listener.categoryChanged(categoryEvent); 123 } 124 } 125 } 126 127 134 public final void removeCategoryListener( 135 final ICategoryListener categoryListener) { 136 if (categoryListener == null) { 137 throw new NullPointerException (); 138 } 139 140 if (categoryListeners != null) { 141 categoryListeners.remove(categoryListener); 142 } 143 } 144 145 150 public String toString() { 151 if (string == null) { 152 final StringBuffer stringBuffer = new StringBuffer (); 153 stringBuffer.append("Category("); stringBuffer.append(id); 155 stringBuffer.append(','); 156 stringBuffer.append(name); 157 stringBuffer.append(','); 158 stringBuffer.append(description); 159 stringBuffer.append(','); 160 stringBuffer.append(defined); 161 stringBuffer.append(')'); 162 string = stringBuffer.toString(); 163 } 164 return string; 165 } 166 167 172 public void undefine() { 173 string = null; 174 175 final boolean definedChanged = defined; 176 defined = false; 177 178 final boolean nameChanged = name != null; 179 name = null; 180 181 final boolean descriptionChanged = description != null; 182 description = null; 183 184 fireCategoryChanged(new CategoryEvent(this, definedChanged, 185 descriptionChanged, nameChanged)); 186 } 187 188 } 189 | Popular Tags |