KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ViewerDescriptor


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.internal;
12
13 import org.eclipse.swt.widgets.Composite;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.compare.*;
18
19 import org.eclipse.jface.viewers.Viewer;
20
21 /**
22  * Creates <code>Viewer</code>s from an <code>IConfigurationElement</code>.
23  */

24 public class ViewerDescriptor implements IViewerDescriptor {
25
26     private final static String JavaDoc CLASS_ATTRIBUTE= "class"; //$NON-NLS-1$
27
private final static String JavaDoc EXTENSIONS_ATTRIBUTE= "extensions"; //$NON-NLS-1$
28

29     private IConfigurationElement fConfiguration;
30     private IViewerCreator fViewerCreator;
31     private Class JavaDoc fViewerClass;
32
33     public ViewerDescriptor(IConfigurationElement config) {
34         fConfiguration= config;
35     }
36
37     public Viewer createViewer(Viewer currentViewer, Composite parent, CompareConfiguration mp) {
38         
39         if (currentViewer != null && currentViewer.getClass() == fViewerClass) {
40             //System.out.println("reused viewer: " + currentViewer.getClass().getName());
41
return currentViewer;
42         }
43         
44         if (fViewerCreator == null) {
45             try {
46                 fViewerCreator= (IViewerCreator) fConfiguration.createExecutableExtension(CLASS_ATTRIBUTE);
47             } catch (CoreException e) {
48                 CompareUIPlugin.log(e);
49             }
50         }
51
52         if (fViewerCreator != null) {
53             // If we are going to return a new viewer, we want to preemptively deregister
54
// any handlers to avoid the logging of conflict warnings
55
if (currentViewer != null) {
56                 CompareHandlerService compareHandlerService = (CompareHandlerService)Utilities.getAdapter(currentViewer, CompareHandlerService.class);
57                 if (compareHandlerService != null)
58                     compareHandlerService.dispose();
59             }
60             Viewer viewer= fViewerCreator.createViewer(parent, mp);
61             if (viewer != null)
62                 fViewerClass= viewer.getClass();
63             return viewer;
64         }
65
66         return null;
67     }
68
69     public String JavaDoc getExtension() {
70         return fConfiguration.getAttribute(EXTENSIONS_ATTRIBUTE);
71     }
72 }
73
Popular Tags