Skip to content

Commit 86ebf70

Browse files
committed
export import settings
1 parent 599084c commit 86ebf70

File tree

3 files changed

+187
-43
lines changed

3 files changed

+187
-43
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/settings/SettingsComponent.java

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.digma.intellij.plugin.settings;
22

33
import com.intellij.icons.AllIcons;
4+
import com.intellij.openapi.fileChooser.*;
5+
import com.intellij.openapi.project.Project;
46
import com.intellij.openapi.ui.*;
57
import com.intellij.ui.*;
68
import com.intellij.ui.components.*;
@@ -49,6 +51,9 @@ class SettingsComponent {
4951

5052
public SettingsComponent() {
5153

54+
var defaultLabelForeground = JBColor.foreground();
55+
56+
5257
extendedObservabilityTextFiled.setToolTipText("package names in format 'my.pkg1;my.pkg2");
5358
extendedObservabilityExcludeTextField.setToolTipText("class/method names to exclude in format 'MyClass;MyOtherClass.myOtherMethod;*get");
5459
var extendedObservabilityTextBoxPanel = new JBPanel<JBPanel<?>>();
@@ -58,8 +63,6 @@ public SettingsComponent() {
5863
extendedObservabilityExcludeTextBoxPanel.setLayout(new BorderLayout());
5964
extendedObservabilityExcludeTextBoxPanel.add(extendedObservabilityExcludeTextField, BorderLayout.CENTER);
6065

61-
var defaultLabelForeground = JBColor.foreground();
62-
6366

6467
var myUrlLabel = new JBLabel("Digma API URL: ");
6568
myApiUrlTextField.setInputVerifier(new InputVerifier() {
@@ -121,7 +124,6 @@ public boolean verify(JComponent input) {
121124

122125

123126
myRuntimeObservabilityBackendUrlLabel.setToolTipText("Where should observability data be sent from the IDE? This would be the Digma collector URL typically listening to port 5050");
124-
125127
myRuntimeObservabilityBackendUrlTextField.setInputVerifier(new InputVerifier() {
126128
@Override
127129
public boolean verify(JComponent input) {
@@ -141,7 +143,6 @@ public boolean verify(JComponent input) {
141143

142144

143145
myEmbeddedJaegerMessage.setForeground(JBColor.BLUE);
144-
145146
var myJaegerLinkModeLabel = new JBLabel("Jaeger link mode: ");
146147
myJaegerLinkModeLabel.setToolTipText("Internal will open the link as an embedded URL within the IDE. "
147148
+ "External will open the link externally to your default browser. "
@@ -157,45 +158,16 @@ public boolean verify(JComponent input) {
157158
+ "Micrometer will use Micrometer tracing, including the annotation of 'Observed' "
158159
);
159160

160-
var resetButton = new JButton("Reset to defaults");
161-
resetButton.setToolTipText("<html><body>Reset the settings to initial defaults</body>");
162-
resetButton.addActionListener(e -> resetToDefaults());
163161

162+
var resetToDefaultsButton = createResetToDefaultsButton();
164163

165-
var userId = "";
166-
if (DigmaDefaultAccountHolder.getInstance().getAccount() != null &&
167-
!DigmaDefaultAccountHolder.getInstance().getAccount().getUserId().equals(DigmaAccountKt.DEFAULT_LOGIN_ID)) {
168-
userId = DigmaDefaultAccountHolder.getInstance().getAccount().getUserId();
169-
}
170-
var userIdLabel = new JBLabel(userId);
171-
userIdLabel.setCopyable(true);
164+
var userIdLabel = createUserIdLabel();
172165

173-
var backendVersionLabel = new JBLabel("Unknown");
174-
var someProject = ProjectUtilsKt.findActiveProject();
175-
if (someProject != null) {
176-
var about = BackendInfoHolder.getInstance(someProject).getAbout();
177-
if (about != null) {
178-
backendVersionLabel.setText(about.getApplicationVersion());
179-
}
180-
}
166+
var backendVersionLabel = createBackendVersionLabel();
181167

168+
var importExportPanel = createImportExportPanel();
182169

183-
pluginResetWarning.setForeground(JBColor.RED);
184-
pluginResetWarning.setVisible(false);
185-
var resetPluginButton = new JButton("Reset plugin");
186-
resetPluginButton.setToolTipText("<html><body>Reset plugin persistent properties to initial values to simulate fresh start</body>");
187-
resetPluginButton.setVisible("true".equalsIgnoreCase(System.getProperty("org.digma.plugin.resetPlugin.enabled")));
188-
resetPluginButton.addActionListener(e -> {
189-
var confirmation = Messages.showYesNoDialog("Are you sure?\n(Plugin will reset and IDE will restart when the settings window is closed)", "Reset Confirmation", AllIcons.General.WarningDialog);
190-
if (confirmation == 0) {
191-
resetPluginRequested = true;
192-
pluginResetWarning.setVisible(true);
193-
} else {
194-
resetPluginRequested = false;
195-
pluginResetWarning.setVisible(false);
196-
}
197-
});
198-
170+
var resetPluginButton = createResetPluginButton();
199171

200172

201173
myMainPanel = FormBuilder.createFormBuilder()
@@ -209,9 +181,10 @@ public boolean verify(JComponent input) {
209181
.addLabeledComponent(myRuntimeObservabilityBackendUrlLabel, myRuntimeObservabilityBackendUrlTextField, 1, false)
210182
.addLabeledComponent("Extended observability (beta)", extendedObservabilityTextBoxPanel, 1, false)
211183
.addLabeledComponent("Extended observability exclude (beta)", extendedObservabilityExcludeTextBoxPanel, 1, false)
212-
.addComponent(resetButton)
184+
.addComponent(resetToDefaultsButton)
213185
.addLabeledComponent(new JBLabel("User id"), userIdLabel)
214186
.addLabeledComponent(new JBLabel("Backend version"), backendVersionLabel)
187+
.addComponent(importExportPanel)
215188
.addComponent(resetPluginButton)
216189
.addComponent(pluginResetWarning)
217190
.addComponentFillVertically(new JPanel(), 0)
@@ -348,10 +321,12 @@ public void resetResetPluginRequested() {
348321
}
349322

350323

351-
352324
private void resetToDefaults() {
353325
//create a new SettingsState object and use it for default startup values
354-
SettingsState settingsState = new SettingsState();
326+
resetFromSettings(new SettingsState());
327+
}
328+
329+
private void resetFromSettings(SettingsState settingsState) {
355330
resetResetPluginRequested();
356331
hidePluginResetWarning();
357332
this.setApiUrl(settingsState.getApiUrl());
@@ -370,4 +345,102 @@ private void resetToDefaults() {
370345
public void hidePluginResetWarning() {
371346
this.pluginResetWarning.setVisible(false);
372347
}
348+
349+
350+
@NotNull
351+
private JButton createResetToDefaultsButton() {
352+
var resetButton = new JButton("Reset to defaults");
353+
resetButton.setToolTipText("<html><body>Reset the settings to initial defaults</body>");
354+
resetButton.addActionListener(e -> resetToDefaults());
355+
return resetButton;
356+
}
357+
358+
359+
@NotNull
360+
private static JBLabel createUserIdLabel() {
361+
var userId = "";
362+
if (DigmaDefaultAccountHolder.getInstance().getAccount() != null &&
363+
!DigmaDefaultAccountHolder.getInstance().getAccount().getUserId().equals(DigmaAccountKt.DEFAULT_LOGIN_ID)) {
364+
userId = DigmaDefaultAccountHolder.getInstance().getAccount().getUserId();
365+
}
366+
var userIdLabel = new JBLabel(userId);
367+
userIdLabel.setCopyable(true);
368+
return userIdLabel;
369+
}
370+
371+
372+
@NotNull
373+
private static JBLabel createBackendVersionLabel() {
374+
var backendVersionLabel = new JBLabel("Unknown");
375+
var someProject = ProjectUtilsKt.findActiveProject();
376+
if (someProject != null) {
377+
var about = BackendInfoHolder.getInstance(someProject).getAbout();
378+
if (about != null) {
379+
backendVersionLabel.setText(about.getApplicationVersion());
380+
}
381+
}
382+
return backendVersionLabel;
383+
}
384+
385+
386+
private JPanel createImportExportPanel() {
387+
//noinspection ExtractMethodRecommender
388+
var exportSettingsButton = new JButton("Export Settings");
389+
exportSettingsButton.addActionListener(e -> {
390+
391+
Messages.showInfoMessage("I Will export the saved settings, Not necessarily what is now showing. to export the shown values please first save and reopen settings to export.", "Export");
392+
393+
var dialog = FileChooserFactory.getInstance().createSaveFileDialog(new FileSaverDescriptor("Export To File", "Export settings to file", "conf"), (Project) null);
394+
var file = dialog.save("digma-setting.conf");
395+
if (file != null) {
396+
var exportResult = SettingsUtils.exportSettingsToFile(file.getFile());
397+
if (!exportResult) {
398+
Messages.showErrorDialog("Could not export settings,Please check the logs.", "Export Error");
399+
}
400+
}
401+
});
402+
403+
var importSettingsButton = new JButton("Import Settings");
404+
importSettingsButton.addActionListener(e -> {
405+
var dialog = FileChooserFactory.getInstance().createPathChooser(new FileChooserDescriptor(true, false, false, false, false, false), null, myMainPanel);
406+
dialog.choose(null, virtualFiles -> {
407+
if (virtualFiles.size() == 1) {
408+
var properties = SettingsUtils.importSettingsFromFile(virtualFiles.get(0));
409+
if (properties == null) {
410+
Messages.showErrorDialog("Could not import settings,Please check the logs.", "Import Error");
411+
} else {
412+
resetFromSettings(SettingsState.fromProperties(properties));
413+
}
414+
}
415+
});
416+
});
417+
418+
var importExportPanel = new JPanel();
419+
importExportPanel.add(exportSettingsButton);
420+
importExportPanel.add(importSettingsButton);
421+
422+
return importExportPanel;
423+
}
424+
425+
426+
@NotNull
427+
private JButton createResetPluginButton() {
428+
pluginResetWarning.setForeground(JBColor.RED);
429+
pluginResetWarning.setVisible(false);
430+
var resetPluginButton = new JButton("Reset plugin");
431+
resetPluginButton.setToolTipText("<html><body>Reset plugin persistent properties to initial values to simulate fresh start</body>");
432+
resetPluginButton.setVisible("true".equalsIgnoreCase(System.getProperty("org.digma.plugin.resetPlugin.enabled")));
433+
resetPluginButton.addActionListener(e -> {
434+
var confirmation = Messages.showYesNoDialog("Are you sure?\n(Plugin will reset and IDE will restart when the settings window is closed)", "Reset Confirmation", AllIcons.General.WarningDialog);
435+
if (confirmation == Messages.YES) {
436+
resetPluginRequested = true;
437+
pluginResetWarning.setVisible(true);
438+
} else {
439+
resetPluginRequested = false;
440+
pluginResetWarning.setVisible(false);
441+
}
442+
});
443+
return resetPluginButton;
444+
}
445+
373446
}

ide-common/src/main/java/org/digma/intellij/plugin/settings/SettingsState.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class SettingsState implements PersistentStateComponent<SettingsState>, D
3838

3939
@NotNull
4040
private String apiUrl = DEFAULT_API_URL;
41-
4241
@Nullable
4342
private String apiToken = null;
4443
@Nullable
@@ -207,5 +206,43 @@ public String getNormalizedExtendedObservabilityExcludes() {
207206
}
208207

209208

209+
public Properties asProperties() {
210+
Properties properties = new Properties();
211+
properties.put("apiUrl", apiUrl);
212+
if (apiToken != null) {
213+
properties.put("apiToken", apiToken);
214+
}
215+
if (jaegerUrl != null) {
216+
properties.put("jaegerUrl", jaegerUrl);
217+
}
218+
if (jaegerQueryUrl != null) {
219+
properties.put("jaegerQueryUrl", jaegerQueryUrl);
220+
}
221+
properties.put("jaegerLinkMode", jaegerLinkMode.name());
222+
properties.put("springBootObservabilityMode", springBootObservabilityMode.name());
223+
properties.put("runtimeObservabilityBackendUrl", runtimeObservabilityBackendUrl);
224+
if (extendedObservability != null) {
225+
properties.put("extendedObservability", extendedObservability);
226+
}
227+
if (extendedObservabilityExcludes != null) {
228+
properties.put("extendedObservabilityExcludes", extendedObservabilityExcludes);
229+
}
230+
return properties;
231+
}
232+
233+
234+
public static SettingsState fromProperties(Properties properties) {
235+
SettingsState settingsState = new SettingsState();
236+
settingsState.apiUrl = (String) properties.get("apiUrl");
237+
settingsState.apiToken = (String) properties.get("apiToken");
238+
settingsState.jaegerUrl = (String) properties.get("jaegerUrl");
239+
settingsState.jaegerQueryUrl = (String) properties.get("jaegerQueryUrl");
240+
settingsState.jaegerLinkMode = JaegerLinkMode.valueOf((String) properties.get("jaegerLinkMode"));
241+
settingsState.springBootObservabilityMode = SpringBootObservabilityMode.valueOf((String) properties.get("springBootObservabilityMode"));
242+
settingsState.runtimeObservabilityBackendUrl = (String) properties.get("runtimeObservabilityBackendUrl");
243+
settingsState.extendedObservability = (String) properties.get("extendedObservability");
244+
settingsState.extendedObservabilityExcludes = (String) properties.get("extendedObservabilityExcludes");
245+
return settingsState;
246+
}
210247
}
211248

ide-common/src/main/java/org/digma/intellij/plugin/settings/SettingsUtils.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package org.digma.intellij.plugin.settings;
22

3-
import java.util.Objects;
3+
import com.intellij.openapi.diagnostic.Logger;
4+
import com.intellij.openapi.vfs.VirtualFile;
5+
import org.digma.intellij.plugin.errorreporting.ErrorReporter;
6+
import org.digma.intellij.plugin.log.Log;
7+
import org.jetbrains.annotations.*;
8+
9+
import java.io.*;
10+
import java.util.*;
411

512
public class SettingsUtils {
613

14+
private static final Logger LOGGER = Logger.getInstance(SettingsUtils.class);
715

816
public static boolean isSettingsPointsToRemoteIp() {
917

@@ -28,4 +36,30 @@ public static boolean isSettingsPointsToRemoteIp() {
2836
}
2937
}
3038

39+
40+
public static boolean exportSettingsToFile(@NotNull File file) {
41+
try (OutputStream out = new FileOutputStream(file)) {
42+
var properties = SettingsState.getInstance().asProperties();
43+
properties.store(out, "Digma Plugin settings");
44+
return true;
45+
} catch (Throwable e) {
46+
Log.warnWithException(LOGGER, e, "error exporting settings {}", e);
47+
ErrorReporter.getInstance().reportError("SettingsUtils.exportSettingsToFile", e);
48+
}
49+
return false;
50+
}
51+
52+
53+
@Nullable
54+
public static Properties importSettingsFromFile(@NotNull VirtualFile virtualFile) {
55+
try (InputStream in = new FileInputStream(virtualFile.toNioPath().toFile())) {
56+
Properties properties = new Properties();
57+
properties.load(in);
58+
return properties;
59+
} catch (IOException e) {
60+
Log.warnWithException(LOGGER, e, "error importing settings {}", e);
61+
ErrorReporter.getInstance().reportError("SettingsUtils.importSettingsFromFile", e);
62+
}
63+
return null;
64+
}
3165
}

0 commit comments

Comments
 (0)