KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > action > AbstractOpenAction


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.action;
28
29 import java.io.FileInputStream JavaDoc;
30
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.FileDialog;
34 import org.eclipse.swt.widgets.Shell;
35 import org.nightlabs.base.io.IOFilterRegistry;
36 import org.nightlabs.base.io.IOUtil;
37 import org.nightlabs.editor2d.DrawComponent;
38 import org.nightlabs.editor2d.viewer.AbstractViewerDialog;
39 import org.nightlabs.editor2d.viewer.ViewerPlugin;
40 import org.nightlabs.io.IOFilter;
41 import org.nightlabs.io.IOFilterMan;
42
43 public abstract class AbstractOpenAction
44 extends Action
45 {
46     public static final String JavaDoc ID = AbstractOpenAction.class.getName();
47     
48     public AbstractOpenAction()
49     {
50         super();
51         init();
52     }
53     
54     protected void init()
55     {
56         setId(ID);
57         setText(ViewerPlugin.getResourceString("action.openFile.text"));
58         setToolTipText(ViewerPlugin.getResourceString("action.openFile.tooltip"));
59     }
60     
61     public void run()
62     {
63         FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell());
64         IOFilterMan ioFilterMan = IOFilterRegistry.sharedInstance().getIOFilterMan();
65         String JavaDoc[] fileExtensions = ioFilterMan.getAvailableFileExtensionsAsStrings(true);
66         fileDialog.setFilterExtensions(fileExtensions);
67         String JavaDoc fileName = fileDialog.open();
68         if (fileName != null) {
69             String JavaDoc fileExtension = IOUtil.getFileExtension(fileName);
70             if (fileExtension != null) {
71                 IOFilter ioFilter = ioFilterMan.getIOFilter(fileExtension);
72                 if (ioFilter != null) {
73                     DrawComponent dc;
74                     try {
75                         dc = (DrawComponent) ioFilter.read(new FileInputStream JavaDoc(fileName));
76                     } catch (Exception JavaDoc e) {
77                         throw new RuntimeException JavaDoc(e);
78                     }
79                     AbstractViewerDialog dialog = initViewerDialog(Display.getCurrent().getActiveShell(), dc);
80                     dialog.open();
81                 }
82             }
83         }
84     }
85     
86     protected abstract AbstractViewerDialog initViewerDialog(Shell shell, DrawComponent dc);
87 }
88
Popular Tags