01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.views;
11:
12: import org.eclipse.core.runtime.IStatus;
13: import org.eclipse.core.runtime.Status;
14: import org.eclipse.ui.IWorkbench;
15: import org.eclipse.ui.IWorkbenchPage;
16: import org.eclipse.ui.IWorkbenchWindow;
17: import org.eclipse.ui.PartInitException;
18: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
19: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
20: import org.eclipse.ui.internal.cheatsheets.Messages;
21:
22: /**
23: * Contains static functions used in cheat sheet display
24: */
25: public class ViewUtilities {
26:
27: /**
28: * Escape any ampersands used in a label
29: **/
30: public static String escapeForLabel(String message) {
31: // Make the most common case - i.e. no ampersand the
32: // most efficient
33: if (message.indexOf('&') < 0) {
34: return message;
35: }
36:
37: int next = 0;
38: StringBuffer result = new StringBuffer();
39: int index = message.indexOf('&');
40: while (index >= 0) {
41: result.append(message.substring(next, index + 1));
42: result.append('&');
43: next = index + 1;
44: index = message.indexOf('&', next);
45: }
46: result.append(message.substring(next));
47: return result.toString();
48: }
49:
50: /**
51: * Get the cheaetSheetView, opening it if necessary and making visible
52: * @return The cheat sheet view, or null if it could not be opened.
53: */
54: public static CheatSheetView showCheatSheetView() {
55: CheatSheetView view;
56: IWorkbench workbench = CheatSheetPlugin.getPlugin()
57: .getWorkbench();
58: IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
59: IWorkbenchPage page = window.getActivePage();
60:
61: view = (CheatSheetView) page
62: .findView(ICheatSheetResource.CHEAT_SHEET_VIEW_ID);
63: if (view == null) {
64: try {
65: view = (CheatSheetView) page
66: .showView(ICheatSheetResource.CHEAT_SHEET_VIEW_ID);
67: page.activate(view);
68: } catch (PartInitException pie) {
69: String message = Messages.LAUNCH_SHEET_ERROR;
70: IStatus status = new Status(IStatus.ERROR,
71: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
72: IStatus.OK, message, pie);
73: CheatSheetPlugin.getPlugin().getLog().log(status);
74: org.eclipse.jface.dialogs.ErrorDialog.openError(window
75: .getShell(),
76: Messages.CHEAT_SHEET_ERROR_OPENING, null, pie
77: .getStatus());
78: return null;
79: }
80: }
81: return view;
82: }
83:
84: }
|