X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.image.ui%2Fsrc%2Forg%2Fsimantics%2Fimage%2Fui%2FImportImagesActionFactory.java;h=a0e966f98f947df987e5f0842eab0a630be68260;hp=cd5cab77c2e44ab96a2c893c0f5f646e5ea9f23e;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.image.ui/src/org/simantics/image/ui/ImportImagesActionFactory.java b/bundles/org.simantics.image.ui/src/org/simantics/image/ui/ImportImagesActionFactory.java index cd5cab77c..a0e966f98 100644 --- a/bundles/org.simantics.image.ui/src/org/simantics/image/ui/ImportImagesActionFactory.java +++ b/bundles/org.simantics.image.ui/src/org/simantics/image/ui/ImportImagesActionFactory.java @@ -1,82 +1,82 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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.image.ui; - - import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.FileDialog; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.PlatformUI; -import org.simantics.db.Resource; -import org.simantics.db.layer0.adapter.ActionFactory; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.FileUtils; -import org.simantics.utils.ui.AdaptionUtils; - -/** - * @author Tuukka Lehtonen - */ -public class ImportImagesActionFactory implements ActionFactory { - - @Override - public Runnable create(Object target) { - final Resource container = AdaptionUtils.adaptToSingle(target, Resource.class); - if (container == null) - return null; - - return new Runnable() { - @Override - public void run() { - IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); - if (win == null) - return; - Collection files = requestImportedImages(win.getShell()); - if (files.isEmpty()) - return; - - SimanticsUI.getSession().markUndoPoint(); - SimanticsUI.getSession().asyncRequest( new CreateImages(container, files) ); - } - }; - } - - public static Collection requestImportedImages(Shell parentShell) { - FileDialog dialog = new FileDialog(parentShell, SWT.MULTI); - dialog.setText("Choose image to be imported"); - dialog.setFilterExtensions( new String[] {"*.jpg;*.png;*.gif;*.svg", "*.jpg;*.jpeg", "*.png", "*.gif", "*.svg"} ); - dialog.setFilterNames( new String[] {"All Images", "JPEG Image", "PNG Image", "GIF Image", "SVG Image"} ); - //dialog.setFilterExtensions( new String[] {"*.jpg", "*.png", "*.gif"} ); - final String filename = dialog.open(); - if (filename == null) - return Collections.emptyList(); - String[] files = dialog.getFileNames(); - File parent = new File(dialog.getFilterPath()); - ArrayList result = new ArrayList(files.length); - for (String file : files) - result.add( new File(parent, file) ); - return result; - } - - public static ImageSource toImageSource(File file) throws IOException { - ImageSource src = new ImageSource(); - src.name = file.getName(); - src.data = FileUtils.readFile(file); - return src; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 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.image.ui; + + import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.simantics.db.Resource; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.FileUtils; +import org.simantics.utils.ui.AdaptionUtils; + +/** + * @author Tuukka Lehtonen + */ +public class ImportImagesActionFactory implements ActionFactory { + + @Override + public Runnable create(Object target) { + final Resource container = AdaptionUtils.adaptToSingle(target, Resource.class); + if (container == null) + return null; + + return new Runnable() { + @Override + public void run() { + IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); + if (win == null) + return; + Collection files = requestImportedImages(win.getShell()); + if (files.isEmpty()) + return; + + SimanticsUI.getSession().markUndoPoint(); + SimanticsUI.getSession().asyncRequest( new CreateImages(container, files) ); + } + }; + } + + public static Collection requestImportedImages(Shell parentShell) { + FileDialog dialog = new FileDialog(parentShell, SWT.MULTI); + dialog.setText("Choose image to be imported"); + dialog.setFilterExtensions( new String[] {"*.jpg;*.png;*.gif;*.svg", "*.jpg;*.jpeg", "*.png", "*.gif", "*.svg"} ); + dialog.setFilterNames( new String[] {"All Images", "JPEG Image", "PNG Image", "GIF Image", "SVG Image"} ); + //dialog.setFilterExtensions( new String[] {"*.jpg", "*.png", "*.gif"} ); + final String filename = dialog.open(); + if (filename == null) + return Collections.emptyList(); + String[] files = dialog.getFileNames(); + File parent = new File(dialog.getFilterPath()); + ArrayList result = new ArrayList(files.length); + for (String file : files) + result.add( new File(parent, file) ); + return result; + } + + public static ImageSource toImageSource(File file) throws IOException { + ImageSource src = new ImageSource(); + src.name = file.getName(); + src.data = FileUtils.readFile(file); + return src; + } + +}