1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.annotation.ui.wizard;
15 import java.util.Collections;
16 import java.util.List;
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;
49 * @author Tuukka Lehtonen
51 public class AnnotationTypeImportPage extends WizardPage {
54 * If non-null, the wizard cannot continue. This message tells why.
58 ImportPlan importModel;
60 CCombo importLocation;
62 List<NamedResource> models = Collections.emptyList();
65 protected AnnotationTypeImportPage(ImportPlan model) {
66 super("Import Annotation Type", "Define Import Location", null);
67 this.importModel = model;
71 public void createControl(Composite parent) {
72 Composite container = new Composite(parent, SWT.NONE);
74 GridLayout layout = new GridLayout();
75 layout.horizontalSpacing = 20;
76 layout.verticalSpacing = 10;
77 layout.numColumns = 3;
78 container.setLayout(layout);
81 new Label(container, SWT.NONE).setText("Import target:");
82 importTarget = new Text(container, SWT.BORDER);
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);
90 new Label(container, SWT.NONE).setText("&Annotation type file:");
91 importLocation = new CCombo(container, SWT.BORDER);
93 importLocation.setText("");
94 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
95 importLocation.addModifyListener(new ModifyListener(){
97 public void modifyText(ModifyEvent e) {
102 Button browseFileButton = new Button(container, SWT.PUSH);
104 browseFileButton.setText("Br&owse...");
105 browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
106 browseFileButton.addSelectionListener(new SelectionAdapter() {
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();
118 importLocation.setText(file);
126 } catch (DatabaseException e) {
127 ErrorLogger.defaultLogError(e);
130 setControl(container);
134 private void initializeData() throws DatabaseException {
135 NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {
137 public NamedResource perform(ReadGraph graph) throws DatabaseException {
138 Layer0 L0 = Layer0.getInstance(graph);
139 ModelingResources MOD = ModelingResources.getInstance(graph);
141 for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {
142 return toNamedResource(graph, r);
144 for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {
145 return toNamedResource(graph, r);
150 private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {
151 Resource model = graph.sync(new PossibleModel(lib));
153 return new NamedResource(NameUtils.getSafeName(graph, lib), lib);
154 return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);
158 if (target == null) {
159 failure = "No models found in the database.";
163 importTarget.setText(target.getName());
164 importModel.selection = target.getResource();
166 for (String path : importModel.recentLocations) {
167 importLocation.add(path);
169 if (importLocation.getItemCount() > 0)
170 importLocation.select(0);
173 void validatePage() {
174 if (failure != null) {
175 setErrorMessage(failure);
176 setPageComplete(false);
179 String importLoc = importLocation.getText();
180 if (importLoc.isEmpty()) {
181 setMessage("Select file to import.");
182 setErrorMessage(null);
183 setPageComplete(false);
186 File file = new File(importLoc);
187 if (!file.exists() || !file.isFile()) {
188 setErrorMessage("Selected file is invalid.");
189 setPageComplete(false);
192 importModel.importLocation = file;
194 setErrorMessage(null);
195 setMessage("Import " + file.getName() + "");
196 setPageComplete(true);