1111import com .magento .idea .magento2plugin .actions .generation .data .DbSchemaXmlData ;
1212import com .magento .idea .magento2plugin .actions .generation .data .DbSchemaXmlSourceData ;
1313import 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 ;
1724import java .awt .event .KeyEvent ;
1825import java .awt .event .WindowAdapter ;
1926import java .awt .event .WindowEvent ;
2229import java .util .LinkedList ;
2330import java .util .List ;
2431import java .util .Map ;
25- import javax .swing .AbstractCellEditor ;
2632import javax .swing .JButton ;
2733import javax .swing .JComboBox ;
2834import javax .swing .JComponent ;
3238import javax .swing .JTable ;
3339import javax .swing .JTextField ;
3440import 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 ;
5041import org .jetbrains .annotations .NotNull ;
5142
5243public 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