KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > DialogUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.dialogs;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.PartInitException;
19 import org.eclipse.ui.internal.WorkbenchPlugin;
20 import org.eclipse.ui.internal.misc.StatusUtil;
21 import org.eclipse.ui.statushandlers.StatusManager;
22
23 /**
24  * Utility class to help with dialogs.
25  * <p>
26  * Note that a copy of this class exists in the
27  * org.eclipse.ui.internal.ide package.
28  * </p>
29  */

30 public class DialogUtil {
31
32     /**
33      * Prevent instantiation.
34      */

35     private DialogUtil() {
36     }
37
38     /**
39      * Open an error style dialog for PartInitException by
40      * including any extra information from the nested
41      * CoreException if present.
42      */

43     public static void openError(Shell parent, String JavaDoc title, String JavaDoc message,
44             PartInitException exception) {
45         // Check for a nested CoreException
46
CoreException nestedException = null;
47         IStatus status = exception.getStatus();
48         if (status != null && status.getException() instanceof CoreException) {
49             nestedException = (CoreException) status.getException();
50         }
51         
52         IStatus errorStatus = null;
53
54         if (nestedException != null) {
55             // Open an error dialog and include the extra
56
// status information from the nested CoreException
57
errorStatus = StatusUtil.newStatus(nestedException.getStatus(),
58                     message);
59         } else {
60             // Open a regular error dialog since there is no
61
// extra information to displa
62
errorStatus = new Status(IStatus.ERROR,
63                     WorkbenchPlugin.PI_WORKBENCH, message);
64         }
65
66         StatusUtil.handleStatus(errorStatus, StatusManager.SHOW, parent);
67     }
68
69     /**
70      * Removes the '&' accelerator indicator from a label, if any. Also removes
71      * the () accelerators which are used in Asian languages.
72      */

73     public static String JavaDoc removeAccel(String JavaDoc label) {
74
75         int startBracket = label.indexOf("(&"); //$NON-NLS-1$
76
//Non latin accelerator?
77
if (startBracket >= 0) {
78             int endBracket = label.indexOf(')');
79
80             //If there is more than one character it is not an accelerator
81
if ((endBracket - startBracket) == 3) {
82                 return label.substring(0, startBracket)
83                         + label.substring(endBracket + 1);
84             }
85         }
86
87         int i = label.indexOf('&');
88         if (i >= 0) {
89             label = label.substring(0, i) + label.substring(i + 1);
90         }
91
92         return label;
93     }
94
95     /**
96      * Return the number of rows available in the current display using the
97      * current font.
98      * @param parent The Composite whose Font will be queried.
99      * @return int The result of the display size divided by the font size.
100      */

101     public static int availableRows(Composite parent) {
102
103         int fontHeight = (parent.getFont().getFontData())[0].getHeight();
104         int displayHeight = parent.getDisplay().getClientArea().height;
105
106         return displayHeight / fontHeight;
107     }
108
109     /**
110      * Return whether or not the font in the parent is the size of a result
111      * font (i.e. smaller than the High Contrast Font). This method is used to
112      * make layout decisions based on screen space.
113      * @param parent The Composite whose Font will be queried.
114      * @return boolean. True if there are more than 50 lines of possible
115      * text in the display.
116      */

117     public static boolean inRegularFontMode(Composite parent) {
118
119         return availableRows(parent) > 50;
120     }
121 }
122
Popular Tags