001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.dialogs;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.swt.widgets.Composite;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.ui.PartInitException;
018: import org.eclipse.ui.internal.WorkbenchPlugin;
019: import org.eclipse.ui.internal.misc.StatusUtil;
020: import org.eclipse.ui.statushandlers.StatusManager;
021:
022: /**
023: * Utility class to help with dialogs.
024: * <p>
025: * Note that a copy of this class exists in the
026: * org.eclipse.ui.internal.ide package.
027: * </p>
028: */
029: public class DialogUtil {
030:
031: /**
032: * Prevent instantiation.
033: */
034: private DialogUtil() {
035: }
036:
037: /**
038: * Open an error style dialog for PartInitException by
039: * including any extra information from the nested
040: * CoreException if present.
041: */
042: public static void openError(Shell parent, String title,
043: String message, PartInitException exception) {
044: // Check for a nested CoreException
045: CoreException nestedException = null;
046: IStatus status = exception.getStatus();
047: if (status != null
048: && status.getException() instanceof CoreException) {
049: nestedException = (CoreException) status.getException();
050: }
051:
052: IStatus errorStatus = null;
053:
054: if (nestedException != null) {
055: // Open an error dialog and include the extra
056: // status information from the nested CoreException
057: errorStatus = StatusUtil.newStatus(nestedException
058: .getStatus(), message);
059: } else {
060: // Open a regular error dialog since there is no
061: // extra information to displa
062: errorStatus = new Status(IStatus.ERROR,
063: WorkbenchPlugin.PI_WORKBENCH, message);
064: }
065:
066: StatusUtil
067: .handleStatus(errorStatus, StatusManager.SHOW, parent);
068: }
069:
070: /**
071: * Removes the '&' accelerator indicator from a label, if any. Also removes
072: * the () accelerators which are used in Asian languages.
073: */
074: public static String removeAccel(String label) {
075:
076: int startBracket = label.indexOf("(&"); //$NON-NLS-1$
077: //Non latin accelerator?
078: if (startBracket >= 0) {
079: int endBracket = label.indexOf(')');
080:
081: //If there is more than one character it is not an accelerator
082: if ((endBracket - startBracket) == 3) {
083: return label.substring(0, startBracket)
084: + label.substring(endBracket + 1);
085: }
086: }
087:
088: int i = label.indexOf('&');
089: if (i >= 0) {
090: label = label.substring(0, i) + label.substring(i + 1);
091: }
092:
093: return label;
094: }
095:
096: /**
097: * Return the number of rows available in the current display using the
098: * current font.
099: * @param parent The Composite whose Font will be queried.
100: * @return int The result of the display size divided by the font size.
101: */
102: public static int availableRows(Composite parent) {
103:
104: int fontHeight = (parent.getFont().getFontData())[0]
105: .getHeight();
106: int displayHeight = parent.getDisplay().getClientArea().height;
107:
108: return displayHeight / fontHeight;
109: }
110:
111: /**
112: * Return whether or not the font in the parent is the size of a result
113: * font (i.e. smaller than the High Contrast Font). This method is used to
114: * make layout decisions based on screen space.
115: * @param parent The Composite whose Font will be queried.
116: * @return boolean. True if there are more than 50 lines of possible
117: * text in the display.
118: */
119: public static boolean inRegularFontMode(Composite parent) {
120:
121: return availableRows(parent) > 50;
122: }
123: }
|