001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.navigator.sorters;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.jface.viewers.ViewerSorter;
016: import org.eclipse.ui.internal.navigator.NavigatorContentService;
017: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
018: import org.eclipse.ui.navigator.INavigatorSorterService;
019:
020: /**
021: *
022: * Provides a default implementation of {@link INavigatorSorterService}.
023: *
024: * @since 3.2
025: *
026: */
027: public class NavigatorSorterService implements INavigatorSorterService {
028:
029: private final NavigatorContentService contentService;
030:
031: /* A map of (CommonSorterDescriptor, ViewerSorter)-pairs */
032: private final Map sorters = new HashMap();
033:
034: /**
035: * Create a sorter service attached to the given content service.
036: *
037: * @param aContentService
038: * The content service used by the viewer that will use this
039: * sorter service.
040: */
041: public NavigatorSorterService(
042: NavigatorContentService aContentService) {
043: contentService = aContentService;
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see org.eclipse.ui.navigator.INavigatorSorterService#findSorterForParent(java.lang.Object)
050: */
051: public ViewerSorter findSorterForParent(Object aParent) {
052:
053: CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager
054: .getInstance().findApplicableSorters(contentService,
055: aParent);
056: if (descriptors.length > 0) {
057: return getSorter(descriptors[0]);
058: }
059: return SkeletonViewerSorter.INSTANCE;
060: }
061:
062: private ViewerSorter getSorter(CommonSorterDescriptor descriptor) {
063: ViewerSorter sorter = null;
064: synchronized (sorters) {
065: sorter = (ViewerSorter) sorters.get(descriptor);
066: if (sorter == null) {
067: sorters.put(descriptor, sorter = descriptor
068: .createSorter());
069: }
070: }
071: return sorter;
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.eclipse.ui.navigator.INavigatorSorterService#findSorterForParent(org.eclipse.ui.navigator.INavigatorContentDescriptor,
078: * java.lang.Object, java.lang.Object, java.lang.Object)
079: */
080: public ViewerSorter findSorter(INavigatorContentDescriptor source,
081: Object parent, Object lvalue, Object rvalue) {
082:
083: CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager
084: .getInstance().findApplicableSorters(contentService,
085: source, parent, lvalue, rvalue);
086: if (descriptors.length > 0) {
087: return getSorter(descriptors[0]);
088: }
089: return null;
090: }
091:
092: /* (non-Javadoc)
093: * @see org.eclipse.ui.navigator.INavigatorSorterService#findAvailableSorters(org.eclipse.ui.navigator.INavigatorContentDescriptor)
094: */
095: public Map findAvailableSorters(
096: INavigatorContentDescriptor theSource) {
097:
098: CommonSorterDescriptor[] descriptors = CommonSorterDescriptorManager
099: .getInstance().findApplicableSorters(theSource);
100: Map sorters = new HashMap();
101:
102: int count = 0;
103: for (int i = 0; i < descriptors.length; i++) {
104: if (descriptors[i].getId() != null
105: && descriptors[i].getId().length() > 0)
106: sorters.put(descriptors[i].getId(),
107: getSorter(descriptors[i]));
108: else
109: sorters
110: .put(
111: theSource.getId()
112: + ".sorter." + (++count), getSorter(descriptors[i])); //$NON-NLS-1$
113: }
114: return sorters;
115: }
116:
117: }
|