]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/util/SWTUtil.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / util / SWTUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.jdt.internal.ui.util;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.accessibility.ACC;
15 import org.eclipse.swt.accessibility.AccessibleAdapter;
16 import org.eclipse.swt.accessibility.AccessibleEvent;
17 import org.eclipse.swt.dnd.DragSource;
18 import org.eclipse.swt.dnd.DropTarget;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Caret;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.ScrollBar;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Table;
29 import org.eclipse.swt.widgets.Widget;
30
31 import org.eclipse.core.runtime.Assert;
32
33 import org.eclipse.jface.dialogs.IDialogConstants;
34 import org.eclipse.jface.layout.PixelConverter;
35 import org.eclipse.jface.resource.JFaceResources;
36
37
38 /**
39  * Utility class to simplify access to some SWT resources.
40  */
41 public class SWTUtil {
42
43         /**
44          * The default visible item count for {@link Combo}s.
45          * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=245569 .
46          *
47          * @see Combo#setVisibleItemCount(int)
48          *
49          * @since 3.5
50          */
51         public static final int COMBO_VISIBLE_ITEM_COUNT= 30;
52
53         /**
54          * Returns the shell for the given widget. If the widget doesn't represent
55          * a SWT object that manage a shell, <code>null</code> is returned.
56          * @param widget the widget
57          *
58          * @return the shell for the given widget
59          */
60         public static Shell getShell(Widget widget) {
61                 if (widget instanceof Control)
62                         return ((Control)widget).getShell();
63                 if (widget instanceof Caret)
64                         return ((Caret)widget).getParent().getShell();
65                 if (widget instanceof DragSource)
66                         return ((DragSource)widget).getControl().getShell();
67                 if (widget instanceof DropTarget)
68                         return ((DropTarget)widget).getControl().getShell();
69                 if (widget instanceof Menu)
70                         return ((Menu)widget).getParent().getShell();
71                 if (widget instanceof ScrollBar)
72                         return ((ScrollBar)widget).getParent().getShell();
73
74                 return null;
75         }
76
77
78         /**
79          * Returns a width hint for a button control.
80          * @param button the button
81          * @return the width hint
82          */
83         public static int getButtonWidthHint(Button button) {
84                 button.setFont(JFaceResources.getDialogFont());
85                 PixelConverter converter= new PixelConverter(button);
86                 int widthHint= converter.convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
87                 return Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
88         }
89
90         /**
91          * Sets width and height hint for the button control.
92          * <b>Note:</b> This is a NOP if the button's layout data is not
93          * an instance of <code>GridData</code>.
94          *
95          * @param button        the button for which to set the dimension hint
96          */
97         public static void setButtonDimensionHint(Button button) {
98                 Assert.isNotNull(button);
99                 Object gd= button.getLayoutData();
100                 if (gd instanceof GridData) {
101                         ((GridData)gd).widthHint= getButtonWidthHint(button);
102                         ((GridData)gd).horizontalAlignment = GridData.FILL;
103                 }
104         }
105
106         public static int getTableHeightHint(Table table, int rows) {
107                 if (table.getFont().equals(JFaceResources.getDefaultFont()))
108                         table.setFont(JFaceResources.getDialogFont());
109                 int result= table.getItemHeight() * rows + table.getHeaderHeight();
110                 if (table.getLinesVisible())
111                         result+= table.getGridLineWidth() * (rows - 1);
112                 return result;
113         }
114
115         /**
116          * Adds an accessibility listener returning the given fixed name.
117          *
118          * @param control the control to add the accessibility support to
119          * @param text the name
120          */
121         public static void setAccessibilityText(Control control, final String text) {
122                 control.getAccessible().addAccessibleListener(new AccessibleAdapter() {
123                         @Override
124                         public void getName(AccessibleEvent e) {
125                                 if (e.childID == ACC.CHILDID_SELF) {
126                                         e.result= text;
127                                 }
128                         }
129                 });
130         }
131
132         /**
133          * Sets the default visible item count for {@link Combo}s.
134          * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=7845 .
135          *
136          * @param combo the combo
137          *
138          * @see Combo#setVisibleItemCount(int)
139          * @see #COMBO_VISIBLE_ITEM_COUNT
140          *
141          * @since 3.5
142          */
143         public static void setDefaultVisibleItemCount(Combo combo) {
144                 combo.setVisibleItemCount(COMBO_VISIBLE_ITEM_COUNT);
145         }
146
147         public static GridLayout newLayoutNoMargins(int columns) {
148                 GridLayout layout= new GridLayout(columns, false);
149                 layout.marginWidth= 0;
150                 layout.marginHeight= 0;
151                 return layout;
152         }
153
154
155 }