KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > AbstractConsole


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  *******************************************************************************/

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 /**
25  * Common function for consoles.
26  * <p>
27  * Clients implementing consoles should subclass this class.
28  * </p>
29  * @since 3.0
30  */

31 public abstract class AbstractConsole implements IConsole {
32     
33     // property listeners
34
private ListenerList fListeners;
35     
36     /**
37      * Console name
38      */

39     private String JavaDoc fName = null;
40     
41     /**
42      * Console image descriptor
43      */

44     private ImageDescriptor fImageDescriptor = null;
45     
46     /**
47      * Console type identifier
48      */

49     private String JavaDoc fType = null;
50     
51     /**
52      * Used to notify this console of lifecycle methods <code>init()</code>
53      * and <code>dispose()</code>.
54      */

55     class Lifecycle implements IConsoleListener {
56         
57         /* (non-Javadoc)
58          * @see org.eclipse.ui.console.IConsoleListener#consolesAdded(org.eclipse.ui.console.IConsole[])
59          */

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         /* (non-Javadoc)
71          * @see org.eclipse.ui.console.IConsoleListener#consolesRemoved(org.eclipse.ui.console.IConsole[])
72          */

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     /**
85      * Notifies listeners of property changes, handling any exceptions
86      */

87     class PropertyNotifier implements ISafeRunnable {
88         
89         private IPropertyChangeListener fListener;
90         private PropertyChangeEvent fEvent;
91         
92         /**
93          * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
94          */

95         public void handleException(Throwable JavaDoc exception) {
96             IStatus status = new Status(IStatus.ERROR, ConsolePlugin.getUniqueIdentifier(), IConsoleConstants.INTERNAL_ERROR, ConsoleMessages.AbstractConsole_0, exception);
97             ConsolePlugin.log(status);
98         }
99
100         /**
101          * @see org.eclipse.core.runtime.ISafeRunnable#run()
102          */

103         public void run() throws Exception JavaDoc {
104             fListener.propertyChange(fEvent);
105         }
106
107         /**
108          * Notifies listeners of the property change
109          *
110          * @param event the event that describes the property that has changed
111          */

112         public void notify(PropertyChangeEvent event) {
113             if (fListeners == null) {
114                 return;
115             }
116             fEvent = event;
117             Object JavaDoc[] 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     /**
127      * Constructs a new console with the given name and image.
128      *
129      * @param name console name, cannot be <code>null</code>
130      * @param imageDescriptor image descriptor, or <code>null</code> if none
131      * @param autoLifecycle whether this console's lifecycle methods should be called
132      * automatically when it is added (<code>initialize()</code>) and removed
133      * (<code>destroy()</code>) from the console manager. When <code>false</code>,
134      * clients are responsible for calling the lifecycle methods.
135      * @since 3.1
136      */

137     public AbstractConsole(String JavaDoc name, ImageDescriptor imageDescriptor, boolean autoLifecycle) {
138         this(name, null, imageDescriptor, autoLifecycle);
139     }
140     
141     /**
142      * Constructs a new console with the given name, type, image and lifecycle.
143      *
144      * @param name console name, cannot be <code>null</code>
145      * @param type console type identifier or <code>null</code>
146      * @param imageDescriptor image descriptor, or <code>null</code> if none
147      * @param autoLifecycle whether this console's lifecycle methods should be called
148      * automatically when it is added (<code>initialize()</code>) and removed
149      * (<code>destroy()</code>) from the console manager. When <code>false</code>,
150      * clients are responsible for calling the lifecycle methods.
151      * @since 3.1
152      */

153     public AbstractConsole(String JavaDoc name, String JavaDoc 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     /**
163      * Constructs a new console with the given name and image. The console's lifecycle
164      * methods <code>init()</code> and <code>dispose()</code> will be called when the
165      * console is added and removed from the console manager.
166      *
167      * @param name console name, cannot be <code>null</code>
168      * @param imageDescriptor image descriptor, or <code>null</code> if none
169      */

170     public AbstractConsole(String JavaDoc name, ImageDescriptor imageDescriptor) {
171         this(name, imageDescriptor, true);
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.ui.console.IConsole#getName()
176      */

177     public String JavaDoc getName() {
178         return fName;
179     }
180
181     /**
182      * Sets the name of this console to the specified value and notifies
183      * property listeners of the change.
184      *
185      * @param name the new name
186      */

187     protected void setName(String JavaDoc name) {
188         if (!name.equals(fName)) {
189             String JavaDoc old = fName;
190             fName = name;
191             firePropertyChange(this, IBasicPropertyConstants.P_TEXT, old, name);
192         }
193     }
194     
195     /* (non-Javadoc)
196      * @see org.eclipse.ui.console.IConsole#getImageDescriptor()
197      */

198     public ImageDescriptor getImageDescriptor() {
199         return fImageDescriptor;
200     }
201     
202     /**
203      * Sets the image descriptor for this console to the specified value and notifies
204      * property listeners of the change.
205      *
206      * @param imageDescriptor the new image descriptor
207      */

208     protected void setImageDescriptor(ImageDescriptor imageDescriptor) {
209         ImageDescriptor old = fImageDescriptor;
210         fImageDescriptor =imageDescriptor;
211         firePropertyChange(this, IBasicPropertyConstants.P_IMAGE, old, imageDescriptor);
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.console.IConsole#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
216      */

217     public void addPropertyChangeListener(IPropertyChangeListener listener) {
218         if (fListeners == null) {
219             fListeners = new ListenerList();
220         }
221         fListeners.add(listener);
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.ui.console.IConsole#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
226      */

227     public void removePropertyChangeListener(IPropertyChangeListener listener) {
228         if (fListeners != null) {
229             fListeners.remove(listener);
230         }
231     }
232
233     /**
234      * Notify all listeners that the given property has changed.
235      *
236      * @param source the object on which a property has changed
237      * @param property identifier of the property that has changed
238      * @param oldValue the old value of the property, or <code>null</code>
239      * @param newValue the new value of the property, or <code>null</code>
240      */

241     public void firePropertyChange(Object JavaDoc source, String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc 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     /**
250      * Initializes this console. This method should only be called by clients managing a
251      * console's lifecycle, otherwise this method will be called automatically when this console
252      * is added to the console manager. The method is called once to initialize this console,
253      * marking the beginning of its lifecycle.
254      *
255      * @since 3.1
256      */

257     public final void initialize() {
258         init();
259     }
260     
261     /**
262      * Called when this console is added to the console manager. Default
263      * implementation does nothing. Subclasses may override.
264      * <p>
265      * Since 3.1, this method is only called automatically if this console was
266      * created with an automatic lifecycle.
267      * </p>
268      */

269     protected void init() {
270     }
271     
272     /**
273      * Disposes this console. This method should only be called by clients managing a
274      * console's lifecycle, otherwise this method will be called automatically when this
275      * console is removed from the console manager. The method is called once to dispose
276      * this console, after which this console will no longer be used.
277      *
278      * @since 3.1
279      */

280     public final void destroy() {
281         dispose();
282     }
283     
284     /**
285      * Called when this console is removed from the console manager. Default
286      * implementation does nothing. Subclasses may override.
287      * <p>
288      * Since 3.1, this methods is only called automatically if this console was
289      * created with an automatic lifecycle.
290      * </p>
291      */

292     protected void dispose() {
293     }
294     
295     /**
296      * Shows this console in all console views. This console will be become visible
297      * if another console is currently pinned.
298      *
299      * @since 3.1
300      */

301     public void activate() {
302         ConsolePlugin.getDefault().getConsoleManager().showConsoleView(this);
303     }
304     
305     /**
306      * Sets this console's type identifier.
307      *
308      * @param typeIdentifier the type identifier for this console
309      * @since 3.1
310      */

311     protected void setType(String JavaDoc typeIdentifier) {
312         fType = typeIdentifier;
313     }
314     
315     /**
316      * @see org.eclipse.ui.console.IConsole#getType()
317      * @since 3.1
318      */

319     public String JavaDoc getType() {
320         return fType;
321     }
322     
323     /**
324      * Returns the help context identifier for this console, or <code>null</code>
325      * if none. When a non-<code>null</code> value is returned the associated help
326      * will be installed for this console.
327      *
328      * @return help context id or <code>null</code>
329      * @since 3.2
330      */

331     public String JavaDoc getHelpContextId() {
332         return null;
333     }
334     
335 }
336
Popular Tags