]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/util/JavaUIHelp.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / util / JavaUIHelp.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2010 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.util;
12
13import org.eclipse.help.HelpSystem;
14import org.eclipse.help.IContext;
15import org.eclipse.help.IContextProvider;
16
17import org.eclipse.swt.custom.StyledText;
18import org.eclipse.swt.events.HelpEvent;
19import org.eclipse.swt.events.HelpListener;
20
21import org.eclipse.core.runtime.CoreException;
22
23import org.eclipse.jface.viewers.ISelection;
24import org.eclipse.jface.viewers.IStructuredSelection;
25import org.eclipse.jface.viewers.StructuredSelection;
26import org.eclipse.jface.viewers.StructuredViewer;
27
28import org.eclipse.ui.IWorkbenchPart;
29
30import org.eclipse.jdt.core.JavaModelException;
31
32import org.eclipse.jdt.internal.ui.JavaPlugin;
33import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
34import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
35
36public class JavaUIHelp {
37
38 public static void setHelp(StructuredViewer viewer, String contextId) {
39 JavaUIHelpListener listener= new JavaUIHelpListener(viewer, contextId);
40 viewer.getControl().addHelpListener(listener);
41 }
42
43 public static void setHelp(JavaEditor editor, StyledText text, String contextId) {
44 JavaUIHelpListener listener= new JavaUIHelpListener(editor, contextId);
45 text.addHelpListener(listener);
46 }
47
48 /**
49 * Creates and returns a help context provider for the given part.
50 *
51 * @param part the part for which to create the help context provider
52 * @param contextId the optional context ID used to retrieve static help
53 * @return the help context provider
54 */
55 public static IContextProvider getHelpContextProvider(IWorkbenchPart part, String contextId) {
56 IStructuredSelection selection;
57 try {
58 selection= SelectionConverter.getStructuredSelection(part);
59 } catch (JavaModelException ex) {
60 JavaPlugin.log(ex);
61 selection= StructuredSelection.EMPTY;
62 }
63 Object[] elements= selection.toArray();
64 return new JavaUIHelpContextProvider(contextId, elements);
65 }
66
67 public static class JavaUIHelpListener implements HelpListener {
68
69 private StructuredViewer fViewer;
70 private String fContextId;
71 public JavaEditor fEditor;
72
73 public JavaUIHelpListener(StructuredViewer viewer, String contextId) {
74 fViewer= viewer;
75 fContextId= contextId;
76 }
77
78 public JavaUIHelpListener(JavaEditor editor, String contextId) {
79 fContextId= contextId;
80 fEditor= editor;
81 }
82
83 /*
84 * @see HelpListener#helpRequested(HelpEvent)
85 *
86 */
87 public void helpRequested(HelpEvent e) {
88 try {
89 Object[] selected= null;
90 if (fViewer != null) {
91 ISelection selection= fViewer.getSelection();
92 if (selection instanceof IStructuredSelection) {
93 selected= ((IStructuredSelection)selection).toArray();
94 }
95 } else if (fEditor != null) {
96 selected= fEditor.generated_9109038168758171395(selected);
97 }
98 JavadocHelpContext.displayHelp(fContextId, selected);
99 } catch (CoreException x) {
100 JavaPlugin.log(x);
101 }
102 }
103 }
104
105 private static class JavaUIHelpContextProvider implements IContextProvider {
106 private String fId;
107 private Object[] fSelected;
108 public JavaUIHelpContextProvider(String id, Object[] selected) {
109 fId= id;
110 fSelected= selected;
111 }
112 public int getContextChangeMask() {
113 return SELECTION;
114 }
115 public IContext getContext(Object target) {
116 IContext context= HelpSystem.getContext(fId);
117 if (fSelected != null && fSelected.length > 0) {
118 try {
119 context= new JavadocHelpContext(context, fSelected);
120 } catch (JavaModelException e) {
121 // since we are updating the UI with async exec it
122 // can happen that the element doesn't exist anymore
123 // but we are still showing it in the user interface
124 if (!e.isDoesNotExist())
125 JavaPlugin.log(e);
126 }
127 }
128 return context;
129 }
130 public String getSearchExpression(Object target) {
131 return null;
132 }
133 }
134}