1 package org.simantics.sysdyn.ui.wizards.functions;
\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.common.utils.NameUtils;
\r
39 import org.simantics.db.exception.DatabaseException;
\r
40 import org.simantics.db.layer0.util.TransferableGraphRequest2;
\r
41 import org.simantics.db.request.Read;
\r
42 import org.simantics.graph.representation.TransferableGraph1;
\r
43 import org.simantics.layer0.Layer0;
\r
44 import org.simantics.sysdyn.SysdynResource;
\r
45 import org.simantics.ui.SimanticsUI;
\r
46 import org.simantics.utils.datastructures.ArrayMap;
\r
47 import org.simantics.utils.datastructures.Pair;
\r
49 public class WizardFunctionsExportPage extends WizardPage {
\r
51 // dialog store id constants
\r
52 private Text filePathField;
\r
54 // Keep track of the archive that we browsed to last time
\r
55 // the wizard was invoked.
\r
56 private static String previouslyBrowsedFile = "";
\r
58 private Button browseDirectoriesButton;
\r
60 //private IStructuredSelection currentSelection;
\r
62 GraphExplorerComposite functionLibraryExplorer;
\r
64 private boolean selectionMade = false;
\r
67 * Creates a new project creation wizard page.
\r
70 public WizardFunctionsExportPage() {
\r
71 this("wizardFunctionsExportPage", null, null); //$NON-NLS-1$
\r
75 * Create a new instance of the receiver.
\r
79 public WizardFunctionsExportPage(String pageName) {
\r
80 this(pageName,null, null);
\r
84 * More (many more) parameters.
\r
87 * @param initialPath
\r
88 * @param currentSelection
\r
91 public WizardFunctionsExportPage(String pageName,String initialPath,
\r
92 IStructuredSelection currentSelection) {
\r
94 //this.currentSelection = currentSelection;
\r
95 setPageComplete(false);
\r
96 setTitle("Export Function Library");
\r
97 setDescription("Choose the Function Library and the export location, then press Finish.");
\r
100 public void createControl(Composite parent) {
\r
102 initializeDialogUnits(parent);
\r
104 Composite workArea = new Composite(parent, SWT.NONE);
\r
105 setControl(workArea);
\r
107 workArea.setLayout(new GridLayout());
\r
108 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
109 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
111 createProjectsRoot(workArea);
\r
112 createTree(workArea);
\r
116 private void createProjectsRoot(Composite workArea) {
\r
118 // set label for field
\r
119 Label title = new Label(workArea, SWT.NONE);
\r
120 title.setText("Select the export location for Function Library:");
\r
122 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
123 GridLayout layout = new GridLayout();
\r
124 layout.numColumns = 2;
\r
125 layout.makeColumnsEqualWidth = false;
\r
126 layout.marginWidth = 0;
\r
128 projectGroup.setLayout(layout);
\r
129 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
131 // function library location entry field
\r
132 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
134 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
135 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
136 filePathField.setLayoutData(directoryPathData);
\r
138 filePathField.addModifyListener(new ModifyListener(){
\r
140 public void modifyText(ModifyEvent e) {
\r
141 previouslyBrowsedFile = filePathField.getText();
\r
144 if (previouslyBrowsedFile != null){
\r
145 filePathField.setText(previouslyBrowsedFile);
\r
150 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
151 browseDirectoriesButton.setText("Browse");
\r
152 setButtonLayoutData(browseDirectoriesButton);
\r
154 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
158 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
159 * elected(org.eclipse.swt.events.SelectionEvent)
\r
161 public void widgetSelected(SelectionEvent e) {
\r
162 handleLocationDirectoryButtonPressed();
\r
168 private void createTree(Composite workArea){
\r
170 //set label for tree
\r
171 Label title = new Label(workArea, SWT.NONE);
\r
172 title.setText("Select Function Library to export:");
\r
175 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
178 public Resource perform(ReadGraph graph)
\r
179 throws DatabaseException {
\r
180 Resource model = SimanticsUI.getProject().get();
\r
186 functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
187 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
189 functionLibraryExplorer
\r
190 .setBrowseContexts(SysdynResource.URIs.FunctionTree);
\r
192 functionLibraryExplorer.finish();
\r
194 functionLibraryExplorer.setInput(null, input);
\r
196 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
197 functionLibraryExplorer);
\r
199 ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
202 public void widgetSelected(SelectionEvent e) {
\r
204 selectionMade = true;
\r
208 public void widgetDefaultSelected(SelectionEvent e) {
\r
210 selectionMade = true;
\r
215 } catch (DatabaseException e) {
\r
216 e.printStackTrace();
\r
222 //Set filePathField active
\r
223 public void setVisible(boolean visible) {
\r
224 super.setVisible(visible);
\r
225 this.filePathField.setFocus();
\r
229 //Open dialog for choosing the file
\r
230 protected void handleLocationDirectoryButtonPressed() {
\r
232 final Shell shell = filePathField.getShell();
\r
234 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
\r
236 String[] ext = {"*.sysdynFunctions"};
\r
237 dialog.setFilterExtensions(ext);
\r
239 dialog.setText("Export Function Library");
\r
241 String dirName = filePathField.getText().trim();
\r
243 File path = new File(dirName);
\r
244 if (path.exists()) {
\r
245 dialog.setFilterPath(new Path(dirName).toOSString());
\r
248 String selectedFile = dialog.open();
\r
249 if (selectedFile != null) {
\r
250 filePathField.setText(selectedFile);
\r
256 //Get selection from the tree
\r
257 @SuppressWarnings("unchecked")
\r
258 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
\r
260 if(explorer == null)
\r
262 ISelection selection = ((ISelectionProvider) explorer
\r
263 .getAdapter(ISelectionProvider.class)).getSelection();
\r
264 if (selection == null)
\r
266 IStructuredSelection iss = (IStructuredSelection) selection;
\r
267 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
\r
270 final T resource = (T) inc.getAdapter(clazz);
\r
275 public boolean createProjects(Resource selection) {
\r
277 final String selected = previouslyBrowsedFile;
\r
278 if(selected == null) return false;
\r
280 final Resource functionLibrary = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
281 if(functionLibrary == null) return false;
\r
283 String name = null;
\r
285 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
288 public String perform(ReadGraph graph) throws DatabaseException {
\r
289 if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))
\r
291 Layer0 l0 = Layer0.getInstance(graph);
\r
292 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
298 } catch (DatabaseException e1) {
\r
299 e1.printStackTrace();
\r
301 if(name == null) return false;
\r
303 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
306 public void run(ReadGraph graph) throws DatabaseException {
\r
307 Layer0 l0 = Layer0.getInstance(graph);
\r
308 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
309 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
310 roots.add(Pair.make(functionLibrary, name));
\r
311 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));
\r
314 Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
315 } catch (RuntimeBindingConstructionException e) {
\r
316 e.printStackTrace();
\r
317 } catch (IOException e) {
\r
318 e.printStackTrace();
\r
328 void validatePage() {
\r
330 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
331 setPageComplete(false);
\r
335 if (functionLibraryExplorer != null){
\r
336 final Resource selectedResource = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
338 String root = null;
\r
340 root = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
343 public String perform(ReadGraph graph) throws DatabaseException {
\r
344 Layer0 l0 = Layer0.getInstance(graph);
\r
345 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);
\r
346 String rootName = NameUtils.getSafeName(graph, model);
\r
352 if (root != null && root.equalsIgnoreCase("Development Project")){
\r
353 setPageComplete(false);
\r
354 setMessage("Select Function Library folder under the Model or from the Shared Functions folder.");
\r
357 } catch (DatabaseException e) {
\r
358 e.printStackTrace();
\r
362 setPageComplete(true);
\r