KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.compare.structuremergeviewer.IDiffContainer;
17 import org.eclipse.compare.structuremergeviewer.IDiffElement;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.action.*;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.Viewer;
23 import org.eclipse.jface.viewers.ViewerSorter;
24 import org.eclipse.team.core.synchronize.*;
25 import org.eclipse.team.internal.ui.*;
26 import org.eclipse.team.ui.TeamImages;
27 import org.eclipse.team.ui.synchronize.*;
28
29 /**
30  * Provides a flat layout
31  */

32 public class FlatModelProvider extends SynchronizeModelProvider {
33
34     public static class FlatModelProviderDescriptor implements ISynchronizeModelProviderDescriptor {
35         public static final String JavaDoc ID = TeamUIPlugin.ID + ".modelprovider_flat"; //$NON-NLS-1$
36
public String JavaDoc getId() {
37             return ID;
38         }
39         public String JavaDoc getName() {
40             return TeamUIMessages.FlatModelProvider_0;
41         }
42         public ImageDescriptor getImageDescriptor() {
43             return TeamImages.getImageDescriptor(ITeamUIImages.IMG_FLAT);
44         }
45     }
46     private static final FlatModelProviderDescriptor flatDescriptor = new FlatModelProviderDescriptor();
47     
48     private static final String JavaDoc P_LAST_RESOURCESORT = TeamUIPlugin.ID + ".P_LAST_RESOURCE_SORT"; //$NON-NLS-1$
49

50     private int sortCriteria = FlatSorter.PATH;
51     
52     /* *****************************************************************************
53      * Model element for the resources in this layout. They are displayed with filename and path
54      * onto the same line.
55      */

56     public static class FullPathSyncInfoElement extends SyncInfoModelElement {
57         public FullPathSyncInfoElement(IDiffContainer parent, SyncInfo info) {
58             super(parent, info);
59         }
60         public String JavaDoc getName() {
61             IResource resource = getResource();
62             return resource.getName() + " - " + resource.getFullPath().toString(); //$NON-NLS-1$
63
}
64     }
65     
66     /**
67      * Sorter that sorts flat path elements using the criteria specified
68      * when the sorter is created
69      */

70     public class FlatSorter extends ViewerSorter {
71         
72         private int resourceCriteria;
73         
74         // Resource sorting options
75
public final static int NAME = 1;
76         public final static int PATH = 2;
77         public final static int PARENT_NAME = 3;
78         
79         public FlatSorter(int resourceCriteria) {
80             this.resourceCriteria = resourceCriteria;
81         }
82         
83         protected int classComparison(Object JavaDoc element) {
84             if (element instanceof FullPathSyncInfoElement) {
85                 return 0;
86             }
87             return 1;
88         }
89         
90         protected int compareClass(Object JavaDoc element1, Object JavaDoc element2) {
91             return classComparison(element1) - classComparison(element2);
92         }
93         
94         protected int compareNames(String JavaDoc s1, String JavaDoc s2) {
95             return collator.compare(s1, s2);
96         }
97         
98         /* (non-Javadoc)
99          * Method declared on ViewerSorter.
100          */

101         public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
102
103             if (o1 instanceof FullPathSyncInfoElement && o2 instanceof FullPathSyncInfoElement) {
104                 IResource r1 = ((FullPathSyncInfoElement)o1).getResource();
105                 IResource r2 = ((FullPathSyncInfoElement)o2).getResource();
106                 if(resourceCriteria == NAME)
107                     return compareNames(r1.getName(), r2.getName());
108                 else if(resourceCriteria == PATH)
109                     return compareNames(r1.getFullPath().toString(), r2.getFullPath().toString());
110                 else if(resourceCriteria == PARENT_NAME)
111                     return compareNames(r1.getParent().getName(), r2.getParent().getName());
112                 else return 0;
113             } else if (o1 instanceof ISynchronizeModelElement)
114                 return 1;
115             else if (o2 instanceof ISynchronizeModelElement)
116                 return -1;
117             
118             return 0;
119         }
120
121         public int getResourceCriteria() {
122             return resourceCriteria;
123         }
124     }
125     
126     /* *****************************************************************************
127      * Action that allows changing the model providers sort order.
128      */

129     private class ToggleSortOrderAction extends Action {
130         private int criteria;
131         protected ToggleSortOrderAction(String JavaDoc name, int criteria) {
132             super(name, IAction.AS_RADIO_BUTTON);
133             this.criteria = criteria;
134             update();
135         }
136
137         public void run() {
138             if (isChecked() && sortCriteria != criteria) {
139                 sortCriteria = criteria;
140                 String JavaDoc key = getSettingsKey();
141                 IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings();
142                 if(pageSettings != null) {
143                     pageSettings.put(key, criteria);
144                 }
145                 update();
146                 FlatModelProvider.this.firePropertyChange(P_VIEWER_SORTER, null, null);
147             }
148         }
149         
150         public void update() {
151             setChecked(sortCriteria == criteria);
152         }
153         
154         protected String JavaDoc getSettingsKey() {
155             return P_LAST_RESOURCESORT;
156         }
157     }
158     
159     /* *****************************************************************************
160      * Action group for this layout. It is added and removed for this layout only.
161      */

162     public class FlatActionGroup extends SynchronizePageActionGroup {
163         private MenuManager sortByResource;
164         public void initialize(ISynchronizePageConfiguration configuration) {
165             super.initialize(configuration);
166             sortByResource = new MenuManager(TeamUIMessages.FlatModelProvider_6);
167             
168             appendToGroup(
169                     ISynchronizePageConfiguration.P_CONTEXT_MENU,
170                     ISynchronizePageConfiguration.SORT_GROUP,
171                     sortByResource);
172             
173             // Ensure that the sort criteria of the provider is properly initialized
174
FlatModelProvider.this.initialize(configuration);
175             
176             sortByResource.add( new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_8, FlatSorter.PATH));
177             sortByResource.add(new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_7, FlatSorter.NAME));
178             sortByResource.add(new ToggleSortOrderAction(TeamUIMessages.FlatModelProvider_9, FlatSorter.PARENT_NAME));
179         }
180
181         /* (non-Javadoc)
182          * @see org.eclipse.team.ui.synchronize.SynchronizePageActionGroup#dispose()
183          */

184         public void dispose() {
185             sortByResource.dispose();
186             sortByResource.removeAll();
187             super.dispose();
188         }
189     }
190     
191     public FlatModelProvider(ISynchronizePageConfiguration configuration, SyncInfoSet set) {
192         super(configuration, set);
193         initialize(configuration);
194     }
195
196     public FlatModelProvider(AbstractSynchronizeModelProvider parentProvider, ISynchronizeModelElement modelRoot, ISynchronizePageConfiguration configuration, SyncInfoSet set) {
197         super(parentProvider, modelRoot, configuration, set);
198         initialize(configuration);
199     }
200     
201     private void initialize(ISynchronizePageConfiguration configuration) {
202         try {
203             IDialogSettings pageSettings = getConfiguration().getSite().getPageSettings();
204             if(pageSettings != null) {
205                 sortCriteria = pageSettings.getInt(P_LAST_RESOURCESORT);
206             }
207         } catch(NumberFormatException JavaDoc e) {
208             // ignore and use the defaults.
209
}
210         switch (sortCriteria) {
211         case FlatSorter.PATH:
212         case FlatSorter.NAME:
213         case FlatSorter.PARENT_NAME:
214             break;
215         default:
216             sortCriteria = FlatSorter.PATH;
217             break;
218         }
219     }
220     
221     /* (non-Javadoc)
222      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#createActionGroup()
223      */

224     protected SynchronizePageActionGroup createActionGroup() {
225         return new FlatActionGroup();
226     }
227
228     /* (non-Javadoc)
229      * @see org.eclipse.team.internal.ui.synchronize.ISynchronizeModelProvider#getViewerSorter()
230      */

231     public ViewerSorter getViewerSorter() {
232         return new FlatSorter(sortCriteria);
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#buildModelObjects(org.eclipse.team.ui.synchronize.ISynchronizeModelElement)
237      */

238     protected IDiffElement[] buildModelObjects(ISynchronizeModelElement node) {
239         if (node == getModelRoot());
240         SyncInfo[] infos = getSyncInfoSet().getSyncInfos();
241         List JavaDoc result = new ArrayList JavaDoc();
242         for (int i = 0; i < infos.length; i++) {
243             SyncInfo info = infos[i];
244             result.add(createModelObject(node, info));
245         }
246         return (IDiffElement[]) result.toArray(new IDiffElement[result.size()]);
247     }
248
249     /* (non-Javadoc)
250      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceAdditions(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
251      */

252     protected void handleResourceAdditions(ISyncInfoTreeChangeEvent event) {
253         addResources(event.getAddedResources());
254     }
255     
256     /* (non-Javadoc)
257      * @see org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider#handleResourceRemovals(org.eclipse.team.core.synchronize.ISyncInfoTreeChangeEvent)
258      */

259     protected void handleResourceRemovals(ISyncInfoTreeChangeEvent event) {
260         IResource[] resources = event.getRemovedResources();
261         removeFromViewer(resources);
262     }
263
264     /* (non-Javadoc)
265      * @see org.eclipse.team.internal.ui.synchronize.ISynchronizeModelProvider#getDescriptor()
266      */

267     public ISynchronizeModelProviderDescriptor getDescriptor() {
268         return flatDescriptor;
269     }
270
271     protected void addResource(SyncInfo info) {
272         // Add the node to the root
273
ISynchronizeModelElement node = getModelObject(info.getLocal());
274         if (node != null) {
275             // Somehow the node exists. Remove it and read it to ensure
276
// what is shown matches the contents of the sync set
277
removeFromViewer(info.getLocal());
278         }
279         createModelObject(getModelRoot(), info);
280     }
281     
282     protected ISynchronizeModelElement createModelObject(ISynchronizeModelElement parent, SyncInfo info) {
283         SynchronizeModelElement newNode = new FullPathSyncInfoElement(parent, info);
284         addToViewer(newNode);
285         return newNode;
286     }
287 }
288
Popular Tags