-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEclipseGuiUtils.java
More file actions
63 lines (45 loc) · 1.81 KB
/
EclipseGuiUtils.java
File metadata and controls
63 lines (45 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package code2code.utils;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
public class EclipseGuiUtils {
public static void openResource(final IFile resource) {
final IWorkbenchPage activePage= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
if (activePage != null) {
final Display display= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay();
if (display != null) {
display.asyncExec(new Runnable() {
public void run() {
try {
IDE.openEditor(activePage, resource, true);
} catch (PartInitException e) {
EclipseGuiUtils.showErrorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), e);
throw new RuntimeException(e);
}
}
});
}
}
}
public static void showErrorDialog(Shell shell, Exception e) {
MessageDialog.openError(shell,
"An Error ocurred",
"An Error ocurred: " + e.getMessage() +
"\nSee Error Log view for more details.");
}
public static void scaleShellToClientArea(Shell shell, double ratio) {
Rectangle clientArea = Display.getCurrent().getClientArea();
int relativeWidth = (int) (clientArea.width * ratio);
int relativeHeight = (int) (clientArea.height * ratio);
int hPadding = (clientArea.width - relativeWidth) / 2;
int vPadding = (clientArea.height - relativeHeight) / 2;
Rectangle bounds = new Rectangle(clientArea.x + hPadding, clientArea.y + vPadding, relativeWidth, relativeHeight);
shell.setBounds(bounds);
}
}