]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/Dialogs.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / Dialogs.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.function.Consumer;
15
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.DirectoryDialog;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.FileDialog;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.handlers.HandlerUtil;
24 import org.simantics.utils.datastructures.UnaryFunction;
25
26 public class Dialogs {
27
28         public static <T> void invoke(final UnaryFunction<SimanticsDialog, Shell> function, final Consumer<T> callback) {
29                 
30                 Display.getDefault().asyncExec(new Runnable() {
31
32                         @SuppressWarnings("unchecked")
33                         @Override
34                         public void run() {
35                                 
36                                 SimanticsDialog dialog = function.call(Display.getCurrent().getActiveShell()); 
37                                 
38                                 dialog.open();
39                                 if(Window.CANCEL == dialog.getReturnCode()) callback.accept(null);
40                                 else callback.accept((T)dialog.getSelection());
41                                 
42                         }
43                         
44                 });
45                 
46         }
47         
48         public static String file(Shell shell, int style, String extension) {
49
50         FileDialog dialog = new FileDialog(shell, style);
51         dialog.setFilterExtensions(new String[] { extension });
52         String newFileName = dialog.open();
53         return newFileName;
54
55         }
56
57         public static String directory(Shell shell, int style, String filterPath) {
58
59         DirectoryDialog dialog = new DirectoryDialog(shell, style);
60         if(filterPath != null) dialog.setFilterPath(filterPath);
61         String newFileName = dialog.open();
62         return newFileName;
63
64         }
65
66         public static String file(int style, String extension) {
67                 return file(Display.getDefault().getActiveShell(), style, extension);
68         }
69
70         public static String file(Shell shell, String extension) {
71                 return file(shell, SWT.OPEN, extension);
72         }
73
74         public static String file(ExecutionEvent event, String extension) {
75                 return file(HandlerUtil.getActiveShell(event), extension);
76         }
77
78         public static String file(ExecutionEvent event, int style, String extension) {
79                 return file(HandlerUtil.getActiveShell(event), style, extension);
80         }
81
82         public static String directory(int style, String filterPath) {
83                 return directory(Display.getDefault().getActiveShell(), style, filterPath);
84         }
85
86 }