001: /*******************************************************************************
002: * Copyright (c) 2000, 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.ide.dialogs;
011:
012: import org.eclipse.core.runtime.Platform;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.ui.PlatformUI;
015: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
016: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
017: import org.osgi.framework.Bundle;
018:
019: /**
020: * Holds the information for an item appearing in the welcome editor
021: */
022: public class WelcomeItem {
023: private String text;
024:
025: private int[][] boldRanges;
026:
027: private int[][] helpRanges;
028:
029: private String[] helpIds;
030:
031: private String[] helpHrefs;
032:
033: private int[][] actionRanges;
034:
035: private String[] actionPluginIds;
036:
037: private String[] actionClasses;
038:
039: /**
040: * Creates a new welcome item
041: */
042: public WelcomeItem(String text, int[][] boldRanges,
043: int[][] actionRanges, String[] actionPluginIds,
044: String[] actionClasses, int[][] helpRanges,
045: String[] helpIds, String[] helpHrefs) {
046:
047: this .text = text;
048: this .boldRanges = boldRanges;
049: this .actionRanges = actionRanges;
050: this .actionPluginIds = actionPluginIds;
051: this .actionClasses = actionClasses;
052: this .helpRanges = helpRanges;
053: this .helpIds = helpIds;
054: this .helpHrefs = helpHrefs;
055: }
056:
057: /**
058: * Returns the action ranges (character locations)
059: */
060: public int[][] getActionRanges() {
061: return actionRanges;
062: }
063:
064: /**
065: * Returns the bold ranges (character locations)
066: */
067: public int[][] getBoldRanges() {
068: return boldRanges;
069: }
070:
071: /**
072: * Returns the help ranges (character locations)
073: */
074: public int[][] getHelpRanges() {
075: return helpRanges;
076: }
077:
078: /**
079: * Returns the text to display
080: */
081: public String getText() {
082: return text;
083: }
084:
085: /**
086: * Returns true is a link (action or help) is present at the given character location
087: */
088: public boolean isLinkAt(int offset) {
089: // Check if there is a link at the offset
090: for (int i = 0; i < helpRanges.length; i++) {
091: if (offset >= helpRanges[i][0]
092: && offset < helpRanges[i][0] + helpRanges[i][1]) {
093: return true;
094: }
095: }
096:
097: // Check if there is an action link at the offset
098: for (int i = 0; i < actionRanges.length; i++) {
099: 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 actionPluginId,
111: String actionClass) {
112: IDEWorkbenchPlugin
113: .log(IDEWorkbenchMessages.WelcomeItem_unableToLoadClass
114: + actionPluginId + " " + actionClass); //$NON-NLS-1$
115: }
116:
117: /**
118: * Open a help topic
119: */
120: private void openHelpTopic(String topic, String href) {
121: if (href != null) {
122: PlatformUI.getWorkbench().getHelpSystem()
123: .displayHelpResource(href);
124: } else {
125: PlatformUI.getWorkbench().getHelpSystem()
126: .displayHelpResource(topic);
127: }
128: }
129:
130: /**
131: * Run an action
132: */
133: private void runAction(String pluginId, String className) {
134: Bundle pluginBundle = Platform.getBundle(pluginId);
135: if (pluginBundle == null) {
136: logActionLinkError(pluginId, className);
137: return;
138: }
139: Class actionClass;
140: IAction action;
141: try {
142: actionClass = pluginBundle.loadClass(className);
143: } catch (ClassNotFoundException e) {
144: logActionLinkError(pluginId, className);
145: return;
146: }
147: try {
148: action = (IAction) actionClass.newInstance();
149: } catch (InstantiationException e) {
150: logActionLinkError(pluginId, className);
151: return;
152: } catch (IllegalAccessException e) {
153: logActionLinkError(pluginId, className);
154: return;
155: } catch (ClassCastException e) {
156: logActionLinkError(pluginId, className);
157: return;
158: }
159: action.run();
160: }
161:
162: /**
163: * Triggers the link at the given offset (if there is one)
164: */
165: public void triggerLinkAt(int offset) {
166: // Check if there is a help link at the offset
167: for (int i = 0; i < helpRanges.length; i++) {
168: if (offset >= helpRanges[i][0]
169: && offset < helpRanges[i][0] + helpRanges[i][1]) {
170: // trigger the link
171: openHelpTopic(helpIds[i], helpHrefs[i]);
172: return;
173: }
174: }
175:
176: // Check if there is an action link at the offset
177: for (int i = 0; i < actionRanges.length; i++) {
178: if (offset >= actionRanges[i][0]
179: && offset < actionRanges[i][0] + actionRanges[i][1]) {
180: // trigger the link
181: runAction(actionPluginIds[i], actionClasses[i]);
182: return;
183: }
184: }
185: }
186: }
|