1 package org.simantics.sysdyn.ui.wizards.modules;
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.Path;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.jface.layout.PixelConverter;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Tree;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.request.Read;
35 import org.simantics.sysdyn.SysdynResource;
36 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
37 import org.simantics.ui.SimanticsUI;
38 import org.simantics.utils.datastructures.ArrayMap;
41 public class WizardModulesImportPage extends WizardPage{
43 public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";
45 // dialog store id constants
47 private Text filePathField;
49 // Keep track of the archive that we browsed to last time
50 // the wizard was invoked.
51 private static String previouslyBrowsedFile = "";
53 private Button browseDirectoriesButton;
57 //private IStructuredSelection currentSelection;
59 private Resource selectedModel;
61 GraphExplorerComposite modelExplorer;
63 private boolean selectionMade = false;
66 * Creates a new project creation wizard page.
69 public WizardModulesImportPage() {
70 this("wizardModulesImportPage", null, null); //$NON-NLS-1$
74 * Create a new instance of the receiver.
78 public WizardModulesImportPage(String pageName) {
79 this(pageName,null, null);
83 * More (many more) parameters.
87 * @param currentSelection
90 public WizardModulesImportPage(String pageName,String initialPath,
91 IStructuredSelection currentSelection) {
93 setPageComplete(false);
94 //this.currentSelection = currentSelection;
95 setTitle("Import Module");
96 setDescription("Choose the Module file and the import location, then press Finish.");
99 public void createControl(Composite parent) {
101 initializeDialogUnits(parent);
103 Composite workArea = new Composite(parent, SWT.NONE);
104 setControl(workArea);
106 workArea.setLayout(new GridLayout());
107 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
108 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
110 createProjectsRoot(workArea);
112 createTree(workArea);
115 private void createProjectsRoot(Composite workArea) {
117 // set label for field
118 Label title = new Label(workArea, SWT.NONE);
119 title.setText("Select Module source:");
121 Composite projectGroup = new Composite(workArea, SWT.NONE);
122 GridLayout layout = new GridLayout();
123 layout.numColumns = 2;
124 layout.makeColumnsEqualWidth = false;
125 layout.marginWidth = 0;
127 projectGroup.setLayout(layout);
128 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
130 // module location entry field
131 this.filePathField = new Text(projectGroup, SWT.BORDER);
133 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
134 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
135 filePathField.setLayoutData(directoryPathData);
137 filePathField.addModifyListener(new ModifyListener(){
139 public void modifyText(ModifyEvent e) {
140 previouslyBrowsedFile = filePathField.getText();
143 if (previouslyBrowsedFile != null){
144 filePathField.setText(previouslyBrowsedFile);
149 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
150 browseDirectoriesButton.setText("Browse");
151 setButtonLayoutData(browseDirectoriesButton);
153 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
157 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
158 * elected(org.eclipse.swt.events.SelectionEvent)
160 public void widgetSelected(SelectionEvent e) {
161 handleLocationDirectoryButtonPressed();
167 private void createTree(Composite workArea){
170 Label title = new Label(workArea, SWT.NONE);
171 title.setText("Select import location:");
174 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
177 public Resource perform(ReadGraph graph)
178 throws DatabaseException {
179 Resource model = SimanticsUI.getProject().get();
185 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
186 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
189 .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
191 modelExplorer.finish();
193 modelExplorer.setInput(null, input);
195 GridDataFactory.fillDefaults().grab(true, true).applyTo(
198 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
201 public void widgetSelected(SelectionEvent e) {
202 selectionMade = true;
206 public void widgetDefaultSelected(SelectionEvent e) {
207 selectionMade = true;
213 } catch (DatabaseException e) {
219 //Set filePathField active
220 public void setVisible(boolean visible) {
221 super.setVisible(visible);
222 this.filePathField.setFocus();
225 //Open dialog for choosing the file
226 protected void handleLocationDirectoryButtonPressed() {
228 shell = filePathField.getShell();
230 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
232 String[] ext = {"*.sysdynModule; *.tg", "*.*"};
233 dialog.setFilterExtensions(ext);
235 dialog.setText("Import Module");
237 String dirName = filePathField.getText().trim();
239 File path = new File(dirName);
241 dialog.setFilterPath(new Path(dirName).toOSString());
244 String selectedFile = dialog.open();
245 if (selectedFile != null) {
246 filePathField.setText(selectedFile);
251 //Get selection from the tree
252 @SuppressWarnings("unchecked")
253 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
258 ISelection selection = ((ISelectionProvider) explorer
259 .getAdapter(ISelectionProvider.class)).getSelection();
260 if (selection == null)
262 IStructuredSelection iss = (IStructuredSelection) selection;
263 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
266 final T resource = (T) inc.getAdapter(clazz);
271 //Create project after finish is pressed.
272 public boolean createProjects() {
274 String selected = previouslyBrowsedFile;
275 if(selected == null){
276 setErrorMessage("Error when retrieving resource");
280 selectedModel= getExplorerResource(modelExplorer, Resource.class);
281 if(selectedModel == null){
282 setErrorMessage("No file selected");
286 IStatus status = ImportUtilsUI.importModuleFile(selectedModel, selected, null);
289 TransferableGraph1 tg = null;
291 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
292 } catch (RuntimeBindingConstructionException e) {
294 } catch (IOException e) {
295 setErrorMessage("The imported file is not of type: Module Type");
299 setErrorMessage("The imported file is not of type: Module Type");
304 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel);
306 DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);
307 } catch (MissingDependencyException e) {
309 } catch (Exception e) {
313 final Resource root = ia.getRoot();
316 SimanticsUI.getSession().syncRequest(new WriteRequest() {
319 public void perform(WriteGraph graph) throws DatabaseException {
320 if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {
321 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);
323 if(instanceOf != null)
324 type = NameUtils.getSafeName(graph, instanceOf);
326 Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);
327 if(inheritedFrom != null)
328 type = NameUtils.getSafeName(graph, inheritedFrom);
330 graph.deny(root, Layer0.getInstance(graph).PartOf);
335 } catch (DatabaseException e) {
339 if (!error.isEmpty()){
340 setErrorMessage("The imported file is not of type: Module Type (" + error +")");
345 if(status == null || !status.equals(Status.OK_STATUS)) {
346 setErrorMessage(status.getMessage());
353 void validatePage() {
355 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
356 setPageComplete(false);
360 setErrorMessage(null);
361 setPageComplete(true);