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.ui.SimanticsUI;
\r
45 import org.simantics.utils.datastructures.ArrayMap;
\r
46 import org.simantics.utils.datastructures.Pair;
\r
48 public class WizardFunctionsExportPage 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
55 private static String previouslyBrowsedFile = "";
\r
57 private Button browseDirectoriesButton;
\r
59 //private IStructuredSelection currentSelection;
\r
61 GraphExplorerComposite functionLibraryExplorer;
\r
63 private boolean selectionMade = false;
\r
66 * Creates a new project creation wizard page.
\r
69 public WizardFunctionsExportPage() {
\r
70 this("wizardFunctionsExportPage", null, null); //$NON-NLS-1$
\r
74 * Create a new instance of the receiver.
\r
78 public WizardFunctionsExportPage(String pageName) {
\r
79 this(pageName,null, null);
\r
83 * More (many more) parameters.
\r
86 * @param initialPath
\r
87 * @param currentSelection
\r
90 public WizardFunctionsExportPage(String pageName,String initialPath,
\r
91 IStructuredSelection currentSelection) {
\r
93 //this.currentSelection = currentSelection;
\r
94 setPageComplete(false);
\r
95 setTitle("Export Function Library");
\r
96 setDescription("Choose the Function Library and the export location, then press Finish.");
\r
99 public void createControl(Composite parent) {
\r
101 initializeDialogUnits(parent);
\r
103 Composite workArea = new Composite(parent, SWT.NONE);
\r
104 setControl(workArea);
\r
106 workArea.setLayout(new GridLayout());
\r
107 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
108 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
110 createProjectsRoot(workArea);
\r
111 createTree(workArea);
\r
115 private void createProjectsRoot(Composite workArea) {
\r
117 // set label for field
\r
118 Label title = new Label(workArea, SWT.NONE);
\r
119 title.setText("Select the export location for Function Library:");
\r
121 Composite projectGroup = new Composite(workArea, SWT.NONE);
\r
122 GridLayout layout = new GridLayout();
\r
123 layout.numColumns = 2;
\r
124 layout.makeColumnsEqualWidth = false;
\r
125 layout.marginWidth = 0;
\r
127 projectGroup.setLayout(layout);
\r
128 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
130 // function library location entry field
\r
131 this.filePathField = new Text(projectGroup, SWT.BORDER);
\r
133 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
\r
134 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
\r
135 filePathField.setLayoutData(directoryPathData);
\r
137 filePathField.addModifyListener(new ModifyListener(){
\r
139 public void modifyText(ModifyEvent e) {
\r
140 previouslyBrowsedFile = filePathField.getText();
\r
143 if (previouslyBrowsedFile != null){
\r
144 filePathField.setText(previouslyBrowsedFile);
\r
149 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
150 browseDirectoriesButton.setText("Browse");
\r
151 setButtonLayoutData(browseDirectoriesButton);
\r
153 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
157 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
158 * elected(org.eclipse.swt.events.SelectionEvent)
\r
160 public void widgetSelected(SelectionEvent e) {
\r
161 handleLocationDirectoryButtonPressed();
\r
167 private void createTree(Composite workArea){
\r
169 //set label for tree
\r
170 Label title = new Label(workArea, SWT.NONE);
\r
171 title.setText("Select Function Library to export:");
\r
174 Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
\r
177 public Resource perform(ReadGraph graph)
\r
178 throws DatabaseException {
\r
179 Resource model = SimanticsUI.getProject().get();
\r
185 functionLibraryExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
186 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
188 functionLibraryExplorer
\r
189 .setBrowseContexts("http://www.simantics.org/Sysdyn-1.0/FunctionTree");
\r
191 functionLibraryExplorer.finish();
\r
193 functionLibraryExplorer.setInput(null, input);
\r
195 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
196 functionLibraryExplorer);
\r
198 ((Tree)functionLibraryExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
201 public void widgetSelected(SelectionEvent e) {
\r
203 selectionMade = true;
\r
207 public void widgetDefaultSelected(SelectionEvent e) {
\r
209 selectionMade = true;
\r
214 } catch (DatabaseException e) {
\r
215 e.printStackTrace();
\r
221 //Set filePathField active
\r
222 public void setVisible(boolean visible) {
\r
223 super.setVisible(visible);
\r
224 this.filePathField.setFocus();
\r
228 //Open dialog for choosing the file
\r
229 protected void handleLocationDirectoryButtonPressed() {
\r
231 final Shell shell = filePathField.getShell();
\r
233 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
\r
235 String[] ext = {"*.tg"};
\r
236 dialog.setFilterExtensions(ext);
\r
238 dialog.setText("Export Function Library");
\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
255 //Get selection from the tree
\r
256 @SuppressWarnings("unchecked")
\r
257 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(Resource selection) {
\r
276 final String selected = previouslyBrowsedFile;
\r
277 if(selected == null) return false;
\r
279 final Resource functionLibrary = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
280 if(functionLibrary == null) return false;
\r
282 String name = null;
\r
284 name = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
287 public String perform(ReadGraph graph) throws DatabaseException {
\r
288 if (!graph.hasStatement(functionLibrary, Layer0.getInstance(graph).PartOf))
\r
290 Layer0 l0 = Layer0.getInstance(graph);
\r
291 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
297 } catch (DatabaseException e1) {
\r
298 e1.printStackTrace();
\r
300 if(name == null) return false;
\r
302 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\r
305 public void run(ReadGraph graph) throws DatabaseException {
\r
306 Layer0 l0 = Layer0.getInstance(graph);
\r
307 String name = graph.syncRequest(new PossibleRelatedValue<String>(functionLibrary, l0.HasName, Bindings.STRING ));
\r
308 ArrayList<Pair<Resource, String>> roots = new ArrayList<Pair<Resource, String>>();
\r
309 roots.add(Pair.make(functionLibrary, name));
\r
310 TransferableGraph1 tg = graph.syncRequest(new TransferableGraphRequest2(roots, functionLibrary));
\r
313 Files.createFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class), tg);
\r
314 } catch (RuntimeBindingConstructionException e) {
\r
315 e.printStackTrace();
\r
316 } catch (IOException e) {
\r
317 e.printStackTrace();
\r
327 void validatePage() {
\r
329 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
330 setPageComplete(false);
\r
334 if (functionLibraryExplorer != null){
\r
335 final Resource selectedResource = getExplorerResource(functionLibraryExplorer, Resource.class);
\r
337 String root = null;
\r
339 root = SimanticsUI.getSession().syncRequest(new Read<String>() {
\r
342 public String perform(ReadGraph graph) throws DatabaseException {
\r
343 Layer0 l0 = Layer0.getInstance(graph);
\r
344 Resource model = graph.getPossibleObject(selectedResource, l0.PartOf);
\r
345 String rootName = NameUtils.getSafeName(graph, model);
\r
351 if (root != null && root.equalsIgnoreCase("Development Project")){
\r
352 setPageComplete(false);
\r
353 setMessage("Select Function Library folder under the Model or from the Shared Functions folder.");
\r
356 } catch (DatabaseException e) {
\r
357 e.printStackTrace();
\r
361 setPageComplete(true);
\r