View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: AboutAction.java
13   * Created: 2008-07-14
14   * Author: apohllo
15   * $Id: ImportFileAction.java 3269 2009-08-28 11:22:09Z tmilos $
16   */
17  
18  package pl.edu.agh.cast.backward.action;
19  
20  import org.eclipse.core.resources.IProject;
21  import org.eclipse.jface.action.IAction;
22  import org.eclipse.jface.viewers.ISelection;
23  import org.eclipse.jface.viewers.IStructuredSelection;
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.widgets.Display;
26  import org.eclipse.swt.widgets.FileDialog;
27  import org.eclipse.swt.widgets.Shell;
28  import org.eclipse.ui.IEditorPart;
29  import org.eclipse.ui.IObjectActionDelegate;
30  import org.eclipse.ui.IWorkbenchPart;
31  
32  import pl.edu.agh.cast.CastApplication;
33  import pl.edu.agh.cast.backward.editor.EditorUtilBackward;
34  import pl.edu.agh.cast.backward.resources.DiagramEditorInput;
35  import pl.edu.agh.cast.backward.resources.ProjectStore;
36  import pl.edu.agh.cast.backward.resources.SerializationUtil;
37  import pl.edu.agh.cast.model.base.DataImporter;
38  import pl.edu.agh.cast.model.base.IDataSet;
39  import pl.edu.agh.cast.model.base.IModel;
40  import pl.edu.agh.cast.model.mapper.Mapper;
41  
42  /**
43   * This action allows to load data to model from external file.
44   *
45   * This is basic version of importer, intended only for loading "artificial" data for testing internal mechanisms of the
46   * system, such as layout algorithms. The "real" data are loaded via <code>pl.edu.agh.cast.data</code> plug-in and
47   * various domain-specific plug-ins.
48   *
49   * @author AGH CAST Team
50   */
51  @Deprecated
52  public class ImportFileAction implements IObjectActionDelegate {
53  
54  	private IModel model;
55  
56  	@SuppressWarnings("unused")
57  	private IWorkbenchPart part;
58  
59  	/**
60  	 * {@inheritDoc}
61  	 *
62  	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
63  	 */
64  	public void run(IAction action) {
65  		// file dialog
66  		Display display = CastApplication.getDisplay();
67  		Shell shell = new Shell(display);
68  		FileDialog dialog = new FileDialog(shell, SWT.NULL);
69  		String path = dialog.open();
70  		if (path == null) {
71  			return;
72  		}
73  
74  		DataImporter importer = new DataImporter(model);
75  		try {
76  			IDataSet data = importer.importDataFromFile(path);
77  			model.addDataSet(data);
78  			data.setModel(model);
79  			IProject project = ProjectStore.getInstance().getProject(model);
80  			Mapper.getInstance().save(data, project);
81  			// open schema editor for imported model
82  			IEditorPart editor = EditorUtilBackward.openNewEditor(data, project, EditorUtilBackward.getDefaultEditor());
83  
84  			if (editor != null) {
85  				DiagramEditorInput input = (DiagramEditorInput)editor.getEditorInput();
86  				SerializationUtil.serializeToFile(input);
87  			}
88  
89  		} catch (Exception e) {
90  			e.printStackTrace();
91  		}
92  
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 *
98  	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
99  	 *      org.eclipse.jface.viewers.ISelection)
100 	 */
101 	public void selectionChanged(IAction action, ISelection selection) {
102 		if (selection instanceof IStructuredSelection) {
103 			Object input = ((IStructuredSelection)selection).getFirstElement();
104 			if (input instanceof IModel) {
105 				model = (IModel)input;
106 			}
107 		}
108 	}
109 
110 	/**
111 	 * {@inheritDoc}
112 	 *
113 	 * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
114 	 *      org.eclipse.ui.IWorkbenchPart)
115 	 */
116 	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
117 		part = targetPart;
118 	}
119 
120 }