]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
bf813ba6ff1ecf0738c52f255c583e5b08fe95cd
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.wizards.mdl;\r
2 \r
3 import java.io.File;\r
4 \r
5 import org.eclipse.core.runtime.Path;\r
6 import org.eclipse.jface.layout.PixelConverter;\r
7 import org.eclipse.jface.viewers.IStructuredSelection;\r
8 import org.eclipse.jface.wizard.WizardPage;\r
9 import org.eclipse.swt.SWT;\r
10 import org.eclipse.swt.events.ModifyEvent;\r
11 import org.eclipse.swt.events.ModifyListener;\r
12 import org.eclipse.swt.events.SelectionAdapter;\r
13 import org.eclipse.swt.events.SelectionEvent;\r
14 import org.eclipse.swt.layout.GridData;\r
15 import org.eclipse.swt.layout.GridLayout;\r
16 import org.eclipse.swt.widgets.Button;\r
17 import org.eclipse.swt.widgets.Composite;\r
18 import org.eclipse.swt.widgets.FileDialog;\r
19 import org.eclipse.swt.widgets.Label;\r
20 import org.eclipse.swt.widgets.Shell;\r
21 import org.eclipse.swt.widgets.Text;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.WriteGraph;\r
24 import org.simantics.db.common.request.WriteRequest;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.sysdyn.mdlImport.MdlParser;\r
27 import org.simantics.sysdyn.mdlImport.mdlElements.Model;\r
28 import org.simantics.ui.SimanticsUI;\r
29 \r
30 public class WizardMdlImportPage extends WizardPage{\r
31         \r
32         // dialog store id constants\r
33         private Text filePathField;\r
34         \r
35         // Keep track of the archive that we browsed to last time\r
36         // the wizard was invoked.\r
37         private static String previouslyBrowsedFile = "";\r
38 \r
39         private Button browseDirectoriesButton;\r
40         \r
41         /**\r
42          * Creates a new project creation wizard page.\r
43          * \r
44          */\r
45         public WizardMdlImportPage() {\r
46                 this("wizardMdlImportPage", null, null); //$NON-NLS-1$\r
47         }\r
48 \r
49         /**\r
50          * Create a new instance of the receiver.\r
51          * \r
52          * @param pageName\r
53          */\r
54         public WizardMdlImportPage(String pageName) {\r
55                 this(pageName,null, null);\r
56         }\r
57                         \r
58         /**\r
59          * More (many more) parameters.\r
60          * \r
61          * @param pageName\r
62          * @param initialPath\r
63          * @param currentSelection\r
64          * @since 3.5\r
65          */\r
66         public WizardMdlImportPage(String pageName,String initialPath,\r
67                         IStructuredSelection currentSelection) {\r
68                 super(pageName);\r
69                 setPageComplete(false);\r
70                 setTitle("Import Vensim model");\r
71                 setDescription("Choose the Vensim model file (.mdl), then press Finish.");\r
72         }\r
73 \r
74         public void createControl(Composite parent) {\r
75         \r
76                 initializeDialogUnits(parent);\r
77 \r
78                 Composite workArea = new Composite(parent, SWT.NONE);\r
79                 setControl(workArea);\r
80 \r
81                 workArea.setLayout(new GridLayout());\r
82                 workArea.setLayoutData(new GridData(GridData.FILL_BOTH\r
83                                 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));\r
84 \r
85                 createProjectsRoot(workArea);   \r
86         }\r
87         \r
88         private void createProjectsRoot(Composite workArea) {\r
89 \r
90                 // set label for field\r
91                 Label title = new Label(workArea, SWT.NONE);\r
92                 title.setText("Select Vensim model source:");\r
93                 \r
94                 Composite projectGroup = new Composite(workArea, SWT.NONE);\r
95                 GridLayout layout = new GridLayout();\r
96                 layout.numColumns = 2;\r
97                 layout.makeColumnsEqualWidth = false;\r
98                 layout.marginWidth = 0;\r
99 \r
100                 projectGroup.setLayout(layout);\r
101                 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));\r
102 \r
103                 // model location entry field\r
104                 this.filePathField = new Text(projectGroup, SWT.BORDER);\r
105                 \r
106                 GridData directoryPathData = new GridData(SWT.FILL, SWT.NONE, true, false);\r
107                 directoryPathData.widthHint = new PixelConverter(filePathField).convertWidthInCharsToPixels(25);\r
108                 filePathField.setLayoutData(directoryPathData);\r
109                 filePathField.addModifyListener(new ModifyListener(){\r
110             @Override\r
111             public void modifyText(ModifyEvent e) {\r
112                 previouslyBrowsedFile = filePathField.getText();        \r
113             }\r
114                 });\r
115                 if (previouslyBrowsedFile != null){\r
116                         filePathField.setText(previouslyBrowsedFile);\r
117                         validatePage();\r
118                 }\r
119                 \r
120                 // browse button\r
121                 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);\r
122                 browseDirectoriesButton.setText("Browse");\r
123                 setButtonLayoutData(browseDirectoriesButton);\r
124                 \r
125                 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {\r
126                         /*\r
127                          * (non-Javadoc)\r
128                          * \r
129                          * @see org.eclipse.swt.events.SelectionAdapter#widgetS\r
130                          * elected(org.eclipse.swt.events.SelectionEvent)\r
131                          */\r
132                         public void widgetSelected(SelectionEvent e) {\r
133                                 handleLocationDirectoryButtonPressed();\r
134                         }\r
135                 });\r
136                 \r
137         }\r
138         \r
139         //Set filePathField active\r
140         public void setVisible(boolean visible) {\r
141                 super.setVisible(visible);\r
142                 this.filePathField.setFocus();\r
143         }\r
144         \r
145         //Open dialog for choosing the file\r
146         protected void handleLocationDirectoryButtonPressed() {\r
147                 \r
148                 final Shell shell = filePathField.getShell();\r
149 \r
150                 FileDialog dialog = new FileDialog(shell, SWT.OPEN);\r
151                 String[] ext = {"*.mdl"};\r
152                 dialog.setFilterExtensions(ext);\r
153                 dialog.setText("Import Vensim model (.mdl)");\r
154 \r
155                 String dirName = filePathField.getText().trim();\r
156                 \r
157                 File path = new File(dirName);\r
158                 if (path.exists()) {\r
159                         dialog.setFilterPath(new Path(dirName).toOSString());   \r
160                 }\r
161                 \r
162                 String selectedFile = dialog.open();\r
163                 if (selectedFile != null) {\r
164                         filePathField.setText(selectedFile);\r
165                         validatePage();\r
166                 }\r
167 \r
168         }\r
169         \r
170         //Create project after finish is pressed.\r
171         public boolean createProjects() {\r
172                 \r
173                 final Resource project = SimanticsUI.getProject().get();\r
174                 if(project == null) return false;\r
175 \r
176                 String selected = previouslyBrowsedFile;\r
177                 if(selected == null) return false;\r
178                 \r
179                 File file = new File(selected);\r
180                 \r
181                 final Model model = MdlParser.parse(file);\r
182                 \r
183                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
184                         \r
185                         @Override\r
186                         public void perform(WriteGraph graph) throws DatabaseException {\r
187                                 model.write(graph, project);\r
188                         }\r
189                 });\r
190                 \r
191                 return true;\r
192                 \r
193         }\r
194     void validatePage(){\r
195         \r
196                 if (previouslyBrowsedFile.isEmpty()){\r
197                         setPageComplete(false);\r
198                         return;\r
199                 }\r
200                 \r
201                 setPageComplete(true);\r
202     }\r
203 }\r
204