]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
fa92bae2406ac9564575c5242eb65e9d9836b73e
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.modules;
2
3 import java.io.File;
4
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.Path;
7 import org.eclipse.core.runtime.Status;
8 import org.eclipse.jface.layout.GridDataFactory;
9 import org.eclipse.jface.layout.PixelConverter;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.ISelectionProvider;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.WizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.FileDialog;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Tree;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.request.Read;
35 import org.simantics.sysdyn.SysdynResource;
36 import org.simantics.sysdyn.ui.utils.imports.ImportUtilsUI;
37 import org.simantics.ui.SimanticsUI;
38 import org.simantics.utils.datastructures.ArrayMap;
39
40
41 public class WizardModulesImportPage extends WizardPage{
42         
43         public static String IMPORTMODULETPATH = "IMPORT_MODULE_PATH";
44         
45         // dialog store id constants
46         
47         private Text filePathField;
48         
49         // Keep track of the archive that we browsed to last time
50         // the wizard was invoked.
51         private static String previouslyBrowsedFile = "";
52
53         private Button browseDirectoriesButton;
54
55         private Shell shell;
56         
57         //private IStructuredSelection currentSelection;
58         
59         private Resource selectedModel;
60         
61         GraphExplorerComposite modelExplorer;
62         
63         private boolean selectionMade = false;
64         
65         /**
66          * Creates a new project creation wizard page.
67          * 
68          */
69         public WizardModulesImportPage() {
70                 this("wizardModulesImportPage", null, null); //$NON-NLS-1$
71         }
72
73         /**
74          * Create a new instance of the receiver.
75          * 
76          * @param pageName
77          */
78         public WizardModulesImportPage(String pageName) {
79                 this(pageName,null, null);
80         }
81                         
82         /**
83          * More (many more) parameters.
84          * 
85          * @param pageName
86          * @param initialPath
87          * @param currentSelection
88          * @since 3.5
89          */
90         public WizardModulesImportPage(String pageName,String initialPath,
91                         IStructuredSelection currentSelection) {
92                 super(pageName);
93                 setPageComplete(false);
94                 //this.currentSelection = currentSelection;
95                 setTitle("Import Module");
96                 setDescription("Choose the Module file and the import location, then press Finish.");
97         }
98         
99         public void createControl(Composite parent) {
100         
101                 initializeDialogUnits(parent);
102
103                 Composite workArea = new Composite(parent, SWT.NONE);
104                 setControl(workArea);
105
106                 workArea.setLayout(new GridLayout());
107                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
108                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
109
110                 createProjectsRoot(workArea);
111                 
112                 createTree(workArea);
113         }
114         
115         private void createProjectsRoot(Composite workArea) {
116                 
117                 // set label for field
118                 Label title = new Label(workArea, SWT.NONE);
119                 title.setText("Select Module source:");
120                 
121                 Composite projectGroup = new Composite(workArea, SWT.NONE);
122                 GridLayout layout = new GridLayout();
123                 layout.numColumns = 2;
124                 layout.makeColumnsEqualWidth = false;
125                 layout.marginWidth = 0;
126                 
127                 projectGroup.setLayout(layout);
128                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
129                 
130                 // module location entry field
131                 this.filePathField = new Text(projectGroup, SWT.BORDER);
132
133                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);
134                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);
135                 filePathField.setLayoutData(directoryPathData);
136                 
137                 filePathField.addModifyListener(new ModifyListener(){
138             @Override
139             public void modifyText(ModifyEvent e) {
140                 previouslyBrowsedFile = filePathField.getText();        
141             }
142                 });
143                 if (previouslyBrowsedFile != null){
144                         filePathField.setText(previouslyBrowsedFile);
145                         validatePage();
146                 }
147                 
148                 // browse button
149                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
150                 browseDirectoriesButton.setText("Browse");
151                 setButtonLayoutData(browseDirectoriesButton);
152                 
153                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
154                         /*
155                          * (non-Javadoc)
156                          * 
157                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS
158                          * elected(org.eclipse.swt.events.SelectionEvent)
159                          */
160                         public void widgetSelected(SelectionEvent e) {
161                                 handleLocationDirectoryButtonPressed();
162                         }
163                 });
164                 
165         }
166
167         private void createTree(Composite workArea){
168                 
169                 //set label for tree
170                 Label title = new Label(workArea, SWT.NONE);
171                 title.setText("Select import location:");
172
173                 try {
174                         Resource input = SimanticsUI.getSession().syncRequest(new Read<Resource>() {
175
176                                 @Override
177                                 public Resource perform(ReadGraph graph)
178                                                 throws DatabaseException {
179                                         Resource model = SimanticsUI.getProject().get();
180                                         return model;
181                                 }
182
183                         });
184
185                         modelExplorer = new GraphExplorerComposite(ArrayMap.keys(
186                                         "displaySelectors", "displayFilter").values(false, false), null, workArea, SWT.BORDER | SWT.SINGLE);
187
188                         modelExplorer
189                         .setBrowseContexts(SysdynResource.URIs.ImportModuleTree);
190
191                         modelExplorer.finish();
192
193                         modelExplorer.setInput(null, input);
194                         
195                         GridDataFactory.fillDefaults().grab(true, true).applyTo(
196                                         modelExplorer);
197                         
198                         ((Tree)modelExplorer.getExplorer().getControl()).addSelectionListener(new SelectionListener() {
199                                 
200                                 @Override
201                                 public void widgetSelected(SelectionEvent e) {
202                                         selectionMade = true;
203                                         validatePage();
204                                 }
205                                 @Override
206                                 public void widgetDefaultSelected(SelectionEvent e) {
207                                         selectionMade = true;
208                                         validatePage();
209                                 }
210                         });
211                         
212
213                 } catch (DatabaseException e) {
214                         e.printStackTrace();
215                 }
216
217         }
218         
219         //Set filePathField active
220         public void setVisible(boolean visible) {
221                 super.setVisible(visible);
222                 this.filePathField.setFocus();
223         }
224         
225         //Open dialog for choosing the file
226         protected void handleLocationDirectoryButtonPressed() {
227                 
228                 shell = filePathField.getShell();
229                 
230                 FileDialog dialog = new FileDialog(shell, SWT.OPEN);
231                 
232                 String[] ext = {"*.sysdynModule; *.tg", "*.*"};
233                 dialog.setFilterExtensions(ext);
234                 
235                 dialog.setText("Import Module");
236
237                 String dirName = filePathField.getText().trim();
238                 
239                 File path = new File(dirName);
240                 if (path.exists()) {
241                         dialog.setFilterPath(new Path(dirName).toOSString());   
242                 }
243                 
244                 String selectedFile = dialog.open();
245                 if (selectedFile != null) {
246                         filePathField.setText(selectedFile);
247                         validatePage();
248                 }               
249         }
250         
251         //Get selection from the tree
252         @SuppressWarnings("unchecked")
253         public static <T> T getExplorerResource(GraphExplorerComposite explorer,
254                         Class<T> clazz) {
255                 
256                 if(explorer == null)
257                         return null;
258                 ISelection selection = ((ISelectionProvider) explorer
259                                 .getAdapter(ISelectionProvider.class)).getSelection();
260                 if (selection == null)
261                         return null;
262                 IStructuredSelection iss = (IStructuredSelection) selection;
263                 AdaptableHintContext inc = (AdaptableHintContext) iss.getFirstElement();
264                 if (inc == null)
265                         return null;
266                 final T resource = (T) inc.getAdapter(clazz);
267
268                 return resource;
269         }
270         
271         //Create project after finish is pressed.
272         public boolean createProjects() {
273                 
274                 String selected = previouslyBrowsedFile;
275                 if(selected == null){ 
276                         setErrorMessage("Error when retrieving resource");
277                         return false;
278                 }
279                 
280                 selectedModel= getExplorerResource(modelExplorer, Resource.class);
281                 if(selectedModel == null){
282                         setErrorMessage("No file selected");
283                         return false;
284                 }
285                 
286                 IStatus status = ImportUtilsUI.importModuleFile(selectedModel, selected, null);
287                 
288                 /*
289                 TransferableGraph1 tg = null;
290                 try {
291                         tg = (TransferableGraph1)Files.readFile(new File(selected), Bindings.getBindingUnchecked(TransferableGraph1.class));
292                 } catch (RuntimeBindingConstructionException e) {
293                         e.printStackTrace();
294                 } catch (IOException e) {
295                         setErrorMessage("The imported file is not of type: Module Type");
296                         return false;
297                 }
298                 if(tg == null){
299                         setErrorMessage("The imported file is not of type: Module Type");
300                         return false;
301                 }
302
303                 
304                 DefaultPasteImportAdvisor ia = new DefaultPasteImportAdvisor(selectedModel);
305                 try {
306                         DefaultPasteHandler.defaultExecute(tg, selectedModel, ia);
307                 } catch (MissingDependencyException e) {
308                         e.printStackTrace();
309                 } catch (Exception e) {
310                         e.printStackTrace();
311                 }
312                 
313                 final Resource root = ia.getRoot();
314                 
315                 try {
316                         SimanticsUI.getSession().syncRequest(new WriteRequest() {
317                                 
318                                 @Override
319                                 public void perform(WriteGraph graph) throws DatabaseException {
320                                         if(!graph.isInheritedFrom(root, SysdynResource.getInstance(graph).Module)) {
321                                                 Resource instanceOf = graph.getPossibleObject(root, Layer0.getInstance(graph).InstanceOf);
322                                                 String type = "...";
323                                                 if(instanceOf != null)
324                                                         type = NameUtils.getSafeName(graph, instanceOf);
325                                                 else {
326                                                         Resource inheritedFrom = graph.getPossibleObject(root, Layer0.getInstance(graph).Inherits);
327                                                         if(inheritedFrom != null)
328                                                                 type = NameUtils.getSafeName(graph, inheritedFrom);
329                                                 }
330                                                 graph.deny(root, Layer0.getInstance(graph).PartOf);
331                                                 error = type;
332                                         }
333                                 }
334                                 });
335                 } catch (DatabaseException e) {
336                         e.printStackTrace();
337                 }
338                 
339                 if (!error.isEmpty()){
340                         setErrorMessage("The imported file is not of type: Module Type (" + error +")");
341                         error = "";
342                         return false;
343                 }
344                 */
345                 if(status == null || !status.equals(Status.OK_STATUS)) {
346                     setErrorMessage(status.getMessage());
347                     return false;
348                 }
349
350                 return true;
351         }
352         
353         void validatePage() {
354                 
355                 if (previouslyBrowsedFile.isEmpty() || selectionMade == false){
356                         setPageComplete(false);
357                         return;
358                 }
359                 
360                 setErrorMessage(null);
361                 setPageComplete(true);
362                 
363         }
364         
365 }