]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0dab6289af021827e24c7ef8a0a72c12b4137a0d
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.models;
2
3 import java.io.File;
4
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;
44
45 public class WizardModelsExportPage extends WizardPage {
46         
47         // dialog store id constants
48         private Text filePathField;
49         
50         // Keep track of the archive that we browsed to last time
51         // the wizard was invoked.
52
53         private static String previouslyBrowsedFile = "";
54
55         private Button browseDirectoriesButton;
56         
57         //private IStructuredSelection currentSelection;
58         
59         private Resource selectedModel;
60         
61         GraphExplorerComposite modelExplorer;
62         
63         private boolean selectionMade = false;
64         
65         
66         
67         /**
68          * Creates a new project creation wizard page.
69          * 
70          */
71         public WizardModelsExportPage() {
72                 this("wizardModelsExportPage", null, null); //$NON-NLS-1$
73         }
74
75         /**
76          * Create a new instance of the receiver.
77          * 
78          * @param pageName
79          */
80         public WizardModelsExportPage(String pageName) {
81                 this(pageName,null, null);
82         }
83                         
84         /**
85          * More (many more) parameters.
86          * 
87          * @param pageName
88          * @param initialPath
89          * @param currentSelection
90          * @since 3.5
91          */
92         public WizardModelsExportPage(String pageName,String initialPath,
93                         IStructuredSelection currentSelection) {
94                 super(pageName);
95                 //this.currentSelection = currentSelection;
96                 setPageComplete(false);
97                 setTitle("Export Model");
98                 setDescription("Choose the Model and the export location, then press Finish.");
99         }
100         
101         public void createControl(Composite parent) {
102                 
103                 initializeDialogUnits(parent);
104
105                 Composite workArea = new Composite(parent, SWT.NONE);
106                 setControl(workArea);
107
108                 workArea.setLayout(new GridLayout());
109                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
110                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
111                 
112                 createProjectsRoot(workArea);
113                 createTree (workArea);
114         }
115         
116         private void createProjectsRoot(Composite workArea) {
117                 
118                 // set label for field
119                 Label title = new Label(workArea, SWT.NONE);
120                 title.setText("Select the export location for Model:");
121                 
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;
127
128                 projectGroup.setLayout(layout);
129                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
130                 
131                 // model location entry field
132                 this.filePathField = new Text(projectGroup, SWT.BORDER);
133
134                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
135                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
136                 filePathField.setLayoutData(directoryPathData);
137                 
138                 filePathField.addModifyListener(new ModifyListener(){
139             @Override
140             public void modifyText(ModifyEvent e) {
141                 previouslyBrowsedFile = filePathField.getText();        
142             }
143                 });
144                 if (previouslyBrowsedFile != null){
145                         filePathField.setText(previouslyBrowsedFile);
146                         validatePage();
147                 }
148
149                 // browse button
150                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
151                 browseDirectoriesButton.setText("Browse");
152                 setButtonLayoutData(browseDirectoriesButton);
153                 
154                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
155                         /*
156                          * (non-Javadoc)
157                          * 
158                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS
159                          * elected(org.eclipse.swt.events.SelectionEvent)
160                          */
161                         public void widgetSelected(SelectionEvent e) {
162                                 handleLocationDirectoryButtonPressed();
163                         }
164                 });
165                 
166         }
167
168         private void createTree(Composite workArea){
169
170                 //set label for tree
171                 Label title = new Label(workArea, SWT.NONE);
172                 title.setText("Select Model to export:");
173
174                 Resource input = SimanticsUI.getProject().get();
175
176                 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
177                                 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
178
179                 modelExplorer
180                 .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
181
182                 modelExplorer.finish();
183
184                 modelExplorer.setInput(null, input);
185
186                 GridDataFactory.fillDefaults().grab(true, true).applyTo(
187                                 modelExplorer);
188
189                 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
190
191                         @Override
192                         public void widgetSelected(SelectionEvent e) {
193                                 selectionMade = true;
194                                 validatePage();
195                         }
196                         @Override
197                         public void widgetDefaultSelected(SelectionEvent e) {
198                                 selectionMade = true;
199                                 validatePage();
200                         }
201                 });
202         }
203
204         //Set filePathField active
205         public void setVisible(boolean visible) {
206                 super.setVisible(visible);
207                 this.filePathField.setFocus();
208         }
209         
210         //Open dialog for choosing the file
211         protected void handleLocationDirectoryButtonPressed() {
212                 final Shell shell = filePathField.getShell();
213                 
214                 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
215                 
216                 String[] ext = {"*.sysdyn"};
217                 dialog.setFilterExtensions(ext);
218                 
219                 dialog.setText("Export Model");
220
221                 String dirName = filePathField.getText().trim();
222                 
223                 File path = new File(dirName);
224                 if (path.exists()) {
225                         dialog.setFilterPath(new Path(dirName).toOSString());
226                 }
227                 
228                 String selectedFile = dialog.open();
229                 if (selectedFile != null) {
230                         filePathField.setText(selectedFile);
231                         validatePage();
232                 }               
233
234         }
235         //Get selection from the tree
236         @SuppressWarnings("unchecked")
237         public static <T> T getExplorerResource(GraphExplorerComposite explorer,
238                         Class<T> clazz) {
239                 
240                 if(explorer == null)
241                         return null;
242                 ISelection selection = ((ISelectionProvider) explorer
243                                 .getAdapter(ISelectionProvider.class)).getSelection();
244                 if (selection == null)
245                         return null;
246                 IStructuredSelection iss = (IStructuredSelection) selection;
247                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
248                 if (inc == null)
249                         return null;
250                 final T resource = (T) inc.getAdapter(clazz);
251
252                 return resource;
253         }
254         
255         public boolean createProjects() {
256                 
257                 final String selected = previouslyBrowsedFile;
258                 if(selected == null) return false;
259                 
260                 selectedModel = getExplorerResource(modelExplorer, Resource.class);
261                 if(selectedModel == null)
262                         return false;
263         
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 
266                 
267                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
268                         
269                         @Override
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));
277                                 }
278                         }
279                 });
280
281                 return true;
282         }
283         
284         void validatePage() {
285                 
286                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
287                         setPageComplete(false);
288                         return;
289                 }
290                 
291                 setPageComplete(true);
292                 
293         }
294 }