]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.image.ui/src/org/simantics/image/ui/ImportImagesActionFactory.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.image.ui / src / org / simantics / image / ui / ImportImagesActionFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.image.ui;
13
14  import java.io.File;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19
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;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class ImportImagesActionFactory implements ActionFactory {
35
36     @Override
37     public Runnable create(Object target) {
38         final Resource container = AdaptionUtils.adaptToSingle(target, Resource.class);
39         if (container == null)
40             return null;
41
42         return new Runnable() {
43             @Override
44             public void run() {
45                 IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
46                 if (win == null)
47                     return;
48                 Collection<File> files = requestImportedImages(win.getShell());
49                 if (files.isEmpty())
50                     return;
51
52                 Simantics.getSession().markUndoPoint();
53                 Simantics.getSession().asyncRequest( new CreateImages(container, files) );
54             }
55         };
56     }
57
58     public static Collection<File> requestImportedImages(Shell parentShell) {
59         FileDialog dialog = new FileDialog(parentShell, SWT.MULTI);
60         dialog.setText(Messages.ImportImagesActionFactory_ChooseImageToBeImported);
61         dialog.setFilterExtensions( new String[] {"*.jpg;*.png;*.gif;*.svg", "*.jpg;*.jpeg", "*.png", "*.gif", "*.svg"} ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
62         dialog.setFilterNames( new String[] {Messages.ImportImagesActionFactory_FilterAllImage, Messages.ImportImagesActionFactory_FilterJPEGImage, Messages.ImportImagesActionFactory_FilterPNGImage, Messages.ImportImagesActionFactory_FilterGIFImages, Messages.ImportImagesActionFactory_FilterSVGImage} );
63         //dialog.setFilterExtensions( new String[] {"*.jpg", "*.png", "*.gif"} );
64         final String filename = dialog.open();
65         if (filename == null)
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) );
72         return result;
73     }
74
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);
79         return src;
80     }
81
82 }