1 package org.simantics.sysdyn.ui.wizards.mdl;
\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
30 public class WizardMdlImportPage extends WizardPage{
\r
32 // dialog store id constants
\r
33 private Text filePathField;
\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
39 private Button browseDirectoriesButton;
\r
42 * Creates a new project creation wizard page.
\r
45 public WizardMdlImportPage() {
\r
46 this("wizardMdlImportPage", null, null); //$NON-NLS-1$
\r
50 * Create a new instance of the receiver.
\r
54 public WizardMdlImportPage(String pageName) {
\r
55 this(pageName,null, null);
\r
59 * More (many more) parameters.
\r
62 * @param initialPath
\r
63 * @param currentSelection
\r
66 public WizardMdlImportPage(String pageName,String initialPath,
\r
67 IStructuredSelection currentSelection) {
\r
69 setPageComplete(false);
\r
70 setTitle("Import Vensim model");
\r
71 setDescription("Choose the Vensim model file (.mdl), then press Finish.");
\r
74 public void createControl(Composite parent) {
\r
76 initializeDialogUnits(parent);
\r
78 Composite workArea = new Composite(parent, SWT.NONE);
\r
79 setControl(workArea);
\r
81 workArea.setLayout(new GridLayout());
\r
82 workArea.setLayoutData(new GridData(GridData.FILL_BOTH
\r
83 | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
\r
85 createProjectsRoot(workArea);
\r
88 private void createProjectsRoot(Composite workArea) {
\r
90 // set label for field
\r
91 Label title = new Label(workArea, SWT.NONE);
\r
92 title.setText("Select Vensim model source:");
\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
100 projectGroup.setLayout(layout);
\r
101 projectGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
\r
103 // model location entry field
\r
104 this.filePathField = new Text(projectGroup, SWT.BORDER);
\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
111 public void modifyText(ModifyEvent e) {
\r
112 previouslyBrowsedFile = filePathField.getText();
\r
115 if (previouslyBrowsedFile != null){
\r
116 filePathField.setText(previouslyBrowsedFile);
\r
121 browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
\r
122 browseDirectoriesButton.setText("Browse");
\r
123 setButtonLayoutData(browseDirectoriesButton);
\r
125 browseDirectoriesButton.addSelectionListener(new SelectionAdapter() {
\r
129 * @see org.eclipse.swt.events.SelectionAdapter#widgetS
\r
130 * elected(org.eclipse.swt.events.SelectionEvent)
\r
132 public void widgetSelected(SelectionEvent e) {
\r
133 handleLocationDirectoryButtonPressed();
\r
139 //Set filePathField active
\r
140 public void setVisible(boolean visible) {
\r
141 super.setVisible(visible);
\r
142 this.filePathField.setFocus();
\r
145 //Open dialog for choosing the file
\r
146 protected void handleLocationDirectoryButtonPressed() {
\r
148 final Shell shell = filePathField.getShell();
\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
155 String dirName = filePathField.getText().trim();
\r
157 File path = new File(dirName);
\r
158 if (path.exists()) {
\r
159 dialog.setFilterPath(new Path(dirName).toOSString());
\r
162 String selectedFile = dialog.open();
\r
163 if (selectedFile != null) {
\r
164 filePathField.setText(selectedFile);
\r
170 //Create project after finish is pressed.
\r
171 public boolean createProjects() {
\r
173 final Resource project = SimanticsUI.getProject().get();
\r
174 if(project == null) return false;
\r
176 String selected = previouslyBrowsedFile;
\r
177 if(selected == null) return false;
\r
179 File file = new File(selected);
\r
181 final Model model = MdlParser.parse(file);
\r
183 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
186 public void perform(WriteGraph graph) throws DatabaseException {
\r
187 model.write(graph, project);
\r
194 void validatePage(){
\r
196 if (previouslyBrowsedFile.isEmpty()){
\r
197 setPageComplete(false);
\r
201 setPageComplete(true);
\r