-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.annotation.ui.wizard;\r
-\r
-import java.io.File;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.wizard.WizardPage;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.custom.CCombo;\r
-import org.eclipse.swt.events.ModifyEvent;\r
-import org.eclipse.swt.events.ModifyListener;\r
-import org.eclipse.swt.events.SelectionAdapter;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.layout.GridData;\r
-import org.eclipse.swt.layout.GridLayout;\r
-import org.eclipse.swt.widgets.Button;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.FileDialog;\r
-import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.NamedResource;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.SelectionHints;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class AnnotationTypeImportPage extends WizardPage {\r
-\r
- /**\r
- * If non-null, the wizard cannot continue. This message tells why.\r
- */\r
- String failure;\r
-\r
- ImportPlan importModel;\r
- Text importTarget;\r
- CCombo importLocation;\r
-\r
- List<NamedResource> models = Collections.emptyList();\r
- Button overwrite;\r
-\r
- protected AnnotationTypeImportPage(ImportPlan model) {\r
- super("Import Annotation Type", "Define Import Location", null);\r
- this.importModel = model;\r
- }\r
-\r
- @Override\r
- public void createControl(Composite parent) {\r
- Composite container = new Composite(parent, SWT.NONE);\r
- {\r
- GridLayout layout = new GridLayout();\r
- layout.horizontalSpacing = 20;\r
- layout.verticalSpacing = 10;\r
- layout.numColumns = 3;\r
- container.setLayout(layout);\r
- }\r
-\r
- new Label(container, SWT.NONE).setText("Import target:");\r
- importTarget = new Text(container, SWT.BORDER);\r
- {\r
- importTarget.setEditable(false);\r
- importTarget.setText("");\r
- importTarget.setToolTipText("Shows the target of the import.");\r
- GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(importTarget);\r
- }\r
-\r
- new Label(container, SWT.NONE).setText("&Annotation type file:");\r
- importLocation = new CCombo(container, SWT.BORDER);\r
- {\r
- importLocation.setText("");\r
- GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);\r
- importLocation.addModifyListener(new ModifyListener(){\r
- @Override\r
- public void modifyText(ModifyEvent e) {\r
- validatePage();\r
- }\r
- });\r
- }\r
- Button browseFileButton = new Button(container, SWT.PUSH);\r
- {\r
- browseFileButton.setText("Br&owse...");\r
- browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));\r
- browseFileButton.addSelectionListener(new SelectionAdapter() {\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);\r
- dialog.setText("Choose Annotation Type to Import");\r
- String loc = importLocation.getText();\r
- dialog.setFilterPath(loc);\r
- dialog.setFilterExtensions(new String[] { "*.annotationType" });\r
- dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });\r
- String file = dialog.open();\r
- if (file == null)\r
- return;\r
- importLocation.setText(file);\r
- validatePage();\r
- }\r
- });\r
- }\r
-\r
- try {\r
- initializeData();\r
- } catch (DatabaseException e) {\r
- ErrorLogger.defaultLogError(e);\r
- }\r
-\r
- setControl(container);\r
- validatePage();\r
- }\r
-\r
- private void initializeData() throws DatabaseException {\r
- NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {\r
- @Override\r
- public NamedResource perform(ReadGraph graph) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- ModelingResources MOD = ModelingResources.getInstance(graph);\r
-\r
- for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {\r
- return toNamedResource(graph, r);\r
- }\r
- for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {\r
- return toNamedResource(graph, r);\r
- }\r
- return null;\r
- }\r
-\r
- private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {\r
- Resource model = graph.sync(new PossibleModel(lib));\r
- if (model == null)\r
- return new NamedResource(NameUtils.getSafeName(graph, lib), lib);\r
- return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);\r
- }\r
- });\r
-\r
- if (target == null) {\r
- failure = "No models found in the database.";\r
- return;\r
- }\r
-\r
- importTarget.setText(target.getName());\r
- importModel.selection = target.getResource();\r
-\r
- for (String path : importModel.recentLocations) {\r
- importLocation.add(path);\r
- }\r
- if (importLocation.getItemCount() > 0)\r
- importLocation.select(0);\r
- }\r
-\r
- void validatePage() {\r
- if (failure != null) {\r
- setErrorMessage(failure);\r
- setPageComplete(false);\r
- return;\r
- }\r
- String importLoc = importLocation.getText();\r
- if (importLoc.isEmpty()) {\r
- setMessage("Select file to import.");\r
- setErrorMessage(null);\r
- setPageComplete(false);\r
- return;\r
- }\r
- File file = new File(importLoc);\r
- if (!file.exists() || !file.isFile()) {\r
- setErrorMessage("Selected file is invalid.");\r
- setPageComplete(false);\r
- return;\r
- }\r
- importModel.importLocation = file;\r
-\r
- setErrorMessage(null);\r
- setMessage("Import " + file.getName() + "");\r
- setPageComplete(true);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.annotation.ui.wizard;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CCombo;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.request.Read;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ISelectionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class AnnotationTypeImportPage extends WizardPage {
+
+ /**
+ * If non-null, the wizard cannot continue. This message tells why.
+ */
+ String failure;
+
+ ImportPlan importModel;
+ Text importTarget;
+ CCombo importLocation;
+
+ List<NamedResource> models = Collections.emptyList();
+ Button overwrite;
+
+ protected AnnotationTypeImportPage(ImportPlan model) {
+ super("Import Annotation Type", "Define Import Location", null);
+ this.importModel = model;
+ }
+
+ @Override
+ public void createControl(Composite parent) {
+ Composite container = new Composite(parent, SWT.NONE);
+ {
+ GridLayout layout = new GridLayout();
+ layout.horizontalSpacing = 20;
+ layout.verticalSpacing = 10;
+ layout.numColumns = 3;
+ container.setLayout(layout);
+ }
+
+ new Label(container, SWT.NONE).setText("Import target:");
+ importTarget = new Text(container, SWT.BORDER);
+ {
+ importTarget.setEditable(false);
+ importTarget.setText("");
+ importTarget.setToolTipText("Shows the target of the import.");
+ GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(importTarget);
+ }
+
+ new Label(container, SWT.NONE).setText("&Annotation type file:");
+ importLocation = new CCombo(container, SWT.BORDER);
+ {
+ importLocation.setText("");
+ GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
+ importLocation.addModifyListener(new ModifyListener(){
+ @Override
+ public void modifyText(ModifyEvent e) {
+ validatePage();
+ }
+ });
+ }
+ Button browseFileButton = new Button(container, SWT.PUSH);
+ {
+ browseFileButton.setText("Br&owse...");
+ browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+ browseFileButton.addSelectionListener(new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
+ dialog.setText("Choose Annotation Type to Import");
+ String loc = importLocation.getText();
+ dialog.setFilterPath(loc);
+ dialog.setFilterExtensions(new String[] { "*.annotationType" });
+ dialog.setFilterNames(new String[] { "Annotation Type (*.annotationType)" });
+ String file = dialog.open();
+ if (file == null)
+ return;
+ importLocation.setText(file);
+ validatePage();
+ }
+ });
+ }
+
+ try {
+ initializeData();
+ } catch (DatabaseException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+
+ setControl(container);
+ validatePage();
+ }
+
+ private void initializeData() throws DatabaseException {
+ NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {
+ @Override
+ public NamedResource perform(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+
+ for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {
+ return toNamedResource(graph, r);
+ }
+ for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {
+ return toNamedResource(graph, r);
+ }
+ return null;
+ }
+
+ private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {
+ Resource model = graph.sync(new PossibleModel(lib));
+ if (model == null)
+ return new NamedResource(NameUtils.getSafeName(graph, lib), lib);
+ return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);
+ }
+ });
+
+ if (target == null) {
+ failure = "No models found in the database.";
+ return;
+ }
+
+ importTarget.setText(target.getName());
+ importModel.selection = target.getResource();
+
+ for (String path : importModel.recentLocations) {
+ importLocation.add(path);
+ }
+ if (importLocation.getItemCount() > 0)
+ importLocation.select(0);
+ }
+
+ void validatePage() {
+ if (failure != null) {
+ setErrorMessage(failure);
+ setPageComplete(false);
+ return;
+ }
+ String importLoc = importLocation.getText();
+ if (importLoc.isEmpty()) {
+ setMessage("Select file to import.");
+ setErrorMessage(null);
+ setPageComplete(false);
+ return;
+ }
+ File file = new File(importLoc);
+ if (!file.exists() || !file.isFile()) {
+ setErrorMessage("Selected file is invalid.");
+ setPageComplete(false);
+ return;
+ }
+ importModel.importLocation = file;
+
+ setErrorMessage(null);
+ setMessage("Import " + file.getName() + "");
+ setPageComplete(true);
+ }
+
+}