1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.image.ui;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.FileDialog;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.PlatformUI;
25 import org.simantics.Simantics;
26 import org.simantics.db.Resource;
27 import org.simantics.db.layer0.adapter.ActionFactory;
28 import org.simantics.utils.FileUtils;
29 import org.simantics.utils.ui.AdaptionUtils;
32 * @author Tuukka Lehtonen
34 public class ImportImagesActionFactory implements ActionFactory {
37 public Runnable create(Object target) {
38 final Resource container = AdaptionUtils.adaptToSingle(target, Resource.class);
39 if (container == null)
42 return new Runnable() {
45 IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
48 Collection<File> files = requestImportedImages(win.getShell());
52 Simantics.getSession().markUndoPoint();
53 Simantics.getSession().asyncRequest( new CreateImages(container, files) );
58 public static Collection<File> requestImportedImages(Shell parentShell) {
59 FileDialog dialog = new FileDialog(parentShell, SWT.MULTI);
60 dialog.setText("Choose image to be imported");
61 dialog.setFilterExtensions( new String[] {"*.jpg;*.png;*.gif;*.svg", "*.jpg;*.jpeg", "*.png", "*.gif", "*.svg"} );
62 dialog.setFilterNames( new String[] {"All Images", "JPEG Image", "PNG Image", "GIF Image", "SVG Image"} );
63 //dialog.setFilterExtensions( new String[] {"*.jpg", "*.png", "*.gif"} );
64 final String filename = dialog.open();
66 return Collections.emptyList();
67 String[] files = dialog.getFileNames();
68 File parent = new File(dialog.getFilterPath());
69 ArrayList<File> result = new ArrayList<File>(files.length);
70 for (String file : files)
71 result.add( new File(parent, file) );
75 public static ImageSource toImageSource(File file) throws IOException {
76 ImageSource src = new ImageSource();
77 src.name = file.getName();
78 src.data = FileUtils.readFile(file);