1 package org.simantics.sysdyn.ui.wizards.modules;
\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.db.ReadGraph;
\r
32 import org.simantics.db.Resource;
\r
33 import org.simantics.db.exception.DatabaseException;
\r
34 import org.simantics.db.request.Read;
\r
35 import org.simantics.sysdyn.SysdynResource;
\r
36 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
\r
37 import org.simantics.ui.SimanticsUI;
\r
38 import org.simantics.utils.datastructures.ArrayMap;
\r
41 public class WizardModulesImportPage extends WizardPage{
\r
43 public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";
\r
45 // dialog store id constants
\r
47 private Text filePathField;
\r
49 // Keep track of the archive that we browsed to last time
\r
50 // the wizard was invoked.
\r
51 private static String previouslyBrowsedFile = "";
\r
53 private Button browseDirectoriesButton;
\r
55 private Shell shell;
\r
57 //private IStructuredSelection currentSelection;
\r
59 private Resource selectedModel;
\r
61 GraphExplorerComposite modelExplorer;
\r
63 private boolean selectionMade = false;
\r
66 * Creates a new project creation wizard page.
\r
69 public WizardModulesImportPage() {
\r
70 this("wizardModulesImportPage", null, null); //$NON-NLS-1$
\r
74 * Create a new instance of the receiver.
\r
78 public WizardModulesImportPage(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 WizardModulesImportPage(String pageName,String initialPath,
\r
91 IStructuredSelection currentSelection) {
\r
93 setPageComplete(false);
\r
94 //this.currentSelection = currentSelection;
\r
95 setTitle("Import Module");
\r
96 setDescription("Choose the Module file and the import 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
112 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 Module source:");
\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 // module 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 import location:");
\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 modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
\r
186 "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
\r
189 .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
\r
191 modelExplorer.finish();
\r
193 modelExplorer.setInput(null, input);
\r
195 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
198 ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
\r
201 public void widgetSelected(SelectionEvent e) {
\r
202 selectionMade = true;
\r
206 public void widgetDefaultSelected(SelectionEvent e) {
\r
207 selectionMade = true;
\r
213 } catch (DatabaseException e) {
\r
214 e.printStackTrace();
\r
219 //Set filePathField active
\r
220 public void setVisible(boolean visible) {
\r
221 super.setVisible(visible);
\r
222 this.filePathField.setFocus();
\r
225 //Open dialog for choosing the file
\r
226 protected void handleLocationDirectoryButtonPressed() {
\r
228 shell = filePathField.getShell();
\r
230 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
\r
232 String[] ext = {"*.tg"};
\r
233 dialog.setFilterExtensions(ext);
\r
235 dialog.setText("Import Module");
\r
237 String dirName = filePathField.getText().trim();
\r
239 File path = new File(dirName);
\r
240 if (path.exists()) {
\r
241 dialog.setFilterPath(new Path(dirName).toOSString());
\r
244 String selectedFile = dialog.open();
\r
245 if (selectedFile != null) {
\r
246 filePathField.setText(selectedFile);
\r
251 //Get selection from the tree
\r
252 @SuppressWarnings("unchecked")
\r
253 public static <T> T getExplorerResource(GraphExplorerComposite explorer,
\r
256 if(explorer == null)
\r
258 ISelection selection = ((ISelectionProvider) explorer
\r
259 .getAdapter(ISelectionProvider.class)).getSelection();
\r
260 if (selection == null)
\r
262 IStructuredSelection iss = (IStructuredSelection) selection;
\r
263 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
\r
266 final T resource = (T) inc.getAdapter(clazz);
\r
271 //Create project after finish is pressed.
\r
272 public boolean createProjects() {
\r
274 String selected = previouslyBrowsedFile;
\r
275 if(selected == null){
\r
276 setErrorMessage("Error when retrieving resource");
\r
280 selectedModel= getExplorerResource(modelExplorer, Resource.class);
\r
281 if(selectedModel == null){
\r
282 setErrorMessage("No file selected");
\r
286 IStatus status = ImportUtilsUI.importModuleFile(selectedModel, selected, null);
\r
289 TransferableGraph1 tg = null;
\r
291 tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
\r
292 } catch (RuntimeBindingConstructionException e) {
\r
293 e.printStackTrace();
\r
294 } catch (IOException e) {
\r
295 setErrorMessage("The imported file is not of type: Module Type");
\r
299 setErrorMessage("The imported file is not of type: Module Type");
\r
304 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel);
\r
306 DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);
\r
307 } catch (MissingDependencyException e) {
\r
308 e.printStackTrace();
\r
309 } catch (Exception e) {
\r
310 e.printStackTrace();
\r
313 final Resource root = ia.getRoot();
\r
316 SimanticsUI.getSession().syncRequest(new WriteRequest() {
\r
319 public void perform(WriteGraph graph) throws DatabaseException {
\r
320 if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {
\r
321 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);
\r
322 String type = "...";
\r
323 if(instanceOf != null)
\r
324 type = NameUtils.getSafeName(graph, instanceOf);
\r
326 Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);
\r
327 if(inheritedFrom != null)
\r
328 type = NameUtils.getSafeName(graph, inheritedFrom);
\r
330 graph.deny(root, Layer0.getInstance(graph).PartOf);
\r
335 } catch (DatabaseException e) {
\r
336 e.printStackTrace();
\r
339 if (!error.isEmpty()){
\r
340 setErrorMessage("The imported file is not of type: Module Type (" + error +")");
\r
345 if(status == null || !status.equals(Status.OK_STATUS)) {
\r
346 setErrorMessage(status.getMessage());
\r
353 void validatePage() {
\r
355 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
\r
356 setPageComplete(false);
\r
360 setErrorMessage(null);
\r
361 setPageComplete(true);
\r