1 package org.simantics.sysdyn.ui.wizards.functions;
\r
5 import org.eclipse.core.runtime.IStatus;
\r
6 import org.eclipse.core.runtime.Path;
\r
7 import org.eclipse.core.runtime.Status;
\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.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
\r
35 import org.simantics.db.common.request.ReadRequest;
\r
36 import org.simantics.db.common.utils.NameUtils;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
\r
39 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
\r
40 import org.simantics.db.request.Read;
\r
41 import org.simantics.graph.db.TGStatusMonitor;
\r
42 import org.simantics.graph.db.TransferableGraphSource;
\r
43 import org.simantics.graph.db.TransferableGraphs;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.sysdyn.SysdynResource;
\r
46 import org.simantics.sysdyn.ui.Activator;
\r
47 import org.simantics.ui.SimanticsUI;
\r
48 import org.simantics.utils.datastructures.ArrayMap;
\r
50 public class WizardFunctionsExportPage extends WizardPage {
\r
52 // dialog store id constants
\r
53 private Text filePathField;
\r
55 // Keep track of the archive that we browsed to last time
\r
56 // the wizard was invoked.
\r
57 private static String previouslyBrowsedFile = "";
\r
59 private Button browseDirectoriesButton;
\r
61 //private IStructuredSelection currentSelection;
\r
63 GraphExplorerComposite functionLibraryExplorer;
\r
65 private boolean selectionMade = false;
\r
68 * Creates a new project creation wizard page.
\r
71 public WizardFunctionsExportPage() {
\r
72 this("wizardFunctionsExportPage", null, null); //$NON-NLS-1$
\r
76 * Create a new instance of the receiver.
\r
80 public WizardFunctionsExportPage(String pageName) {
\r
81 this(pageName,null, null);
\r
85 * More (many more) parameters.
\r
88 * @param initialPath
\r
89 * @param currentSelection
\r
92 public WizardFunctionsExportPage(String pageName,String initialPath,
\r
93 IStructuredSelection currentSelection) {
\r
95 //this.currentSelection = currentSelection;
\r
96 setPageComplete(false);
\r
97 setTitle("Export Function Library");
\r
98 setDescription("Choose the Function Library and the export location, then press Finish.");
\r
101 public void createControl(Composite parent) {
\r
103 initializeDialogUnits(parent);
\r
105 Composite workArea = new Composite(parent, SWT.NONE);
\r
106 setControl(workArea);
\r
108 workArea.setLayout(new GridLayout());
\r
109 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
110 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
112 createProjectsRoot(workArea);
\r
113 createTree(workArea);
\r
117 private void createProjectsRoot(Composite workArea) {
\r
119 // set label for field
\r
120 Label title = new Label(workArea, SWT.NONE);
\r
121 title.setText("Select the export location for Function Library:");
\r
123 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
124 GridLayout layout = new GridLayout();
\r
125 layout.numColumns = 2;
\r
126 layout.makeColumnsEqualWidth = false;
\r
127 layout.marginWidth = 0;
\r
129 projectGroup.setLayout(layout);
\r
130 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
132 // function library location entry field
\r
133 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
135 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
136 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
137 filePathField.setLayoutData(directoryPathData);
\r
139 filePathField.addModifyListener(new ModifyListener(){
\r
141 public void modifyText(ModifyEvent e) {
\r
142 previouslyBrowsedFile = filePathField.getText();
\r
145 if (previouslyBrowsedFile != null){
\r
146 filePathField.setText(previouslyBrowsedFile);
\r
151 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
152 browseDirectoriesButton.setText("Browse");
\r
153 setButtonLayoutData(browseDirectoriesButton);
\r
155 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
159 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
160 * elected(org.eclipse.swt.events.SelectionEvent)
\r
162 public void widgetSelected(SelectionEvent e) {
\r
163 handleLocationDirectoryButtonPressed();
\r
169 private void createTree(Composite workArea){
\r
171 //set label for tree
\r
172 Label title = new Label(workArea, SWT.NONE);
\r
173 title.setText("Select Function Library to export:");
\r
175 Resource input = SimanticsUI.getProject().get();
\r
177 functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
178 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
180 functionLibraryExplorer
\r
181 .setBrowseContexts(SysdynResource.URIs.FunctionTree);
\r
183 functionLibraryExplorer.finish();
\r
185 functionLibraryExplorer.setInput(null, input);
\r
187 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
188 functionLibraryExplorer);
\r
190 ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
193 public void widgetSelected(SelectionEvent e) {
\r
195 selectionMade = true;
\r
199 public void widgetDefaultSelected(SelectionEvent e) {
\r
201 selectionMade = true;
\r
207 //Set filePathField active
\r
208 public void setVisible(boolean visible) {
\r
209 super.setVisible(visible);
\r
210 this.filePathField.setFocus();
\r
214 //Open dialog for choosing the file
\r
215 protected void handleLocationDirectoryButtonPressed() {
\r
217 final Shell shell = filePathField.getShell();
\r
219 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
\r
221 String[] ext = {"*.sysdynFunctions"};
\r
222 dialog.setFilterExtensions(ext);
\r
224 dialog.setText("Export Function Library");
\r
226 String dirName = filePathField.getText().trim();
\r
228 File path = new File(dirName);
\r
229 if (path.exists()) {
\r
230 dialog.setFilterPath(new Path(dirName).toOSString());
\r
233 String selectedFile = dialog.open();
\r
234 if (selectedFile != null) {
\r
235 filePathField.setText(selectedFile);
\r
241 //Get selection from the tree
\r
242 @SuppressWarnings("unchecked")
\r
243 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
\r
245 if(explorer == null)
\r
247 ISelection selection = ((ISelectionProvider) explorer
\r
248 .getAdapter(ISelectionProvider.class)).getSelection();
\r
249 if (selection == null)
\r
251 IStructuredSelection iss = (IStructuredSelection) selection;
\r
252 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
\r
255 final T resource = (T) inc.getAdapter(clazz);
\r
260 public boolean createProjects(Resource selection) {
\r
262 final String selected = previouslyBrowsedFile;
\r
263 if(selected == null) return false;
\r
265 final Resource functionLibrary = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
266 if(functionLibrary == null) return false;
\r
268 String name = null;
\r
270 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
273 public String perform(ReadGraph graph) throws DatabaseException {
\r
274 if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))
\r
276 Layer0 l0 = Layer0.getInstance(graph);
\r
277 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
283 } catch (DatabaseException ex) {
\r
284 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
285 "Failed to read name of function library " + functionLibrary + ", see exception for details.", ex));
\r
287 if(name == null) return false;
\r
289 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
291 public void run(ReadGraph graph) throws DatabaseException {
\r
292 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, functionLibrary);
\r
293 try (TransferableGraphSource tgs = graph.syncRequest( new ModelTransferableGraphSourceRequest(conf) )) {
\r
294 TransferableGraphs.writeTransferableGraph(graph, tgs, new File(selected), TGStatusMonitor.NULL_MONITOR);
\r
295 } catch (Exception e) {
\r
296 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
297 "Failed to export function library, see exception for details.", e));
\r
305 void validatePage() {
\r
307 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
308 setPageComplete(false);
\r
312 if (functionLibraryExplorer != null){
\r
313 final Resource selectedResource = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
315 String root = null;
\r
317 root = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
320 public String perform(ReadGraph graph) throws DatabaseException {
\r
321 Layer0 l0 = Layer0.getInstance(graph);
\r
322 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);
\r
323 String rootName = NameUtils.getSafeName(graph, model);
\r
329 if (root != null && root.equalsIgnoreCase("Development Project")){
\r
330 setPageComplete(false);
\r
331 setMessage("Select Function Library folder under the Model or from the Shared Functions folder.");
\r
334 } catch (DatabaseException e) {
\r
335 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
\r
336 "Failed to validate wizard page contents, see exception for details.", e));
\r
340 setPageComplete(true);
\r