Skip to content

Commit 200e7c9

Browse files
author
Vitaliy
authored
Merge pull request #546 from ProkopovVitaliy/545-attribute-options
Added possibility to set options
2 parents b30c699 + d5e2476 commit 200e7c9

File tree

13 files changed

+518
-30
lines changed

13 files changed

+518
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ${CLASS_NAME} implements ${IMPLEMENTS} {
8383
[
8484
#set ($attributeSet = ${ATTRIBUTE_SET})
8585
#foreach ($attribute in $attributeSet.split("->"))
86-
$attribute,
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 {
@@ -26,6 +27,8 @@ public class ProductEntityData implements EavEntityDataInterface {
2627
private boolean htmlAllowedOnFront;
2728
private boolean visibleOnFront;
2829
private int sortOrder;
30+
private Map<Integer, String> options;
31+
private Map<Integer, String> optionsSortOrder;
2932

3033
private String dataPatchName;
3134
private String namespace;
@@ -126,6 +129,22 @@ public void setModuleName(final String moduleName) {
126129
this.moduleName = moduleName;
127130
}
128131

132+
public void setOptions(final Map<Integer, String> options) {
133+
this.options = options;
134+
}
135+
136+
public void setOptionsSortOrder(final Map<Integer, String> optionsSortOrder) {
137+
this.optionsSortOrder = optionsSortOrder;
138+
}
139+
140+
public Map<Integer, String> getOptions() {
141+
return options;
142+
}
143+
144+
public Map<Integer, String> getOptionsSortOrder() {
145+
return optionsSortOrder;
146+
}
147+
129148
@Override
130149
public String getCode() {
131150
return code;

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

Lines changed: 42 additions & 3 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.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>
66
<xy x="-1" y="5" width="764" height="763"/>
@@ -373,13 +373,13 @@
373373
</grid>
374374
<hspacer id="75d92">
375375
<constraints>
376-
<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"/>
376+
<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"/>
377377
</constraints>
378378
</hspacer>
379379
<grid id="181ea" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
380380
<margin top="0" left="0" bottom="0" right="0"/>
381381
<constraints>
382-
<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"/>
382+
<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"/>
383383
</constraints>
384384
<properties/>
385385
<border type="none"/>
@@ -418,6 +418,45 @@
418418
</grid>
419419
</children>
420420
</grid>
421+
<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">
422+
<margin top="5" left="5" bottom="5" right="5"/>
423+
<constraints>
424+
<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"/>
425+
</constraints>
426+
<properties>
427+
<visible value="false"/>
428+
</properties>
429+
<border type="empty" title="Options" title-justification="2" title-position="2"/>
430+
<children>
431+
<scrollpane id="47220">
432+
<constraints>
433+
<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"/>
434+
</constraints>
435+
<properties>
436+
<enabled value="true"/>
437+
</properties>
438+
<border type="none"/>
439+
<children>
440+
<component id="b0960" class="javax.swing.JTable" binding="optionTable">
441+
<constraints/>
442+
<properties>
443+
<intercellSpacing width="1" height="3"/>
444+
<requestFocusEnabled value="true"/>
445+
<rowHeight value="30"/>
446+
</properties>
447+
</component>
448+
</children>
449+
</scrollpane>
450+
<component id="835cb" class="javax.swing.JButton" binding="addOptionButton">
451+
<constraints>
452+
<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"/>
453+
</constraints>
454+
<properties>
455+
<text value="Add Option"/>
456+
</properties>
457+
</component>
458+
</children>
459+
</grid>
421460
</children>
422461
</grid>
423462
</form>

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

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,33 @@
1313
import com.magento.idea.magento2plugin.actions.generation.data.SourceModelData;
1414
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
1515
import com.magento.idea.magento2plugin.actions.generation.dialog.event.ApplyToVisibleListener;
16+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.AttributeSourcePanelComponentListener;
17+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.AttributeSourceRelationsItemListener;
1618
import com.magento.idea.magento2plugin.actions.generation.dialog.event.EavAttributeInputItemListener;
19+
import com.magento.idea.magento2plugin.actions.generation.dialog.event.OptionsPanelVisibilityChangeListener;
1720
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
1821
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
1922
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.CommaSeparatedStringRule;
2023
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
2124
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
2225
import com.magento.idea.magento2plugin.actions.generation.generator.EavAttributeSetupPatchGenerator;
2326
import com.magento.idea.magento2plugin.actions.generation.generator.SourceModelGenerator;
27+
import com.magento.idea.magento2plugin.actions.generation.generator.util.GetAttributeOptionPropertiesUtil;
2428
import com.magento.idea.magento2plugin.magento.files.SourceModelFile;
2529
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeInput;
2630
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeScope;
2731
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeSourceModel;
2832
import com.magento.idea.magento2plugin.magento.packages.eav.AttributeType;
2933
import com.magento.idea.magento2plugin.magento.packages.eav.EavEntity;
34+
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
3035
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
3136
import com.magento.idea.magento2plugin.util.magento.GetProductTypesListUtil;
32-
import java.awt.event.ItemEvent;
33-
import java.awt.event.ItemListener;
3437
import java.awt.event.KeyEvent;
3538
import java.awt.event.WindowAdapter;
3639
import java.awt.event.WindowEvent;
40+
import java.util.Arrays;
41+
import java.util.HashMap;
42+
import java.util.LinkedList;
3743
import java.util.List;
3844
import javax.swing.DefaultListModel;
3945
import javax.swing.JButton;
@@ -42,6 +48,7 @@
4248
import javax.swing.JComponent;
4349
import javax.swing.JList;
4450
import javax.swing.JPanel;
51+
import javax.swing.JTable;
4552
import javax.swing.JTextField;
4653
import javax.swing.KeyStroke;
4754
import javax.swing.event.DocumentEvent;
@@ -98,14 +105,17 @@ public class NewEavAttributeDialog extends AbstractDialog {
98105
@FieldValidation(rule = RuleRegistry.NOT_EMPTY,
99106
message = {NotEmptyRule.MESSAGE, "Source Model Name"})
100107
private JTextField sourceModelNameTexField;
108+
private JTable optionTable;
109+
private JButton addOptionButton;
110+
private JPanel optionsPanel;
101111
@FieldValidation(rule = RuleRegistry.COMMA_SEPARATED_STRING,
102112
message = {CommaSeparatedStringRule.MESSAGE, "Apply To"})
103-
private JTextField applyToTextField;
104113
private JCheckBox applyToAllProductsCheckBox;
105114
private JPanel applyToPanel;
106115
private JList productsTypesList;
107116
private final Project project;
108117
private final SourceModelData sourceModelData;
118+
private TableGroupWrapper entityPropertiesTableGroupWrapper;
109119

110120
/**
111121
* Constructor.
@@ -120,18 +130,37 @@ public NewEavAttributeDialog(final Project project, final PsiDirectory directory
120130
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
121131
this.sourceModelData = new SourceModelData();
122132

133+
fillEntityComboBoxes();
134+
initPropertiesTable();
123135
setPanelConfiguration();
124136
addActionListenersForButtons();
125137
addCancelActionForWindow();
126138
addCancelActionForEsc();
127139
addApplyToVisibilityAction();
128-
setAutocompleteListenerForAttributeCodeField();
129140
fillEntityComboBoxes();
130141
fillProductsTypesList();
131142
addDependBetweenInputAndSourceModel();
143+
addOptionPanelListener();
144+
setAutocompleteListenerForAttributeCodeField();
132145
setDefaultSources();
133146
}
134147

148+
private void initPropertiesTable() {
149+
final List<String> columns = new LinkedList<>(Arrays.asList(
150+
"Value",
151+
"Sort Order"
152+
));
153+
// Initialize entity properties Table Group
154+
entityPropertiesTableGroupWrapper = new TableGroupWrapper(
155+
optionTable,
156+
addOptionButton,
157+
columns,
158+
new HashMap<>(),
159+
new HashMap<>()
160+
);
161+
entityPropertiesTableGroupWrapper.initTableGroup();
162+
}
163+
135164
private void fillProductsTypesList() {
136165
final List<String> productTypes = GetProductTypesListUtil.execute(project);
137166

@@ -151,25 +180,23 @@ private void addDependBetweenInputAndSourceModel() {
151180
new EavAttributeInputItemListener(sourceComboBox)
152181
);
153182

154-
sourceComboBox.addItemListener(new ItemListener() {
155-
@Override
156-
public void itemStateChanged(final ItemEvent itemEvent) {
157-
final String selectedSource = itemEvent.getItem().toString();
158-
159-
if (selectedSource.equals(AttributeSourceModel.GENERATE_SOURCE.getSource())) {
160-
customSourceModelPanel.setVisible(true);
161-
sourceModelData.setModuleName(moduleName);
162-
163-
if (sourceModelDirectoryTexField.getText().trim().isEmpty()) {
164-
sourceModelDirectoryTexField.setText(sourceModelData.getDirectory());
165-
}
166-
} else {
167-
customSourceModelPanel.setVisible(false);
168-
}
169-
}
170-
});
183+
sourceComboBox.addItemListener(
184+
new AttributeSourceRelationsItemListener(customSourceModelPanel)
185+
);
171186

172-
sourceModelDirectoryTexField.setText(sourceModelData.getDirectory());
187+
customSourceModelPanel.addComponentListener(
188+
new AttributeSourcePanelComponentListener(sourceModelDirectoryTexField)
189+
);
190+
}
191+
192+
@SuppressWarnings("PMD.AccessorMethodGeneration")
193+
private void addOptionPanelListener() {
194+
sourceComboBox.addItemListener(
195+
new OptionsPanelVisibilityChangeListener(
196+
optionsPanel,
197+
inputComboBox
198+
)
199+
);
173200
}
174201

175202
private void setDefaultSources() {
@@ -362,6 +389,10 @@ private ProductEntityData populateProductEntityData(final ProductEntityData prod
362389
productEntityData.setInput(getAttributeInput());
363390
productEntityData.setScope(getAttributeScope());
364391
productEntityData.setSource(getAttributeSource());
392+
productEntityData.setOptions(GetAttributeOptionPropertiesUtil.getValues(
393+
entityPropertiesTableGroupWrapper.getColumnsData()));
394+
productEntityData.setOptionsSortOrder(GetAttributeOptionPropertiesUtil.getSortOrders(
395+
entityPropertiesTableGroupWrapper.getColumnsData()));
365396

366397
if (!applyToAllProductsCheckBox.isSelected()) {
367398
productEntityData.setApplyTo(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
initSetDirectoryFieldValue();
35+
}
36+
37+
@Override
38+
public void componentHidden(final ComponentEvent componentEvent) {
39+
initSetDirectoryFieldValue();
40+
}
41+
42+
private void initSetDirectoryFieldValue() {
43+
final String sourceDirectoryValue = sourceModelDirectoryTexField.getText().trim();
44+
45+
if (sourceDirectoryValue.isEmpty()) {
46+
sourceModelDirectoryTexField.setText(SourceModelFile.DEFAULT_DIR);
47+
}
48+
}
49+
}
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)