1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.annotation.ui.wizard;
\r
14 import java.io.File;
\r
15 import java.util.Collections;
\r
16 import java.util.List;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.wizard.WizardPage;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.custom.CCombo;
\r
22 import org.eclipse.swt.events.ModifyEvent;
\r
23 import org.eclipse.swt.events.ModifyListener;
\r
24 import org.eclipse.swt.events.SelectionAdapter;
\r
25 import org.eclipse.swt.events.SelectionEvent;
\r
26 import org.eclipse.swt.layout.GridData;
\r
27 import org.eclipse.swt.layout.GridLayout;
\r
28 import org.eclipse.swt.widgets.Button;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.FileDialog;
\r
31 import org.eclipse.swt.widgets.Label;
\r
32 import org.eclipse.swt.widgets.Text;
\r
33 import org.simantics.Simantics;
\r
34 import org.simantics.db.ReadGraph;
\r
35 import org.simantics.db.Resource;
\r
36 import org.simantics.db.common.NamedResource;
\r
37 import org.simantics.db.common.request.ObjectsWithType;
\r
38 import org.simantics.db.common.utils.NameUtils;
\r
39 import org.simantics.db.exception.DatabaseException;
\r
40 import org.simantics.db.layer0.SelectionHints;
\r
41 import org.simantics.db.layer0.request.PossibleModel;
\r
42 import org.simantics.db.request.Read;
\r
43 import org.simantics.layer0.Layer0;
\r
44 import org.simantics.modeling.ModelingResources;
\r
45 import org.simantics.utils.ui.ErrorLogger;
\r
46 import org.simantics.utils.ui.ISelectionUtils;
\r
49 * @author Tuukka Lehtonen
\r
51 public class AnnotationTypeImportPage extends WizardPage {
\r
54 * If non-null, the wizard cannot continue. This message tells why.
\r
58 ImportPlan importModel;
\r
60 CCombo importLocation;
\r
62 List<NamedResource> models = Collections.emptyList();
\r
65 protected AnnotationTypeImportPage(ImportPlan model) {
\r
66 super("Import Annotation Type", "Define Import Location", null);
\r
67 this.importModel = model;
\r
71 public void createControl(Composite parent) {
\r
72 Composite container = new Composite(parent, SWT.NONE);
\r
74 GridLayout layout = new GridLayout();
\r
75 layout.horizontalSpacing = 20;
\r
76 layout.verticalSpacing = 10;
\r
77 layout.numColumns = 3;
\r
78 container.setLayout(layout);
\r
81 new Label(container, SWT.NONE).setText("Import target:");
\r
82 importTarget = new Text(container, SWT.BORDER);
\r
84 importTarget.setEditable(false);
\r
85 importTarget.setText("");
\r
86 importTarget.setToolTipText("Shows the target of the import.");
\r
87 GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(importTarget);
\r
90 new Label(container, SWT.NONE).setText("&Annotation type file:");
\r
91 importLocation = new CCombo(container, SWT.BORDER);
\r
93 importLocation.setText("");
\r
94 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
\r
95 importLocation.addModifyListener(new ModifyListener(){
\r
97 public void modifyText(ModifyEvent e) {
\r
102 Button browseFileButton = new Button(container, SWT.PUSH);
\r
104 browseFileButton.setText("Br&owse...");
\r
105 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
\r
106 browseFileButton.addSelectionListener(new SelectionAdapter() {
\r
108 public void widgetSelected(SelectionEvent e) {
\r
109 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
\r
110 dialog.setText("Choose Annotation Type to Import");
\r
111 String loc = importLocation.getText();
\r
112 dialog.setFilterPath(loc);
\r
113 dialog.setFilterExtensions(new String[] { "*.annotationType" });
\r
114 dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });
\r
115 String file = dialog.open();
\r
118 importLocation.setText(file);
\r
126 } catch (DatabaseException e) {
\r
127 ErrorLogger.defaultLogError(e);
\r
130 setControl(container);
\r
134 private void initializeData() throws DatabaseException {
\r
135 NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {
\r
137 public NamedResource perform(ReadGraph graph) throws DatabaseException {
\r
138 Layer0 L0 = Layer0.getInstance(graph);
\r
139 ModelingResources MOD = ModelingResources.getInstance(graph);
\r
141 for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {
\r
142 return toNamedResource(graph, r);
\r
144 for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {
\r
145 return toNamedResource(graph, r);
\r
150 private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {
\r
151 Resource model = graph.sync(new PossibleModel(lib));
\r
153 return new NamedResource(NameUtils.getSafeName(graph, lib), lib);
\r
154 return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);
\r
158 if (target == null) {
\r
159 failure = "No models found in the database.";
\r
163 importTarget.setText(target.getName());
\r
164 importModel.selection = target.getResource();
\r
166 for (String path : importModel.recentLocations) {
\r
167 importLocation.add(path);
\r
169 if (importLocation.getItemCount() > 0)
\r
170 importLocation.select(0);
\r
173 void validatePage() {
\r
174 if (failure != null) {
\r
175 setErrorMessage(failure);
\r
176 setPageComplete(false);
\r
179 String importLoc = importLocation.getText();
\r
180 if (importLoc.isEmpty()) {
\r
181 setMessage("Select file to import.");
\r
182 setErrorMessage(null);
\r
183 setPageComplete(false);
\r
186 File file = new File(importLoc);
\r
187 if (!file.exists() || !file.isFile()) {
\r
188 setErrorMessage("Selected file is invalid.");
\r
189 setPageComplete(false);
\r
192 importModel.importLocation = file;
\r
194 setErrorMessage(null);
\r
195 setMessage("Import " + file.getName() + "");
\r
196 setPageComplete(true);
\r