-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3b31e7
commit b21d808
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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; | ||
|
||
@State( | ||
name = "org.intellij.sdk.settings.AppSettings", | ||
storages = @Storage("SdkSettingsPlugin.xml") | ||
) | ||
public final class AppSettings | ||
implements PersistentStateComponent<AppSettings.State> { | ||
|
||
public static class State { | ||
|
||
@NonNls | ||
public String serverURL = ""; | ||
@NonNls | ||
public String userName = ""; | ||
@NonNls | ||
public String apiToken = ""; | ||
|
||
} | ||
|
||
private State state = new State(); | ||
|
||
public static AppSettings getInstance() { | ||
return ApplicationManager.getApplication() | ||
.getService(AppSettings.class); | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return state; | ||
} | ||
|
||
@Override | ||
public void loadState(@NotNull State state) { | ||
this.state = state; | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.intellij.sdk.settings; | ||
|
||
import com.intellij.ui.components.JBCheckBox; | ||
import com.intellij.ui.components.JBLabel; | ||
import com.intellij.ui.components.JBPasswordField; | ||
import com.intellij.ui.components.JBTextField; | ||
import com.intellij.util.ui.FormBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
|
||
/** | ||
* Supports creating and managing a {@link JPanel} for the Settings Dialog. | ||
*/ | ||
public class AppSettingsComponent { | ||
|
||
private final JPanel mainPanel; | ||
private final JBTextField userNameText = new JBTextField(); | ||
private final JBTextField serverUrlText = new JBTextField(); | ||
private final JBPasswordField apiTokenText = new JBPasswordField(); | ||
|
||
public AppSettingsComponent() { | ||
mainPanel = FormBuilder.createFormBuilder() | ||
.addLabeledComponent(new JBLabel("Server URL:"), serverUrlText, 1, false) | ||
.addLabeledComponent(new JBLabel("User name:"), userNameText, 1, false) | ||
.addLabeledComponent(new JBLabel("API token:"), apiTokenText, 1, false) | ||
.addComponentFillVertically(new JPanel(), 0) | ||
.getPanel(); | ||
} | ||
|
||
public JPanel getPanel() { | ||
return mainPanel; | ||
} | ||
|
||
public JComponent getPreferredFocusedComponent() { | ||
return userNameText; | ||
} | ||
|
||
@NotNull | ||
public String getUserNameText() { | ||
return userNameText.getText(); | ||
} | ||
|
||
@NotNull | ||
public String getServerUrlText() { | ||
return serverUrlText.getText(); | ||
} | ||
|
||
@NotNull | ||
public String getApiTokenText() { | ||
return apiTokenText.getText(); | ||
} | ||
|
||
public void setUserNameText(@NotNull String newText) { | ||
userNameText.setText(newText); | ||
} | ||
|
||
public void setServerUrlText(@NotNull String newText) { | ||
serverUrlText.setText(newText); | ||
} | ||
|
||
public void setApiTokenText(@NotNull String newText) { | ||
apiTokenText.setText(newText); | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/intellij/sdk/settings/AppSettingsConfigurable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.intellij.sdk.settings; | ||
|
||
import com.intellij.ide.passwordSafe.PasswordSafe; | ||
import com.intellij.ide.passwordSafe.PasswordSafeException; | ||
import com.intellij.openapi.options.Configurable; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Provides controller functionality for application settings. | ||
*/ | ||
final class AppSettingsConfigurable implements Configurable { | ||
|
||
private AppSettingsComponent appSettingsComponent; | ||
|
||
// A default constructor with no arguments is required because | ||
// this implementation is registered as an applicationConfigurable | ||
|
||
@Nls(capitalization = Nls.Capitalization.Title) | ||
@Override | ||
public String getDisplayName() { | ||
return "SecHub Configuration"; | ||
} | ||
|
||
@Override | ||
public JComponent getPreferredFocusedComponent() { | ||
return appSettingsComponent.getPreferredFocusedComponent(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public JComponent createComponent() { | ||
appSettingsComponent = new AppSettingsComponent(); | ||
return appSettingsComponent.getPanel(); | ||
} | ||
|
||
@Override | ||
public boolean isModified() { | ||
AppSettings.State state = | ||
Objects.requireNonNull(AppSettings.getInstance().getState()); | ||
String currentPassword = ""; | ||
try { | ||
currentPassword = PasswordSafe.getInstance().getPassword(null, AppSettings.class, "PASSWORD_KEY"); | ||
} catch (PasswordSafeException e) { | ||
e.printStackTrace(); | ||
} | ||
return !appSettingsComponent.getUserNameText().equals(state.userName) || | ||
appSettingsComponent.getApiTokenText().equals(StringUtil.notNullize(currentPassword)) || | ||
appSettingsComponent.getServerUrlText().equals(state.serverURL); | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
AppSettings.State state = | ||
Objects.requireNonNull(AppSettings.getInstance().getState()); | ||
state.userName = appSettingsComponent.getUserNameText(); | ||
state.serverURL = appSettingsComponent.getServerUrlText(); | ||
try { | ||
PasswordSafe.getInstance().storePassword(null, AppSettings.class, "PASSWORD_KEY", appSettingsComponent.getApiTokenText()); | ||
} catch (PasswordSafeException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
AppSettings.State state = | ||
Objects.requireNonNull(AppSettings.getInstance().getState()); | ||
appSettingsComponent.setUserNameText(state.userName); | ||
appSettingsComponent.setServerUrlText(state.serverURL); | ||
String currentPassword = ""; | ||
try { | ||
currentPassword = PasswordSafe.getInstance().getPassword(null, AppSettings.class, "PASSWORD_KEY"); | ||
} catch (PasswordSafeException e) { | ||
e.printStackTrace(); | ||
} | ||
appSettingsComponent.setApiTokenText(StringUtil.notNullize(currentPassword)); | ||
|
||
} | ||
|
||
@Override | ||
public void disposeUIResources() { | ||
appSettingsComponent = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters