|
| 1 | +package io.github.gabrielbbaldez.stacktale.idea; |
| 2 | + |
| 3 | +import com.intellij.openapi.options.Configurable; |
| 4 | +import com.intellij.openapi.project.Project; |
| 5 | +import com.intellij.ui.JBIntSpinner; |
| 6 | +import com.intellij.ui.components.JBLabel; |
| 7 | +import com.intellij.ui.components.JBTextField; |
| 8 | +import com.intellij.util.ui.FormBuilder; |
| 9 | +import org.jetbrains.annotations.Nls; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +import javax.swing.JComponent; |
| 14 | +import javax.swing.JPanel; |
| 15 | + |
| 16 | +/** Settings → Tools → Stacktale: point the tool window at a log file and tune the poll. */ |
| 17 | +public final class StacktaleConfigurable implements Configurable { |
| 18 | + |
| 19 | + private final Project project; |
| 20 | + |
| 21 | + private JBTextField logPathField; |
| 22 | + private JBIntSpinner pollSpinner; |
| 23 | + private JPanel panel; |
| 24 | + |
| 25 | + public StacktaleConfigurable(@NotNull Project project) { |
| 26 | + this.project = project; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public @Nls(capitalization = Nls.Capitalization.Title) String getDisplayName() { |
| 31 | + return "Stacktale"; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public @Nullable JComponent createComponent() { |
| 36 | + logPathField = new JBTextField(); |
| 37 | + pollSpinner = new JBIntSpinner( |
| 38 | + StSettings.DEFAULT_POLL_SECONDS, |
| 39 | + StSettings.MIN_POLL_SECONDS, |
| 40 | + StSettings.MAX_POLL_SECONDS); |
| 41 | + |
| 42 | + panel = FormBuilder.createFormBuilder() |
| 43 | + .addLabeledComponent(new JBLabel("Log file:"), logPathField, 1, false) |
| 44 | + .addTooltip("Absolute, or relative to the project root. " |
| 45 | + + "Leave empty to auto-detect errors-ai.log.") |
| 46 | + .addLabeledComponent(new JBLabel("Poll interval (seconds):"), pollSpinner, 1, false) |
| 47 | + .addComponentFillVertically(new JPanel(), 0) |
| 48 | + .getPanel(); |
| 49 | + |
| 50 | + reset(); |
| 51 | + return panel; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean isModified() { |
| 56 | + StacktaleSettings settings = StacktaleSettings.getInstance(project); |
| 57 | + return !settings.logPath().equals(logPath()) || settings.pollSeconds() != pollSeconds(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void apply() { |
| 62 | + StacktaleSettings settings = StacktaleSettings.getInstance(project); |
| 63 | + settings.setLogPath(logPath()); |
| 64 | + settings.setPollSeconds(pollSeconds()); |
| 65 | + |
| 66 | + // The next tick would pick both up on its own; re-reading now means a changed path |
| 67 | + // shows its reports as soon as the dialog closes. |
| 68 | + StacktaleReportService.getInstance(project).refreshNow(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void reset() { |
| 73 | + StacktaleSettings settings = StacktaleSettings.getInstance(project); |
| 74 | + logPathField.setText(settings.logPath()); |
| 75 | + pollSpinner.setNumber(settings.pollSeconds()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void disposeUIResources() { |
| 80 | + panel = null; |
| 81 | + logPathField = null; |
| 82 | + pollSpinner = null; |
| 83 | + } |
| 84 | + |
| 85 | + private String logPath() { |
| 86 | + return StSettings.normalizeLogPath(logPathField.getText()); |
| 87 | + } |
| 88 | + |
| 89 | + /** The spinner already holds the value in range, so the settings class has nothing to clamp. */ |
| 90 | + private int pollSeconds() { |
| 91 | + return pollSpinner.getNumber(); |
| 92 | + } |
| 93 | +} |
0 commit comments