From 13f47a755d8b283f6ffcdedac683c575c50c2f37 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Fri, 16 Feb 2024 01:03:10 +0100 Subject: [PATCH] Separate warnings tooltip to sim table columns --- core/resources/l10n/messages.properties | 6 +- .../openrocket/logging/MessagePriority.java | 25 ++++- .../net/sf/openrocket/logging/MessageSet.java | 17 +++ .../openrocket/gui/main/SimulationPanel.java | 100 +++++++++++++++--- .../openrocket/gui/util/ColorConversion.java | 10 ++ .../net/sf/openrocket/gui/util/UITheme.java | 2 +- 6 files changed, 141 insertions(+), 19 deletions(-) diff --git a/core/resources/l10n/messages.properties b/core/resources/l10n/messages.properties index 65080c7f0..2d2cc8b79 100644 --- a/core/resources/l10n/messages.properties +++ b/core/resources/l10n/messages.properties @@ -634,8 +634,10 @@ simpanel.ttip.outdated = Out of date
Click simpanel.ttip.external = Imported data simpanel.ttip.notSimulated = Not simulated yet
Click Run simulations to simulate. simpanel.ttip.noData = No simulation data available. -simpanel.ttip.noWarnings = No warnings. -simpanel.ttip.warnings = Warnings: +simpanel.ttip.noWarnings = No warnings. +simpanel.ttip.criticalWarnings = Critical Warnings: +simpanel.ttip.normalWarnings = Warnings: +simpanel.ttip.informativeWarnings = Informative: simpanel.ttip.simAbort = Simulation Abort simpanel.msg.invalidCopySelection = Invalid copy selection diff --git a/core/src/net/sf/openrocket/logging/MessagePriority.java b/core/src/net/sf/openrocket/logging/MessagePriority.java index 8c3f01f0e..885f6bcea 100644 --- a/core/src/net/sf/openrocket/logging/MessagePriority.java +++ b/core/src/net/sf/openrocket/logging/MessagePriority.java @@ -4,7 +4,26 @@ package net.sf.openrocket.logging; * The priority of a message. */ public enum MessagePriority { - LOW, - NORMAL, - HIGH + LOW("LOW"), + NORMAL("NORMAL"), + HIGH("HIGH"); + + private String exportLabel; + + MessagePriority(String exportLabel) { + this.exportLabel = exportLabel; + } + + public String getExportLabel() { + return exportLabel; + } + + public static MessagePriority fromExportLabel(String exportLabel) { + for (MessagePriority priority : MessagePriority.values()) { + if (priority.exportLabel.equals(exportLabel)) { + return priority; + } + } + return NORMAL; + } } diff --git a/core/src/net/sf/openrocket/logging/MessageSet.java b/core/src/net/sf/openrocket/logging/MessageSet.java index 3e7ace376..805d4108d 100644 --- a/core/src/net/sf/openrocket/logging/MessageSet.java +++ b/core/src/net/sf/openrocket/logging/MessageSet.java @@ -8,6 +8,7 @@ import net.sf.openrocket.util.Mutable; import java.util.AbstractSet; import java.util.Iterator; +import java.util.List; /** * A set that contains multiple Messages. When adding a @@ -131,6 +132,22 @@ public abstract class MessageSet extends AbstractSet imple return count; } + /** + * Returns a list of messages with the specified priority. + * + * @param priority the priority of the messages to retrieve + * @return a list of messages with the specified priority + */ + public List getMessagesWithPriority(MessagePriority priority) { + List list = new ArrayList<>(); + for (E m : messages) { + if (m.getPriority() == priority) { + list.add(m); + } + } + return list; + } + public void immute() { mutable.immute(); } diff --git a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java index 682b4d4f5..5f762ee11 100644 --- a/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java +++ b/swing/src/net/sf/openrocket/gui/main/SimulationPanel.java @@ -2,6 +2,7 @@ package net.sf.openrocket.gui.main; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; @@ -19,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.Serial; import java.util.Comparator; +import java.util.List; import javax.swing.AbstractAction; import javax.swing.Box; @@ -43,16 +45,18 @@ import javax.swing.table.DefaultTableCellRenderer; import net.sf.openrocket.arch.SystemInfo; import net.sf.openrocket.gui.components.CsvOptionPanel; +import net.sf.openrocket.gui.util.ColorConversion; import net.sf.openrocket.gui.util.FileHelper; import net.sf.openrocket.gui.util.GUIUtil; import net.sf.openrocket.gui.util.SwingPreferences; import net.sf.openrocket.gui.util.UITheme; import net.sf.openrocket.gui.widgets.SaveFileChooser; +import net.sf.openrocket.logging.Message; +import net.sf.openrocket.logging.MessagePriority; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.miginfocom.swing.MigLayout; -import net.sf.openrocket.logging.SimulationAbort; import net.sf.openrocket.logging.Warning; import net.sf.openrocket.logging.WarningSet; import net.sf.openrocket.document.OpenRocketDocument; @@ -123,6 +127,16 @@ public class SimulationPanel extends JPanel { private int[] previousSelection = null; + + private static Color dimTextColor; + private static Color warningColor; + private static Color errorColor; + private static Color informationColor; + + static { + initColors(); + } + public SimulationPanel(OpenRocketDocument doc) { super(new MigLayout("fill", "[grow][][][][][][grow]")); @@ -244,15 +258,14 @@ public class SimulationPanel extends JPanel { runSimulation(); } } - // Show the warnings for the simulation - else if (column == 1) { - int selected = simulationTable.convertRowIndexToModel(selectedRow); - SimulationWarningDialog.showWarningDialog(SimulationPanel.this, document.getSimulations().get(selected)); - } } else if (e.getClickCount() == 2) { int selected = simulationTable.convertRowIndexToModel(selectedRow); + // Show the warnings for the simulation + if (column == 1) { + SimulationWarningDialog.showWarningDialog(SimulationPanel.this, document.getSimulations().get(selected)); + } // Edit the simulation or plot/export - if (column > 1) { + else if (column > 1) { simulationTable.clearSelection(); simulationTable.addRowSelectionInterval(selectedRow, selectedRow); @@ -316,6 +329,18 @@ public class SimulationPanel extends JPanel { updateActions(); } + private static void initColors() { + updateColors(); + UITheme.Theme.addUIThemeChangeListener(SimulationPanel::updateColors); + } + + private static void updateColors() { + dimTextColor = GUIUtil.getUITheme().getDimTextColor(); + warningColor = GUIUtil.getUITheme().getWarningColor(); + errorColor = GUIUtil.getUITheme().getErrorColor(); + informationColor = GUIUtil.getUITheme().getInformationColor(); + } + /** * Returns the action used for editing selected simulations. * @return the action used for editing selected simulations. @@ -806,25 +831,55 @@ public class SimulationPanel extends JPanel { tip = new StringBuilder(""); if (includeSimName) { - tip.append("").append(sim.getName()).append("
"); + tip.append("").append(sim.getName()).append(""); } if (data == null) { - tip.append(trans.get("simpanel.ttip.noData")); + tip.append("
").append(trans.get("simpanel.ttip.noData")); return tip.toString(); } WarningSet warnings = data.getWarningSet(); if (warnings.isEmpty()) { - tip.append(trans.get("simpanel.ttip.noWarnings")); + tip.append("
").append(ColorConversion.formatHTMLColor(dimTextColor, trans.get("simpanel.ttip.noWarnings"))); return tip.toString(); } - tip.append(trans.get("simpanel.ttip.warnings")); - for (Warning w : warnings) { - tip.append("
").append(w.toString()); + List criticalWarnings = warnings.getMessagesWithPriority(MessagePriority.HIGH); + List normalWarnings = warnings.getMessagesWithPriority(MessagePriority.NORMAL); + List informativeWarnings = warnings.getMessagesWithPriority(MessagePriority.LOW); + + // Critical warnings + if (!criticalWarnings.isEmpty()) { + tip.append("
") + .append(ColorConversion.formatHTMLColor(errorColor, trans.get("simpanel.ttip.criticalWarnings"))) + .append(""); + for (Message m : criticalWarnings) { + tip.append("
").append(m.toString()); + } } + // Warnings + if (!normalWarnings.isEmpty()) { + tip.append("
") + .append(ColorConversion.formatHTMLColor(warningColor, trans.get("simpanel.ttip.normalWarnings"))) + .append(""); + for (Message m : normalWarnings) { + tip.append("
").append(m.toString()); + } + } + + // Informative warnings + if (!informativeWarnings.isEmpty()) { + tip.append("
") + .append(ColorConversion.formatHTMLColor(informationColor, trans.get("simpanel.ttip.informativeWarnings"))) + .append(""); + for (Message m : informativeWarnings) { + tip.append("
").append(m.toString()); + } + } + + return tip.toString(); } @@ -1190,6 +1245,7 @@ public class SimulationPanel extends JPanel { public WarningsBox(Simulation simulation) { super(BoxLayout.X_AXIS); // Horizontal box + setOpaque(false); this.simulation = simulation; updateContent(); } @@ -1258,11 +1314,29 @@ public class SimulationPanel extends JPanel { JPanel panel = new JPanel(new BorderLayout()); panel.setToolTipText(box.getToolTipText()); panel.add(box, BorderLayout.EAST); // Align to the right within the panel + panel.setOpaque(true); + if (isSelected) { + panel.setBackground(table.getSelectionBackground()); + updateBoxColors(box, table.getSelectionForeground()); + } else { + panel.setBackground(table.getBackground()); + updateBoxColors(box, table.getForeground()); + } return panel; } return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } + + private void updateBoxColors(WarningsBox box, Color foreground) { + // Set the foreground for the box and its child components + box.setForeground(foreground); // Assuming this sets the box's own foreground + for (Component comp : box.getComponents()) { + if (comp instanceof JLabel) { + comp.setForeground(foreground); + } + } + } } private class SimulationTableModel extends ColumnTableModel { diff --git a/swing/src/net/sf/openrocket/gui/util/ColorConversion.java b/swing/src/net/sf/openrocket/gui/util/ColorConversion.java index af1f17c5c..bf12959c1 100644 --- a/swing/src/net/sf/openrocket/gui/util/ColorConversion.java +++ b/swing/src/net/sf/openrocket/gui/util/ColorConversion.java @@ -2,6 +2,8 @@ package net.sf.openrocket.gui.util; import net.sf.openrocket.util.ORColor; +import java.awt.Color; + public class ColorConversion { public static java.awt.Color toAwtColor( ORColor c ) { @@ -17,4 +19,12 @@ public class ColorConversion { } return new ORColor( c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()); } + + public static String formatHTMLColor(Color c, String content) { + if (c == null) { + return null; + } + String hexColor = String.format("#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue()); + return String.format("%s", hexColor, content); + } } diff --git a/swing/src/net/sf/openrocket/gui/util/UITheme.java b/swing/src/net/sf/openrocket/gui/util/UITheme.java index 33e576106..210901ab9 100644 --- a/swing/src/net/sf/openrocket/gui/util/UITheme.java +++ b/swing/src/net/sf/openrocket/gui/util/UITheme.java @@ -230,7 +230,7 @@ public class UITheme { @Override public Color getWarningColor() { - return new Color(180, 90, 17); + return new Color(192, 135, 0); } @Override