]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeImportPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / wizard / AnnotationTypeImportPage.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * 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.annotation.ui.wizard;
13
14 import java.io.File;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.jface.layout.GridDataFactory;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.CCombo;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.FileDialog;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33 import org.simantics.Simantics;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.common.NamedResource;
37 import org.simantics.db.common.request.ObjectsWithType;
38 import org.simantics.db.common.utils.NameUtils;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.SelectionHints;
41 import org.simantics.db.layer0.request.PossibleModel;
42 import org.simantics.db.request.Read;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.modeling.ModelingResources;
45 import org.simantics.utils.ui.ErrorLogger;
46 import org.simantics.utils.ui.ISelectionUtils;
47
48 /**
49  * @author Tuukka Lehtonen
50  */
51 public class AnnotationTypeImportPage extends WizardPage {
52
53     /**
54      * If non-null, the wizard cannot continue. This message tells why.
55      */
56     String              failure;
57
58     ImportPlan          importModel;
59     Text                importTarget;
60     CCombo              importLocation;
61
62     List<NamedResource> models = Collections.emptyList();
63     Button              overwrite;
64
65     protected AnnotationTypeImportPage(ImportPlan model) {
66         super("Import Annotation Type", "Define Import Location", null);
67         this.importModel = model;
68     }
69
70     @Override
71     public void createControl(Composite parent) {
72         Composite container = new Composite(parent, SWT.NONE);
73         {
74             GridLayout layout = new GridLayout();
75             layout.horizontalSpacing = 20;
76             layout.verticalSpacing = 10;
77             layout.numColumns = 3;
78             container.setLayout(layout);
79         }
80
81         new Label(container, SWT.NONE).setText("Import target:");
82         importTarget = new Text(container, SWT.BORDER);
83         {
84             importTarget.setEditable(false);
85             importTarget.setText("");
86             importTarget.setToolTipText("Shows the target of the import.");
87             GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(importTarget);
88         }
89
90         new Label(container, SWT.NONE).setText("&Annotation type file:");
91         importLocation = new CCombo(container, SWT.BORDER);
92         {
93             importLocation.setText("");
94             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
95             importLocation.addModifyListener(new ModifyListener(){
96                 @Override
97                 public void modifyText(ModifyEvent e) {
98                     validatePage();
99                 }
100             });
101         }
102         Button browseFileButton = new Button(container, SWT.PUSH);
103         {
104             browseFileButton.setText("Br&owse...");
105             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
106             browseFileButton.addSelectionListener(new SelectionAdapter() {
107                 @Override
108                 public void widgetSelected(SelectionEvent e) {
109                     FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
110                     dialog.setText("Choose Annotation Type to Import");
111                     String loc = importLocation.getText();
112                     dialog.setFilterPath(loc);
113                     dialog.setFilterExtensions(new String[] { "*.annotationType" });
114                     dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });
115                     String file = dialog.open();
116                     if (file == null)
117                         return;
118                     importLocation.setText(file);
119                     validatePage();
120                 }
121             });
122         }
123
124         try {
125             initializeData();
126         } catch (DatabaseException e) {
127             ErrorLogger.defaultLogError(e);
128         }
129
130         setControl(container);
131         validatePage();
132     }
133
134     private void initializeData() throws DatabaseException {
135         NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {
136             @Override
137             public NamedResource perform(ReadGraph graph) throws DatabaseException {
138                 Layer0 L0 = Layer0.getInstance(graph);
139                 ModelingResources MOD = ModelingResources.getInstance(graph);
140
141                 for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {
142                         return toNamedResource(graph, r);
143                 }
144                 for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {
145                         return toNamedResource(graph, r);
146                 }
147                 return null;
148             }
149
150             private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {
151                 Resource model = graph.sync(new PossibleModel(lib));
152                 if (model == null)
153                     return new NamedResource(NameUtils.getSafeName(graph, lib), lib);
154                 return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);
155             }
156         });
157
158         if (target == null) {
159             failure = "No models found in the database.";
160             return;
161         }
162
163         importTarget.setText(target.getName());
164         importModel.selection = target.getResource();
165
166         for (String path : importModel.recentLocations) {
167             importLocation.add(path);
168         }
169         if (importLocation.getItemCount() > 0)
170             importLocation.select(0);
171     }
172
173     void validatePage() {
174         if (failure != null) {
175             setErrorMessage(failure);
176             setPageComplete(false);
177             return;
178         }
179         String importLoc = importLocation.getText();
180         if (importLoc.isEmpty()) {
181             setMessage("Select file to import.");
182             setErrorMessage(null);
183             setPageComplete(false);
184             return;
185         }
186         File file = new File(importLoc);
187         if (!file.exists() || !file.isFile()) {
188             setErrorMessage("Selected file is invalid.");
189             setPageComplete(false);
190             return;
191         }
192         importModel.importLocation = file;
193
194         setErrorMessage(null);
195         setMessage("Import " + file.getName() + "");
196         setPageComplete(true);
197     }
198
199 }