Skip to content

Commit

Permalink
Added sechub plugin settings #152
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Aug 9, 2024
1 parent b3b31e7 commit b21d808
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/org/intellij/sdk/settings/AppSettings.java
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 src/main/java/org/intellij/sdk/settings/AppSettingsComponent.java
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);
}

}
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;
}

}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<toolWindow id="SecHub" icon="/icons/toolWindowSecHub.svg" factoryClass="com.mercedesbenz.sechub.plugin.idea.window.SecHubToolWindowFactory" anchor="left"/>

<applicationService serviceImplementation="org.intellij.sdk.settings.AppSettings"/>

<applicationConfigurable parentId="tools" instance="org.intellij.sdk.settings.AppSettingsConfigurable"
id="org.intellij.sdk.settings.AppSettingsConfigurable"
displayName="SecHub Configuration"/>
</extensions>

<actions>
Expand Down

0 comments on commit b21d808

Please sign in to comment.