Browse Source

save/load options

tags/v1.1.0
Robin Thoni 7 years ago
parent
commit
12d9a7b3b8

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="" vcs="Git" />
5
+  </component>
6
+</project>

+ 1
- 0
code-from-ds.iml View File

@@ -9,5 +9,6 @@
9 9
     </content>
10 10
     <orderEntry type="inheritedJdk" />
11 11
     <orderEntry type="sourceFolder" forTests="false" />
12
+    <orderEntry type="library" name="json-20160810" level="project" />
12 13
   </component>
13 14
 </module>

+ 47
- 0
src/com/rthoni/intellij/codefromds/business/Generator.java View File

@@ -0,0 +1,47 @@
1
+package com.rthoni.intellij.codefromds.business;
2
+
3
+import com.intellij.database.psi.DbDataSource;
4
+import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
5
+import org.json.JSONObject;
6
+
7
+import java.io.FileOutputStream;
8
+import java.nio.charset.StandardCharsets;
9
+import java.nio.file.Files;
10
+import java.nio.file.Paths;
11
+import java.util.HashMap;
12
+
13
+/**
14
+ * Created by robin on 11/15/16.
15
+ */
16
+public abstract class Generator {
17
+
18
+    public static void saveOptions(GenerateOptions options) throws Exception {
19
+        HashMap<String, Object> map = options.toMap();
20
+        JSONObject obj = new JSONObject(map);
21
+        FileOutputStream file = new FileOutputStream(options.getConfigPath());
22
+        file.write(obj.toString(4).getBytes());
23
+        file.close();
24
+    }
25
+
26
+    public static GenerateOptions loadOptions(String configPath) throws Exception {
27
+        String data = Files.readAllLines(Paths.get(configPath), StandardCharsets.UTF_8).stream().reduce("", (s1, s2) -> s1 + s2);
28
+
29
+        JSONObject obj = new JSONObject(data);
30
+        String src = obj.getJSONObject("selection").getString("source");
31
+
32
+        DbDataSource dataSource = Helper.findDataSource(src);
33
+        if (dataSource == null) {
34
+            throw new Exception("Data source " + src + " not found");
35
+        }
36
+
37
+        GenerateOptions options = new GenerateOptions(dataSource);
38
+        options.setConfigPath(configPath);
39
+        options.fromJson(obj);
40
+        return options;
41
+    }
42
+
43
+    public static void generate(GenerateOptions options)
44
+    {
45
+
46
+    }
47
+}

+ 58
- 0
src/com/rthoni/intellij/codefromds/business/Helper.java View File

@@ -0,0 +1,58 @@
1
+package com.rthoni.intellij.codefromds.business;
2
+
3
+import com.intellij.database.psi.DbDataSource;
4
+import com.intellij.database.psi.DbPsiFacade;
5
+import com.intellij.openapi.project.Project;
6
+import com.intellij.openapi.project.ProjectManager;
7
+import com.rthoni.intellij.codefromds.dbo.TableSelection;
8
+import org.json.JSONArray;
9
+import org.json.JSONObject;
10
+
11
+import java.util.Arrays;
12
+import java.util.Collection;
13
+import java.util.List;
14
+import java.util.Optional;
15
+import java.util.stream.Collectors;
16
+
17
+/**
18
+ * Created by robin on 11/15/16.
19
+ */
20
+public abstract class Helper {
21
+
22
+    public static List<DbDataSource> getDataSources()
23
+    {
24
+        ProjectManager pm = ProjectManager.getInstance();
25
+        Project[] projects = pm.getOpenProjects();
26
+        return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
27
+                .flatMap(Collection::stream).collect(Collectors.toList());
28
+    }
29
+
30
+    public static DbDataSource findDataSource(String name)
31
+    {
32
+        Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
33
+        if (opt.isPresent()) {
34
+            return opt.get();
35
+        }
36
+        return null;
37
+    }
38
+
39
+    public static JSONObject findTableInJson(JSONArray tables, String name)
40
+    {
41
+        for (int i = 0; i < tables.length(); ++i) {
42
+            if (tables.getJSONObject(i).getString("table").equals(name)) {
43
+                return tables.getJSONObject(i);
44
+            }
45
+        }
46
+        return null;
47
+    }
48
+
49
+    public static JSONObject findColumnInJson(JSONArray tables, String name)
50
+    {
51
+        for (int i = 0; i < tables.length(); ++i) {
52
+            if (tables.getJSONObject(i).getString("column").equals(name)) {
53
+                return tables.getJSONObject(i);
54
+            }
55
+        }
56
+        return null;
57
+    }
58
+}

+ 18
- 0
src/com/rthoni/intellij/codefromds/dbo/ColumnSelection.java View File

@@ -1,6 +1,9 @@
1 1
 package com.rthoni.intellij.codefromds.dbo;
2 2
 
3 3
 import com.intellij.database.model.DasColumn;
4
+import org.json.JSONObject;
5
+
6
+import java.util.HashMap;
4 7
 
5 8
 /**
6 9
  * Created by robin on 11/15/16.
@@ -20,6 +23,21 @@ public class ColumnSelection {
20 23
 
21 24
     private boolean _selected;
22 25
 
26
+    public HashMap<String, Object> toMap()
27
+    {
28
+        HashMap<String, Object> map = new HashMap<>();
29
+
30
+        map.put("column", _column == null ? null : _column.getName());
31
+        map.put("selected", _selected);
32
+
33
+        return map;
34
+    }
35
+
36
+    public void fromJson(JSONObject json)
37
+    {
38
+        _selected = json.getBoolean("selected");
39
+    }
40
+
23 41
     public DasColumn getColumn() {
24 42
         return _column;
25 43
     }

+ 33
- 3
src/com/rthoni/intellij/codefromds/dbo/DataSourceSelection.java View File

@@ -3,7 +3,11 @@ package com.rthoni.intellij.codefromds.dbo;
3 3
 import com.intellij.database.model.DasTable;
4 4
 import com.intellij.database.psi.DbDataSource;
5 5
 import com.intellij.database.util.DasUtil;
6
+import com.rthoni.intellij.codefromds.business.Helper;
7
+import org.json.JSONArray;
8
+import org.json.JSONObject;
6 9
 
10
+import java.util.HashMap;
7 11
 import java.util.List;
8 12
 import java.util.Vector;
9 13
 import java.util.stream.Collectors;
@@ -13,9 +17,6 @@ import java.util.stream.Collectors;
13 17
  */
14 18
 public class DataSourceSelection {
15 19
 
16
-    public DataSourceSelection() {
17
-    }
18
-
19 20
     public DataSourceSelection(DbDataSource source) {
20 21
         _source = source;
21 22
         final List<? extends DasTable> tables = DasUtil.getTables(source).toList();
@@ -32,6 +33,35 @@ public class DataSourceSelection {
32 33
 
33 34
     private List<TableSelection> _tables;
34 35
 
36
+    public HashMap<String, Object> toMap()
37
+    {
38
+        HashMap<String, Object> map = new HashMap<>();
39
+
40
+        map.put("source", _source == null ? null : _source.getName());
41
+        List<Object> tables = new Vector<>();
42
+        for (TableSelection table : _tables) {
43
+            tables.add(table.toMap());
44
+        }
45
+        map.put("tables", tables);
46
+
47
+        return map;
48
+    }
49
+
50
+    public void fromJson(JSONObject json)
51
+    {
52
+        JSONArray array = json.getJSONArray("tables");
53
+        for (TableSelection table : _tables) {
54
+            JSONObject obj = Helper.findTableInJson(array, table.getTable().getName());
55
+            if (obj != null) {
56
+                table.fromJson(obj);
57
+            } else {
58
+                for (ColumnSelection column : table.getColumns()) {
59
+                    column.setSelected(false);
60
+                }
61
+            }
62
+        }
63
+    }
64
+
35 65
     public DbDataSource getSource() {
36 66
         return _source;
37 67
     }

+ 22
- 3
src/com/rthoni/intellij/codefromds/dbo/GenerateOptions.java View File

@@ -3,8 +3,10 @@ package com.rthoni.intellij.codefromds.dbo;
3 3
 import com.intellij.database.model.DasTable;
4 4
 import com.intellij.database.psi.DbDataSource;
5 5
 import com.intellij.database.util.DasUtil;
6
+import org.json.JSONObject;
6 7
 
7 8
 import java.io.File;
9
+import java.util.HashMap;
8 10
 import java.util.List;
9 11
 import java.util.Vector;
10 12
 import java.util.stream.Collectors;
@@ -14,9 +16,6 @@ import java.util.stream.Collectors;
14 16
  */
15 17
 public class GenerateOptions {
16 18
 
17
-    public GenerateOptions() {
18
-    }
19
-
20 19
     public GenerateOptions(DbDataSource source) {
21 20
         _modelsPath = source.getProject().getBasePath() + File.separator + "Models";
22 21
         _dataSourceTemplatePath = source.getProject().getBasePath() + File.separator + "Templates" + File.separator + "DataSource.twig";
@@ -50,6 +49,26 @@ public class GenerateOptions {
50 49
 
51 50
     private String _configPath;
52 51
 
52
+    public HashMap<String, Object> toMap()
53
+    {
54
+        HashMap<String, Object> map = new HashMap<>();
55
+
56
+        map.put("modelsPath", _modelsPath);
57
+        map.put("dataSourceTemplatePath", _dataSourceTemplatePath);
58
+        map.put("modelsTemplatePath", _modelsTemplatePath);
59
+        map.put("selection", _selection == null ? null : new JSONObject(_selection.toMap()));
60
+
61
+        return map;
62
+    }
63
+
64
+    public void fromJson(JSONObject json)
65
+    {
66
+        _modelsPath = json.getString("modelsPath");
67
+        _dataSourceTemplatePath = json.getString("dataSourceTemplatePath");
68
+        _modelsTemplatePath = json.getString("modelsTemplatePath");
69
+        _selection.fromJson(json.getJSONObject("selection"));
70
+    }
71
+
53 72
     public DataSourceSelection getSelection() {
54 73
         return _selection;
55 74
     }

+ 32
- 0
src/com/rthoni/intellij/codefromds/dbo/TableSelection.java View File

@@ -1,8 +1,13 @@
1 1
 package com.rthoni.intellij.codefromds.dbo;
2 2
 
3 3
 import com.intellij.database.model.DasTable;
4
+import com.rthoni.intellij.codefromds.business.Helper;
5
+import org.json.JSONArray;
6
+import org.json.JSONObject;
4 7
 
8
+import java.util.HashMap;
5 9
 import java.util.List;
10
+import java.util.Vector;
6 11
 
7 12
 /**
8 13
  * Created by robin on 11/15/16.
@@ -13,6 +18,33 @@ public class TableSelection {
13 18
 
14 19
     private List<ColumnSelection> _columns;
15 20
 
21
+    public HashMap<String, Object> toMap()
22
+    {
23
+        HashMap<String, Object> map = new HashMap<>();
24
+
25
+        map.put("table", _table == null ? null : _table.getName());
26
+        List<Object> tables = new Vector<>();
27
+        for (ColumnSelection col : _columns) {
28
+            tables.add(col.toMap());
29
+        }
30
+        map.put("columns", tables);
31
+
32
+        return map;
33
+    }
34
+
35
+    public void fromJson(JSONObject json)
36
+    {
37
+        JSONArray array = json.getJSONArray("columns");
38
+        for (ColumnSelection column : _columns) {
39
+            JSONObject obj = Helper.findColumnInJson(array, column.getColumn().getName());
40
+            if (obj != null) {
41
+                column.fromJson(obj);
42
+            } else {
43
+                column.setSelected(false);
44
+            }
45
+        }
46
+    }
47
+
16 48
     public DasTable getTable() {
17 49
         return _table;
18 50
     }

+ 18
- 3
src/com/rthoni/intellij/codefromds/ui/actions/GenerateAction.java View File

@@ -8,6 +8,7 @@ import com.intellij.openapi.project.Project;
8 8
 import com.intellij.openapi.project.ProjectManager;
9 9
 import com.intellij.openapi.ui.DialogWrapper;
10 10
 import com.intellij.openapi.vfs.VirtualFile;
11
+import com.rthoni.intellij.codefromds.business.Generator;
11 12
 import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
12 13
 import com.rthoni.intellij.codefromds.ui.dialogs.GenerateDialog;
13 14
 
@@ -21,13 +22,27 @@ public class GenerateAction extends AnAction {
21 22
     @Override
22 23
     public void actionPerformed(AnActionEvent e) {
23 24
         GenerateDialog dlg = new GenerateDialog(null);
25
+        if (!e.getPlace().equals("MainMenu")) {
26
+            final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
27
+            if (file != null) {
28
+                try {
29
+                    GenerateOptions options = Generator.loadOptions(file.getPath());
30
+                    dlg.setOptions(options);
31
+                } catch (Exception e1) {
32
+                    e1.printStackTrace();
33
+                }
34
+            }
35
+        }
24 36
         dlg.show();
25 37
         int res = dlg.getExitCode();
26 38
         if (res == DialogWrapper.OK_EXIT_CODE) {
27 39
             GenerateOptions options = dlg.getOptions();
28
-            dlg = new GenerateDialog(null);
29
-            dlg.setOptions(options);
30
-            dlg.show();
40
+            try {
41
+                Generator.saveOptions(options);
42
+            } catch (Exception e1) {
43
+                e1.printStackTrace();
44
+            }
45
+            Generator.generate(options);
31 46
         }
32 47
     }
33 48
 

+ 53
- 35
src/com/rthoni/intellij/codefromds/ui/dialogs/GenerateDialog.java View File

@@ -15,6 +15,7 @@ import com.intellij.openapi.ui.ValidationInfo;
15 15
 import com.intellij.ui.ColoredListCellRenderer;
16 16
 import com.intellij.ui.JBColor;
17 17
 import com.intellij.ui.components.JBList;
18
+import com.rthoni.intellij.codefromds.business.Helper;
18 19
 import com.rthoni.intellij.codefromds.dbo.ColumnSelection;
19 20
 import com.rthoni.intellij.codefromds.dbo.DataSourceSelection;
20 21
 import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
@@ -32,6 +33,7 @@ import java.util.Arrays;
32 33
 import java.util.Collection;
33 34
 import java.util.List;
34 35
 import java.util.Vector;
36
+import java.util.function.Consumer;
35 37
 import java.util.stream.Collectors;
36 38
 
37 39
 /**
@@ -53,7 +55,7 @@ public class GenerateDialog extends DialogWrapper {
53 55
     public GenerateDialog(@Nullable Project project) {
54 56
         super(project);
55 57
         setTitle("Code FROM data source");
56
-        showSource(null);
58
+        setOptions(null);
57 59
         init();
58 60
     }
59 61
 
@@ -65,11 +67,6 @@ public class GenerateDialog extends DialogWrapper {
65 67
         _options = options;
66 68
         if (_options != null) {
67 69
             showSource(_options.getSelection());
68
-            Project project = _options.getSelection().getSource().getProject();
69
-            setupTextField(_textModels, project);
70
-            setupTextField(_textDataSOurceTemplate, project);
71
-            setupTextField(_textModelsTemplate, project);
72
-            setupTextField(_textConfigPath, project);
73 70
             _textModels.setText(_options.getModelsPath());
74 71
             _textDataSOurceTemplate.setText(_options.getDataSourceTemplatePath());
75 72
             _textModelsTemplate.setText(_options.getModelsTemplatePath());
@@ -77,10 +74,6 @@ public class GenerateDialog extends DialogWrapper {
77 74
         }
78 75
         else {
79 76
             showSource(null);
80
-            setupTextField(_textModels, null);
81
-            setupTextField(_textDataSOurceTemplate, null);
82
-            setupTextField(_textModelsTemplate, null);
83
-            setupTextField(_textConfigPath, null);
84 77
             _textModels.setText("");
85 78
             _textDataSOurceTemplate.setText("");
86 79
             _textModelsTemplate.setText("");
@@ -93,11 +86,24 @@ public class GenerateDialog extends DialogWrapper {
93 86
     protected ValidationInfo doValidate() {
94 87
         ValidationInfo info = null;
95 88
         File modelDir = _options == null ? null : new File(_options.getModelsPath());
89
+        File dataSourceTemplatePath = _options == null ? null : new File(_options.getDataSourceTemplatePath());
90
+        File modelsTemplatePath = _options == null ? null : new File(_options.getModelsTemplatePath());
91
+        File configPath = _options == null ? null : new File(_options.getConfigPath());
92
+        File configDir = _options == null ? null : new File(configPath.getParent());
96 93
         if (_options == null) {
97 94
             info = new ValidationInfo("No Data Source Selected", _listDatasources);
98 95
         }
99 96
         else if (!modelDir.exists() || !modelDir.isDirectory()) {
100
-            info = new ValidationInfo("Bad Model Folder", _textModels.getTextField());
97
+            info = new ValidationInfo("Models folder does not exists", _textModels.getTextField());
98
+        }
99
+        else if (!dataSourceTemplatePath.exists() || !dataSourceTemplatePath.isFile()) {
100
+            info = new ValidationInfo("Data source template file does not exists", _textDataSOurceTemplate.getTextField());
101
+        }
102
+        else if (!modelsTemplatePath.exists() || !modelsTemplatePath.isFile()) {
103
+            info = new ValidationInfo("Models template file does not exists", _textModelsTemplate.getTextField());
104
+        }
105
+        else if (!configDir.exists() || !configDir.isDirectory()) {
106
+            info = new ValidationInfo("Configuration file parent folder does not exists", _textConfigPath.getTextField());
101 107
         }
102 108
 
103 109
         return info;
@@ -106,10 +112,8 @@ public class GenerateDialog extends DialogWrapper {
106 112
     @Nullable
107 113
     @Override
108 114
     protected JComponent createCenterPanel() {
109
-        ProjectManager pm = ProjectManager.getInstance();
110
-        Project[] projects = pm.getOpenProjects();
111 115
 
112
-        List<DbDataSource> dataSources = Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources()).flatMap(Collection::stream).collect(Collectors.toList());
116
+        List<DbDataSource> dataSources = Helper.getDataSources();
113 117
 
114 118
         _listDatasources.setListData(dataSources.stream().map(DasObject::getName).toArray(String[]::new));
115 119
         _listDatasources.addListSelectionListener(e -> {
@@ -134,26 +138,16 @@ public class GenerateDialog extends DialogWrapper {
134 138
                 }
135 139
             }
136 140
         });
137
-        _textModels.getTextField().getDocument().addDocumentListener(new DocumentListener() {
138
-            @Override
139
-            public void insertUpdate(DocumentEvent e) {
140
-                changed();
141
-            }
142
-
143
-            @Override
144
-            public void removeUpdate(DocumentEvent e) {
145
-                changed();
146
-            }
147 141
 
148
-            @Override
149
-            public void changedUpdate(DocumentEvent e) {
150
-                changed();
151
-            }
142
+        setupTextField(_textModels, null, true);
143
+        setupTextField(_textDataSOurceTemplate, null, false);
144
+        setupTextField(_textModelsTemplate, null, false);
145
+        setupTextField(_textConfigPath, null, false);
152 146
 
153
-            public void changed() {
154
-                _options.setModelsPath(_textModels.getText());
155
-            }
156
-        });
147
+        setupTextFieldListener(_textModels.getTextField(), s -> _options.setModelsPath(s));
148
+        setupTextFieldListener(_textDataSOurceTemplate.getTextField(), s -> _options.setDataSourceTemplatePath(s));
149
+        setupTextFieldListener(_textModelsTemplate.getTextField(), s -> _options.setModelsTemplatePath(s));
150
+        setupTextFieldListener(_textConfigPath.getTextField(), s -> _options.setConfigPath(s));
157 151
 
158 152
         _listTables.setCellRenderer(new ColoredListCellRenderer() {
159 153
             @Override
@@ -179,13 +173,37 @@ public class GenerateDialog extends DialogWrapper {
179 173
         return _panel;
180 174
     }
181 175
 
182
-    private void setupTextField(TextFieldWithBrowseButton field, Project project)
176
+    private void setupTextFieldListener(JTextField field, Consumer<String> consumer)
177
+    {
178
+        field.getDocument().addDocumentListener(new DocumentListener() {
179
+            @Override
180
+            public void insertUpdate(DocumentEvent e) {
181
+                changed();
182
+            }
183
+
184
+            @Override
185
+            public void removeUpdate(DocumentEvent e) {
186
+                changed();
187
+            }
188
+
189
+            @Override
190
+            public void changedUpdate(DocumentEvent e) {
191
+                changed();
192
+            }
193
+
194
+            public void changed() {
195
+                consumer.accept(field.getText());
196
+            }
197
+        });
198
+    }
199
+
200
+    private void setupTextField(TextFieldWithBrowseButton field, Project project, boolean dirsOnly)
183 201
     {
184 202
         for (ActionListener l : field.getButton().getActionListeners()) {
185 203
             field.getButton().removeActionListener(l);
186 204
         }
187
-        field.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder",
188
-                project, FileChooserDescriptorFactory.createSingleFolderDescriptor());
205
+        field.addBrowseFolderListener("Choose Models Destination Folder", "Choose folder", project,
206
+                dirsOnly ? FileChooserDescriptorFactory.createSingleFolderDescriptor() : FileChooserDescriptorFactory.createSingleFileDescriptor());
189 207
     }
190 208
 
191 209
     private void showSource(DataSourceSelection source)

Loading…
Cancel
Save