KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > help > ViewContextComputer


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.ui.help;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.help.IContext;
17 import org.eclipse.swt.events.HelpEvent;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.ui.IViewPart;
20
21 /**
22  * For determining the help context for controls in a view.
23  * <p>
24  * This class may be instantiated; it is not intended to be subclassed.
25  * </p>
26  * @deprecated nested contexts are no longer supported by the help support system
27  */

28 public class ViewContextComputer implements IContextComputer {
29     private IViewPart view;
30
31     private ArrayList JavaDoc contextList;
32
33     private Object JavaDoc context;
34
35     /**
36      * Creates a new context computer for the given view and help context.
37      *
38      * @param viewPart the view
39      * @param helpContext a single help context id (type <code>String</code>) or
40      * help context object (type <code>IContext</code>)
41      */

42     public ViewContextComputer(IViewPart viewPart, Object JavaDoc helpContext) {
43         Assert.isTrue(helpContext instanceof String JavaDoc
44                 || helpContext instanceof IContext);
45         view = viewPart;
46         context = helpContext;
47     }
48
49     /**
50      * Add the contexts to the context list.
51      *
52      * @param object the contexts (<code>Object[]</code> or <code>IContextComputer</code>)
53      * @param event the help event
54      */

55     private void addContexts(Object JavaDoc object, HelpEvent event) {
56         Assert.isTrue(object instanceof Object JavaDoc[]
57                 || object instanceof IContextComputer
58                 || object instanceof String JavaDoc);
59
60         if (object instanceof String JavaDoc) {
61             contextList.add(object);
62             return;
63         }
64
65         Object JavaDoc[] contexts;
66         if (object instanceof IContextComputer) {
67             // get local contexts
68
contexts = ((IContextComputer) object).getLocalContexts(event);
69         } else {
70             contexts = (Object JavaDoc[]) object;
71         }
72
73         // copy the contexts into our list
74
for (int i = 0; i < contexts.length; i++) {
75             contextList.add(contexts[i]);
76         }
77     }
78
79     /**
80      * Add the contexts for the given control to the context list.
81      *
82      * @param control the control from which to obtain the contexts
83      * @param event the help event
84      */

85     private void addContextsForControl(Control control, HelpEvent event) {
86         // See if there is are help contexts on the control
87
Object JavaDoc object = WorkbenchHelp.getHelp(control);
88
89         if (object == null || object == this) {
90             // We need to check for this in order to avoid recursion
91
return;
92         }
93
94         addContexts(object, event);
95     }
96
97     /* (non-Javadoc)
98      * Method declared on IContextComputer.
99      */

100     public Object JavaDoc[] computeContexts(HelpEvent event) {
101         contextList = new ArrayList JavaDoc();
102
103         // Add the local context
104
contextList.add(context);
105
106         // Add the contexts for the window shell
107
addContextsForControl(view.getSite().getShell(), event);
108
109         // Return the contexts
110
return contextList.toArray();
111     }
112
113     /* (non-Javadoc)
114      * Method declared on IContextComputer.
115      */

116     public Object JavaDoc[] getLocalContexts(HelpEvent event) {
117         return new Object JavaDoc[] { context };
118     }
119 }
120
Popular Tags