KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > HierarchicalModelProvider


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.team.internal.ui.synchronize;
12
13 import org.eclipse.compare.structuremergeviewer.IDiffElement;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.ViewerSorter;
18 import org.eclipse.team.core.synchronize.*;
19 import org.eclipse.team.internal.ui.*;
20 import org.eclipse.team.ui.TeamImages;
21 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
23
24 /**
25  * The
26  * job of this input is to create the logical model of the contents of the
27  * sync set for displaying to the user. The created logical model must diff
28  * nodes.
29  * <p>
30  * 1. First, prepareInput is called to initialize the model with the given sync
31  * set. Building the model occurs in the ui thread.
32  * 2. The input must react to changes in the sync set and adjust its diff node
33  * model then update the viewer. In effect mediating between the sync set
34  * changes and the model shown to the user. This happens in the ui thread.
35  * </p>
36  * NOT ON DEMAND - model is created then maintained!
37  * @since 3.0
38  */

39 public class HierarchicalModelProvider extends SynchronizeModelProvider {
40     
41     public static class HierarchicalModelProviderDescriptor implements ISynchronizeModelProviderDescriptor {
42         public static final String JavaDoc ID = TeamUIPlugin.ID + ".modelprovider_hierarchical"; //$NON-NLS-1$
43
public String JavaDoc getId() {
44             return ID;
45         }
46         public String JavaDoc getName() {
47             return TeamUIMessages.HierarchicalModelProvider_0;
48         }
49         public ImageDescriptor getImageDescriptor() {
50             return TeamImages.getImageDescriptor(ITeamUIImages.IMG_HIERARCHICAL);
51         }
52     }
53     private static final HierarchicalModelProviderDescriptor hierarchicalDescriptor = new HierarchicalModelProviderDescriptor();
54     
55     /**
56      * Create an input based on the provide sync set. The input is not initialized
57      * until <code>prepareInput</code> is called.
58      *
59      * @param set the sync set used as the basis for the model created by this input.
60      */

61     public HierarchicalModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) {
62         super(configuration, set);
63     }
64
65     public HierarchicalModelProvider(
66             AbstractSynchronizeModelProvider parentProvider,
67             ISynchronizeModelElement modelRoot,
68             ISynchronizePageConfiguration configuration, SyncInfoSet set) {
69         super(parentProvider, modelRoot, configuration, set);
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.team.internal.ui.synchronize.ISynchronizeModelProvider#getDescriptor()
74      */

75     public ISynchronizeModelProviderDescriptor getDescriptor() {
76         return hierarchicalDescriptor;
77     }
78     
79     public ViewerSorter getViewerSorter() {
80         return new SynchronizeModelElementSorter();
81     }
82
83     protected SyncInfoTree getSyncInfoTree() {
84         return (SyncInfoTree)getSyncInfoSet();
85     }
86
87     /**
88      * Invoked by the <code>buildModelObject</code> method to create
89      * the childen of the given node. This method can be overriden
90      * by subclasses but subclasses should inv
91      * @param container
92      * @return the diff elements
93      */

94     protected IDiffElement[] createModelObjects(ISynchronizeModelElement container) {
95         IResource resource = null;
96         if (container == getModelRoot()) {
97             resource = ResourcesPlugin.getWorkspace().getRoot();
98         } else {
99             resource = container.getResource();
100         }
101         if(resource != null) {
102             SyncInfoTree infoTree = getSyncInfoTree();
103             IResource[] children = infoTree.members(resource);
104             ISynchronizeModelElement[] nodes = new ISynchronizeModelElement[children.length];
105             for (int i = 0; i < children.length; i++) {
106                 nodes[i] = createModelObject(container, children[i]);
107             }
108             return nodes;
109         }
110         return new IDiffElement[0];
111     }
112
113     protected ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, IResource resource) {
114         SyncInfo info = getSyncInfoTree().getSyncInfo(resource);
115         SynchronizeModelElement newNode;
116         if(info != null) {
117             newNode = new SyncInfoModelElement(parent, info);
118         } else {
119             newNode = new UnchangedResourceModelElement(parent, resource);
120         }
121         addToViewer(newNode);
122         return newNode;
123     }
124
125     /**
126      * Invokes <code>getModelObject(Object)</code> on an array of resources.
127      * @param resources
128      * the resources
129      * @return the model objects for the resources
130      */

131     protected Object JavaDoc[] getModelObjects(IResource[] resources) {
132         Object JavaDoc[] result = new Object JavaDoc[resources.length];
133         for (int i = 0; i < resources.length; i++) {
134             result[i] = getModelObject(resources[i]);
135         }
136         return result;
137     }
138
139     protected void addResources(IResource[] added) {
140         for (int i = 0; i < added.length; i++) {
141             IResource resource = added[i];
142             addResource(resource);
143         }
144     }
145
146     private void addResource(IResource resource) {
147         ISynchronizeModelElement node = getModelObject(resource);
148         if (node != null) {
149             // Somehow the node exists. Remove it and read it to ensure
150
// what is shown matches the contents of the sync set
151
removeFromViewer(resource);
152         }
153         // Build the sub-tree rooted at this node
154
ISynchronizeModelElement parent = getModelObject(resource.getParent());
155         if (parent != null) {
156             node = createModelObject(parent, resource);
157             buildModelObjects(node);
158         }
159     }
160
161     /* (non-Javadoc)
162      * @see org.eclipse.team.ui.synchronize.viewers.SynchronizeModelProvider#buildModelObjects(org.eclipse.team.ui.synchronize.viewers.SynchronizeModelElement)
163      */

164     protected IDiffElement[] buildModelObjects(ISynchronizeModelElement node) {
165         IDiffElement[] children = createModelObjects(node);
166         for (int i = 0; i < children.length; i++) {
167             IDiffElement element = children[i];
168             if (element instanceof ISynchronizeModelElement) {
169                 buildModelObjects((ISynchronizeModelElement) element);
170             }
171         }
172         return children;
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.team.ui.synchronize.viewers.SynchronizeModelProvider#handleResourceAdditions(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
177      */

178     protected void handleResourceAdditions(ISyncInfoTreeChangeEvent event) {
179         IResource[] added = event.getAddedSubtreeRoots();
180         addResources(added);
181     }
182
183     /* (non-Javadoc)
184      * @see org.eclipse.team.ui.synchronize.viewers.SynchronizeModelProvider#handleResourceRemovals(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
185      */

186     protected void handleResourceRemovals(ISyncInfoTreeChangeEvent event) {
187         // Remove the removed subtrees
188
IResource[] removedRoots = event.getRemovedSubtreeRoots();
189         removeFromViewer(removedRoots);
190         // We have to look for folders that may no longer be in the set
191
// (i.e. are in-sync) but still have descendants in the set
192
IResource[] removedResources = event.getRemovedResources();
193         for (int i = 0; i < removedResources.length; i++) {
194             IResource resource = removedResources[i];
195             if (resource.getType() != IResource.FILE) {
196                 ISynchronizeModelElement node = getModelObject(resource);
197                 if (node != null) {
198                     removeFromViewer(resource);
199                     addResources(new IResource[] {resource});
200                 }
201             }
202         }
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.team.internal.ui.synchronize.SynchronizeModelProvider#createModelObject(org.eclipse.team.ui.synchronize.ISynchronizeModelElement, org.eclipse.team.core.synchronize.SyncInfo)
207      */

208     protected ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, SyncInfo info) {
209         return createModelObject(parent, info.getLocal());
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.team.internal.ui.synchronize.SynchronizeModelProvider#addResource(org.eclipse.team.core.synchronize.SyncInfo)
214      */

215     protected void addResource(SyncInfo info) {
216         addResource(info.getLocal());
217     }
218 }
219
Popular Tags