1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.function.Consumer;
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;
26 public class Dialogs {
28 public static <T> void invoke(final UnaryFunction<SimanticsDialog, Shell> function, final Consumer<T> callback) {
30 Display.getDefault().asyncExec(new Runnable() {
32 @SuppressWarnings("unchecked")
36 SimanticsDialog dialog = function.call(Display.getCurrent().getActiveShell());
39 if(Window.CANCEL == dialog.getReturnCode()) callback.accept(null);
40 else callback.accept((T)dialog.getSelection());
48 public static String file(Shell shell, int style, String extension) {
50 FileDialog dialog = new FileDialog(shell, style);
51 dialog.setFilterExtensions(new String[] { extension });
52 String newFileName = dialog.open();
57 public static String directory(Shell shell, int style, String filterPath) {
59 DirectoryDialog dialog = new DirectoryDialog(shell, style);
60 if(filterPath != null) dialog.setFilterPath(filterPath);
61 String newFileName = dialog.open();
66 public static String file(int style, String extension) {
67 return file(Display.getDefault().getActiveShell(), style, extension);
70 public static String file(Shell shell, String extension) {
71 return file(shell, SWT.OPEN, extension);
74 public static String file(ExecutionEvent event, String extension) {
75 return file(HandlerUtil.getActiveShell(event), extension);
78 public static String file(ExecutionEvent event, int style, String extension) {
79 return file(HandlerUtil.getActiveShell(event), style, extension);
82 public static String directory(int style, String filterPath) {
83 return directory(Display.getDefault().getActiveShell(), style, filterPath);