forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsForm.java
337 lines (284 loc) · 11.9 KB
/
SettingsForm.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.project;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.javaee.ExternalResourceManagerEx;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComponentWithBrowseButton;
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FilenameIndex;
import com.jetbrains.php.frameworks.PhpFrameworkConfigurable;
import com.magento.idea.magento2plugin.util.magento.MagentoVersion;
import com.magento.idea.magento2plugin.indexes.IndexManager;
import com.magento.idea.magento2plugin.init.ConfigurationManager;
import com.magento.idea.magento2plugin.magento.packages.ComposerPackageModel;
import com.magento.idea.magento2plugin.magento.packages.MagentoComponent;
import com.magento.idea.magento2plugin.magento.packages.MagentoComponentManager;
import com.magento.idea.magento2plugin.magento.packages.MagentoModule;
import com.magento.idea.magento2plugin.project.validator.SettingsFormValidator;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Stack;
public class SettingsForm implements PhpFrameworkConfigurable {
private final static String DISPLAY_NAME = "Magento";
private final Project project;
private JCheckBox pluginEnabled;
private JButton buttonReindex;
private JPanel jPanel;
private JButton regenerateUrnMapButton;
private JTextField magentoVersion;
private JTextField moduleDefaultLicenseName;
private JCheckBox mftfSupportEnabled;
private JLabel magentoPathLabel;
private TextFieldWithBrowseButton magentoPath;
private JLabel magentoVersionLabel;
private MagentoVersion magentoVersionUtil = MagentoVersion.getInstance();
private SettingsFormValidator validator = SettingsFormValidator.getInstance(this);
public SettingsForm(@NotNull final Project project) {
this.project = project;
}
@Nls
@Override
public String getDisplayName() {
return SettingsForm.DISPLAY_NAME;
}
@Nullable
@Override
public String getHelpTopic() {
return null;
}
@Nullable
@Override
public JComponent createComponent() {
buttonReindex.addMouseListener(
new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
reindex();
super.mouseClicked(e);
}
}
);
buttonReindex.setEnabled(getSettings().pluginEnabled);
regenerateUrnMapButton.setEnabled(getSettings().pluginEnabled);
regenerateUrnMapButton.addMouseListener(
new RegenerateUrnMapListener(project)
);
moduleDefaultLicenseName.setText(getSettings().DEFAULT_LICENSE);
mftfSupportEnabled.setSelected(getSettings().mftfSupportEnabled);
magentoPath.getTextField().setText(getSettings().magentoPath);
resolveMagentoVersion();
addPathListener();
addMagentoVersionListener();
return (JComponent) jPanel;
}
private void reindex() {
IndexManager.manualReindex();
MagentoComponentManager.getInstance(project).flushModules();
}
@Override
public boolean isModified() {
boolean licenseChanged = !moduleDefaultLicenseName.getText().equals(getSettings().DEFAULT_LICENSE);
boolean statusChanged = !pluginEnabled.isSelected() == getSettings().pluginEnabled;
boolean mftfSupportChanged = mftfSupportEnabled.isSelected() != getSettings().mftfSupportEnabled;
boolean magentoPathChanged = isMagentoPathChanged();
return statusChanged || licenseChanged || mftfSupportChanged || magentoPathChanged;
}
private void resolveMagentoVersion() {
if (getSettings().magentoVersion == null) {
this.updateMagentoVersion();
return;
}
magentoVersion.setText(getSettings().magentoVersion);
}
private boolean isMagentoPathChanged() {
return !magentoPath.getTextField().getText().equals(getSettings().magentoPath);
}
@Override
public void apply() throws ConfigurationException {
this.validator.validate();
saveSettings();
ConfigurationManager.getInstance().refreshIncludePaths(getSettings().getState(), project);
if (buttonReindex.isEnabled()) {
reindex();
}
}
private void saveSettings() {
getSettings().pluginEnabled = pluginEnabled.isSelected();
getSettings().DEFAULT_LICENSE = moduleDefaultLicenseName.getText();
getSettings().mftfSupportEnabled = mftfSupportEnabled.isSelected();
getSettings().magentoPath = getMagentoPath();
getSettings().magentoVersion = getMagentoVersion();
buttonReindex.setEnabled(getSettings().pluginEnabled);
regenerateUrnMapButton.setEnabled(getSettings().pluginEnabled);
}
@NotNull
public String getMagentoVersion() {
return magentoVersion.getText().trim();
}
@NotNull
public String getMagentoPath() {
return magentoPath.getTextField().getText().trim();
}
@Override
public void reset() {
pluginEnabled.setSelected(getSettings().pluginEnabled);
}
@Override
public void disposeUIResources() {
}
public Settings getSettings() {
return Settings.getInstance(project);
}
private void addPathListener() {
FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
ComponentWithBrowseButton.BrowseFolderActionListener<JTextField> browseFolderListener = new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>(
"Magento Root Directory",
"Choose Magento root directory",
this.magentoPath,
null,
descriptor,
TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT
) {
@Nullable
protected VirtualFile getInitialFile() {
return super.getInitialFile();
}
};
this.magentoPath.addActionListener(browseFolderListener);
}
private void addMagentoVersionListener() {
DocumentListener onPathChange = new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent documentEvent) {
updateMagentoVersion();
}
@Override
public void removeUpdate(DocumentEvent documentEvent) {
updateMagentoVersion();
}
@Override
public void changedUpdate(DocumentEvent documentEvent) {
updateMagentoVersion();
}
};
this.magentoPath.getTextField().getDocument().addDocumentListener(onPathChange);
}
public void updateMagentoVersion() {
String magentoPathValue = this.magentoPath.getTextField().getText();
String resolvedVersion = magentoVersionUtil.get(project, magentoPathValue);
magentoVersion.setText(resolvedVersion);
}
@Override
public boolean isBeingUsed() {
return this.pluginEnabled.isSelected();
}
@NotNull
@Override
public String getId() {
return "Magento2.SettingsForm";
}
}
class RegenerateUrnMapListener extends MouseAdapter {
private final static String MODULE = "urn:magento:module:";
private final static String FRAMEWORK = "urn:magento:framework:";
private Project project;
public RegenerateUrnMapListener(@NotNull Project project) {
this.project = project;
}
@Override
public void mouseClicked(MouseEvent e) {
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
ExternalResourceManager externalResourceManager = ExternalResourceManager.getInstance();
PsiManager psiManager = PsiManager.getInstance(project);
MagentoComponentManager componentManager = MagentoComponentManager.getInstance(project);
Collection<VirtualFile> xsdFiles = FilenameIndex.getAllFilesByExt(project, "xsd");
Collection<MagentoComponent> components = componentManager.getAllComponents();
for (VirtualFile virtualFile: xsdFiles) {
PsiFile psiFile = psiManager.findFile(virtualFile);
if (psiFile == null) {
continue;
}
MagentoComponent xsdOwner = findComponentForXsd(psiFile, components);
if (xsdOwner == null) {
continue;
}
String urnKey = buildUrnKeyForFile(psiFile, xsdOwner);
if (urnKey == null) {
continue;
}
// we need to attach resource to a project scope
// but with ExternalResourceManager itself it's not possible unfortunately
if (externalResourceManager instanceof ExternalResourceManagerEx) {
((ExternalResourceManagerEx)externalResourceManager).addResource(
urnKey, virtualFile.getCanonicalPath(), project
);
} else {
externalResourceManager.addResource(urnKey, virtualFile.getCanonicalPath());
}
}
}
}
);
super.mouseClicked(e);
}
@Nullable
private MagentoComponent findComponentForXsd(@NotNull PsiFile psiFile, Collection<MagentoComponent> components) {
for (MagentoComponent component: components) {
if (component.isFileInContext(psiFile)) {
return component;
}
}
return null;
}
@Nullable
private String buildUrnKeyForFile(@NotNull PsiFile psiFile, @NotNull MagentoComponent magentoComponent) {
String prefix = null;
if (magentoComponent instanceof MagentoModule) {
prefix = MODULE + ((MagentoModule)magentoComponent).getMagentoName() + ":";
} else {
ComposerPackageModel composerPackageModel = magentoComponent.getComposerModel();
if ("magento2-library".equals(composerPackageModel.getType())) {
prefix = FRAMEWORK;
}
}
if (prefix == null) {
return null;
}
Stack<String> relativePath = new Stack<>();
relativePath.push(psiFile.getName());
PsiManager psiManager = magentoComponent.getDirectory().getManager();
PsiDirectory parentDir = psiFile.getParent();
while (parentDir != null && !psiManager.areElementsEquivalent(parentDir, magentoComponent.getDirectory())) {
relativePath.push("/");
relativePath.push(parentDir.getName());
parentDir = parentDir.getParentDirectory();
}
StringBuilder stringBuilder = new StringBuilder(prefix);
while (!relativePath.empty()) {
stringBuilder.append(relativePath.pop());
}
return stringBuilder.toString();
}
}