1 package org.simantics.sysdyn.ui.wizards.models;
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.Path;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.jface.layout.PixelConverter;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Tree;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.common.request.ReadRequest;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
36 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
37 import org.simantics.graph.db.TGStatusMonitor;
38 import org.simantics.graph.db.TransferableGraphSource;
39 import org.simantics.graph.db.TransferableGraphs;
40 import org.simantics.sysdyn.SysdynResource;
41 import org.simantics.sysdyn.ui.Activator;
42 import org.simantics.ui.SimanticsUI;
43 import org.simantics.utils.datastructures.ArrayMap;
45 public class WizardModelsExportPage extends WizardPage {
47 // dialog store id constants
48 private Text filePathField;
50 // Keep track of the archive that we browsed to last time
51 // the wizard was invoked.
53 private static String previouslyBrowsedFile = "";
55 private Button browseDirectoriesButton;
57 //private IStructuredSelection currentSelection;
59 private Resource selectedModel;
61 GraphExplorerComposite modelExplorer;
63 private boolean selectionMade = false;
68 * Creates a new project creation wizard page.
71 public WizardModelsExportPage() {
72 this("wizardModelsExportPage", null, null); //$NON-NLS-1$
76 * Create a new instance of the receiver.
80 public WizardModelsExportPage(String pageName) {
81 this(pageName,null, null);
85 * More (many more) parameters.
89 * @param currentSelection
92 public WizardModelsExportPage(String pageName,String initialPath,
93 IStructuredSelection currentSelection) {
95 //this.currentSelection = currentSelection;
96 setPageComplete(false);
97 setTitle("Export Model");
98 setDescription("Choose the Model and the export location, then press Finish.");
101 public void createControl(Composite parent) {
103 initializeDialogUnits(parent);
105 Composite workArea = new Composite(parent, SWT.NONE);
106 setControl(workArea);
108 workArea.setLayout(new GridLayout());
109 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
110 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
112 createProjectsRoot(workArea);
113 createTree (workArea);
116 private void createProjectsRoot(Composite workArea) {
118 // set label for field
119 Label title = new Label(workArea, SWT.NONE);
120 title.setText("Select the export location for Model:");
122 Composite projectGroup = new Composite(workArea, SWT.NONE);
123 GridLayout layout = new GridLayout();
124 layout.numColumns = 2;
125 layout.makeColumnsEqualWidth = false;
126 layout.marginWidth = 0;
128 projectGroup.setLayout(layout);
129 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131 // model location entry field
132 this.filePathField = new Text(projectGroup, SWT.BORDER);
134 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
135 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
136 filePathField.setLayoutData(directoryPathData);
138 filePathField.addModifyListener(new ModifyListener(){
140 public void modifyText(ModifyEvent e) {
141 previouslyBrowsedFile = filePathField.getText();
144 if (previouslyBrowsedFile != null){
145 filePathField.setText(previouslyBrowsedFile);
150 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
151 browseDirectoriesButton.setText("Browse");
152 setButtonLayoutData(browseDirectoriesButton);
154 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
158 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
159 * elected(org.eclipse.swt.events.SelectionEvent)
161 public void widgetSelected(SelectionEvent e) {
162 handleLocationDirectoryButtonPressed();
168 private void createTree(Composite workArea){
171 Label title = new Label(workArea, SWT.NONE);
172 title.setText("Select Model to export:");
174 Resource input = SimanticsUI.getProject().get();
176 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
177 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
180 .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
182 modelExplorer.finish();
184 modelExplorer.setInput(null, input);
186 GridDataFactory.fillDefaults().grab(true, true).applyTo(
189 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
192 public void widgetSelected(SelectionEvent e) {
193 selectionMade = true;
197 public void widgetDefaultSelected(SelectionEvent e) {
198 selectionMade = true;
204 //Set filePathField active
205 public void setVisible(boolean visible) {
206 super.setVisible(visible);
207 this.filePathField.setFocus();
210 //Open dialog for choosing the file
211 protected void handleLocationDirectoryButtonPressed() {
212 final Shell shell = filePathField.getShell();
214 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
216 String[] ext = {"*.sysdyn"};
217 dialog.setFilterExtensions(ext);
219 dialog.setText("Export Model");
221 String dirName = filePathField.getText().trim();
223 File path = new File(dirName);
225 dialog.setFilterPath(new Path(dirName).toOSString());
228 String selectedFile = dialog.open();
229 if (selectedFile != null) {
230 filePathField.setText(selectedFile);
235 //Get selection from the tree
236 @SuppressWarnings("unchecked")
237 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
242 ISelection selection = ((ISelectionProvider) explorer
243 .getAdapter(ISelectionProvider.class)).getSelection();
244 if (selection == null)
246 IStructuredSelection iss = (IStructuredSelection) selection;
247 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
250 final T resource = (T) inc.getAdapter(clazz);
255 public boolean createProjects() {
257 final String selected = previouslyBrowsedFile;
258 if(selected == null) return false;
260 selectedModel = getExplorerResource(modelExplorer, Resource.class);
261 if(selectedModel == null)
264 // FIXME: Model browser doesn't change its selection even if the selected object is removed,
265 // so you can try to export a removed model
267 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
270 public void run(ReadGraph graph) throws DatabaseException {
271 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, selectedModel);
272 try (TransferableGraphSource tgs = graph.syncRequest( new ModelTransferableGraphSourceRequest(conf) )) {
273 TransferableGraphs.writeTransferableGraph(graph, tgs, new File(selected), TGStatusMonitor.NULL_MONITOR);
274 } catch (Exception e) {
275 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
276 "Failed to export model, see exception for details.", e));
284 void validatePage() {
286 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
287 setPageComplete(false);
291 setPageComplete(true);