Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class DartGeneratorPeer implements ProjectGeneratorPeer<DartProjectWizard

private boolean myDartCreateCalcStarted;
private List<DartProjectTemplate> myDartCreateTemplates;// not-null means that it's been already calculated
private String myDartCreateTemplatesSdkPath; //used to expire the above cache if the sdk is changed an alternative would be to use the same cache method as com.jetbrains.lang.dart.sdk.DartSdkUtil.getSdkVersion

public DartGeneratorPeer() {
// set initial values before initDartSdkControls() because listeners should not be triggered on initialization
Expand Down Expand Up @@ -95,9 +96,7 @@ public Component getListCellRendererComponent(JList list, Object value, int inde
editorComponent.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(final @NotNull DocumentEvent e) {
final String sdkPath = mySdkPathComboWithBrowse.getComboBox().getEditor().getItem().toString().trim();
final String message = DartSdkUtil.getErrorMessageIfWrongSdkRootPath(sdkPath);
if (message == null) {
if(e.getType() == DocumentEvent.EventType.INSERT){
onSdkPathChanged();
}
}
Expand All @@ -108,10 +107,13 @@ protected void textChanged(final @NotNull DocumentEvent e) {

private void onSdkPathChanged() {
String sdkPath = mySdkPathComboWithBrowse.getComboBox().getEditor().getItem().toString().trim();
// If the sdk path has changed, recalculate the create template options
if (myDartCreateTemplatesSdkPath != null && !myDartCreateTemplatesSdkPath.equals(sdkPath)) {
clearTemplates();
}
String errorMessage = DartSdkUtil.getErrorMessageIfWrongSdkRootPath(sdkPath);
if (errorMessage != null) {
myLoadingTemplatesPanel.setVisible(false);
myTemplatesPanel.setVisible(false);
clearTemplates();
return;
}

Expand All @@ -131,6 +133,21 @@ private void onSdkPathChanged() {
}
}

// Clears myTemplateList which is required when an invalid Dart SDK directory
// is selected
private void clearTemplates() {
myDartCreateTemplates = null;
myDartCreateTemplatesSdkPath = null;
myDartCreateCalcStarted = false;

myLoadingTemplatesPanel.setVisible(false);
// myLoadedTemplatesPanel should be visible to show the default text
myLoadedTemplatesPanel.setVisible(true);
myCreateSampleProjectCheckBox.setEnabled(false);
myTemplatesList.setEnabled(false);
myTemplatesList.setModel(new DefaultListModel<>());
}

private void startLoadingTemplates() {
myLoadingTemplatesPanel.setVisible(true);
myLoadingTemplatesPanel.setPreferredSize(myLoadedTemplatesPanel.getPreferredSize());
Expand All @@ -142,14 +159,16 @@ private void startLoadingTemplates() {
asyncProcessIcon.resume();

ApplicationManager.getApplication().executeOnPooledThread(() -> {
final String comboSdkPath = mySdkPathComboWithBrowse.getComboBox().getEditor().getItem().toString().trim();
final String sdkPath =
FileUtil.toSystemIndependentName(mySdkPathComboWithBrowse.getComboBox().getEditor().getItem().toString().trim());
FileUtil.toSystemIndependentName(comboSdkPath);
DartProjectTemplate.loadTemplatesAsync(sdkPath, templates -> {
asyncProcessIcon.suspend();
myLoadingTemplatesPanel.remove(asyncProcessIcon);
Disposer.dispose(asyncProcessIcon);

myDartCreateTemplates = templates;
myDartCreateTemplatesSdkPath = comboSdkPath;

// it's better to call onSdkPathChanged() but not showTemplates() directly as sdk path could have been changed during this long calculation
onSdkPathChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class DartProjectTemplate {

private static final DartCreate DART_CREATE = new DartCreate();
private static List<DartProjectTemplate> ourDartCreateTemplateCache;
private static String ourDartCreateTemplateCacheSdkPath; //used to expire the above cache if the sdk is changed, an alternative would be to use the same cache method as com.jetbrains.lang.dart.sdk.DartSdkUtil.getSdkVersion

private static final Logger LOG = PluginLogger.INSTANCE.createLogger(DartProjectTemplate.class);

Expand Down Expand Up @@ -79,13 +80,14 @@ public static void loadTemplatesAsync(@NotNull String sdkRoot, @NotNull Consumer
}

private static @NotNull List<DartProjectTemplate> getDartCreateTemplates(@NotNull String sdkRoot) {
if (ourDartCreateTemplateCache != null) {
if (ourDartCreateTemplateCache != null && sdkRoot.equals(ourDartCreateTemplateCacheSdkPath)) {
return ourDartCreateTemplateCache;
}

final List<DartCreateTemplate> templates = DART_CREATE.getAvailableTemplates(sdkRoot);

ourDartCreateTemplateCache = new ArrayList<>();
ourDartCreateTemplateCacheSdkPath = sdkRoot;
for (DartCreateTemplate template : templates) {
ourDartCreateTemplateCache.add(new DartCreateProjectTemplate(DART_CREATE, template));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public final class DartSdkUtil {

final String version = FileUtil.loadFileOrNull(versionFile);
if (version != null) {
return ourVersions.put(versionPair, version);
ourVersions.put(versionPair, version);
return version;
}

return null;
Expand Down Expand Up @@ -81,7 +82,8 @@ public static void initDartSdkControls(final @Nullable Project project,
}

final String sdkHomePath = getItemFromCombo(dartSdkPathComponent.getComboBox());
versionLabel.setText(sdkHomePath.isEmpty() ? "" : getSdkVersion(sdkHomePath));
final String sdkVersion = getSdkVersion(sdkHomePath);
versionLabel.setText(sdkVersion == null ? "" : sdkVersion);

final TextComponentAccessor<JComboBox> textComponentAccessor = new TextComponentAccessor<>() {
@Override
Expand Down Expand Up @@ -112,7 +114,10 @@ public void setText(final @NotNull JComboBox component, final @NotNull String te
@Override
protected void textChanged(final @NotNull DocumentEvent e) {
final String sdkHomePath = getItemFromCombo(dartSdkPathComponent.getComboBox());
versionLabel.setText(sdkHomePath.isEmpty() ? "" : getSdkVersion(sdkHomePath));
if(e.getType() == DocumentEvent.EventType.INSERT){
final String sdkVersion = getSdkVersion(sdkHomePath);
versionLabel.setText(sdkVersion == null ? "" : sdkVersion);
}
}
});
}
Expand Down
Loading