1 11 package org.eclipse.ui.console; 12 13 import org.eclipse.core.runtime.ISafeRunnable; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.core.runtime.SafeRunner; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.viewers.IBasicPropertyConstants; 22 import org.eclipse.ui.internal.console.ConsoleMessages; 23 24 31 public abstract class AbstractConsole implements IConsole { 32 33 private ListenerList fListeners; 35 36 39 private String fName = null; 40 41 44 private ImageDescriptor fImageDescriptor = null; 45 46 49 private String fType = null; 50 51 55 class Lifecycle implements IConsoleListener { 56 57 60 public void consolesAdded(IConsole[] consoles) { 61 for (int i = 0; i < consoles.length; i++) { 62 IConsole console = consoles[i]; 63 if (console == AbstractConsole.this) { 64 initialize(); 65 } 66 } 67 68 } 69 70 73 public void consolesRemoved(IConsole[] consoles) { 74 for (int i = 0; i < consoles.length; i++) { 75 IConsole console = consoles[i]; 76 if (console == AbstractConsole.this) { 77 ConsolePlugin.getDefault().getConsoleManager().removeConsoleListener(this); 78 destroy(); 79 } 80 } 81 } 82 } 83 84 87 class PropertyNotifier implements ISafeRunnable { 88 89 private IPropertyChangeListener fListener; 90 private PropertyChangeEvent fEvent; 91 92 95 public void handleException(Throwable exception) { 96 IStatus status = new Status(IStatus.ERROR, ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, ConsoleMessages.AbstractConsole_0, exception); 97 ConsolePlugin.log(status); 98 } 99 100 103 public void run() throws Exception { 104 fListener.propertyChange(fEvent); 105 } 106 107 112 public void notify(PropertyChangeEvent event) { 113 if (fListeners == null) { 114 return; 115 } 116 fEvent = event; 117 Object [] copiedListeners= fListeners.getListeners(); 118 for (int i= 0; i < copiedListeners.length; i++) { 119 fListener = (IPropertyChangeListener)copiedListeners[i]; 120 SafeRunner.run(this); 121 } 122 fListener = null; 123 } 124 } 125 126 137 public AbstractConsole(String name, ImageDescriptor imageDescriptor, boolean autoLifecycle) { 138 this(name, null, imageDescriptor, autoLifecycle); 139 } 140 141 153 public AbstractConsole(String name, String type, ImageDescriptor imageDescriptor, boolean autoLifecycle) { 154 setName(name); 155 setType(type); 156 setImageDescriptor(imageDescriptor); 157 if (autoLifecycle) { 158 ConsolePlugin.getDefault().getConsoleManager().addConsoleListener(new Lifecycle()); 159 } 160 } 161 162 170 public AbstractConsole(String name, ImageDescriptor imageDescriptor) { 171 this(name, imageDescriptor, true); 172 } 173 174 177 public String getName() { 178 return fName; 179 } 180 181 187 protected void setName(String name) { 188 if (!name.equals(fName)) { 189 String old = fName; 190 fName = name; 191 firePropertyChange(this, IBasicPropertyConstants.P_TEXT, old, name); 192 } 193 } 194 195 198 public ImageDescriptor getImageDescriptor() { 199 return fImageDescriptor; 200 } 201 202 208 protected void setImageDescriptor(ImageDescriptor imageDescriptor) { 209 ImageDescriptor old = fImageDescriptor; 210 fImageDescriptor =imageDescriptor; 211 firePropertyChange(this, IBasicPropertyConstants.P_IMAGE, old, imageDescriptor); 212 } 213 214 217 public void addPropertyChangeListener(IPropertyChangeListener listener) { 218 if (fListeners == null) { 219 fListeners = new ListenerList(); 220 } 221 fListeners.add(listener); 222 } 223 224 227 public void removePropertyChangeListener(IPropertyChangeListener listener) { 228 if (fListeners != null) { 229 fListeners.remove(listener); 230 } 231 } 232 233 241 public void firePropertyChange(Object source, String property, Object oldValue, Object newValue) { 242 if (fListeners == null) { 243 return; 244 } 245 PropertyNotifier notifier = new PropertyNotifier(); 246 notifier.notify(new PropertyChangeEvent(source, property, oldValue, newValue)); 247 } 248 249 257 public final void initialize() { 258 init(); 259 } 260 261 269 protected void init() { 270 } 271 272 280 public final void destroy() { 281 dispose(); 282 } 283 284 292 protected void dispose() { 293 } 294 295 301 public void activate() { 302 ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this); 303 } 304 305 311 protected void setType(String typeIdentifier) { 312 fType = typeIdentifier; 313 } 314 315 319 public String getType() { 320 return fType; 321 } 322 323 331 public String getHelpContextId() { 332 return null; 333 } 334 335 } 336 | Popular Tags |