KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > sorters > NavigatorSorterService


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.navigator.sorters;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.viewers.ViewerSorter;
18 import org.eclipse.ui.internal.navigator.NavigatorContentService;
19 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
20 import org.eclipse.ui.navigator.INavigatorSorterService;
21
22 /**
23  *
24  * Provides a default implementation of {@link INavigatorSorterService}.
25  *
26  * @since 3.2
27  *
28  */

29 public class NavigatorSorterService implements INavigatorSorterService {
30
31     private final NavigatorContentService contentService;
32
33     /* A map of (CommonSorterDescriptor, ViewerSorter)-pairs */
34     private final Map JavaDoc sorters = new HashMap JavaDoc();
35
36     /**
37      * Create a sorter service attached to the given content service.
38      *
39      * @param aContentService
40      * The content service used by the viewer that will use this
41      * sorter service.
42      */

43     public NavigatorSorterService(NavigatorContentService aContentService) {
44         contentService = aContentService;
45     }
46
47     /*
48      * (non-Javadoc)
49      *
50      * @see org.eclipse.ui.navigator.INavigatorSorterService#findSorterForParent(java.lang.Object)
51      */

52     public ViewerSorter findSorterForParent(Object JavaDoc aParent) {
53
54         CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager
55                 .getInstance().findApplicableSorters(contentService, aParent);
56         if (descriptors.length > 0) {
57             return getSorter(descriptors[0]);
58         }
59         return SkeletonViewerSorter.INSTANCE;
60     }
61
62     private ViewerSorter getSorter(CommonSorterDescriptor descriptor) {
63         ViewerSorter sorter = null;
64         synchronized (sorters) {
65             sorter = (ViewerSorter) sorters.get(descriptor);
66             if (sorter == null) {
67                 sorters.put(descriptor, sorter = descriptor.createSorter());
68             }
69         }
70         return sorter;
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.eclipse.ui.navigator.INavigatorSorterService#findSorterForParent(org.eclipse.ui.navigator.INavigatorContentDescriptor,
77      * java.lang.Object, java.lang.Object, java.lang.Object)
78      */

79     public ViewerSorter findSorter(INavigatorContentDescriptor source,
80             Object JavaDoc parent, Object JavaDoc lvalue, Object JavaDoc rvalue) {
81
82         CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager
83                 .getInstance().findApplicableSorters(contentService, source, parent, lvalue, rvalue);
84         if(descriptors.length > 0) {
85             return getSorter(descriptors[0]);
86         }
87         return null;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.ui.navigator.INavigatorSorterService#findAvailableSorters(org.eclipse.ui.navigator.INavigatorContentDescriptor)
92      */

93     public Map JavaDoc findAvailableSorters(INavigatorContentDescriptor theSource) {
94         
95         CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager.getInstance().findApplicableSorters(theSource);
96         Map JavaDoc sorters = new HashMap JavaDoc();
97
98         int count = 0;
99         for (int i = 0; i < descriptors.length; i++) {
100             if(descriptors[i].getId() != null && descriptors[i].getId().length() > 0)
101                 sorters.put(descriptors[i].getId(), getSorter(descriptors[i]));
102             else
103                 sorters.put(theSource.getId()+".sorter."+ (++count), getSorter(descriptors[i])); //$NON-NLS-1$
104
}
105         return sorters;
106     }
107      
108
109 }
110
Popular Tags