refactoring: extract part of functionality in StatusPanel class #7199

This commit is contained in:
kifir23917 2025-01-20 19:36:41 +03:00 committed by kifir23917
parent 6891f4da1e
commit 7fa49183a7
3 changed files with 108 additions and 79 deletions

View File

@ -8,7 +8,7 @@ public class StatusWindowAnimation {
animation = new StatusAnimation(new StatusAnimation.StatusConsumer() {
@Override
public void onStatus(String niceStatus) {
wnd.setStatus(niceStatus);
wnd.getContent().setStatus(niceStatus);
}
}, "Working");
}

View File

@ -1,51 +1,23 @@
package com.rusefi.ui;
import com.devexperts.logging.Logging;
import com.rusefi.FileLog;
import com.rusefi.core.io.BundleUtil;
import com.rusefi.core.rusEFIVersion;
import com.rusefi.core.ui.FrameHelper;
import com.rusefi.io.UpdateOperationCallbacks;
import com.rusefi.ui.util.UiUtils;
import com.rusefi.ui.widgets.StatusPanel;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.FileLog.isWindows;
/**
* Andrey Belomutskiy, (c) 2013-2020
* 3/7/2015
*/
public class StatusWindow implements StatusConsumer, UpdateOperationCallbacks {
private static final Logging log = getLogging(StatusWindow.class);
private static final Color LIGHT_RED = new Color(255, 102, 102);
private static final Color LIGHT_GREEN = new Color(102, 255, 102);
public class StatusWindow implements StatusConsumer {
// todo: extract driver from console bundle? find a separate driver bundle?
private final JTextArea logTextArea = new JTextArea();
private final JPanel content = new JPanel(new BorderLayout());
private final JLabel bottomStatusLabel = new JLabel();
StatusPanel content = new StatusPanel();
@NotNull
protected final FrameHelper frameHelper = new FrameHelper();
public StatusWindow() {
logTextArea.setLineWrap(true);
JScrollPane messagesScroll = new JScrollPane(logTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
content.add(messagesScroll, BorderLayout.CENTER);
content.add(bottomStatusLabel, BorderLayout.SOUTH);
appendLine("Console version " + rusEFIVersion.CONSOLE_VERSION);
appendLine(FileLog.getOsName() + " " + System.getProperty("os.version"));
appendLine("Bundle " + BundleUtil.readBundleFullNameNotNull());
}
@NotNull
@ -53,34 +25,17 @@ public class StatusWindow implements StatusConsumer, UpdateOperationCallbacks {
return frameHelper;
}
public JPanel getContent() {
public StatusPanel getContent() {
return content;
}
public void setErrorState() {
logTextArea.setBackground(LIGHT_RED);
copyContentToClipboard();
content.setErrorState();
}
@Override
public void log(final String message, final boolean breakLineOnTextArea, final boolean sendToLogger) {
append(message, breakLineOnTextArea, sendToLogger);
}
@Override
public void done() {
setSuccessState();
}
@Override
public void error() {
setErrorState();
}
public void setSuccessState() {
logTextArea.setBackground(LIGHT_GREEN);
content.setSuccessState();
}
public JFrame getFrame() {
@ -91,36 +46,11 @@ public class StatusWindow implements StatusConsumer, UpdateOperationCallbacks {
frameHelper.getFrame().setTitle(title);
frameHelper.showFrame(content, false);
UiUtils.centerWindow(frameHelper.getFrame());
logTextArea.setText(""); // let's remove stuff from previous invocation
content.clear(); // let's remove stuff from previous invocation
}
@Override
public void append(final String string, final boolean breakLineOnTextArea, final boolean sendToLogger) {
// todo: check if AWT thread and do not invokeLater if already on AWT thread
SwingUtilities.invokeLater(() -> {
String s = string.replaceAll(Character.toString((char) 219), "");
if (sendToLogger) {
log.info(s);
}
String stringForTestArea = s;
if (breakLineOnTextArea) {
stringForTestArea += "\r\n";
}
logTextArea.append(stringForTestArea);
UiUtils.trueLayout(logTextArea);
});
}
public void copyContentToClipboard() {
// kludge: due to 'append' method using invokeLater even while on AWT thread we also need invokeLater to
// actually get overall status message
SwingUtilities.invokeLater(() -> Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(logTextArea.getText()), null));
appendLine("hint: error state is already in your clipboard, please use PASTE or Ctrl-V while reporting issues");
}
public void setStatus(String status) {
bottomStatusLabel.setText(status);
content.append(string, breakLineOnTextArea, sendToLogger);
}
}

View File

@ -0,0 +1,99 @@
package com.rusefi.ui.widgets;
import com.devexperts.logging.Logging;
import com.rusefi.FileLog;
import com.rusefi.core.io.BundleUtil;
import com.rusefi.core.rusEFIVersion;
import com.rusefi.io.UpdateOperationCallbacks;
import com.rusefi.ui.StatusWindow;
import com.rusefi.ui.util.UiUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import static com.devexperts.logging.Logging.getLogging;
public class StatusPanel extends JPanel implements UpdateOperationCallbacks {
private static final Logging log = getLogging(StatusWindow.class);
private static final Color LIGHT_RED = new Color(255, 102, 102);
private static final Color LIGHT_GREEN = new Color(102, 255, 102);
private final JTextArea logTextArea = new JTextArea();
private final JLabel bottomStatusLabel = new JLabel();
public StatusPanel() {
super(new BorderLayout());
logTextArea.setLineWrap(true);
JScrollPane messagesScroll = new JScrollPane(logTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
};
super.add(messagesScroll, BorderLayout.CENTER);
super.add(bottomStatusLabel, BorderLayout.SOUTH);
appendLine("Console version " + rusEFIVersion.CONSOLE_VERSION);
appendLine(FileLog.getOsName() + " " + System.getProperty("os.version"));
appendLine("Bundle " + BundleUtil.readBundleFullNameNotNull());
}
public void setErrorState() {
logTextArea.setBackground(LIGHT_RED);
copyContentToClipboard();
}
@Override
public void log(final String message, final boolean breakLineOnTextArea, final boolean sendToLogger) {
append(message, breakLineOnTextArea, sendToLogger);
}
@Override
public void done() {
setSuccessState();
}
@Override
public void error() {
setErrorState();
}
public void setSuccessState() {
logTextArea.setBackground(LIGHT_GREEN);
}
public void copyContentToClipboard() {
// kludge: due to 'append' method using invokeLater even while on AWT thread we also need invokeLater to
// actually get overall status message
SwingUtilities.invokeLater(() -> Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(logTextArea.getText()), null));
appendLine("hint: error state is already in your clipboard, please use PASTE or Ctrl-V while reporting issues");
}
public void clear() {
logTextArea.setText("");
}
@Override
public void append(final String string, final boolean breakLineOnTextArea, final boolean sendToLogger) {
// todo: check if AWT thread and do not invokeLater if already on AWT thread
SwingUtilities.invokeLater(() -> {
String s = string.replaceAll(Character.toString((char) 219), "");
if (sendToLogger) {
log.info(s);
}
String stringForTestArea = s;
if (breakLineOnTextArea) {
stringForTestArea += "\r\n";
}
logTextArea.append(stringForTestArea);
UiUtils.trueLayout(logTextArea);
});
}
public void setStatus(String status) {
bottomStatusLabel.setText(status);
}
}