X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FDialogs.java;fp=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FDialogs.java;h=59fa17ce2514f4bbd8a43c50f28064ca5e6a38d2;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/Dialogs.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/Dialogs.java new file mode 100644 index 000000000..59fa17ce2 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/Dialogs.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.swt; + +import java.util.function.Consumer; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.jface.window.Window; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.DirectoryDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.utils.datastructures.UnaryFunction; + +public class Dialogs { + + public static void invoke(final UnaryFunction function, final Consumer callback) { + + Display.getDefault().asyncExec(new Runnable() { + + @SuppressWarnings("unchecked") + @Override + public void run() { + + SimanticsDialog dialog = function.call(Display.getCurrent().getActiveShell()); + + dialog.open(); + if(Window.CANCEL == dialog.getReturnCode()) callback.accept(null); + else callback.accept((T)dialog.getSelection()); + + } + + }); + + } + + public static String file(Shell shell, int style, String extension) { + + FileDialog dialog = new FileDialog(shell, style); + dialog.setFilterExtensions(new String[] { extension }); + String newFileName = dialog.open(); + return newFileName; + + } + + public static String directory(Shell shell, int style, String filterPath) { + + DirectoryDialog dialog = new DirectoryDialog(shell, style); + if(filterPath != null) dialog.setFilterPath(filterPath); + String newFileName = dialog.open(); + return newFileName; + + } + + public static String file(int style, String extension) { + return file(Display.getDefault().getActiveShell(), style, extension); + } + + public static String file(Shell shell, String extension) { + return file(shell, SWT.OPEN, extension); + } + + public static String file(ExecutionEvent event, String extension) { + return file(HandlerUtil.getActiveShell(event), extension); + } + + public static String file(ExecutionEvent event, int style, String extension) { + return file(HandlerUtil.getActiveShell(event), style, extension); + } + + public static String directory(int style, String filterPath) { + return directory(Display.getDefault().getActiveShell(), style, filterPath); + } + +}