Skip to content

Commit

Permalink
Add Server Panel and refactored code #152
Browse files Browse the repository at this point in the history
  • Loading branch information
lorriborri committed Aug 12, 2024
1 parent 3d520a1 commit 2a3734e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mercedesbenz.sechub.plugin.idea.window;

import javax.swing.*;

public interface SecHubPanel {
JPanel getContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.awt.event.MouseEvent;
import java.util.UUID;

public class SecHubReportPanel {
public class SecHubReportPanel implements SecHubPanel {
private static final Logger LOG = Logger.getInstance(SecHubReportPanel.class);
private static final int SECHUB_REPORT_DEFAULT_GAP = 5;
private static SecHubReportPanel INSTANCE;
Expand Down Expand Up @@ -398,7 +398,7 @@ private void showInEditor(FindingNode callStep) {
showInEditorSupport.showInEditor(toolWindow, callStep);
}


@Override
public JPanel getContent() {
return contentPanel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mercedesbenz.sechub.plugin.idea.window;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBTextField;
import com.mercedesbenz.sechub.sdk.settings.AppSettings;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class SecHubServerPanel implements SecHubPanel{

private static final Logger LOG = Logger.getInstance(SecHubServerPanel.class);
private static SecHubServerPanel INSTANCE;
private JPanel contentPanel;
private final JBTextField serverUrlText = new JBTextField();

public SecHubServerPanel() {
createComponents();
}

public static void registerInstance(SecHubServerPanel secHubToolWindow) {
LOG.info("register tool windows instance:" + secHubToolWindow);
INSTANCE = secHubToolWindow;
}

public static SecHubServerPanel getInstance() {
return INSTANCE;
}

@Override
public JPanel getContent() {
return contentPanel;
}

public void update(String serverURL) {
serverUrlText.setText(serverURL);
}

private void createComponents() {
contentPanel = new JBPanel<>();
contentPanel.setLayout(new BorderLayout());

JPanel top = new JPanel(new BorderLayout());
String serverURL = Objects.requireNonNull(AppSettings.getInstance().getState()).serverURL;
serverUrlText.setText(serverURL);
serverUrlText.setEditable(false);

top.add(new JLabel("Server URL: "), BorderLayout.WEST);
top.add(serverUrlText);
contentPanel.add(top, BorderLayout.NORTH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class SecHubToolWindowFactory implements ToolWindowFactory, DumbAware {
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {

SecHubServerPanel serverPanel = new SecHubServerPanel();
SecHubServerPanel.registerInstance(serverPanel);

SecHubReportPanel reportPanel = new SecHubReportPanel(toolWindow);
SecHubReportPanel.registerInstance(reportPanel);

Expand All @@ -35,23 +38,24 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
// toolbarActions.add(importAction);
// toolbarActions.add(resetReportData);

addToolWindowTab(toolWindow, reportPanel, toolbar);
addToolWindowTab(toolWindow, reportPanel, toolbar, "Report");
addToolWindowTab(toolWindow, serverPanel, toolbar, "Server");

List<AnAction> titleActions = new ArrayList<>();
titleActions.add(importAction);
titleActions.add(resetReportData);
toolWindow.setTitleActions(titleActions);
}

private static void addToolWindowTab(@NotNull ToolWindow toolWindow, SecHubReportPanel reportPanel, ActionToolbar toolbar) {
private static void addToolWindowTab(@NotNull ToolWindow toolWindow, SecHubPanel panel, ActionToolbar toolbar, String name) {
ContentFactory contentFactory = ContentFactory.getInstance();
SimpleToolWindowPanel toolWindowPanel = new SimpleToolWindowPanel(false,false);
toolWindowPanel.add(reportPanel.getContent());
toolWindowPanel.add(panel.getContent());

toolbar.setTargetComponent(toolWindowPanel);
toolWindowPanel.setToolbar(toolbar.getComponent());

Content content = contentFactory.createContent(toolWindowPanel, "Report", false);
Content content = contentFactory.createContent(toolWindowPanel, name, false);
toolWindow.getContentManager().addContent(content);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.intellij.sdk.settings;
package com.mercedesbenz.sechub.sdk.settings;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
Expand All @@ -8,7 +8,7 @@
import org.jetbrains.annotations.NotNull;

@State(
name = "org.intellij.sdk.settings.AppSettings",
name = "com.mercedesbenz.sechub.sdk.settings.AppSettings",
storages = @Storage("SdkSettingsPlugin.xml")
)
public final class AppSettings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.intellij.sdk.settings;
package com.mercedesbenz.sechub.sdk.settings;

import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBPasswordField;
Expand All @@ -7,11 +7,7 @@
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.Arrays;

/**
* Supports creating and managing a {@link JPanel} for the Settings Dialog.
*/
public class AppSettingsComponent {

private final JPanel mainPanel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.intellij.sdk.settings;
package com.mercedesbenz.sechub.sdk.settings;

import com.intellij.credentialStore.CredentialAttributes;
import com.intellij.credentialStore.CredentialAttributesKt;
import com.intellij.credentialStore.Credentials;
import com.intellij.ide.passwordSafe.PasswordSafe;
import com.intellij.openapi.options.Configurable;
import com.mercedesbenz.sechub.plugin.idea.window.SecHubServerPanel;
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 {
Expand All @@ -20,7 +21,6 @@ final class AppSettingsConfigurable implements Configurable {

private final String sechubCredentialsKey = "SECHUB_CREDENTIALS";


// A default constructor with no arguments is required because
// this implementation is registered as an applicationConfigurable

Expand Down Expand Up @@ -65,8 +65,11 @@ public void apply() {
AppSettings.State state =
Objects.requireNonNull(AppSettings.getInstance().getState());
state.serverURL = appSettingsComponent.getServerUrlText();
// Updating the server URL in the SecHubServerPanel
SecHubServerPanel secHubServerPanel = SecHubServerPanel.getInstance();
secHubServerPanel.update(state.serverURL);

CredentialAttributes attributes = createCredentialAttributes(sechubCredentialsKey);
CredentialAttributes attributes = createCredentialAttributes();
Credentials credentials = new Credentials(appSettingsComponent.getUserNameText(), appSettingsComponent.getApiTokenPassword());

PasswordSafe.getInstance().set(attributes, credentials);
Expand Down Expand Up @@ -97,14 +100,14 @@ public void disposeUIResources() {
}

private Credentials retrieveCredentials() {
CredentialAttributes attributes = createCredentialAttributes(sechubCredentialsKey);
CredentialAttributes attributes = createCredentialAttributes();
PasswordSafe passwordSafe = PasswordSafe.getInstance();
return passwordSafe.get(attributes);
}

private CredentialAttributes createCredentialAttributes(String key) {
private CredentialAttributes createCredentialAttributes() {
return new CredentialAttributes(
CredentialAttributesKt.generateServiceName("SecHub", key)
CredentialAttributesKt.generateServiceName("SecHub", sechubCredentialsKey)
);
}
}
7 changes: 4 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<!-- 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"/>
<!-- Extension for storing server credentials (package need to be in main/java) -->
<applicationService serviceImplementation="com.mercedesbenz.sechub.sdk.settings.AppSettings"/>

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

Expand Down

0 comments on commit 2a3734e

Please sign in to comment.