Skip to content

Commit 2dd86f4

Browse files
Added possibility to set options
1 parent fb65c63 commit 2dd86f4

File tree

14 files changed

+520
-35
lines changed

14 files changed

+520
-35
lines changed

resources/fileTemplates/internal/Magento Eav Attribute Data Patch Class.php.ft

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class ${CLASS_NAME} implements ${IMPLEMENTS} {
8282
'${ATTRIBUTE_CODE}',
8383
[
8484
#set ($attributeSet = ${ATTRIBUTE_SET})
85-
#foreach ($attribute in $attributeSet.split(","))
86-
$attribute,
85+
#foreach ($attribute in $attributeSet.split("->"))
86+
$attribute
8787
#end
8888
]
8989
);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchFile;
99
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
10+
import java.util.Map;
1011

1112
@SuppressWarnings({"PMD.TooManyFields"})
1213
public class ProductEntityData implements EavEntityDataInterface {
@@ -25,6 +26,8 @@ public class ProductEntityData implements EavEntityDataInterface {
2526
private boolean htmlAllowedOnFront;
2627
private boolean visibleOnFront;
2728
private int sortOrder;
29+
private Map<Integer, String> options;
30+
private Map<Integer, String> optionsSortOrder;
2831

2932
private String dataPatchName;
3033
private String namespace;
@@ -121,6 +124,22 @@ public void setModuleName(final String moduleName) {
121124
this.moduleName = moduleName;
122125
}
123126

127+
public void setOptions(final Map<Integer, String> options) {
128+
this.options = options;
129+
}
130+
131+
public void setOptionsSortOrder(final Map<Integer, String> optionsSortOrder) {
132+
this.optionsSortOrder = optionsSortOrder;
133+
}
134+
135+
public Map<Integer, String> getOptions() {
136+
return options;
137+
}
138+
139+
public Map<Integer, String> getOptionsSortOrder() {
140+
return optionsSortOrder;
141+
}
142+
124143
@Override
125144
public String getCode() {
126145
return code;

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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.NewEavAttributeDialog">
3-
<grid id="28350" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<grid id="28350" binding="contentPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="-1" y="5" width="870" height="659"/>
6+
<xy x="-1" y="5" width="870" height="790"/>
77
</constraints>
88
<properties>
99
<enabled value="true"/>
@@ -326,13 +326,13 @@
326326
</grid>
327327
<hspacer id="75d92">
328328
<constraints>
329-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
329+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
330330
</constraints>
331331
</hspacer>
332332
<grid id="181ea" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
333333
<margin top="0" left="0" bottom="0" right="0"/>
334334
<constraints>
335-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
335+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
336336
</constraints>
337337
<properties/>
338338
<border type="none"/>
@@ -371,6 +371,45 @@
371371
</grid>
372372
</children>
373373
</grid>
374+
<grid id="9fd98" binding="optionsPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
375+
<margin top="5" left="5" bottom="5" right="5"/>
376+
<constraints>
377+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
378+
</constraints>
379+
<properties>
380+
<visible value="false"/>
381+
</properties>
382+
<border type="empty" title="Options" title-justification="2" title-position="2"/>
383+
<children>
384+
<scrollpane id="47220">
385+
<constraints>
386+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
387+
</constraints>
388+
<properties>
389+
<enabled value="true"/>
390+
</properties>
391+
<border type="none"/>
392+
<children>
393+
<component id="b0960" class="javax.swing.JTable" binding="optionTable">
394+
<constraints/>
395+
<properties>
396+
<intercellSpacing width="1" height="3"/>
397+
<requestFocusEnabled value="true"/>
398+
<rowHeight value="30"/>
399+
</properties>
400+
</component>
401+
</children>
402+
</scrollpane>
403+
<component id="835cb" class="javax.swing.JButton" binding="addOptionButton">
404+
<constraints>
405+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
406+
</constraints>
407+
<properties>
408+
<text value="Add Option"/>
409+
</properties>
410+
</component>
411+
</children>
412+
</grid>
374413
</children>
375414
</grid>
376415
</form>

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

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,38 @@
1212
import com.magento.idea.magento2plugin.actions.generation.data.ProductEntityData;
1313
import com.magento.idea.magento2plugin.actions.generation.data.SourceModelData;
1414
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
15+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.AttributeSourcePanelComponentListener;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.AttributeSourceRelationsItemListener;
1517
import com.magento.idea.magento2plugin.actions.generation.dialog.event.EavAttributeInputItemListener;
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.OptionsPanelVisibilityChangeListener;
1619
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
1720
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
1821
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
1922
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
2023
import com.magento.idea.magento2plugin.actions.generation.generator.EavAttributeSetupPatchGenerator;
2124
import com.magento.idea.magento2plugin.actions.generation.generator.SourceModelGenerator;
25+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetAttributeOptionPropertiesUtil;
2226
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
2327
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput;
2428
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScope;
2529
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel;
2630
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeType;
2731
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
32+
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
2833
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
29-
import java.awt.event.ItemEvent;
30-
import java.awt.event.ItemListener;
3134
import java.awt.event.KeyEvent;
3235
import java.awt.event.WindowAdapter;
3336
import java.awt.event.WindowEvent;
37+
import java.util.Arrays;
38+
import java.util.HashMap;
39+
import java.util.LinkedList;
40+
import java.util.List;
3441
import javax.swing.JButton;
3542
import javax.swing.JCheckBox;
3643
import javax.swing.JComboBox;
3744
import javax.swing.JComponent;
3845
import javax.swing.JPanel;
46+
import javax.swing.JTable;
3947
import javax.swing.JTextField;
4048
import javax.swing.KeyStroke;
4149
import javax.swing.event.DocumentEvent;
@@ -92,8 +100,12 @@ public class NewEavAttributeDialog extends AbstractDialog {
92100
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
93101
message = {NotEmptyRule.MESSAGE, "Source Model Name"})
94102
private JTextField sourceModelNameTexField;
103+
private JTable optionTable;
104+
private JButton addOptionButton;
105+
private JPanel optionsPanel;
95106
private final Project project;
96107
private final SourceModelData sourceModelData;
108+
private TableGroupWrapper entityPropertiesTableGroupWrapper;
97109

98110
/**
99111
* Constructor.
@@ -108,41 +120,57 @@ public NewEavAttributeDialog(final Project project, final PsiDirectory directory
108120
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
109121
this.sourceModelData = new SourceModelData();
110122

123+
fillEntityComboBoxes();
124+
initPropertiesTable();
111125
setPanelConfiguration();
112126
addActionListenersForButtons();
113127
addCancelActionForWindow();
114128
addCancelActionForEsc();
115-
setAutocompleteListenerForAttributeCodeField();
116-
fillEntityComboBoxes();
117129
addDependBetweenInputAndSourceModel();
130+
addOptionPanelListener();
131+
setAutocompleteListenerForAttributeCodeField();
118132
setDefaultSources();
119133
}
120134

135+
private void initPropertiesTable() {
136+
final List<String> columns = new LinkedList<>(Arrays.asList(
137+
"Value",
138+
"Sort Order"
139+
));
140+
// Initialize entity properties Table Group
141+
entityPropertiesTableGroupWrapper = new TableGroupWrapper(
142+
optionTable,
143+
addOptionButton,
144+
columns,
145+
new HashMap<>(),
146+
new HashMap<>()
147+
);
148+
entityPropertiesTableGroupWrapper.initTableGroup();
149+
}
150+
121151
@SuppressWarnings("PMD.AccessorMethodGeneration")
122152
private void addDependBetweenInputAndSourceModel() {
123153
inputComboBox.addItemListener(
124154
new EavAttributeInputItemListener(sourceComboBox)
125155
);
126156

127-
sourceComboBox.addItemListener(new ItemListener() {
128-
@Override
129-
public void itemStateChanged(final ItemEvent itemEvent) {
130-
final String selectedSource = itemEvent.getItem().toString();
131-
132-
if (selectedSource.equals(AttributeSourceModel.GENERATE_SOURCE.getSource())) {
133-
customSourceModelPanel.setVisible(true);
134-
sourceModelData.setModuleName(moduleName);
135-
136-
if (sourceModelDirectoryTexField.getText().trim().isEmpty()) {
137-
sourceModelDirectoryTexField.setText(sourceModelData.getDirectory());
138-
}
139-
} else {
140-
customSourceModelPanel.setVisible(false);
141-
}
142-
}
143-
});
157+
sourceComboBox.addItemListener(
158+
new AttributeSourceRelationsItemListener(customSourceModelPanel)
159+
);
144160

145-
sourceModelDirectoryTexField.setText(sourceModelData.getDirectory());
161+
customSourceModelPanel.addComponentListener(
162+
new AttributeSourcePanelComponentListener(sourceModelDirectoryTexField)
163+
);
164+
}
165+
166+
@SuppressWarnings("PMD.AccessorMethodGeneration")
167+
private void addOptionPanelListener() {
168+
sourceComboBox.addItemListener(
169+
new OptionsPanelVisibilityChangeListener(
170+
optionsPanel,
171+
inputComboBox
172+
)
173+
);
146174
}
147175

148176
private void setDefaultSources() {
@@ -295,7 +323,7 @@ private void generateSourceModelFile() {
295323

296324
if (selectedSource == null
297325
|| !selectedSource.getText().equals(
298-
AttributeSourceModel.GENERATE_SOURCE.getSource()
326+
AttributeSourceModel.GENERATE_SOURCE.getSource()
299327
)) {
300328
return;
301329
}
@@ -335,6 +363,10 @@ private ProductEntityData populateProductEntityData(final ProductEntityData prod
335363
productEntityData.setInput(getAttributeInput());
336364
productEntityData.setScope(getAttributeScope());
337365
productEntityData.setSource(getAttributeSource());
366+
productEntityData.setOptions(GetAttributeOptionPropertiesUtil.getValues(
367+
entityPropertiesTableGroupWrapper.getColumnsData()));
368+
productEntityData.setOptionsSortOrder(GetAttributeOptionPropertiesUtil.getSortOrders(
369+
entityPropertiesTableGroupWrapper.getColumnsData()));
338370

339371
return productEntityData;
340372
}
@@ -344,7 +376,7 @@ private String getAttributeSource() {
344376

345377
if (selectedItem == null
346378
|| selectedItem.getText().equals(
347-
AttributeSourceModel.NULLABLE_SOURCE.getSource()
379+
AttributeSourceModel.NULLABLE_SOURCE.getSource()
348380
)) {
349381
return null;
350382
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.dialog.event;
7+
8+
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
9+
import java.awt.event.ComponentEvent;
10+
import java.awt.event.ComponentListener;
11+
import javax.swing.JTextField;
12+
13+
public class AttributeSourcePanelComponentListener implements ComponentListener {
14+
private final JTextField sourceModelDirectoryTexField;
15+
16+
public AttributeSourcePanelComponentListener(
17+
final JTextField sourceModelDirectoryTexField
18+
) {
19+
this.sourceModelDirectoryTexField = sourceModelDirectoryTexField;
20+
}
21+
22+
@Override
23+
public void componentResized(final ComponentEvent componentEvent) {
24+
//
25+
}
26+
27+
@Override
28+
public void componentMoved(final ComponentEvent componentEvent) {
29+
//
30+
}
31+
32+
@Override
33+
public void componentShown(final ComponentEvent componentEvent) {
34+
final String sourceDirectoryValue = sourceModelDirectoryTexField.getText().trim();
35+
36+
if (sourceDirectoryValue.isEmpty()) {
37+
sourceModelDirectoryTexField.setText(SourceModelFile.DEFAULT_DIR);
38+
}
39+
}
40+
41+
@Override
42+
public void componentHidden(final ComponentEvent componentEvent) {
43+
//
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.dialog.event;
7+
8+
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel;
9+
import java.awt.event.ItemEvent;
10+
import java.awt.event.ItemListener;
11+
import javax.swing.JPanel;
12+
13+
public class AttributeSourceRelationsItemListener implements ItemListener {
14+
private final JPanel customSourcePanel;
15+
16+
public AttributeSourceRelationsItemListener(final JPanel customSourcePanel) {
17+
this.customSourcePanel = customSourcePanel;
18+
}
19+
20+
@Override
21+
public void itemStateChanged(final ItemEvent itemEvent) {
22+
final String selectedSource = itemEvent.getItem().toString();
23+
24+
if (selectedSource.equals(AttributeSourceModel.GENERATE_SOURCE.getSource())) {
25+
customSourcePanel.setVisible(true);
26+
} else {
27+
customSourcePanel.setVisible(false);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)