1 package org.simantics.sysdyn.ui.wizards.modules;
\r
4 import java.io.IOException;
\r
5 import java.util.ArrayList;
\r
6 import java.util.Collection;
\r
8 import org.eclipse.core.runtime.Path;
\r
9 import org.eclipse.jface.layout.GridDataFactory;
\r
10 import org.eclipse.jface.layout.PixelConverter;
\r
11 import org.eclipse.jface.viewers.ISelection;
\r
12 import org.eclipse.jface.viewers.ISelectionProvider;
\r
13 import org.eclipse.jface.viewers.IStructuredSelection;
\r
14 import org.eclipse.jface.wizard.WizardPage;
\r
15 import org.eclipse.swt.SWT;
\r
16 import org.eclipse.swt.events.ModifyEvent;
\r
17 import org.eclipse.swt.events.ModifyListener;
\r
18 import org.eclipse.swt.events.SelectionAdapter;
\r
19 import org.eclipse.swt.events.SelectionEvent;
\r
20 import org.eclipse.swt.events.SelectionListener;
\r
21 import org.eclipse.swt.layout.GridData;
\r
22 import org.eclipse.swt.layout.GridLayout;
\r
23 import org.eclipse.swt.widgets.Button;
\r
24 import org.eclipse.swt.widgets.Composite;
\r
25 import org.eclipse.swt.widgets.FileDialog;
\r
26 import org.eclipse.swt.widgets.Label;
\r
27 import org.eclipse.swt.widgets.Shell;
\r
28 import org.eclipse.swt.widgets.Text;
\r
29 import org.eclipse.swt.widgets.Tree;
\r
30 import org.simantics.browsing.ui.swt.AdaptableHintContext;
\r
31 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
\r
32 import org.simantics.databoard.Bindings;
\r
33 import org.simantics.databoard.Files;
\r
34 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
\r
35 import org.simantics.db.ReadGraph;
\r
36 import org.simantics.db.Resource;
\r
37 import org.simantics.db.WriteGraph;
\r
38 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
\r
39 import org.simantics.db.common.request.ObjectsWithType;
\r
40 import org.simantics.db.common.request.ReadRequest;
\r
41 import org.simantics.db.common.request.WriteRequest;
\r
42 import org.simantics.db.common.utils.NameUtils;
\r
43 import org.simantics.db.exception.DatabaseException;
\r
44 import org.simantics.db.layer0.util.TransferableGraphRequest2;
\r
45 import org.simantics.db.request.Read;
\r
46 import org.simantics.graph.representation.TransferableGraph1;
\r
47 import org.simantics.layer0.Layer0;
\r
48 import org.simantics.modeling.ModelingResources;
\r
49 import org.simantics.structural.stubs.StructuralResource2;
\r
50 import org.simantics.sysdyn.SysdynResource;
\r
51 import org.simantics.ui.SimanticsUI;
\r
52 import org.simantics.utils.datastructures.ArrayMap;
\r
53 import org.simantics.utils.datastructures.Pair;
\r
55 public class WizardModulesExportPage extends WizardPage {
\r
57 // dialog store id constants
\r
58 private Text filePathField;
\r
60 // Keep track of the archive that we browsed to last time
\r
61 // the wizard was invoked.
\r
62 private static String previouslyBrowsedFile = "";
\r
64 private Button browseDirectoriesButton;
\r
66 private Resource selectedModule;
\r
68 GraphExplorerComposite modelExplorer;
\r
70 private boolean selectionMade = false;
\r
73 * Creates a new project creation wizard page.
\r
76 public WizardModulesExportPage() {
\r
77 this("wizardModulesExportPage", null, null); //$NON-NLS-1$
\r
81 * Create a new instance of the receiver.
\r
85 public WizardModulesExportPage(String pageName) {
\r
86 this(pageName,null, null);
\r
90 * More (many more) parameters.
\r
93 * @param initialPath
\r
94 * @param currentSelection
\r
97 public WizardModulesExportPage(String pageName,String initialPath,
\r
98 IStructuredSelection currentSelection) {
\r
100 //this.currentSelection = currentSelection;
\r
101 setPageComplete(false);
\r
102 setTitle("Export Module");
\r
103 setDescription("Choose the Module and the export location, then press Finish.");
\r
106 public void createControl(Composite parent) {
\r
108 initializeDialogUnits(parent);
\r
110 Composite workArea = new Composite(parent, SWT.NONE);
\r
111 setControl(workArea);
\r
113 workArea.setLayout(new GridLayout());
\r
114 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
115 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
117 createProjectsRoot(workArea);
\r
118 createTree(workArea);
\r
121 private void createProjectsRoot(Composite workArea) {
\r
123 // set label for field
\r
124 Label title = new Label(workArea, SWT.NONE);
\r
125 title.setText("Select the export location for Module:");
\r
127 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
128 GridLayout layout = new GridLayout();
\r
129 layout.numColumns = 2;
\r
130 layout.makeColumnsEqualWidth = false;
\r
131 layout.marginWidth = 0;
\r
133 projectGroup.setLayout(layout);
\r
134 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
136 // module location entry field
\r
137 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
139 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
140 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
141 filePathField.setLayoutData(directoryPathData);
\r
143 filePathField.addModifyListener(new ModifyListener(){
\r
145 public void modifyText(ModifyEvent e) {
\r
146 previouslyBrowsedFile = filePathField.getText();
\r
149 if (previouslyBrowsedFile != null){
\r
150 filePathField.setText(previouslyBrowsedFile);
\r
155 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
156 browseDirectoriesButton.setText("Browse");
\r
157 setButtonLayoutData(browseDirectoriesButton);
\r
159 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
163 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
164 * elected(org.eclipse.swt.events.SelectionEvent)
\r
166 public void widgetSelected(SelectionEvent e) {
\r
167 handleLocationDirectoryButtonPressed();
\r
173 private void createTree(Composite workArea){
\r
175 //set label for tree
\r
176 Label title = new Label(workArea, SWT.NONE);
\r
177 title.setText("Select Module to export:");
\r
180 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
183 public Resource perform(ReadGraph graph)
\r
184 throws DatabaseException {
\r
185 Resource model = SimanticsUI.getProject().get();
\r
191 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
192 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
195 .setBrowseContexts(SysdynResource.URIs.ExportModuleTree);
\r
197 modelExplorer.finish();
\r
199 modelExplorer.setInput(null, input);
\r
201 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
204 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
207 public void widgetSelected(SelectionEvent e) {
\r
209 selectionMade = true;
\r
213 public void widgetDefaultSelected(SelectionEvent e) {
\r
215 selectionMade = true;
\r
220 } catch (DatabaseException e) {
\r
221 e.printStackTrace();
\r
226 //Set filePathField active
\r
227 public void setVisible(boolean visible) {
\r
228 super.setVisible(visible);
\r
229 this.filePathField.setFocus();
\r
232 //Open dialog for choosing the file
\r
233 protected void handleLocationDirectoryButtonPressed() {
\r
235 final Shell shell = filePathField.getShell();
\r
237 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
\r
239 String[] ext = {"*.tg"};
\r
240 dialog.setFilterExtensions(ext);
\r
242 dialog.setText("Export Module");
\r
244 String dirName = filePathField.getText().trim();
\r
246 File path = new File(dirName);
\r
247 if (path.exists()) {
\r
248 dialog.setFilterPath(new Path(dirName).toOSString());
\r
251 String selectedFile = dialog.open();
\r
252 if (selectedFile != null) {
\r
253 filePathField.setText(selectedFile);
\r
259 //Get selection from the tree
\r
260 @SuppressWarnings("unchecked")
\r
261 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
\r
264 if(explorer == null)
\r
266 ISelection selection = ((ISelectionProvider) explorer
\r
267 .getAdapter(ISelectionProvider.class)).getSelection();
\r
268 if (selection == null)
\r
270 IStructuredSelection iss = (IStructuredSelection) selection;
\r
271 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
\r
274 final T resource = (T) inc.getAdapter(clazz);
\r
279 //Create export file when finish is pressed
\r
280 public boolean createProjects() {
\r
282 final String selected = previouslyBrowsedFile;
\r
283 if(selected == null) return false;
\r
285 selectedModule= getExplorerResource(modelExplorer, Resource.class);
\r
286 if(selectedModule == null)
\r
289 String name = null;
\r
291 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
294 public String perform(ReadGraph graph) throws DatabaseException {
\r
295 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
296 SysdynResource sr = SysdynResource.getInstance(graph);
\r
297 Layer0 l0 = Layer0.getInstance(graph);
\r
299 Resource component = selectedModule;
\r
300 if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))
\r
303 Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);
\r
304 if (configuration == null)
\r
307 ArrayList<String> dependencies = null;
\r
308 for(Resource r : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {
\r
309 if(dependencies == null)
\r
310 dependencies = new ArrayList<String>();
\r
311 String name = NameUtils.getSafeName(graph, r);
\r
312 String instanceOf = NameUtils.getSafeName(graph, graph.getSingleObject(r, l0.InstanceOf));
\r
313 dependencies.add(name + " : " + instanceOf);
\r
315 if(dependencies != null && !dependencies.isEmpty())
\r
316 throw new ContainsDependenciesException(dependencies);
\r
318 String name = graph.getPossibleRelatedValue(component, l0.HasName, Bindings.STRING);
\r
325 catch (DatabaseException e1) {
\r
326 e1.printStackTrace();
\r
328 if(name == null) return false;
\r
330 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
333 public void run(ReadGraph graph) throws DatabaseException {
\r
334 Layer0 l0 = Layer0.getInstance(graph);
\r
336 final Resource component = selectedModule;
\r
337 //final Resource component = graph.getPossibleObject(modulesymbol, mr.SymbolToComponentType);
\r
338 if (component == null || !graph.hasStatement(component, Layer0.getInstance(graph).PartOf))
\r
340 String name = graph.syncRequest(new PossibleRelatedValue<String>(component, l0.HasName, Bindings.STRING ));
\r
341 final ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
342 roots.add(Pair.make(component, name));
\r
344 graph.asyncRequest(new WriteRequest() {
\r
347 public void perform(WriteGraph graph) throws DatabaseException {
\r
348 Layer0 l0 = Layer0.getInstance(graph);
\r
349 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
350 ModelingResources mr = ModelingResources.getInstance(graph);
\r
351 final Resource modulesymbol = graph.getPossibleObject(component, mr.ComponentTypeToSymbol);
\r
352 Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);
\r
353 if (!graph.hasStatement(configuration, l0.PartOf, component)&&
\r
354 !graph.hasStatement(modulesymbol, l0.PartOf, component)) {
\r
355 // Make sure that configuration and symbol are included.
\r
356 // In old versions, they were attached to model, not to module.
\r
357 Resource previousPartof = graph.getSingleObject(configuration, l0.PartOf);
\r
359 graph.deny(configuration, l0.PartOf);
\r
360 graph.deny(modulesymbol, l0.PartOf);
\r
361 graph.claim(configuration, l0.PartOf, l0.ConsistsOf, component);
\r
362 graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, component);
\r
364 export(graph, selected, roots, component);
\r
366 graph.deny(configuration, l0.PartOf);
\r
367 graph.deny(modulesymbol, l0.PartOf);
\r
368 graph.claim(configuration, l0.PartOf, l0.ConsistsOf, previousPartof);
\r
369 graph.claim(modulesymbol, l0.PartOf, l0.ConsistsOf, previousPartof);
\r
372 export(graph, selected, roots, component);
\r
383 private void export(WriteGraph graph, String path, ArrayList<Pair<Resource, String>> roots, Resource component) {
\r
385 // FIXME: Enumeration replacement handling like this is not suitable.
\r
387 Layer0 l0 = Layer0.getInstance(graph);
\r
388 SysdynResource sr = SysdynResource.getInstance(graph);
\r
389 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
391 Resource configuration = graph.getPossibleObject(component, sr2.IsDefinedBy);
\r
392 ArrayList<Pair<Resource, Resource>> replacements = new ArrayList<Pair<Resource, Resource>>();
\r
394 for(Resource enumeration : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Enumeration))) {
\r
395 if(graph.hasStatement(enumeration, sr.Redeclaration_replacedEnumeration_Inverse)) {
\r
396 for(Resource replacement : graph.getObjects(enumeration, sr.Redeclaration_replacedEnumeration_Inverse)) {
\r
397 replacements.add(new Pair<Resource, Resource>(enumeration, replacement));
\r
402 for(Pair<Resource,Resource> replacement : replacements)
\r
403 graph.deny(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);
\r
405 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, component));
\r
406 Files.createFile(new File(path), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
408 for(Pair<Resource,Resource> replacement : replacements)
\r
409 graph.claim(replacement.first, sr.Redeclaration_replacedEnumeration_Inverse, replacement.second);
\r
411 } catch (RuntimeBindingConstructionException e) {
\r
412 e.printStackTrace();
\r
413 } catch (IOException e) {
\r
414 e.printStackTrace();
\r
415 } catch (DatabaseException e) {
\r
416 e.printStackTrace();
\r
420 class ContainsDependenciesException extends DatabaseException {
\r
421 private static final long serialVersionUID = -1533706136673146020L;
\r
423 private Collection<String> dependencies;
\r
425 ContainsDependenciesException(Collection<String> dependencies) {
\r
426 this.dependencies = dependencies;
\r
429 public Collection<String> getDependencies() {
\r
430 return this.dependencies;
\r
435 void validatePage() {
\r
437 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
438 setPageComplete(false);
\r
442 if (modelExplorer != null){
\r
443 final Resource selectedResource = getExplorerResource(modelExplorer, Resource.class);
\r
445 String root = null;
\r
447 root = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
450 public String perform(ReadGraph graph) throws DatabaseException {
\r
451 Layer0 l0 = Layer0.getInstance(graph);
\r
452 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);
\r
453 String rootName = NameUtils.getSafeName(graph, model);
\r
459 if (root != null && root.equalsIgnoreCase("Development Project")){
\r
460 setPageComplete(false);
\r
461 setMessage("Select Module under the Model.");
\r
464 } catch (DatabaseException e) {
\r
465 e.printStackTrace();
\r
469 setPageComplete(true);
\r