KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > LinkHelperService


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.extensions;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.internal.navigator.NavigatorContentService;
25 import org.eclipse.ui.navigator.ILinkHelper;
26
27 /**
28  * @since 3.2
29  *
30  */

31 public class LinkHelperService {
32
33     private static final ILinkHelper[] CANT_GET_NO_HELP = new ILinkHelper[0];
34
35     private NavigatorContentService contentService;
36
37     private final Map JavaDoc linkHelpers = new HashMap JavaDoc();
38
39     /**
40      * @param aContentService
41      * The associated content service for this link helper service.
42      */

43     public LinkHelperService(NavigatorContentService aContentService) {
44         contentService = aContentService;
45     }
46
47     /**
48      *
49      * @param anObject
50      * An object from the viewer
51      * @return An array of link helpers that know about elements in the
52      * selection or null
53      */

54     public ILinkHelper[] getLinkHelpersFor(Object JavaDoc anObject) {
55
56         LinkHelperDescriptor[] descriptors = LinkHelperManager.getInstance()
57                 .getLinkHelpersFor(anObject, contentService);
58         if (descriptors.length == 0) {
59             return CANT_GET_NO_HELP;
60         }
61
62         Set JavaDoc helpers = new LinkedHashSet JavaDoc();
63         for (int i = 0; i < descriptors.length; i++) {
64             helpers.add(getLinkHelper(descriptors[i]));
65         }
66         if (helpers.size() == 0)
67             return CANT_GET_NO_HELP;
68         return (ILinkHelper[]) helpers.toArray(new ILinkHelper[helpers.size()]);
69
70     }
71
72     /**
73      *
74      * @param input
75      * The Editor input from the active viewer.
76      * @return An array of link helpers that know about elements in the
77      * selection or null
78      */

79     public ILinkHelper[] getLinkHelpersFor(IEditorInput input) {
80         LinkHelperDescriptor[] descriptors = LinkHelperManager.getInstance()
81                 .getLinkHelpersFor(input, contentService);
82         if (descriptors.length == 0) {
83             return CANT_GET_NO_HELP;
84         }
85
86         Set JavaDoc helpers = new LinkedHashSet JavaDoc();
87         for (int i = 0; i < descriptors.length; i++) {
88             helpers.add(getLinkHelper(descriptors[i]));
89         }
90         if (helpers.size() == 0)
91             return CANT_GET_NO_HELP;
92         return (ILinkHelper[]) helpers.toArray(new ILinkHelper[helpers.size()]);
93     }
94
95     private ILinkHelper getLinkHelper(LinkHelperDescriptor descriptor) {
96         ILinkHelper helper = (ILinkHelper) linkHelpers.get(descriptor);
97         if (helper == null) {
98             synchronized (this) {
99                 if (helper == null) {
100                     linkHelpers.put(descriptor, helper = descriptor
101                             .createLinkHelper());
102                 }
103             }
104         }
105         return helper;
106     }
107     
108     /**
109      * Return a selection that contains the elements that the given editor input
110      * represent.
111      * @param input the editor input
112      * @return a selection that contains the elements that the given editor input
113      * represent
114      */

115     public IStructuredSelection getSelectionFor(IEditorInput input) {
116         ILinkHelper[] helpers = getLinkHelpersFor(input);
117
118         IStructuredSelection selection = StructuredSelection.EMPTY;
119         IStructuredSelection newSelection = StructuredSelection.EMPTY;
120
121         for (int i = 0; i < helpers.length; i++) {
122             selection = helpers[i].findSelection(input);
123             if (selection != null && !selection.isEmpty()) {
124                 newSelection = mergeSelection(newSelection, selection);
125             }
126         }
127         return newSelection;
128     }
129     
130     private IStructuredSelection mergeSelection(IStructuredSelection aBase,
131             IStructuredSelection aSelectionToAppend) {
132         if (aBase == null || aBase.isEmpty()) {
133             return (aSelectionToAppend != null) ? aSelectionToAppend
134                     : StructuredSelection.EMPTY;
135         } else if (aSelectionToAppend == null || aSelectionToAppend.isEmpty()) {
136             return aBase;
137         } else {
138             List JavaDoc newItems = new ArrayList JavaDoc(aBase.toList());
139             newItems.addAll(aSelectionToAppend.toList());
140             return new StructuredSelection(newItems);
141         }
142     }
143 }
144
Popular Tags