KickJava   Java API By Example, From Geeks To Geeks.

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


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.compare.contentmergeviewer.IFlushable;
14 import org.eclipse.compare.internal.*;
15 import org.eclipse.compare.structuremergeviewer.ICompareInput;
16 import org.eclipse.jface.viewers.*;
17 import org.eclipse.swt.events.DisposeEvent;
18 import org.eclipse.swt.events.DisposeListener;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.*;
21
22 import com.ibm.icu.text.MessageFormat;
23
24
25 /**
26  * A custom <code>CompareViewerPane</code> that supports dynamic viewer switching.
27  *
28  * <p>
29  * Clients must implement the viewer switching strategy by implementing
30  * the <code>getViewer(Viewer, Object)</code> method.
31  * <p>
32  * If a property with the name <code>CompareUI.COMPARE_VIEWER_TITLE</code> is set
33  * on the top level SWT control of a viewer, it is used as a title in the <code>CompareViewerPane</code>'s
34  * title bar.
35  *
36  * @since 2.0
37  */

38 public abstract class CompareViewerSwitchingPane extends CompareViewerPane {
39     
40     private Viewer fViewer;
41     private boolean fControlVisibility= false;
42     private String JavaDoc fTitle;
43     private String JavaDoc fTitleArgument;
44     
45     /**
46      * Creates a <code>CompareViewerSwitchingPane</code> as a child of the given parent and with the
47      * specified SWT style bits.
48      *
49      * @param parent a widget which will be the parent of the new instance (cannot be null)
50      * @param style the style of widget to construct
51      *
52      * @exception IllegalArgumentException <ul>
53      * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
54      * </ul>
55      * @exception org.eclipse.swt.SWTException <ul>
56      * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
57      * </ul>
58      */

59     public CompareViewerSwitchingPane(Composite parent, int style) {
60         this(parent, style, false);
61     }
62     
63     /**
64      * Creates a <code>CompareViewerSwitchingPane</code> as a child of the given parent and with the
65      * specified SWT style bits.
66      *
67      * @param parent a widget which will be the parent of the new instance (cannot be null)
68      * @param style the style of widget to construct
69      * @param visibility the initial visibility of the CompareViewerSwitchingPane
70      *
71      * @exception IllegalArgumentException <ul>
72      * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
73      * </ul>
74      * @exception org.eclipse.swt.SWTException <ul>
75      * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
76      * </ul>
77      */

78     public CompareViewerSwitchingPane(Composite parent, int style, boolean visibility) {
79         super(parent, style);
80
81         fControlVisibility= visibility;
82         
83         setViewer(new NullViewer(this));
84         
85         addDisposeListener(
86             new DisposeListener() {
87                 public void widgetDisposed(DisposeEvent e) {
88                     if (fViewer != null)
89                         fViewer.removeSelectionChangedListener(CompareViewerSwitchingPane.this);
90                     if (fViewer instanceof StructuredViewer) {
91                         StructuredViewer sv= (StructuredViewer) fViewer;
92                         sv.removeDoubleClickListener(CompareViewerSwitchingPane.this);
93                         sv.removeOpenListener(CompareViewerSwitchingPane.this);
94                     }
95                     fViewer= null;
96                 }
97             }
98         );
99     }
100     
101     /**
102      * Returns the current viewer.
103      *
104      * @return the current viewer
105      */

106     public Viewer getViewer() {
107         return fViewer;
108     }
109     
110     private void setViewer(Viewer newViewer) {
111         
112         if (newViewer == fViewer)
113             return;
114                 
115         boolean oldEmpty= isEmpty();
116
117         if (fViewer != null) {
118             
119             fViewer.removeSelectionChangedListener(this);
120                  
121             if (fViewer instanceof StructuredViewer) {
122                 StructuredViewer sv= (StructuredViewer) fViewer;
123                 sv.removeDoubleClickListener(this);
124                 sv.removeOpenListener(this);
125             }
126
127             Control content= getContent();
128             setContent(null);
129             
130             fViewer.setInput(null);
131                                 
132             if (content != null && !content.isDisposed())
133                 content.dispose();
134
135         } else
136             oldEmpty= false;
137         setContent(null);
138
139         fViewer= newViewer;
140
141         if (fViewer != null) {
142             // we have to remember and restore the old visibility of the CustomPane
143
// since setContent changes the visibility
144
boolean old= getVisible();
145             setContent(fViewer.getControl());
146             setVisible(old); // restore old visibility
147

148             boolean newEmpty= isEmpty();
149
150             fViewer.addSelectionChangedListener(this);
151
152             if (fViewer instanceof StructuredViewer) {
153                 StructuredViewer sv= (StructuredViewer) fViewer;
154                 sv.addDoubleClickListener(this);
155                 sv.addOpenListener(this);
156             }
157             
158             if (oldEmpty != newEmpty) { // re-layout my container
159
Composite parent= getParent();
160                 if (parent instanceof Splitter)
161                     ((Splitter)parent).setVisible(this, fControlVisibility ? !newEmpty : true);
162             }
163                 
164             layout(true);
165         }
166     }
167
168     /**
169      * Returns the optional title argument that has been set with <code>setTitelArgument</code>
170      * or <code>null</code> if no optional title argument has been set.
171      * <p>
172      * Note: this method is for internal use only. Clients should not call this method.
173      *
174      * @return the optional title argument or <code>null</code>
175      */

176     public String JavaDoc getTitleArgument() {
177         return fTitleArgument;
178     }
179
180     /**
181      * Returns <code>true</code> if no viewer is installed or if the current viewer
182      * is a <code>NullViewer</code>.
183      *
184      * @return <code>true</code> if no viewer is installed or if the current viewer is a <code>NullViewer</code>
185      */

186     public boolean isEmpty() {
187         return fViewer == null || fViewer instanceof NullViewer;
188     }
189
190     /* (non-Javadoc)
191      * @see org.eclipse.compare.CompareViewerPane#getSelection()
192      */

193     public ISelection getSelection() {
194         if (fViewer != null)
195             return fViewer.getSelection();
196         return super.getSelection();
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.compare.CompareViewerPane#setSelection(org.eclipse.jface.viewers.ISelection)
201      */

202     public void setSelection(ISelection s) {
203         if (fViewer != null)
204              fViewer.setSelection(s);
205     }
206     
207     private boolean hasFocus2() {
208         // do we have focus?
209
Display display= getDisplay();
210         if (display != null)
211             for (Control focus= display.getFocusControl(); focus != null; focus= focus.getParent())
212                 if (focus == this)
213                     return true;
214         return false;
215     }
216         
217     /**
218      * Sets the input object of this pane.
219      * For this input object a suitable viewer is determined by calling the abstract
220      * method <code>getViewer(Viewer, Object)</code>.
221      * If the returned viewer differs from the current one, the old viewer
222      * is disposed and the new one installed. Then the input object is fed
223      * into the newly installed viewer by calling its <code>setInput(Object)</code> method.
224      * If new and old viewer don't differ no new viewer is installed but just
225      * <code>setInput(Object)</code> is called.
226      * If the input is <code>null</code> the pane is cleared,
227      * that is the current viewer is disposed.
228      *
229      * @param input the new input object or <code>null</code>
230      */

231     public void setInput(Object JavaDoc input) {
232
233         if (getInput() == input)
234             return;
235             
236         boolean hadFocus= hasFocus2();
237         
238         super.setInput(input);
239
240         // viewer switching
241
Viewer newViewer= null;
242         if (input != null)
243             newViewer= getViewer(fViewer, input);
244
245         if (newViewer == null) {
246             if (fViewer instanceof NullViewer)
247                 return;
248             newViewer= new NullViewer(this);
249         }
250         
251         setViewer(newViewer);
252
253         // set input
254
fViewer.setInput(input);
255
256         Image image= null;
257         if (!(fViewer instanceof NullViewer) && input instanceof ICompareInput)
258             image= ((ICompareInput)input).getImage();
259         setImage(image);
260         
261         String JavaDoc title= null;
262         if (fViewer != null) {
263             Control c= fViewer.getControl();
264             if (c != null) {
265                 Object JavaDoc data= c.getData(CompareUI.COMPARE_VIEWER_TITLE);
266                 if (data instanceof String JavaDoc)
267                     title= (String JavaDoc) data;
268                 if (hadFocus)
269                     c.setFocus();
270             }
271         }
272             
273         fTitle= title;
274         updateTitle();
275     }
276     
277     /**
278      * Sets an additional and optional argument for the pane's title.
279      * Note: this method is for internal use only. Clients should not call this method.
280      *
281      * @param argument an optional argument for the pane's title
282      */

283     public void setTitleArgument(String JavaDoc argument) {
284         fTitleArgument= argument;
285         updateTitle();
286     }
287
288     private void updateTitle() {
289         if (fTitle != null) {
290             if (fTitleArgument != null) {
291                 String JavaDoc format= CompareMessages.CompareViewerSwitchingPane_Titleformat;
292                 String JavaDoc t= MessageFormat.format(format, new String JavaDoc[] { fTitle, fTitleArgument } );
293                 setText(t);
294             } else
295                 setText(fTitle);
296         } else {
297             setText(""); //$NON-NLS-1$
298
}
299     }
300     
301     /**
302      * {@inheritDoc}
303      * @since 3.3
304      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
305      */

306     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
307         if (adapter == INavigatable.class) {
308             if (isEmpty())
309                 return null;
310             Viewer viewer= getViewer();
311             if (viewer == null)
312                 return null;
313             Control control= viewer.getControl();
314             if (control == null)
315                 return null;
316             Object JavaDoc data= control.getData(INavigatable.NAVIGATOR_PROPERTY);
317             if (data instanceof INavigatable)
318                 return data;
319         }
320         if (adapter == IFlushable.class) {
321             Viewer v= getViewer();
322             if (v != null) {
323                 IFlushable flushable = (IFlushable)Utilities.getAdapter(v, IFlushable.class);
324                 if (flushable != null)
325                     return flushable;
326             }
327         }
328         return super.getAdapter(adapter);
329     }
330     
331     /* (non-Javadoc)
332      * @see org.eclipse.swt.widgets.Composite#setFocus()
333      */

334     public boolean setFocus() {
335         Viewer v= getViewer();
336         if (v != null) {
337             Control c= v.getControl();
338             if (c != null) {
339                 if (c.setFocus())
340                     return true;
341             }
342         }
343         return super.setFocus();
344     }
345
346     /**
347      * Returns a viewer which is able to display the given input.
348      * If no viewer can be found, <code>null</code> is returned.
349      * The additional argument oldViewer represents the viewer currently installed
350      * in the pane (or <code>null</code> if no viewer is installed).
351      * It can be returned from this method if the current viewer can deal with the
352      * input (and no new viewer must be created).
353      *
354      * @param oldViewer the currently installed viewer or <code>null</code>
355      * @param input the input object for which a viewer must be determined or <code>null</code>
356      * @return a viewer for the given input, or <code>null</code> if no viewer can be determined
357      */

358     abstract protected Viewer getViewer(Viewer oldViewer, Object JavaDoc input);
359 }
360
Popular Tags