KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > provisional > action > ToolBarManager2


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.jface.internal.provisional.action;
13
14 import org.eclipse.core.runtime.ListenerList;
15 import org.eclipse.jface.action.ToolBarManager;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.ToolBar;
21
22 /**
23  * Extends <code>ToolBarManager</code> to implement <code>IToolBarManager2</code>.
24  *
25  * <p>
26  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
27  * part of a work in progress. There is a guarantee neither that this API will
28  * work nor that it will remain the same. Please do not use this API without
29  * consulting with the Platform/UI team.
30  * </p>
31  *
32  * @since 3.2
33  */

34 public class ToolBarManager2 extends ToolBarManager implements IToolBarManager2 {
35
36     /**
37      * A collection of objects listening to changes to this manager. This
38      * collection is <code>null</code> if there are no listeners.
39      */

40     private transient ListenerList listenerList = null;
41     
42     /**
43      * Creates a new tool bar manager with the default SWT button style. Use the
44      * <code>createControl</code> method to create the tool bar control.
45      */

46     public ToolBarManager2() {
47         super();
48     }
49
50     /**
51      * Creates a tool bar manager with the given SWT button style. Use the
52      * <code>createControl</code> method to create the tool bar control.
53      *
54      * @param style
55      * the tool bar item style
56      * @see org.eclipse.swt.widgets.ToolBar for valid style bits
57      */

58     public ToolBarManager2(int style) {
59         super(style);
60     }
61
62     /**
63      * Creates a tool bar manager for an existing tool bar control. This manager
64      * becomes responsible for the control, and will dispose of it when the
65      * manager is disposed.
66      *
67      * @param toolbar
68      * the tool bar control
69      */

70     public ToolBarManager2(ToolBar toolbar) {
71         super(toolbar);
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.jface.action.IToolBarManager2#createControl2(org.eclipse.swt.widgets.Composite)
76      */

77     public Control createControl2(Composite parent) {
78         return createControl(parent);
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.jface.action.IToolBarManager2#getControl2()
83      */

84     public Control getControl2() {
85         return getControl();
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jface.action.IToolBarManager2#getItemCount()
90      */

91     public int getItemCount() {
92         ToolBar toolBar = getControl();
93         if (toolBar == null || toolBar.isDisposed()) {
94             return 0;
95         }
96         return toolBar.getItemCount();
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.jface.action.IToolBarManager2#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
101      */

102     public void addPropertyChangeListener(IPropertyChangeListener listener) {
103         if (listenerList == null) {
104             listenerList = new ListenerList(ListenerList.IDENTITY);
105         }
106
107         listenerList.add(listener);
108     }
109
110     /* (non-Javadoc)
111      * @see org.eclipse.jface.action.IToolBarManager2#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
112      */

113     public void removePropertyChangeListener(IPropertyChangeListener listener) {
114         if (listenerList != null) {
115             listenerList.remove(listener);
116
117             if (listenerList.isEmpty()) {
118                 listenerList = null;
119             }
120         }
121     }
122     
123     /**
124      * @return the listeners attached to this event manager.
125      * The listeners currently attached; may be empty, but never
126      * null.
127      *
128      */

129     protected final Object JavaDoc[] getListeners() {
130         final ListenerList list = listenerList;
131         if (list == null) {
132             return new Object JavaDoc[0];
133         }
134
135         return list.getListeners();
136     }
137
138     /*
139      * Notifies any property change listeners that a property has changed. Only
140      * listeners registered at the time this method is called are notified.
141      */

142     private void firePropertyChange(final PropertyChangeEvent event) {
143         final Object JavaDoc[] list = getListeners();
144         for (int i = 0; i < list.length; ++i) {
145             ((IPropertyChangeListener) list[i]).propertyChange(event);
146         }
147     }
148
149     /*
150      * Notifies any property change listeners that a property has changed. Only
151      * listeners registered at the time this method is called are notified. This
152      * method avoids creating an event object if there are no listeners
153      * registered, but calls firePropertyChange(PropertyChangeEvent) if there are.
154      */

155     private void firePropertyChange(final String JavaDoc propertyName,
156             final Object JavaDoc oldValue, final Object JavaDoc newValue) {
157         if (listenerList != null) {
158             firePropertyChange(new PropertyChangeEvent(this, propertyName,
159                     oldValue, newValue));
160         }
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.jface.action.ToolBarManager#relayout(org.eclipse.swt.widgets.ToolBar, int, int)
165      */

166     protected void relayout(ToolBar layoutBar, int oldCount, int newCount) {
167         super.relayout(layoutBar, oldCount, newCount);
168         firePropertyChange(PROP_LAYOUT, new Integer JavaDoc(oldCount), new Integer JavaDoc(newCount));
169     }
170 }
171
Popular Tags