1 11 12 package org.eclipse.core.commands; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import org.eclipse.core.commands.common.NamedHandleObject; 19 import org.eclipse.core.commands.common.NotDefinedException; 20 21 32 abstract class NamedHandleObjectWithState extends NamedHandleObject implements 33 IObjectWithState { 34 35 39 private static final String [] NO_STATE = new String [0]; 40 41 45 private Map states = null; 46 47 53 protected NamedHandleObjectWithState(final String id) { 54 super(id); 55 } 56 57 public void addState(final String stateId, final State state) { 58 if (state == null) { 59 throw new NullPointerException ("Cannot add a null state"); } 61 62 if (states == null) { 63 states = new HashMap (3); 64 } 65 states.put(stateId, state); 66 } 67 68 public final String getDescription() throws NotDefinedException { 69 final String description = super.getDescription(); 71 final State descriptionState = getState(INamedHandleStateIds.DESCRIPTION); 72 if (descriptionState != null) { 73 final Object value = descriptionState.getValue(); 74 if (value != null) { 75 return value.toString(); 76 } 77 } 78 79 return description; 80 } 81 82 public final String getName() throws NotDefinedException { 83 final String name = super.getName(); 85 final State nameState = getState(INamedHandleStateIds.NAME); 86 if (nameState != null) { 87 final Object value = nameState.getValue(); 88 if (value != null) { 89 return value.toString(); 90 } 91 } 92 93 return name; 94 } 95 96 public final State getState(final String stateId) { 97 if ((states == null) || (states.isEmpty())) { 98 return null; 99 } 100 101 return (State) states.get(stateId); 102 } 103 104 public final String [] getStateIds() { 105 if ((states == null) || (states.isEmpty())) { 106 return NO_STATE; 107 } 108 109 final Set stateIds = states.keySet(); 110 return (String []) stateIds.toArray(new String [stateIds.size()]); 111 } 112 113 public void removeState(final String id) { 114 if (id == null) { 115 throw new NullPointerException ("Cannot remove a null id"); } 117 118 if (states != null) { 119 states.remove(id); 120 if (states.isEmpty()) { 121 states = null; 122 } 123 } 124 } 125 126 } 127 | Popular Tags |