KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > compare > XMLStructureViewer


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.pde.internal.ui.compare;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 import org.eclipse.compare.CompareConfiguration;
18 import org.eclipse.compare.ITypedElement;
19 import org.eclipse.compare.structuremergeviewer.DiffNode;
20 import org.eclipse.compare.structuremergeviewer.ICompareInput;
21 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
22 import org.eclipse.compare.structuremergeviewer.StructureDiffViewer;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26 import org.eclipse.jface.viewers.Viewer;
27 import org.eclipse.jface.viewers.ViewerComparator;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.swt.widgets.Composite;
30
31 /**
32  * An XML diff tree viewer that can be configured with a <code>IStructureCreator</code>
33  * to retrieve a hierarchical structure from the input object (an <code>ICompareInput</code>)
34  * and perform a two-way or three-way compare on it.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed outside
37  * this package.
38  * </p>
39  *
40  * @see ICompareInput
41  */

42 public class XMLStructureViewer extends StructureDiffViewer {
43     
44     class XMLComparator extends ViewerComparator {
45
46         public XMLComparator() {
47             super();
48         }
49
50         public int category(Object JavaDoc node) {
51             if (node instanceof DiffNode) {
52                 Object JavaDoc o = ((DiffNode) node).getId();
53                 if (o instanceof XMLNode) {
54                     String JavaDoc xmlType= ((XMLNode) o).getXMLType();
55                     if (xmlType.equals(XMLStructureCreator.TYPE_ATTRIBUTE))
56                         return 1;
57                     if (xmlType.equals(XMLStructureCreator.TYPE_ELEMENT)
58                             || xmlType.equals(XMLStructureCreator.TYPE_TEXT)
59                             || xmlType.equals(XMLStructureCreator.TYPE_EXTENSION)
60                             || xmlType.equals(XMLStructureCreator.TYPE_EXTENSIONPOINT))
61                         return 2;
62                 }
63             }
64             return 0;
65         }
66
67         public void sort(final Viewer viewer, Object JavaDoc[] elements) {
68             if (elements != null
69                     && elements.length > 0
70                     && elements[0] instanceof DiffNode) {
71                 Object JavaDoc o = ((DiffNode) elements[0]).getId();
72                 if (o instanceof XMLNode) {
73                     XMLNode parent = ((XMLNode) o).getParent();
74                     String JavaDoc sig = parent.getSignature();
75                     if (sig.endsWith(XMLStructureCreator.SIGN_ELEMENT)) {
76                         final ArrayList JavaDoc originalTree =
77                             new ArrayList JavaDoc(Arrays.asList(parent.getChildren()));
78                         Arrays.sort(elements, new Comparator JavaDoc() {
79                             public int compare(Object JavaDoc a, Object JavaDoc b) {
80                                 return XMLComparator.this.compare(
81                                     (DiffNode) a,
82                                     (DiffNode) b,
83                                     originalTree);
84                             }
85                         });
86                         return;
87                     }
88                 }
89             }
90             super.sort(viewer, elements);
91         }
92
93         private int compare(DiffNode a, DiffNode b, ArrayList JavaDoc originalTree) {
94
95             int index_a = originalTree.indexOf(a.getId());
96             int index_b = originalTree.indexOf(b.getId());
97             if (index_a < index_b)
98                 return -1;
99             return 1;
100         }
101     }
102
103     /**
104      * Creates a new viewer under the given SWT parent with the specified configuration.
105      *
106      * @param parent the SWT control under which to create the viewer
107      * @param configuration the configuration for this viewer
108      */

109     public XMLStructureViewer(Composite parent, CompareConfiguration configuration) {
110         super(parent, configuration);
111         setStructureCreator(new XMLStructureCreator());
112         setComparator(new XMLComparator());
113     }
114
115
116     protected XMLStructureCreator getXMLStructureCreator() {
117         return (XMLStructureCreator) getStructureCreator();
118     }
119
120
121     /*
122      * Recreates the comparable structures for the input sides.
123      */

124     protected void compareInputChanged(ICompareInput input) {
125         if (input != null) {
126             ITypedElement t = input.getLeft();
127             if (t != null) {
128                 String JavaDoc fileExtension = t.getType();
129                 getXMLStructureCreator().setFileExtension(fileExtension);
130             }
131         }
132
133         getXMLStructureCreator().initIdMaps();
134         super.compareInputChanged(input);
135
136     }
137
138     protected void preDiffHook(
139             IStructureComparator ancestor,
140             IStructureComparator left,
141             IStructureComparator right, IProgressMonitor monitor) {
142         if (left != null && right != null) {
143             performMatching((XMLNode)left, (XMLNode)right, (XMLNode)ancestor, monitor);
144         }
145     }
146
147
148     private void performMatching(final XMLNode left, final XMLNode right,
149             final XMLNode ancestor, IProgressMonitor monitor) {
150         
151         if (monitor == null)
152             monitor = new NullProgressMonitor();
153         
154         int totalWork;
155         if (ancestor != null)
156             totalWork = 1;
157         else
158             totalWork = 3;
159         monitor.beginTask(PDEUIMessages.XMLStructureViewer_taskName, totalWork);
160         
161         AbstractMatching m = new OrderedMatching();
162         try {
163             m.match(left, right, false, monitor);
164             if (ancestor != null) {
165                 m.match(left, ancestor, true,
166                     new SubProgressMonitor(monitor, 1));
167                 m.match(right, ancestor, true,
168                     new SubProgressMonitor(monitor, 1));
169             }
170         } finally {
171             monitor.done();
172         }
173     }
174
175 }
176
Popular Tags