Skip to content

Commit 1b3d3f4

Browse files
committed
Restructured properties string to class
1 parent 48d3938 commit 1b3d3f4

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data.code;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import org.apache.commons.lang.StringUtils;
11+
12+
public class ClassPropertyData {
13+
private final List<String> data = new ArrayList<>();
14+
15+
public ClassPropertyData(
16+
final String type,
17+
final String lowerCamelName,
18+
final String upperCamelName,
19+
final String lowerSnakeName,
20+
final String upperSnakeName
21+
) {
22+
data.add(upperSnakeName);
23+
data.add(lowerSnakeName);
24+
data.add(type);
25+
data.add(upperCamelName);
26+
data.add(lowerCamelName);
27+
}
28+
29+
public String string() {
30+
return StringUtils.join(data, ";");
31+
}
32+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<properties/>
5454
<border type="none"/>
5555
<children>
56-
<component id="df4c9" class="javax.swing.JTable" binding="properties">
56+
<component id="df4c9" class="javax.swing.JTable" binding="propertyTable">
5757
<constraints/>
5858
<properties>
5959
<intercellSpacing width="1" height="3"/>

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
1515
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
1616
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
17+
import com.magento.idea.magento2plugin.actions.generation.data.code.ClassPropertyData;
1718
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
1819
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
1920
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
@@ -37,6 +38,7 @@
3738
import java.awt.event.WindowAdapter;
3839
import java.awt.event.WindowEvent;
3940
import java.util.ArrayList;
41+
import java.util.List;
4042
import javax.swing.JButton;
4143
import javax.swing.JCheckBox;
4244
import javax.swing.JComponent;
@@ -58,9 +60,9 @@ public class NewDataModelDialog extends AbstractDialog {
5860
private final String moduleName;
5961
private final ValidatorBundle validatorBundle;
6062
private final CommonBundle commonBundle;
63+
private final List<String> properties;
6164
private NamespaceBuilder interfaceNamespace;
6265
private NamespaceBuilder modelNamespace;
63-
private String formattedProperties;
6466

6567
private static final String MODEL_NAME = "Model Name";
6668
private static final String PROPERTY_NAME = "Name";
@@ -73,7 +75,7 @@ public class NewDataModelDialog extends AbstractDialog {
7375
private JPanel contentPanel;
7476
private JButton buttonOK;
7577
private JButton buttonCancel;
76-
private JTable properties;
78+
private JTable propertyTable;
7779
private JButton addProperty;
7880

7981
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
@@ -92,6 +94,7 @@ public NewDataModelDialog(final Project project, final PsiDirectory directory) {
9294
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
9395
this.validatorBundle = new ValidatorBundle();
9496
this.commonBundle = new CommonBundle();
97+
this.properties = new ArrayList<>();
9598

9699
setContentPane(contentPanel);
97100
setModal(true);
@@ -148,8 +151,8 @@ protected boolean validateFormFields() {
148151
valid = true;
149152
final String errorTitle = commonBundle.message("common.error");
150153
final int column = 0;
151-
for (int row = 0; row < properties.getRowCount(); row++) {
152-
final String propertyName = ((String) properties.getValueAt(row, column)).trim();
154+
for (int row = 0; row < propertyTable.getRowCount(); row++) {
155+
final String propertyName = ((String) propertyTable.getValueAt(row, column)).trim();
153156
if (propertyName.isEmpty()) {
154157
valid = false;
155158
final String errorMessage = validatorBundle.message(
@@ -227,29 +230,25 @@ private void buildNamespaces() {
227230
}
228231

229232
/**
230-
* Formats properties into a string format, ready for templating.
231-
* "UPPER_SNAKE;lower_snake;type;UpperCamel;lowerCamel".
233+
* Formats properties into an array of ClassPropertyData objects.
232234
*/
233235
private void formatProperties() {
234236
final DefaultTableModel propertiesTable = getPropertiesTable();
235-
final ArrayList<String> properties = new ArrayList<>();
236-
final ArrayList<String> propertyData = new ArrayList<>();
237237
final int rowCount = propertiesTable.getRowCount();
238238
String name;
239239
String type;
240240

241-
for (int index = 0; index < rowCount; index++, propertyData.clear()) {
241+
for (int index = 0; index < rowCount; index++) {
242242
name = propertiesTable.getValueAt(index, 0).toString();
243243
type = propertiesTable.getValueAt(index, 1).toString();
244-
propertyData.add(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, name));
245-
propertyData.add(name);
246-
propertyData.add(type);
247-
propertyData.add(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name));
248-
propertyData.add(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name));
249-
properties.add(StringUtils.join(propertyData, ";"));
244+
properties.add((new ClassPropertyData(
245+
type,
246+
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name),
247+
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name),
248+
name,
249+
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, name)
250+
)).string());
250251
}
251-
252-
formattedProperties = StringUtils.join(properties, ",");
253252
}
254253

255254
private String getModuleName() {
@@ -280,8 +279,12 @@ private String getModelFQN() {
280279
return modelNamespace.getClassFqn();
281280
}
282281

282+
/**
283+
* Gets properties as a string, ready for templating.
284+
* "UPPER_SNAKE;lower_snake;type;UpperCamel;lowerCamel".
285+
*/
283286
private String getProperties() {
284-
return formattedProperties;
287+
return StringUtils.join(properties, ",");
285288
}
286289

287290
private void initPropertiesTable() {
@@ -295,7 +298,7 @@ private void initPropertiesTable() {
295298
}
296299
);
297300

298-
final TableColumn column = properties.getColumn(PROPERTY_ACTION);
301+
final TableColumn column = propertyTable.getColumn(PROPERTY_ACTION);
299302
column.setCellRenderer(new TableButton(PROPERTY_DELETE));
300303
column.setCellEditor(new DeleteRowButton(new JCheckBox()));
301304

@@ -311,12 +314,12 @@ private void initPropertiesTable() {
311314
}
312315

313316
private void initPropertyTypeColumn() {
314-
final TableColumn formElementTypeColumn = properties.getColumn(PROPERTY_TYPE);
317+
final TableColumn formElementTypeColumn = propertyTable.getColumn(PROPERTY_TYPE);
315318
formElementTypeColumn.setCellEditor(new ComboBoxEditor(PROPERTY_TYPES));
316319
formElementTypeColumn.setCellRenderer(new ComboBoxTableRenderer<>(PROPERTY_TYPES));
317320
}
318321

319322
private DefaultTableModel getPropertiesTable() {
320-
return (DefaultTableModel) properties.getModel();
323+
return (DefaultTableModel) propertyTable.getModel();
321324
}
322325
}

0 commit comments

Comments
 (0)