Skip to content

Commit 00c9171

Browse files
309: Added columns, pk and pk index generation
1 parent 35d0e0d commit 00c9171

File tree

7 files changed

+323
-199
lines changed

7 files changed

+323
-199
lines changed

src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
99
import java.util.LinkedHashMap;
10+
import java.util.LinkedList;
11+
import java.util.List;
1012
import java.util.Map;
1113

1214
public class DbSchemaXmlData {
1315
private String tableName;
1416
private String tableResource;
1517
private String tableEngine;
1618
private String tableComment;
19+
private List<Map<String, String>> columns;
1720

1821
/**
1922
* Constructor.
@@ -22,17 +25,20 @@ public class DbSchemaXmlData {
2225
* @param tableResource String
2326
* @param tableEngine String
2427
* @param tableComment String
28+
* @param columns List
2529
*/
2630
public DbSchemaXmlData(
27-
final String tableName,
28-
final String tableResource,
29-
final String tableEngine,
30-
final String tableComment
31+
final String tableName,
32+
final String tableResource,
33+
final String tableEngine,
34+
final String tableComment,
35+
final List<Map<String, String>> columns
3136
) {
3237
this.tableName = tableName;
3338
this.tableResource = tableResource;
3439
this.tableEngine = tableEngine;
3540
this.tableComment = tableComment;
41+
this.columns = columns;
3642
}
3743

3844
public String getTableName() {
@@ -67,6 +73,14 @@ public void setTableComment(final String tableComment) {
6773
this.tableComment = tableComment;
6874
}
6975

76+
public List<Map<String, String>> getColumns() {
77+
return new LinkedList<>(columns);
78+
}
79+
80+
public void setColumns(List<Map<String, String>> columns) {
81+
this.columns = columns;
82+
}
83+
7084
/**
7185
* Get table attributes values map.
7286
*

src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public final class DbSchemaXmlSourceData {
3939
public static final String COLUMN_TYPE_LONGTEXT = "longtext";
4040
// boolean
4141
public static final String COLUMN_TYPE_BOOLEAN = "boolean";
42+
// date
43+
public static final String COLUMN_TYPE_DATETIME = "datetime";
44+
public static final String COLUMN_TYPE_DATE = "date";
45+
public static final String COLUMN_TYPE_TIMESTAMP = "timestamp";
4246

4347
/**
4448
* Denying the possibility to initialize this class.
@@ -93,7 +97,10 @@ public static List<String> getColumnTypes() {
9397
COLUMN_TYPE_TEXT,
9498
COLUMN_TYPE_MEDIUMTEXT,
9599
COLUMN_TYPE_LONGTEXT,
96-
COLUMN_TYPE_BOOLEAN
100+
COLUMN_TYPE_BOOLEAN,
101+
COLUMN_TYPE_DATETIME,
102+
COLUMN_TYPE_DATE,
103+
COLUMN_TYPE_TIMESTAMP
97104
));
98105
}
99106
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.magento.idea.magento2plugin.actions.generation.dialog.NewDbSchemaDialog">
3-
<grid id="27dc6" binding="contentPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="27dc6" binding="contentPanel" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
66
<xy x="20" y="20" width="668" height="400"/>
@@ -14,7 +14,7 @@
1414
<grid id="3fa0c" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1515
<margin top="0" left="0" bottom="0" right="0"/>
1616
<constraints>
17-
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
17+
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
1818
</constraints>
1919
<properties/>
2020
<border type="none"/>
@@ -48,7 +48,7 @@
4848
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
4949
</constraints>
5050
</vspacer>
51-
<grid id="a7920" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
51+
<grid id="a7920" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
5252
<margin top="0" left="0" bottom="0" right="0"/>
5353
<constraints>
5454
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -139,15 +139,6 @@
139139
<text value="Columns"/>
140140
</properties>
141141
</component>
142-
<component id="78a96" class="javax.swing.JLabel" binding="tableConstraintsLabel">
143-
<constraints>
144-
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
145-
</constraints>
146-
<properties>
147-
<labelFor value="12ffa"/>
148-
<text value="Constraints"/>
149-
</properties>
150-
</component>
151142
<scrollpane id="d1c53" binding="columnsScrollPanel">
152143
<constraints>
153144
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -180,27 +171,6 @@
180171
</component>
181172
</children>
182173
</grid>
183-
<scrollpane id="12ffa" binding="constraintsScrollPanel">
184-
<constraints>
185-
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
186-
</constraints>
187-
<properties/>
188-
<border type="none"/>
189-
<children>
190-
<component id="43a50" class="javax.swing.JTable" binding="constraintsTable">
191-
<constraints/>
192-
<properties/>
193-
</component>
194-
</children>
195-
</scrollpane>
196-
<component id="c7481" class="javax.swing.JButton" binding="addConstraintButton">
197-
<constraints>
198-
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
199-
</constraints>
200-
<properties>
201-
<text value="Add Constraint"/>
202-
</properties>
203-
</component>
204174
</children>
205175
</grid>
206176
</form>

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java

Lines changed: 44 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
1212
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData;
1313
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
14-
15-
import java.awt.event.ActionEvent;
16-
import java.awt.event.ActionListener;
14+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength;
20+
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
21+
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
22+
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
23+
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
1724
import java.awt.event.KeyEvent;
1825
import java.awt.event.WindowAdapter;
1926
import java.awt.event.WindowEvent;
@@ -22,7 +29,6 @@
2229
import java.util.LinkedList;
2330
import java.util.List;
2431
import java.util.Map;
25-
import javax.swing.AbstractCellEditor;
2632
import javax.swing.JButton;
2733
import javax.swing.JComboBox;
2834
import javax.swing.JComponent;
@@ -32,41 +38,11 @@
3238
import javax.swing.JTable;
3339
import javax.swing.JTextField;
3440
import javax.swing.KeyStroke;
35-
import javax.swing.DefaultCellEditor;
36-
import javax.swing.table.TableCellEditor;
37-
import javax.swing.table.TableColumn;
38-
39-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
40-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
41-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
42-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
43-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
44-
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength;
45-
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
46-
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
47-
import com.magento.idea.magento2plugin.ui.table.ComboBoxCellEditor;
48-
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
49-
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
5041
import org.jetbrains.annotations.NotNull;
5142

5243
public class NewDbSchemaDialog extends AbstractDialog {
5344
private static final String TABLE_NAME = "Table Name";
5445

55-
// Table Columns
56-
private static final String COLUMN_TYPE = "Type";
57-
private static final String COLUMN_NAME = "Name";
58-
private static final String COLUMN_PADDING = "Padding";
59-
private static final String COLUMN_UNSIGNED = "Unsigned";
60-
private static final String COLUMN_NULLABLE = "Nullable";
61-
private static final String COLUMN_IDENTITY = "Identity";
62-
private static final String COLUMN_COMMENT = "Comment";
63-
private static final String COLUMN_LENGTH = "Length";
64-
private static final String COLUMN_PRECISION = "Precision";
65-
private static final String COLUMN_SCALE = "Scale";
66-
private static final String COLUMN_DEFAULT = "Default";
67-
private static final String COLUMN_ON_CREATE = "On Create";
68-
private static final String COLUMN_ON_UPDATE = "On Update";
69-
7046
private final Project project;
7147
private final String moduleName;
7248
private final PsiDirectory directory;
@@ -94,22 +70,17 @@ public class NewDbSchemaDialog extends AbstractDialog {
9470
private JComboBox<ComboBoxItemData> tableResource;
9571

9672
// Table Columns UI components group
73+
private TableGroupWrapper columnsTableGroupWrapper;
9774
private JTable columnsTable;
9875
private JButton addColumnButton;
9976
private JScrollPane columnsScrollPanel;//NOPMD
10077

101-
// Table Constraints UI components group
102-
private JTable constraintsTable;
103-
private JButton addConstraintButton;
104-
private JScrollPane constraintsScrollPanel;//NOPMD
105-
10678
// Labels
10779
private JLabel tableNameLabel;//NOPMD
10880
private JLabel tableEngineLabel;//NOPMD
10981
private JLabel tableResourceLabel;//NOPMD
11082
private JLabel tableCommentLabel;//NOPMD
11183
private JLabel tableColumnsLabel;//NOPMD
112-
private JLabel tableConstraintsLabel;//NOPMD
11384

11485
/**
11586
* Constructor.
@@ -186,7 +157,8 @@ private void generateDbSchemaXmlFile() {
186157
getTableName(),
187158
getTableResource(),
188159
getTableEngine(),
189-
getTableComment()
160+
getTableComment(),
161+
getColumns()
190162
),
191163
project,
192164
moduleName
@@ -195,79 +167,40 @@ private void generateDbSchemaXmlFile() {
195167

196168
private void initializeColumnsUiComponentGroup() {
197169
final List<String> columns = new LinkedList<>(Arrays.asList(
198-
COLUMN_TYPE,
199-
COLUMN_NAME,
200-
COLUMN_PADDING,
201-
COLUMN_UNSIGNED,
202-
COLUMN_NULLABLE,
203-
COLUMN_IDENTITY,
204-
COLUMN_LENGTH,
205-
COLUMN_PRECISION,
206-
COLUMN_SCALE,
207-
COLUMN_DEFAULT,
208-
COLUMN_ON_CREATE,
209-
COLUMN_ON_UPDATE,
210-
COLUMN_COMMENT
170+
ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE,
171+
ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME,
172+
ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING,
173+
ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED,
174+
ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE,
175+
ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY,
176+
ModuleDbSchemaXml.XML_ATTR_COLUMN_LENGTH,
177+
ModuleDbSchemaXml.XML_ATTR_COLUMN_PRECISION,
178+
ModuleDbSchemaXml.XML_ATTR_COLUMN_SCALE,
179+
ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE,
180+
ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT,
181+
ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT
211182
));
212183
// Set default values for columns
213184
final Map<String, String> defaultValues = new HashMap<>();
214-
defaultValues.put(COLUMN_NULLABLE, "false");
185+
defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false");
186+
defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false");
215187
// Set sources for columns
216188
final Map<String, List<String>> sources = new HashMap<>();
217189
final List<String> booleanSource = Arrays.asList("true", "false");
218-
sources.put(COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes());
219-
sources.put(COLUMN_UNSIGNED, booleanSource);
220-
sources.put(COLUMN_NULLABLE, booleanSource);
221-
sources.put(COLUMN_IDENTITY, booleanSource);
222-
sources.put(COLUMN_ON_CREATE, booleanSource);
223-
sources.put(COLUMN_ON_UPDATE, booleanSource);
224-
// Set action listeners for columns
225-
final Map<String, ActionListener> actionListeners = new HashMap<>();
226-
actionListeners.put(COLUMN_TYPE, event -> {
227-
if (event.getActionCommand().equals("comboBoxChanged")) {
228-
if (event.getSource() instanceof JComboBox) {
229-
final JComboBox source = (JComboBox) event.getSource();
230-
final JTable table = (JTable) source.getParent();
231-
final String value = source.getSelectedItem().toString();
232-
233-
if (!value.isEmpty()) {
234-
final int editingRow = ((JTable) source.getParent()).getEditingRow();
235-
// Index should starts from the `1` to not consider xsi:type attribute
236-
for (int index = 1; index < columns.size(); index++) {
237-
TableCellEditor cellEditor = table.getCellEditor(editingRow, index);
238-
JComponent component = null;
239-
240-
if (cellEditor instanceof DefaultCellEditor) {
241-
component = (JComponent) ((DefaultCellEditor) cellEditor)
242-
.getComponent();
243-
} else if (cellEditor instanceof ComboBoxCellEditor) {
244-
component = (JComponent) ((ComboBoxCellEditor) cellEditor)
245-
.getComponent();
246-
}
247-
248-
if (component instanceof JComboBox) {
249-
component.setEnabled(ModuleDbSchemaXml.getAllowedAttributes(value)
250-
.contains(columns.get(index).toLowerCase()));
251-
} else if (component instanceof JTextField) {
252-
((JTextField) component).setEditable(
253-
ModuleDbSchemaXml.getAllowedAttributes(value)
254-
.contains(columns.get(index).toLowerCase())
255-
);
256-
}
257-
}
258-
}
259-
}
260-
}
261-
});
190+
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes());
191+
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, booleanSource);
192+
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, booleanSource);
193+
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, booleanSource);
194+
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE, booleanSource);
262195
// Initialize new Table Group
263-
TableGroupWrapper tableGroupWrapper = new TableGroupWrapper(
196+
columnsTableGroupWrapper = new TableGroupWrapper(
264197
columnsTable,
265198
addColumnButton,
266199
columns,
267200
defaultValues,
268-
sources,
269-
actionListeners
201+
sources
270202
);
203+
columnsTableGroupWrapper.initTableGroup();
271204
}
272205

273206
/**
@@ -319,4 +252,13 @@ private String getTableEngine() {
319252
private String getTableComment() {
320253
return tableComment.getText().trim();
321254
}
255+
256+
/**
257+
* Get columnsTable values.
258+
*
259+
* @return List
260+
*/
261+
private List<Map<String, String>> getColumns() {
262+
return columnsTableGroupWrapper.getColumnsData();
263+
}
322264
}

0 commit comments

Comments
 (0)