KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > CompareViewerPane


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.compare;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.jface.action.ToolBarManager;
15 import org.eclipse.jface.viewers.*;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.accessibility.*;
18 import org.eclipse.swt.custom.CLabel;
19 import org.eclipse.swt.custom.ViewForm;
20 import org.eclipse.swt.events.*;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.*;
24
25 /**
26  * A <code>CompareViewerPane</code> is a convenience class which installs a
27  * <code>CLabel</code> and a <code>Toolbar</code> in a <code>ViewForm</code>.
28  * <P>
29  * Double clicking onto the <code>CompareViewerPane</code>'s title bar maximizes
30  * the <code>CompareViewerPane</code> to the size of an enclosing <code>Splitter</code>
31  * (if there is one).
32  * If more <code>Splitters</code> are nested maximizing walks up and
33  * maximizes to the outermost <code>Splitter</code>.
34  *
35  * @since 2.0
36  */

37 public class CompareViewerPane extends ViewForm implements ISelectionProvider,
38         IDoubleClickListener, ISelectionChangedListener, IOpenListener, IAdaptable {
39     
40     private ToolBarManager fToolBarManager;
41     private Object JavaDoc fInput;
42     private ListenerList fSelectionListeners= new ListenerList();
43     private ListenerList fDoubleClickListener= new ListenerList();
44     private ListenerList fOpenListener= new ListenerList();
45
46     /**
47      * Constructs a new instance of this class given its parent
48      * and a style value describing its behavior and appearance.
49      *
50      * @param container a widget which will be the container of the new instance (cannot be null)
51      * @param style the style of widget to construct
52      *
53      * @exception IllegalArgumentException <ul>
54      * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
55      * </ul>
56      * @exception org.eclipse.swt.SWTException <ul>
57      * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
58      * </ul>
59      */

60     public CompareViewerPane(Composite container, int style) {
61         super(container, style);
62         
63         marginWidth= 0;
64         marginHeight= 0;
65         
66         CLabel label= new CLabel(this, SWT.NONE) {
67             public Point computeSize(int wHint, int hHint, boolean changed) {
68                 return super.computeSize(wHint, Math.max(24, hHint), changed);
69             }
70         };
71         setTopLeft(label);
72         
73         MouseAdapter ml= new MouseAdapter() {
74             public void mouseDoubleClick(MouseEvent e) {
75                 Control content= getContent();
76                 if (content != null && content.getBounds().contains(e.x, e.y))
77                     return;
78                 Control parent= getParent();
79                 if (parent instanceof Splitter)
80                     ((Splitter)parent).setMaximizedControl(CompareViewerPane.this);
81             }
82         };
83                 
84         addMouseListener(ml);
85         label.addMouseListener(ml);
86         
87         addDisposeListener(new DisposeListener() {
88             public void widgetDisposed(DisposeEvent e) {
89                 if (fToolBarManager != null) {
90                     fToolBarManager.removeAll();
91                     fToolBarManager.dispose();
92                 }
93                 fInput= null;
94                 fSelectionListeners= null;
95                 setImage(null);
96             }
97         });
98     }
99     
100     /**
101      * Set the pane's title text.
102      * The value <code>null</code> clears it.
103      *
104      * @param label the text to be displayed in the pane or null
105      */

106     public void setText(String JavaDoc label) {
107         CLabel cl= (CLabel) getTopLeft();
108         if (cl != null && !cl.isDisposed())
109             cl.setText(label);
110     }
111     
112     /**
113      * Set the pane's title Image.
114      * The value <code>null</code> clears it.
115      *
116      * @param image the image to be displayed in the pane or null
117      */

118     public void setImage(Image image) {
119         CLabel cl= (CLabel) getTopLeft();
120         if (cl != null)
121             cl.setImage(image);
122     }
123     
124     /**
125      * Returns a <code>ToolBarManager</code> if the given parent is a
126      * <code>CompareViewerPane</code> or <code>null</code> otherwise.
127      *
128      * @param parent a <code>Composite</code> or <code>null</code>
129      * @return a <code>ToolBarManager</code> if the given parent is a <code>CompareViewerPane</code> otherwise <code>null</code>
130      */

131     public static ToolBarManager getToolBarManager(Composite parent) {
132         if (parent instanceof CompareViewerPane) {
133             CompareViewerPane pane= (CompareViewerPane) parent;
134             return pane.getToolBarManager();
135         }
136         return null;
137     }
138
139     /**
140      * Clears tool items in the <code>CompareViewerPane</code>'s control bar.
141      *
142      * @param parent a <code>Composite</code> or <code>null</code>
143      */

144     public static void clearToolBar(Composite parent) {
145         ToolBarManager tbm= getToolBarManager(parent);
146         if (tbm != null) {
147             tbm.removeAll();
148             tbm.update(true);
149         }
150     }
151     
152     //---- private stuff
153

154     private ToolBarManager getToolBarManager() {
155         if (fToolBarManager != null && fToolBarManager.getControl() == null)
156             return null;
157         if (fToolBarManager == null) {
158             final ToolBar tb = new ToolBar(this, SWT.FLAT);
159             setTopCenter(tb);
160             fToolBarManager = new ToolBarManager(tb);
161             tb.getAccessible().addAccessibleListener(new AccessibleAdapter() {
162                 public void getName(AccessibleEvent e) {
163                     if (e.childID != ACC.CHILDID_SELF) {
164                         ToolItem item = tb.getItem(e.childID);
165                         if (item != null) {
166                             String JavaDoc toolTip = item.getToolTipText();
167                             if (toolTip != null) {
168                                 e.result = toolTip;
169                             }
170                         }
171                     }
172                 }
173             });
174         }
175         return fToolBarManager;
176     }
177     
178     /**
179      * Returns the current input of this pane or null if the pane has no input.
180      *
181      * @return an <code>Object</code> that is the input to this pane or null if the pane has no input.
182      *
183      * @since 3.3
184      */

185     public Object JavaDoc getInput() {
186         return fInput;
187     }
188     
189     /**
190      * Sets the input object of this pane.
191      *
192      * @param input the new input object or <code>null</code>
193      * @since 3.3
194      */

195     public void setInput(Object JavaDoc input) {
196         if (fInput != input)
197             fInput= input;
198     }
199     
200     /* (non-Javadoc)
201      * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
202      */

203     public void addSelectionChangedListener(ISelectionChangedListener l) {
204         fSelectionListeners.add(l);
205     }
206
207     /* (non-Javadoc)
208      * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
209      */

210     public void removeSelectionChangedListener(ISelectionChangedListener l) {
211         fSelectionListeners.remove(l);
212     }
213     
214     /* (non-Javadoc)
215      * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
216      */

217     public ISelection getSelection() {
218         return null;
219     }
220     
221     /* (non-Javadoc)
222      * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
223      */

224     public void setSelection(ISelection s) {
225         // Default is to do nothing
226
}
227     
228     /* (non-Javadoc)
229      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
230      */

231     public void selectionChanged(SelectionChangedEvent ev) {
232         Object JavaDoc[] listeners= fSelectionListeners.getListeners();
233         for (int i= 0; i < listeners.length; i++)
234             ((ISelectionChangedListener) listeners[i]).selectionChanged(ev);
235     }
236     
237     /* (non-Javadoc)
238      * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
239      */

240     public void doubleClick(DoubleClickEvent event) {
241         Object JavaDoc[] listeners= fDoubleClickListener.getListeners();
242         for (int i= 0; i < listeners.length; i++)
243             ((IDoubleClickListener) listeners[i]).doubleClick(event);
244     }
245
246     /**
247      * Add a double-click listener to the pane. The listener will get
248      * invoked when the contents of the pane are double-clicked. Adding
249      * a listener that is already registered has no effect.
250      * @param listener the listener
251      * @since 3.3
252      */

253     public void addDoubleClickListener(IDoubleClickListener listener) {
254         fDoubleClickListener.add(listener);
255     }
256
257     /**
258      * Remove a double-click listener. Removing a listener that is not
259      * registered has no effect.
260      * @param listener the listener
261      * @since 3.3
262      */

263     public void removeDoubleClickListener(IDoubleClickListener listener) {
264         fDoubleClickListener.remove(listener);
265     }
266
267     /**
268      * Add an open listener to the pane. The listener will get
269      * invoked when the contents of the pane are double-clicked. Adding
270      * a listener that is already registered has no effect.
271      * @param listener the listener
272      * @since 3.3
273      */

274     public void addOpenListener(IOpenListener listener) {
275         fOpenListener.add(listener);
276     }
277
278     /**
279      * Remove an open listener. Removing a listener that is not
280      * registered has no effect.
281      * @param listener the listener
282      * @since 3.3
283      */

284     public void removeOpenListener(IOpenListener listener) {
285         fOpenListener.remove(listener);
286     }
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.jface.viewers.IOpenListener#open(org.eclipse.jface.viewers.OpenEvent)
290      */

291     public void open(OpenEvent event) {
292         Object JavaDoc[] listeners= fOpenListener.getListeners();
293         for (int i= 0; i < listeners.length; i++)
294             ((IOpenListener) listeners[i]).open(event);
295     }
296     
297     /* (non-Javadoc)
298      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
299      */

300     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
301         return Platform.getAdapterManager().getAdapter(this, adapter);
302     }
303 }
304
Popular Tags