KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > tags > TagSourceWorkbenchAdapter


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.ccvs.ui.tags;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.*;
18 import org.eclipse.team.internal.ccvs.core.CVSTag;
19 import org.eclipse.ui.model.IWorkbenchAdapter;
20
21 /**
22  * A workbench adapter for a tag source that creates a model
23  * for displaying the tags from a tag source in a tree or table
24  * viewer. The workbench adapter is not a singleton since it needs
25  * to be configured to display certain types of tags.
26  */

27 public class TagSourceWorkbenchAdapter implements IAdaptable, IWorkbenchAdapter {
28
29     /**
30      * Constants for configuring which types of tags should be displayed.
31      */

32     public static final int INCLUDE_HEAD_TAG = 1;
33     public static final int INCLUDE_BASE_TAG = 2;
34     public static final int INCLUDE_BRANCHES = 4;
35     public static final int INCLUDE_VERSIONS = 8;
36     public static final int INCLUDE_DATES = 16;
37     public static final int INCLUDE_ALL_TAGS = INCLUDE_HEAD_TAG | INCLUDE_BASE_TAG | INCLUDE_BRANCHES | INCLUDE_VERSIONS | INCLUDE_DATES;
38     
39     TagRootElement branches;
40     TagRootElement versions;
41     TagRootElement dates;
42     int includeFlags;
43     
44     public static class ProjectElementComparator extends ViewerComparator {
45     
46         /*
47          * The order in the diaog should be HEAD, Branches, Versions, Dates, BASE
48          */

49         public int category(Object JavaDoc element) {
50             if (element instanceof TagElement) {
51                 CVSTag tag = ((TagElement)element).getTag();
52                 if (tag == CVSTag.DEFAULT) return 1;
53                 if (tag == CVSTag.BASE) return 5;
54                 if (tag.getType() == CVSTag.BRANCH) return 2;
55                 if (tag.getType() == CVSTag.VERSION) return 3;
56                 if (tag.getType() == CVSTag.DATE) return 4;
57             } else if (element instanceof TagRootElement) {
58                 if(((TagRootElement)element).getTypeOfTagRoot() == CVSTag.BRANCH) return 2;
59                 if(((TagRootElement)element).getTypeOfTagRoot() == CVSTag.VERSION) return 3;
60                 if(((TagRootElement)element).getTypeOfTagRoot() == CVSTag.DATE) return 4;
61             }
62             return 0;
63         }
64         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
65             int cat1 = category(e1);
66             int cat2 = category(e2);
67             if (cat1 != cat2) return cat1 - cat2;
68             // Sort version tags in reverse order
69
if (e1 instanceof TagElement){
70                 CVSTag tag1 = ((TagElement)e1).getTag();
71                 int type = tag1.getType();
72                 if(type == CVSTag.VERSION) {
73                     return -1 * super.compare(viewer, e1, e2);
74                 }else if(type == CVSTag.DATE){
75                     return -1*tag1.compareTo(((TagElement)e2).getTag());
76                 }
77             }
78             return super.compare(viewer, e1, e2);
79         }
80     }
81
82     
83     /**
84      * Create a viewer input for the tag source
85      * @param tagSource the tag source
86      * @param includeFlags the types of tags to include
87      * @return a tree viewer input
88      */

89     public static Object JavaDoc createInput(TagSource tagSource, int includeFlags) {
90         if (includeFlags == INCLUDE_VERSIONS) {
91             // Versions only is requested by the merge start page.
92
// Only need to show version tags
93
return new TagRootElement(null, tagSource, CVSTag.VERSION);
94         }
95         return new TagSourceWorkbenchAdapter(tagSource, includeFlags);
96     }
97     
98     public TagSourceWorkbenchAdapter(TagSource tagSource, int includeFlags) {
99         this.includeFlags = includeFlags;
100         if (this.includeFlags == 0) this.includeFlags = INCLUDE_ALL_TAGS;
101         if ((includeFlags & INCLUDE_BRANCHES) > 0) {
102             branches = new TagRootElement(this, tagSource, CVSTag.BRANCH);
103         }
104         if ((includeFlags & INCLUDE_VERSIONS) > 0) {
105             versions = new TagRootElement(this, tagSource, CVSTag.VERSION);
106         }
107         if ((includeFlags & INCLUDE_DATES) > 0) {
108             dates = new TagRootElement(this, tagSource, CVSTag.DATE);
109         }
110     }
111     
112     public Object JavaDoc[] getChildren(Object JavaDoc o) {
113         ArrayList JavaDoc children = new ArrayList JavaDoc(4);
114         if ((includeFlags & INCLUDE_HEAD_TAG) > 0) {
115             children.add(new TagElement(this, CVSTag.DEFAULT));
116         }
117         if ((includeFlags & INCLUDE_BASE_TAG) > 0) {
118             children.add(new TagElement(this, CVSTag.BASE));
119         }
120         if ((includeFlags & INCLUDE_BRANCHES) > 0) {
121             children.add(branches);
122         }
123         if ((includeFlags & INCLUDE_VERSIONS) > 0) {
124             children.add(versions);
125         }
126         if ((includeFlags & INCLUDE_DATES) > 0) {
127             children.add(dates);
128         }
129         return children.toArray(new Object JavaDoc[children.size()]);
130     }
131     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
132         if (adapter == IWorkbenchAdapter.class) return this;
133         return null;
134     }
135     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
136         return null;
137     }
138     public String JavaDoc getLabel(Object JavaDoc o) {
139         return null;
140     }
141     public Object JavaDoc getParent(Object JavaDoc o) {
142         return null;
143     }
144 }
145
Popular Tags