]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateImportPage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / wizard / DrawingTemplateImportPage.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.modeling.template2d.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.modeling.template2d.ontology.Template2dResource;
46 import org.simantics.utils.ui.ErrorLogger;
47 import org.simantics.utils.ui.ISelectionUtils;
48
49 /**
50  * @author Tuukka Lehtonen
51  */
52 public class DrawingTemplateImportPage extends WizardPage {
53
54     /**
55      * If non-null, the wizard cannot continue. This message tells why.
56      */
57     String              failure;
58
59     ImportPlan          importModel;
60     Text                importTarget;
61     CCombo              importLocation;
62
63     List<NamedResource> models = Collections.emptyList();
64     Button              overwrite;
65
66     protected DrawingTemplateImportPage(ImportPlan model) {
67         super("Import Diagram Template", "Define Import Location", null);
68         this.importModel = model;
69     }
70
71     @Override
72     public void createControl(Composite parent) {
73         Composite container = new Composite(parent, SWT.NONE);
74         {
75             GridLayout layout = new GridLayout();
76             layout.horizontalSpacing = 20;
77             layout.verticalSpacing = 10;
78             layout.numColumns = 3;
79             container.setLayout(layout);
80         }
81
82         new Label(container, SWT.NONE).setText("Import target:");
83         importTarget = new Text(container, SWT.BORDER);
84         {
85             importTarget.setEditable(false);
86             importTarget.setText("");
87             importTarget.setToolTipText("Shows the target of the import.");
88             GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(importTarget);
89         }
90
91         new Label(container, SWT.NONE).setText("&Diagram template file:");
92         importLocation = new CCombo(container, SWT.BORDER);
93         {
94             importLocation.setText("");
95             GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(importLocation);
96             importLocation.addModifyListener(new ModifyListener(){
97                 @Override
98                 public void modifyText(ModifyEvent e) {
99                     validatePage();
100                 }
101             });
102         }
103         Button browseFileButton = new Button(container, SWT.PUSH);
104         {
105             browseFileButton.setText("Br&owse...");
106             browseFileButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
107             browseFileButton.addSelectionListener(new SelectionAdapter() {
108                 @Override
109                 public void widgetSelected(SelectionEvent e) {
110                     FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
111                     dialog.setText("Choose Diagram Template to Import");
112                     String loc = importLocation.getText();
113                     dialog.setFilterPath(loc);
114                     dialog.setFilterExtensions(new String[] { "*.diagramTemplate;*.drawingTemplate" });
115                     dialog.setFilterNames(new String[] { "Diagram Template (*.diagramTemplate, *.drawingTemplate)" });
116                     String file = dialog.open();
117                     if (file == null)
118                         return;
119                     importLocation.setText(file);
120                     validatePage();
121                 }
122             });
123         }
124
125         try {
126             initializeData();
127         } catch (DatabaseException e) {
128             ErrorLogger.defaultLogError(e);
129         }
130
131         setControl(container);
132         validatePage();
133     }
134
135     private void initializeData() throws DatabaseException {
136         NamedResource target = importModel.sessionContext.getSession().syncRequest(new Read<NamedResource>() {
137             @Override
138             public NamedResource perform(ReadGraph graph) throws DatabaseException {
139                 Layer0 L0 = Layer0.getInstance(graph);
140                 ModelingResources MOD = ModelingResources.getInstance(graph);
141
142                 for (Resource r : ISelectionUtils.getPossibleKeys(importModel.selection, SelectionHints.KEY_MAIN, Resource.class)) {
143                     Resource lib = findTemplateLibrary(graph, r);
144                     if (lib != null)
145                         return toNamedResource(graph, lib);
146                 }
147                 for (Resource r : graph.sync(new ObjectsWithType(Simantics.getProjectResource(), L0.ConsistsOf, MOD.StructuralModel))) {
148                     Resource lib = findTemplateLibrary(graph, r);
149                     if (lib != null)
150                         return toNamedResource(graph, lib);
151                 }
152                 return null;
153             }
154
155             private Resource findTemplateLibrary(ReadGraph graph, Resource r) throws DatabaseException {
156                 Layer0 L0 = Layer0.getInstance(graph);
157                 Template2dResource TMPL = Template2dResource.getInstance(graph);
158
159                 if (graph.isInstanceOf(r, TMPL.DrawingTemplateLibrary))
160                     return r;
161
162                 Resource model = graph.sync( new PossibleModel(r) );
163                 if (model != null) {
164                     for (Resource child : graph.sync(new ObjectsWithType(model, L0.ConsistsOf, TMPL.DrawingTemplateLibrary))) {
165                         return child;
166                     }
167                 }
168                 return null;
169             }
170
171             private NamedResource toNamedResource(ReadGraph graph, Resource lib) throws DatabaseException {
172                 Resource model = graph.sync(new PossibleModel(lib));
173                 if (model == null)
174                     return new NamedResource(NameUtils.getSafeName(graph, lib), lib);
175                 return new NamedResource(NameUtils.getSafeName(graph, model) + ": " + NameUtils.getSafeName(graph, lib), lib);
176             }
177         });
178
179         if (target == null) {
180             failure = "No models with diagram template libraries found in the database.";
181             return;
182         }
183
184         importTarget.setText(target.getName());
185         importModel.selection = target.getResource();
186
187         for (String path : importModel.recentLocations) {
188             importLocation.add(path);
189         }
190         if (importLocation.getItemCount() > 0)
191             importLocation.select(0);
192     }
193
194     void validatePage() {
195         if (failure != null) {
196             setErrorMessage(failure);
197             setPageComplete(false);
198             return;
199         }
200         String importLoc = importLocation.getText();
201         if (importLoc.isEmpty()) {
202             setMessage("Select file to import.");
203             setErrorMessage(null);
204             setPageComplete(false);
205             return;
206         }
207         File file = new File(importLoc);
208         if (!file.exists() || !file.isFile()) {
209             setErrorMessage("Selected file is invalid.");
210             setPageComplete(false);
211             return;
212         }
213         importModel.importLocation = file;
214
215         setErrorMessage(null);
216         setMessage("Import " + file.getName() + "");
217         setPageComplete(true);
218     }
219
220 }