forked from smooth80/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlutterGeneratorPeer.java
152 lines (130 loc) · 4.82 KB
/
FlutterGeneratorPeer.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
/*
* Copyright 2016 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.module;
import com.intellij.icons.AllIcons;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.ui.ComboboxWithBrowseButton;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBLabel;
import com.intellij.xml.util.XmlStringUtil;
import io.flutter.FlutterBundle;
import io.flutter.FlutterUtils;
import io.flutter.module.settings.SettingsHelpForm;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkUtil;
import javax.swing.ComboBoxEditor;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.event.DocumentEvent;
import javax.swing.text.JTextComponent;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class FlutterGeneratorPeer {
private final WizardContext myContext;
private JPanel myMainPanel;
private ComboboxWithBrowseButton mySdkPathComboWithBrowse;
private JBLabel myVersionContent;
private JLabel errorIcon;
private JTextPane errorText;
private JScrollPane errorPane;
private SettingsHelpForm myHelpForm;
public FlutterGeneratorPeer(WizardContext context) {
myContext = context;
errorIcon.setText("");
errorIcon.setIcon(AllIcons.Actions.Lightning);
Messages.installHyperlinkSupport(errorText);
// Hide pending real content.
myVersionContent.setVisible(false);
if (!FlutterUtils.isNewAndroidStudioProjectWizard()) {
myHelpForm.getComponent().setVisible(false);
}
init();
}
private void init() {
mySdkPathComboWithBrowse.getComboBox().setEditable(true);
FlutterSdkUtil.addKnownSDKPathsToCombo(mySdkPathComboWithBrowse.getComboBox());
mySdkPathComboWithBrowse.addBrowseFolderListener(FlutterBundle.message("flutter.sdk.browse.path.label"), null, null,
FileChooserDescriptorFactory.createSingleFolderDescriptor(),
TextComponentAccessor.STRING_COMBOBOX_WHOLE_TEXT);
mySdkPathComboWithBrowse.getComboBox().addActionListener(e -> fillSdkCache());
fillSdkCache();
final JTextComponent editorComponent = (JTextComponent)getSdkEditor().getEditorComponent();
editorComponent.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(@NotNull DocumentEvent e) {
validate();
}
});
errorIcon.setVisible(false);
errorPane.setVisible(false);
}
private void fillSdkCache() {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
String path = (String)mySdkPathComboWithBrowse.getComboBox().getSelectedItem();
if (path != null) {
FlutterSdk sdk = FlutterSdk.forPath(path);
if (sdk != null) {
sdk.queryConfiguredPlatforms(false);
sdk.queryFlutterChannel(false);
}
}
});
}
@SuppressWarnings("EmptyMethod")
void apply() {
}
@NotNull
public JComponent getComponent() {
return myMainPanel;
}
private void createUIComponents() {
mySdkPathComboWithBrowse = new ComboboxWithBrowseButton(new ComboBox<>());
}
// TODO Link this to actual validation.
public boolean validate() {
final ValidationInfo info = validateSdk();
if (info != null) {
errorText.setText(XmlStringUtil.wrapInHtml(info.message));
}
errorIcon.setVisible(info != null);
errorPane.setVisible(info != null);
return info == null;
}
@Nullable
private ValidationInfo validateSdk() {
final String sdkPath = getSdkComboPath();
if (StringUtils.isEmpty(sdkPath)) {
return new ValidationInfo("A Flutter SDK must be specified for project creation.", mySdkPathComboWithBrowse);
}
final String message = FlutterSdkUtil.getErrorMessageIfWrongSdkRootPath(sdkPath);
if (message != null) {
return new ValidationInfo(message, mySdkPathComboWithBrowse);
}
return null;
}
@NotNull
public String getSdkComboPath() {
return FileUtilRt.toSystemIndependentName(getSdkEditor().getItem().toString().trim());
}
@NotNull
public ComboBoxEditor getSdkEditor() {
return mySdkPathComboWithBrowse.getComboBox().getEditor();
}
public SettingsHelpForm getHelpForm() {
return myHelpForm;
}
}