1 package org.simantics.sysdyn.ui.wizards.models;
\r
4 import java.io.IOException;
\r
5 import java.util.ArrayList;
\r
7 import org.eclipse.core.runtime.Path;
\r
8 import org.eclipse.jface.layout.GridDataFactory;
\r
9 import org.eclipse.jface.layout.PixelConverter;
\r
10 import org.eclipse.jface.viewers.ISelection;
\r
11 import org.eclipse.jface.viewers.ISelectionProvider;
\r
12 import org.eclipse.jface.viewers.IStructuredSelection;
\r
13 import org.eclipse.jface.wizard.WizardPage;
\r
14 import org.eclipse.swt.SWT;
\r
15 import org.eclipse.swt.events.ModifyEvent;
\r
16 import org.eclipse.swt.events.ModifyListener;
\r
17 import org.eclipse.swt.events.SelectionAdapter;
\r
18 import org.eclipse.swt.events.SelectionEvent;
\r
19 import org.eclipse.swt.events.SelectionListener;
\r
20 import org.eclipse.swt.layout.GridData;
\r
21 import org.eclipse.swt.layout.GridLayout;
\r
22 import org.eclipse.swt.widgets.Button;
\r
23 import org.eclipse.swt.widgets.Composite;
\r
24 import org.eclipse.swt.widgets.FileDialog;
\r
25 import org.eclipse.swt.widgets.Label;
\r
26 import org.eclipse.swt.widgets.Shell;
\r
27 import org.eclipse.swt.widgets.Text;
\r
28 import org.eclipse.swt.widgets.Tree;
\r
29 import org.simantics.browsing.ui.swt.AdaptableHintContext;
\r
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
\r
31 import org.simantics.databoard.Bindings;
\r
32 import org.simantics.databoard.Files;
\r
33 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
\r
34 import org.simantics.db.ReadGraph;
\r
35 import org.simantics.db.Resource;
\r
36 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
\r
37 import org.simantics.db.common.request.ReadRequest;
\r
38 import org.simantics.db.exception.DatabaseException;
\r
39 import org.simantics.db.layer0.util.TransferableGraphRequest2;
\r
40 import org.simantics.db.request.Read;
\r
41 import org.simantics.graph.representation.TransferableGraph1;
\r
42 import org.simantics.layer0.Layer0;
\r
43 import org.simantics.sysdyn.SysdynResource;
\r
44 import org.simantics.ui.SimanticsUI;
\r
45 import org.simantics.utils.datastructures.ArrayMap;
\r
46 import org.simantics.utils.datastructures.Pair;
\r
48 public class WizardModelsExportPage extends WizardPage {
\r
50 // dialog store id constants
\r
51 private Text filePathField;
\r
53 // Keep track of the archive that we browsed to last time
\r
54 // the wizard was invoked.
\r
56 private static String previouslyBrowsedFile = "";
\r
58 private Button browseDirectoriesButton;
\r
60 //private IStructuredSelection currentSelection;
\r
62 private Resource selectedModel;
\r
64 GraphExplorerComposite modelExplorer;
\r
66 private boolean selectionMade = false;
\r
71 * Creates a new project creation wizard page.
\r
74 public WizardModelsExportPage() {
\r
75 this("wizardModelsExportPage", null, null); //$NON-NLS-1$
\r
79 * Create a new instance of the receiver.
\r
83 public WizardModelsExportPage(String pageName) {
\r
84 this(pageName,null, null);
\r
88 * More (many more) parameters.
\r
91 * @param initialPath
\r
92 * @param currentSelection
\r
95 public WizardModelsExportPage(String pageName,String initialPath,
\r
96 IStructuredSelection currentSelection) {
\r
98 //this.currentSelection = currentSelection;
\r
99 setPageComplete(false);
\r
100 setTitle("Export Model");
\r
101 setDescription("Choose the Model and the export location, then press Finish.");
\r
104 public void createControl(Composite parent) {
\r
106 initializeDialogUnits(parent);
\r
108 Composite workArea = new Composite(parent, SWT.NONE);
\r
109 setControl(workArea);
\r
111 workArea.setLayout(new GridLayout());
\r
112 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
113 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
115 createProjectsRoot(workArea);
\r
116 createTree (workArea);
\r
119 private void createProjectsRoot(Composite workArea) {
\r
121 // set label for field
\r
122 Label title = new Label(workArea, SWT.NONE);
\r
123 title.setText("Select the export location for Model:");
\r
125 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
126 GridLayout layout = new GridLayout();
\r
127 layout.numColumns = 2;
\r
128 layout.makeColumnsEqualWidth = false;
\r
129 layout.marginWidth = 0;
\r
131 projectGroup.setLayout(layout);
\r
132 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
134 // model location entry field
\r
135 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
137 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
138 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
139 filePathField.setLayoutData(directoryPathData);
\r
141 filePathField.addModifyListener(new ModifyListener(){
\r
143 public void modifyText(ModifyEvent e) {
\r
144 previouslyBrowsedFile = filePathField.getText();
\r
147 if (previouslyBrowsedFile != null){
\r
148 filePathField.setText(previouslyBrowsedFile);
\r
153 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
154 browseDirectoriesButton.setText("Browse");
\r
155 setButtonLayoutData(browseDirectoriesButton);
\r
157 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
161 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
162 * elected(org.eclipse.swt.events.SelectionEvent)
\r
164 public void widgetSelected(SelectionEvent e) {
\r
165 handleLocationDirectoryButtonPressed();
\r
171 private void createTree(Composite workArea){
\r
173 //set label for tree
\r
174 Label title = new Label(workArea, SWT.NONE);
\r
175 title.setText("Select Model to export:");
\r
178 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
181 public Resource perform(ReadGraph graph)
\r
182 throws DatabaseException {
\r
183 Resource model = SimanticsUI.getProject().get();
\r
189 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
190 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
193 .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
\r
195 modelExplorer.finish();
\r
197 modelExplorer.setInput(null, input);
\r
199 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
202 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
205 public void widgetSelected(SelectionEvent e) {
\r
206 selectionMade = true;
\r
210 public void widgetDefaultSelected(SelectionEvent e) {
\r
211 selectionMade = true;
\r
217 } catch (DatabaseException e) {
\r
218 e.printStackTrace();
\r
223 //Set filePathField active
\r
224 public void setVisible(boolean visible) {
\r
225 super.setVisible(visible);
\r
226 this.filePathField.setFocus();
\r
229 //Open dialog for choosing the file
\r
230 protected void handleLocationDirectoryButtonPressed() {
\r
231 final Shell shell = filePathField.getShell();
\r
233 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
\r
235 String[] ext = {"*.sysdyn"};
\r
236 dialog.setFilterExtensions(ext);
\r
238 dialog.setText("Export Model");
\r
240 String dirName = filePathField.getText().trim();
\r
242 File path = new File(dirName);
\r
243 if (path.exists()) {
\r
244 dialog.setFilterPath(new Path(dirName).toOSString());
\r
247 String selectedFile = dialog.open();
\r
248 if (selectedFile != null) {
\r
249 filePathField.setText(selectedFile);
\r
254 //Get selection from the tree
\r
255 @SuppressWarnings("unchecked")
\r
256 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
\r
259 if(explorer == null)
\r
261 ISelection selection = ((ISelectionProvider) explorer
\r
262 .getAdapter(ISelectionProvider.class)).getSelection();
\r
263 if (selection == null)
\r
265 IStructuredSelection iss = (IStructuredSelection) selection;
\r
266 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
\r
269 final T resource = (T) inc.getAdapter(clazz);
\r
274 public boolean createProjects() {
\r
276 final String selected = previouslyBrowsedFile;
\r
277 if(selected == null) return false;
\r
279 selectedModel= getExplorerResource(modelExplorer, Resource.class);
\r
280 if(selectedModel == null)
\r
283 // FIXME: Model browser doesn't change its selection even if the selected object is removed,
\r
284 // so you can try to export a removed model
\r
286 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
289 public void run(ReadGraph graph) throws DatabaseException {
\r
290 Layer0 l0 = Layer0.getInstance(graph);
\r
291 String name = graph.syncRequest(new PossibleRelatedValue<String>(selectedModel, l0.HasName, Bindings.STRING ));
\r
292 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
293 roots.add(Pair.make(selectedModel, name));
\r
294 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, selectedModel));
\r
297 Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
298 } catch (RuntimeBindingConstructionException e) {
\r
299 e.printStackTrace();
\r
300 } catch (IOException e) {
\r
301 e.printStackTrace();
\r
310 void validatePage() {
\r
312 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
313 setPageComplete(false);
\r
317 setPageComplete(true);
\r