forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewModuleForm.java
269 lines (236 loc) · 9.46 KB
/
NewModuleForm.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
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.generation.php;//NOPMD
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.ComponentWithBrowseButton;
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.platform.ProjectGeneratorPeer;
import com.intellij.ui.DocumentAdapter;
import com.magento.idea.magento2plugin.generation.php.validator.NewModuleFormValidator;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.Licenses;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings({"PMD.TooManyFields", "PMD.UnusedPrivateMethod"})
public class NewModuleForm implements ListSelectionListener {
private final NewModuleFormValidator newModuleFormValidator;
private JPanel contentPane;
private JTextField packageName;
private JTextField moduleName;
private final CamelCaseToHyphen camelCaseToHyphen;
private JTextArea moduleDescription;
private final List<ProjectGeneratorPeer.SettingsListener> myStateListeners;
private JTextField moduleVersion;
private TextFieldWithBrowseButton magentoPath;
private JList moduleLicense;
private JTextField moduleLicenseCustom;
private JLabel moduleLicenseLabel;//NOPMD
private JLabel moduleVersionLabel;//NOPMD
private JScrollPane moduleLicenseScrollPanel;//NOPMD
private JLabel magentoPathLabel;//NOPMD
private JLabel moduleDescriptionLabel;//NOPMD
private JLabel moduleNameLabel;//NOPMD
private JLabel packageNameLabel;//NOPMD
/**
* Constructor.
*/
public NewModuleForm() {
this.camelCaseToHyphen = CamelCaseToHyphen.getInstance();
this.newModuleFormValidator = NewModuleFormValidator.getInstance(this);
this.myStateListeners = new ArrayList();
final Runnable listener = () -> {
this.fireStateChanged();
};
addPathListener();
addComponentChangesListener(listener);
setLicenses();
}
private void addPathListener() {
final FileChooserDescriptor descriptor =
FileChooserDescriptorFactory.createSingleFolderDescriptor();
final ComponentWithBrowseButton.BrowseFolderActionListener<JTextField> browseFolderListener
= new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>(
"Magento Root Directory",
"Choose Magento root directory",
this.magentoPath,
null,
descriptor,
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
) {
@Override
protected VirtualFile getInitialFile() {
String directoryName = this.getComponentText();
if (!StringUtil.isEmptyOrSpaces(directoryName)) {
String lastSavedPath = Settings.getLastMagentoPath();
if (!StringUtil.isEmptyOrSpaces(lastSavedPath)) {
lastSavedPath = FileUtil.toSystemIndependentName(lastSavedPath);
return LocalFileSystem.getInstance().findFileByPath(lastSavedPath);
}
}
return super.getInitialFile();
}
};
this.magentoPath.addActionListener(browseFolderListener);
}
public JComponent getContentPane() {
return this.contentPane;
}
/**
* Returns project generation settings.
*
* @return MagentoProjectGeneratorSettings
*/
public MagentoProjectGeneratorSettings getSettings() {
final Settings.State state = new Settings.State();
state.setPluginEnabled(true);
state.setMftfSupportEnabled(true);
state.setDefaultLicenseName(Settings.defaultLicense);
state.setMagentoPathAndUpdateLastUsed(this.magentoPath.getTextField().getText().trim());
return new MagentoProjectGeneratorSettings(
state,
getPackageName(),
getModuleName(),
getModuleDescription(),
getComposerPackageName(),
getModuleVersion(),
getModuleLicenses()
);
}
public String getPackageName() {
return this.packageName.getText().trim();
}
public String getModuleName() {
return this.moduleName.getText().trim();
}
public String getModuleDescription() {
return this.moduleDescription.getText().trim();
}
public String getModuleVersion() {
return this.moduleVersion.getText().trim();
}
public String getMagentoPath() {
return this.magentoPath.getTextField().getText().trim();
}
/**
* Validate settings.
*
* @return ValidationInfo
*/
public ValidationInfo validate() {
final String message = newModuleFormValidator.validate();
if (message == null) {
return null;
}
return new ValidationInfo(message, getContentPane());
}
/**
* Get Licenses.
*
* @return List
*/
public List getModuleLicenses() {
final List selectedLicenses = this.moduleLicense.getSelectedValuesList();
final Licenses customLicense = Licenses.CUSTOM;
if (selectedLicenses.contains(customLicense.getLicenseName())) {
selectedLicenses.remove(customLicense.getLicenseName());
selectedLicenses.add(moduleLicenseCustom.getText());
}
return selectedLicenses;
}
public void addSettingsStateListener(final ProjectGeneratorPeer.SettingsListener listener) {
this.myStateListeners.add(listener);
}
private void addComponentChangesListener(final Runnable listener) {
this.magentoPath.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final @NotNull DocumentEvent event) {
listener.run();
}
});
this.moduleName.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final @NotNull DocumentEvent event) {
listener.run();
}
});
this.packageName.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final @NotNull DocumentEvent event) {
listener.run();
}
});
this.moduleVersion.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final @NotNull DocumentEvent event) {
listener.run();
}
});
}
private void fireStateChanged() {
final boolean validSettings = this.validate() == null;
final Iterator iterator = this.myStateListeners.iterator();
while (iterator.hasNext()) {
final ProjectGeneratorPeer.SettingsListener listener =
(ProjectGeneratorPeer.SettingsListener)iterator.next();
listener.stateChanged(validSettings);
}
}
private void setLicenses() {
final Licenses[] licenses = Licenses.values();
Vector<String> licenseNames = new Vector<>(licenses.length);//NOPMD
for (final Licenses license: licenses) {
licenseNames.add(license.getLicenseName());
}
moduleLicense.setListData(licenseNames);
moduleLicense.setSelectedIndex(0);
moduleLicense.addListSelectionListener(this);
}
private void handleModuleCustomLicenseInputVisibility() {
boolean isCustomLicenseSelected = false;
for (final Object value: moduleLicense.getSelectedValuesList()) {
if (Licenses.CUSTOM.getLicenseName().equals(value.toString())) {
isCustomLicenseSelected = true;
break;
}
}
moduleLicenseCustom.setEnabled(isCustomLicenseSelected);
moduleLicenseCustom.setEditable(isCustomLicenseSelected);
}
@NotNull
private String getComposerPackageName() {
return camelCaseToHyphen.convert(getPackageName())
.concat(File.separator)
.concat(camelCaseToHyphen.convert(getModuleName()));
}
private void createUIComponents() {
this.magentoPath = new TextFieldWithBrowseButton();
}
@Override
public void valueChanged(final ListSelectionEvent listSelectionEvent) {
handleModuleCustomLicenseInputVisibility();
}
}