KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > WelcomeItem


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.internal.ide.dialogs;
12
13 import org.eclipse.core.runtime.Platform;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.ui.PlatformUI;
16 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
17 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
18 import org.osgi.framework.Bundle;
19
20 /**
21  * Holds the information for an item appearing in the welcome editor
22  */

23 public class WelcomeItem {
24     private String JavaDoc text;
25
26     private int[][] boldRanges;
27
28     private int[][] helpRanges;
29
30     private String JavaDoc[] helpIds;
31
32     private String JavaDoc[] helpHrefs;
33
34     private int[][] actionRanges;
35
36     private String JavaDoc[] actionPluginIds;
37
38     private String JavaDoc[] actionClasses;
39
40     /**
41      * Creates a new welcome item
42      */

43     public WelcomeItem(String JavaDoc text, int[][] boldRanges, int[][] actionRanges,
44             String JavaDoc[] actionPluginIds, String JavaDoc[] actionClasses,
45             int[][] helpRanges, String JavaDoc[] helpIds, String JavaDoc[] helpHrefs) {
46
47         this.text = text;
48         this.boldRanges = boldRanges;
49         this.actionRanges = actionRanges;
50         this.actionPluginIds = actionPluginIds;
51         this.actionClasses = actionClasses;
52         this.helpRanges = helpRanges;
53         this.helpIds = helpIds;
54         this.helpHrefs = helpHrefs;
55     }
56
57     /**
58      * Returns the action ranges (character locations)
59      */

60     public int[][] getActionRanges() {
61         return actionRanges;
62     }
63
64     /**
65      * Returns the bold ranges (character locations)
66      */

67     public int[][] getBoldRanges() {
68         return boldRanges;
69     }
70
71     /**
72      * Returns the help ranges (character locations)
73      */

74     public int[][] getHelpRanges() {
75         return helpRanges;
76     }
77
78     /**
79      * Returns the text to display
80      */

81     public String JavaDoc getText() {
82         return text;
83     }
84
85     /**
86      * Returns true is a link (action or help) is present at the given character location
87      */

88     public boolean isLinkAt(int offset) {
89         // Check if there is a link at the offset
90
for (int i = 0; i < helpRanges.length; i++) {
91             if (offset >= helpRanges[i][0]
92                     && offset < helpRanges[i][0] + helpRanges[i][1]) {
93                 return true;
94             }
95         }
96
97         // Check if there is an action link at the offset
98
for (int i = 0; i < actionRanges.length; i++) {
99             if (offset >= actionRanges[i][0]
100                     && offset < actionRanges[i][0] + actionRanges[i][1]) {
101                 return true;
102             }
103         }
104         return false;
105     }
106
107     /**
108      * Logs a error to the workbench log
109      */

110     public void logActionLinkError(String JavaDoc actionPluginId, String JavaDoc actionClass) {
111         IDEWorkbenchPlugin
112                 .log(IDEWorkbenchMessages.WelcomeItem_unableToLoadClass + actionPluginId + " " + actionClass); //$NON-NLS-1$
113
}
114
115     /**
116      * Open a help topic
117      */

118     private void openHelpTopic(String JavaDoc topic, String JavaDoc href) {
119         if (href != null) {
120             PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(href);
121         } else {
122             PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(topic);
123         }
124     }
125
126     /**
127      * Run an action
128      */

129     private void runAction(String JavaDoc pluginId, String JavaDoc className) {
130         Bundle pluginBundle = Platform.getBundle(pluginId);
131         if (pluginBundle == null) {
132             logActionLinkError(pluginId, className);
133             return;
134         }
135         Class JavaDoc actionClass;
136         IAction action;
137         try {
138             actionClass = pluginBundle.loadClass(className);
139         } catch (ClassNotFoundException JavaDoc e) {
140             logActionLinkError(pluginId, className);
141             return;
142         }
143         try {
144             action = (IAction) actionClass.newInstance();
145         } catch (InstantiationException JavaDoc e) {
146             logActionLinkError(pluginId, className);
147             return;
148         } catch (IllegalAccessException JavaDoc e) {
149             logActionLinkError(pluginId, className);
150             return;
151         } catch (ClassCastException JavaDoc e) {
152             logActionLinkError(pluginId, className);
153             return;
154         }
155         action.run();
156     }
157
158     /**
159      * Triggers the link at the given offset (if there is one)
160      */

161     public void triggerLinkAt(int offset) {
162         // Check if there is a help link at the offset
163
for (int i = 0; i < helpRanges.length; i++) {
164             if (offset >= helpRanges[i][0]
165                     && offset < helpRanges[i][0] + helpRanges[i][1]) {
166                 // trigger the link
167
openHelpTopic(helpIds[i], helpHrefs[i]);
168                 return;
169             }
170         }
171
172         // Check if there is an action link at the offset
173
for (int i = 0; i < actionRanges.length; i++) {
174             if (offset >= actionRanges[i][0]
175                     && offset < actionRanges[i][0] + actionRanges[i][1]) {
176                 // trigger the link
177
runAction(actionPluginIds[i], actionClasses[i]);
178                 return;
179             }
180         }
181     }
182 }
183
Popular Tags