forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewDataModelDialog.java
318 lines (282 loc) · 11 KB
/
NewDataModelDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBoxTableRenderer;
import com.intellij.psi.PsiDirectory;
import com.magento.idea.magento2plugin.actions.generation.NewDataModelAction;
import com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction;
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
import com.magento.idea.magento2plugin.actions.generation.dialog.util.ClassPropertyFormatterUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule;
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
import com.magento.idea.magento2plugin.bundles.CommonBundle;
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
import com.magento.idea.magento2plugin.magento.files.DataModelFile;
import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile;
import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes;
import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor;
import com.magento.idea.magento2plugin.ui.table.DeleteRowButton;
import com.magento.idea.magento2plugin.ui.table.TableButton;
import com.magento.idea.magento2plugin.util.RegExUtil;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
@SuppressWarnings({
"PMD.ExcessiveImports"
})
public class NewDataModelDialog extends AbstractDialog {
private final Project project;
private final String moduleName;
private final ValidatorBundle validatorBundle;
private final CommonBundle commonBundle;
private final List<String> properties;
private static final String MODEL_NAME = "Model Name";
private static final String PROPERTY_NAME = "Name";
private static final String PROPERTY_TYPE = "Type";
private static final String PROPERTY_ACTION = "Action";
private static final String PROPERTY_DELETE = "Delete";
private JPanel contentPanel;
private JButton buttonOK;
private JButton buttonCancel;
private JTable propertyTable;
private JButton addProperty;
private JCheckBox createInterface;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
message = {NotEmptyRule.MESSAGE, MODEL_NAME})
@FieldValidation(rule = RuleRegistry.PHP_CLASS,
message = {PhpClassRule.MESSAGE, MODEL_NAME})
private JTextField modelName;
/**
* Constructor.
*/
public NewDataModelDialog(final Project project, final PsiDirectory directory) {
super();
this.project = project;
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
this.validatorBundle = new ValidatorBundle();
this.commonBundle = new CommonBundle();
this.properties = new ArrayList<>();
setContentPane(contentPanel);
setModal(true);
setTitle(NewDataModelAction.ACTION_DESCRIPTION);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener((final ActionEvent event) -> onOK());
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
// call onCancel() on dialog close
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent event) {
onCancel();
}
});
initPropertiesTable();
// call onCancel() on ESCAPE KEY press
contentPanel.registerKeyboardAction(
(final ActionEvent event) -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
}
/**
* Opens the dialog window.
*/
public static void open(final Project project, final PsiDirectory directory) {
final NewDataModelDialog dialog = new NewDataModelDialog(project, directory);
dialog.pack();
dialog.centerDialog(dialog);
dialog.setVisible(true);
}
/**
* Proceed with generation.
*/
private void onOK() {
if (propertyTable.isEditing()) {
propertyTable.getCellEditor().stopCellEditing();
}
if (validateFormFields()) {
formatProperties();
generateDataModelFile();
if (createInterface.isSelected()) {
generateDataModelInterfaceFile();
generatePreferenceForInterface();
}
this.setVisible(false);
}
}
@Override
protected boolean validateFormFields() {
boolean valid = false;
if (super.validateFormFields()) {
valid = true;
final String errorTitle = commonBundle.message("common.error");
final int column = 0;
for (int row = 0; row < propertyTable.getRowCount(); row++) {
final String propertyName = ((String) propertyTable.getValueAt(row, column)).trim();
if (propertyName.isEmpty()) {
valid = false;
final String errorMessage = validatorBundle.message(
"validator.notEmpty", "name"
);
JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
);
break;
} else if (!propertyName.matches(RegExUtil.LOWER_SNAKE_CASE)) {
valid = false;
final String errorMessage = validatorBundle.message(
"validator.lowerSnakeCase", "name"
);
JOptionPane.showMessageDialog(
null,
errorMessage,
errorTitle,
JOptionPane.ERROR_MESSAGE
);
break;
}
}
}
return valid;
}
@Override
public void onCancel() {
dispose();
}
/**
* Generate DTO interface file.
*/
private void generateDataModelInterfaceFile() {
new DataModelInterfaceGenerator(new DataModelInterfaceData(
getDtoInterfaceName(),
getModuleName(),
ClassPropertyFormatterUtil.joinProperties(properties)
), project).generate(NewDataModelAction.ACTION_NAME, true);
}
/**
* Generate DTO model file.
*/
private void generateDataModelFile() {
new DataModelGenerator(project, new DataModelData(
getDtoModelName(),
getDtoInterfaceName(),
getModuleName(),
ClassPropertyFormatterUtil.joinProperties(properties),
createInterface.isSelected()
)).generate(NewDataModelAction.ACTION_NAME, true);
}
/**
* Generate preference for interface DTO.
*/
private void generatePreferenceForInterface() {
new PreferenceDiXmlGenerator(new PreferenceDiXmFileData(
getModuleName(),
new DataModelInterfaceFile(getModuleName(), getDtoInterfaceName()).getClassFqn(),
new DataModelFile(getModuleName(), getDtoModelName()).getClassFqn(),
"base"
), project).generate(OverrideClassByAPreferenceAction.ACTION_NAME);
}
/**
* Get module name.
*
* @return String
*/
private String getModuleName() {
return moduleName;
}
/**
* Get DTO model name.
*
* @return String
*/
private String getDtoModelName() {
return modelName.getText().trim();
}
/**
* Get DTO interface name.
*
* @return String
*/
private String getDtoInterfaceName() {
return modelName.getText().trim().concat("Interface");
}
/**
* Formats properties into an array of ClassPropertyData objects.
*/
private void formatProperties() {
properties.addAll(ClassPropertyFormatterUtil.formatProperties(getPropertiesTable()));
}
/**
* Initialize properties table.
*/
private void initPropertiesTable() {
final DefaultTableModel propertiesTable = getPropertiesTable();
propertiesTable.setDataVector(
new Object[][]{},
new Object[]{
PROPERTY_NAME,
PROPERTY_TYPE,
PROPERTY_ACTION
}
);
final TableColumn column = propertyTable.getColumn(PROPERTY_ACTION);
column.setCellRenderer(new TableButton(PROPERTY_DELETE));
column.setCellEditor(new DeleteRowButton(new JCheckBox()));
addProperty.addActionListener(e -> {
propertiesTable.addRow(new Object[]{
"",
PropertiesTypes.valueOf(PropertiesTypes.INT.toString())
.getPropertyType(),
PROPERTY_DELETE
});
});
initPropertyTypeColumn();
}
/**
* Initialize property type column.
*/
private void initPropertyTypeColumn() {
final TableColumn formElementTypeColumn = propertyTable.getColumn(PROPERTY_TYPE);
formElementTypeColumn.setCellEditor(
new ComboBoxEditor(PropertiesTypes.getPropertyTypes())
);
formElementTypeColumn.setCellRenderer(
new ComboBoxTableRenderer<>(PropertiesTypes.getPropertyTypes())
);
}
/**
* Get properties table.
*
* @return DefaultTableModel
*/
private DefaultTableModel getPropertiesTable() {
return (DefaultTableModel) propertyTable.getModel();
}
}