From 5a256a5e9aca6cbd4139f801f26f3645bfa47281 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Thu, 20 Jun 2024 12:42:04 +0200 Subject: [PATCH] Settings tutorial: Change the implementation to follow the recommended approach (a separate state class) Also: - remove unnecessary screenshot - change labels to follow UX guidelines --- settings/README.md | 9 ++-- .../intellij/sdk/settings/AppSettings.java | 49 +++++++++++++++++++ .../sdk/settings/AppSettingsComponent.java | 6 +-- .../sdk/settings/AppSettingsConfigurable.java | 27 +++++----- .../sdk/settings/AppSettingsState.java | 40 --------------- .../src/main/resources/META-INF/plugin.xml | 5 +- 6 files changed, 73 insertions(+), 63 deletions(-) create mode 100644 settings/src/main/java/org/intellij/sdk/settings/AppSettings.java delete mode 100644 settings/src/main/java/org/intellij/sdk/settings/AppSettingsState.java diff --git a/settings/README.md b/settings/README.md index cef98198a..84927e9e7 100644 --- a/settings/README.md +++ b/settings/README.md @@ -4,8 +4,8 @@ ## Quickstart This project illustrates a custom Application-level Settings through the implementation of: -- `AppSettingsConfigurable` is analogous to a Controller in the MVC model - it interacts with the other two Settings classes and the IntelliJ Platform, -- `AppSettingsState` is like a Model because it stores the Settings persistently, +- `AppSettingsConfigurable` is analogous to a Controller in the MVC model – it interacts with the other two Settings classes and the IntelliJ Platform, +- `AppSettings` is like a Model because it stores the Settings persistently, - `AppSettingsComponent` is similar to a View because it displays and captures edits to the values of the Settings. ### Extension Points @@ -13,7 +13,7 @@ This project illustrates a custom Application-level Settings through the impleme | Name | Implementation | Extension Point Class | |----------------------------------------|---------------------------------------------------------|----------------------------| | `com.intellij.applicationConfigurable` | [AppSettingsConfigurable][file:AppSettingsConfigurable] | `Configurable` | -| `com.intellij.applicationService` | [AppSettingsState][file:AppSettingsState] | `PersistentStateComponent` | +| `com.intellij.applicationService` | [AppSettings][file:AppSettings] | `PersistentStateComponent` | *Reference: [Plugin Extension Points in IntelliJ SDK Docs][docs:ep]* @@ -23,5 +23,4 @@ This project illustrates a custom Application-level Settings through the impleme [docs:ep]: https://plugins.jetbrains.com/docs/intellij/plugin-extensions.html [file:AppSettingsConfigurable]: ./src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java -[file:AppSettingsState]: ./src/main/java/org/intellij/sdk/settings/AppSettingsState.java - +[file:AppSettings]: ./src/main/java/org/intellij/sdk/settings/AppSettings.java diff --git a/settings/src/main/java/org/intellij/sdk/settings/AppSettings.java b/settings/src/main/java/org/intellij/sdk/settings/AppSettings.java new file mode 100644 index 000000000..ac8a3ac08 --- /dev/null +++ b/settings/src/main/java/org/intellij/sdk/settings/AppSettings.java @@ -0,0 +1,49 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. + +package org.intellij.sdk.settings; + +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.components.PersistentStateComponent; +import com.intellij.openapi.components.State; +import com.intellij.openapi.components.Storage; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +/* + * Supports storing the application settings in a persistent way. + * The {@link com.intellij.openapi.components.State State} and {@link Storage} + * annotations define the name of the data and the filename where these persistent + * application settings are stored. + */ + +@State( + name = "org.intellij.sdk.settings.AppSettings", + storages = @Storage("SdkSettingsPlugin.xml") +) +final class AppSettings + implements PersistentStateComponent { + + static class State { + @NonNls + public String userId = "John Smith"; + public boolean ideaStatus = false; + } + + private State myState = new State(); + + static AppSettings getInstance() { + return ApplicationManager.getApplication() + .getService(AppSettings.class); + } + + @Override + public State getState() { + return myState; + } + + @Override + public void loadState(@NotNull State state) { + myState = state; + } + +} diff --git a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java b/settings/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java index 1de5e949c..e3f35c592 100644 --- a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java +++ b/settings/src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.intellij.sdk.settings; @@ -17,11 +17,11 @@ public class AppSettingsComponent { private final JPanel myMainPanel; private final JBTextField myUserNameText = new JBTextField(); - private final JBCheckBox myIdeaUserStatus = new JBCheckBox("Do you use IntelliJ IDEA? "); + private final JBCheckBox myIdeaUserStatus = new JBCheckBox("IntelliJ IDEA user"); public AppSettingsComponent() { myMainPanel = FormBuilder.createFormBuilder() - .addLabeledComponent(new JBLabel("Enter user name: "), myUserNameText, 1, false) + .addLabeledComponent(new JBLabel("User name:"), myUserNameText, 1, false) .addComponent(myIdeaUserStatus, 1) .addComponentFillVertically(new JPanel(), 0) .getPanel(); diff --git a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java b/settings/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java index d8fb641e7..e38a60874 100644 --- a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java +++ b/settings/src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.util.Objects; /** * Provides controller functionality for application settings. @@ -14,8 +15,8 @@ final class AppSettingsConfigurable implements Configurable { private AppSettingsComponent mySettingsComponent; - // A default constructor with no arguments is required because this implementation - // is registered in an applicationConfigurable EP + // A default constructor with no arguments is required because + // this implementation is registered as an applicationConfigurable @Nls(capitalization = Nls.Capitalization.Title) @Override @@ -37,24 +38,26 @@ final class AppSettingsConfigurable implements Configurable { @Override public boolean isModified() { - AppSettingsState settings = AppSettingsState.getInstance(); - boolean modified = !mySettingsComponent.getUserNameText().equals(settings.userId); - modified |= mySettingsComponent.getIdeaUserStatus() != settings.ideaStatus; - return modified; + AppSettings.State state = + Objects.requireNonNull(AppSettings.getInstance().getState()); + return !mySettingsComponent.getUserNameText().equals(state.userId) || + mySettingsComponent.getIdeaUserStatus() != state.ideaStatus; } @Override public void apply() { - AppSettingsState settings = AppSettingsState.getInstance(); - settings.userId = mySettingsComponent.getUserNameText(); - settings.ideaStatus = mySettingsComponent.getIdeaUserStatus(); + AppSettings.State state = + Objects.requireNonNull(AppSettings.getInstance().getState()); + state.userId = mySettingsComponent.getUserNameText(); + state.ideaStatus = mySettingsComponent.getIdeaUserStatus(); } @Override public void reset() { - AppSettingsState settings = AppSettingsState.getInstance(); - mySettingsComponent.setUserNameText(settings.userId); - mySettingsComponent.setIdeaUserStatus(settings.ideaStatus); + AppSettings.State state = + Objects.requireNonNull(AppSettings.getInstance().getState()); + mySettingsComponent.setUserNameText(state.userId); + mySettingsComponent.setIdeaUserStatus(state.ideaStatus); } @Override diff --git a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsState.java b/settings/src/main/java/org/intellij/sdk/settings/AppSettingsState.java deleted file mode 100644 index 665cc58de..000000000 --- a/settings/src/main/java/org/intellij/sdk/settings/AppSettingsState.java +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. - -package org.intellij.sdk.settings; - -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.components.PersistentStateComponent; -import com.intellij.openapi.components.State; -import com.intellij.openapi.components.Storage; -import com.intellij.util.xmlb.XmlSerializerUtil; -import org.jetbrains.annotations.NotNull; - -/** - * Supports storing the application settings in a persistent way. - * The {@link State} and {@link Storage} annotations define the name of the data and the file name where - * these persistent application settings are stored. - */ -@State( - name = "org.intellij.sdk.settings.AppSettingsState", - storages = @Storage("SdkSettingsPlugin.xml") -) -final class AppSettingsState implements PersistentStateComponent { - - public String userId = "John Q. Public"; - public boolean ideaStatus = false; - - static AppSettingsState getInstance() { - return ApplicationManager.getApplication().getService(AppSettingsState.class); - } - - @Override - public AppSettingsState getState() { - return this; - } - - @Override - public void loadState(@NotNull AppSettingsState state) { - XmlSerializerUtil.copyBean(state, this); - } - -} diff --git a/settings/src/main/resources/META-INF/plugin.xml b/settings/src/main/resources/META-INF/plugin.xml index 1d3c6b92b..575dadd5a 100644 --- a/settings/src/main/resources/META-INF/plugin.xml +++ b/settings/src/main/resources/META-INF/plugin.xml @@ -1,5 +1,4 @@ - - + @@ -35,7 +34,7 @@ - +