forked from srizzo/code2code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEclipseGuiUtils.java
More file actions
88 lines (69 loc) · 2.66 KB
/
EclipseGuiUtils.java
File metadata and controls
88 lines (69 loc) · 2.66 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package code2code.utils;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
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);
}
public static String openFileDialog(Shell shell, String filterName,
String filterExt) {
FileDialog fileDialog = createFileDialog(shell, filterName, filterExt,SWT.OPEN);
return fileDialog.open();
}
public static String saveFileDialog(Shell shell, String filterName,
String filterExt) {
FileDialog fileDialog = createFileDialog(shell, filterName, filterExt,SWT.SAVE);
return fileDialog.open();
}
private static FileDialog createFileDialog(Shell shell, String filterName,
String filterExt,int style) {
FileDialog fileDialog = new FileDialog(shell,style);
fileDialog.setText("Select File");
fileDialog.setFilterExtensions(new String[] { filterExt });
fileDialog.setFilterNames(new String[] { String.format("%s(%s)",
filterName, filterExt) });
return fileDialog;
}
}