Also use MaterialComboBox for other material widgets

This commit is contained in:
SiboVG 2024-08-09 19:05:58 +02:00
parent c963d8104c
commit 9f247a6ef9
5 changed files with 78 additions and 54 deletions

View File

@ -21,10 +21,15 @@ import javax.swing.SwingUtilities;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import info.openrocket.core.material.MaterialGroup;
import info.openrocket.core.preferences.ApplicationPreferences;
import info.openrocket.swing.gui.components.SVGOptionPanel;
import info.openrocket.swing.gui.dialogs.preferences.PreferencesDialog;
import info.openrocket.swing.gui.main.BasicFrame;
import info.openrocket.swing.gui.util.FileHelper;
import info.openrocket.swing.gui.util.SwingPreferences;
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import info.openrocket.swing.gui.widgets.MaterialComboBox;
import net.miginfocom.swing.MigLayout;
import info.openrocket.core.document.OpenRocketDocument;
@ -629,10 +634,10 @@ public abstract class FinSetConfig extends RocketComponentConfig {
MaterialModel mm = new MaterialModel(filletPanel, document, component, Material.Type.BULK, "FilletMaterial");
register(mm);
JComboBox<Material> materialCombo = new JComboBox<>(mm);
//// The component material affects the weight of the component.
materialCombo.setToolTipText(trans.get("MaterialPanel.combo.ttip.ComponentMaterialAffects"));
// Material selection combo box
GroupableAndSearchableComboBox<MaterialGroup, Material> materialCombo = MaterialComboBox.createComboBox(document, mm);
materialCombo.setToolTipText(trans.get("MaterialPanel.combo.ttip.ComponentMaterialAffects"));
filletPanel.add(materialCombo, "spanx 4, growx");
order.add(materialCombo);
filletPanel.setToolTipText(tip);

View File

@ -5,6 +5,7 @@ import info.openrocket.core.util.Invalidatable;
import info.openrocket.swing.gui.dialogs.preferences.PreferencesDialog;
import info.openrocket.swing.gui.main.BasicFrame;
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import info.openrocket.swing.gui.widgets.MaterialComboBox;
import net.miginfocom.swing.MigLayout;
import info.openrocket.core.document.OpenRocketDocument;
@ -52,36 +53,8 @@ public class MaterialPanel extends JPanel implements Invalidatable, Invalidating
MaterialModel mm = new MaterialModel(this, document, component, type, partName);
register(mm);
// Set custom material button
JButton customMaterialButton = new JButton(trans.get("MaterialPanel.but.AddCustomMaterial"));
customMaterialButton.addActionListener(e -> {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mm.addCustomMaterial();
}
});
});
// Edit materials button
JButton editMaterialsButton = new JButton(trans.get("MaterialPanel.but.EditMaterials"));
editMaterialsButton.addActionListener(e -> {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (BasicFrame frame : BasicFrame.getAllFrames()) {
if (frame.getRocketPanel().getDocument() == document) {
PreferencesDialog.showPreferences(frame, 5);
return;
}
}
}
});
});
// Material selection combo box
this.materialCombo = MaterialComboBox.createComboBox(mm, customMaterialButton, editMaterialsButton);
this.materialCombo.setSelectedItem(mm.getSelectedItem());
this.materialCombo = MaterialComboBox.createComboBox(document, mm);
this.materialCombo.setToolTipText(trans.get("MaterialPanel.combo.ttip.ComponentMaterialAffects"));
this.add(this.materialCombo, "spanx 4, growx, wrap paragraph");
order.add(this.materialCombo);
@ -156,23 +129,4 @@ public class MaterialPanel extends JPanel implements Invalidatable, Invalidating
i.invalidateMe();
}
}
public static class MaterialComboBox extends JComboBox<Material> {
private static final Translator trans = Application.getTranslator();
public static GroupableAndSearchableComboBox<MaterialGroup, Material> createComboBox(
MaterialModel mm, Component... extraCategoryWidgets) {
return new GroupableAndSearchableComboBox<>(mm,
trans.get("MaterialPanel.MaterialComboBox.placeholder"), extraCategoryWidgets) {
@Override
public String getDisplayString(Material item) {
String baseText = item.toString();
if (item.isUserDefined()) {
baseText = "(ud) " + baseText;
}
return baseText;
}
};
}
}
}

View File

@ -18,6 +18,9 @@ import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;
import info.openrocket.core.material.MaterialGroup;
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import info.openrocket.swing.gui.widgets.MaterialComboBox;
import net.miginfocom.swing.MigLayout;
import info.openrocket.core.document.OpenRocketDocument;
@ -89,7 +92,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig {
MaterialModel mm = new MaterialModel(canopyPanel, document, component, Material.Type.SURFACE);
register(mm);
JComboBox<Material> surfaceMaterialCombo = new JComboBox<>(mm);
GroupableAndSearchableComboBox<MaterialGroup, Material> surfaceMaterialCombo = MaterialComboBox.createComboBox(document, mm);
surfaceMaterialCombo.setToolTipText(trans.get("ParachuteCfg.combo.MaterialModel"));
canopyPanel.add(surfaceMaterialCombo, "spanx, growx, wrap 15lp");
order.add(surfaceMaterialCombo);
@ -160,7 +163,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig {
mm = new MaterialModel(shroudPanel, document, component, Material.Type.LINE, "LineMaterial");
register(mm);
JComboBox<Material> shroudMaterialCombo = new JComboBox<>(mm);
GroupableAndSearchableComboBox<MaterialGroup, Material> shroudMaterialCombo = MaterialComboBox.createComboBox(document, mm);
shroudPanel.add(shroudMaterialCombo, "spanx, growx");
order.add(shroudMaterialCombo);

View File

@ -15,6 +15,9 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import info.openrocket.core.material.MaterialGroup;
import info.openrocket.swing.gui.widgets.GroupableAndSearchableComboBox;
import info.openrocket.swing.gui.widgets.MaterialComboBox;
import net.miginfocom.swing.MigLayout;
import info.openrocket.core.document.OpenRocketDocument;
@ -111,7 +114,7 @@ public class StreamerConfig extends RecoveryDeviceConfig {
MaterialModel mm = new MaterialModel(panel, document, component, Material.Type.SURFACE);
register(mm);
JComboBox<Material> streamerMaterialCombo = new JComboBox<>(mm);
GroupableAndSearchableComboBox<MaterialGroup, Material> streamerMaterialCombo = MaterialComboBox.createComboBox(document, mm);
//// The component material affects the weight of the component.
streamerMaterialCombo.setToolTipText(trans.get("StreamerCfg.combo.ttip.MaterialModel"));
panel.add(streamerMaterialCombo, "spanx 3, growx, wrap 15lp");

View File

@ -0,0 +1,59 @@
package info.openrocket.swing.gui.widgets;
import info.openrocket.core.document.OpenRocketDocument;
import info.openrocket.core.l10n.Translator;
import info.openrocket.core.material.Material;
import info.openrocket.core.material.MaterialGroup;
import info.openrocket.core.startup.Application;
import info.openrocket.swing.gui.adaptors.MaterialModel;
import info.openrocket.swing.gui.dialogs.preferences.PreferencesDialog;
import info.openrocket.swing.gui.main.BasicFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
public abstract class MaterialComboBox extends JComboBox<Material> {
private static final Translator trans = Application.getTranslator();
public static GroupableAndSearchableComboBox<MaterialGroup, Material> createComboBox(OpenRocketDocument document, MaterialModel mm) {
// Set custom material button
JButton customMaterialButton = new JButton(trans.get("MaterialPanel.but.AddCustomMaterial"));
customMaterialButton.addActionListener(e -> {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mm.addCustomMaterial();
}
});
});
// Edit materials button
JButton editMaterialsButton = new JButton(trans.get("MaterialPanel.but.EditMaterials"));
editMaterialsButton.addActionListener(e -> {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
for (BasicFrame frame : BasicFrame.getAllFrames()) {
if (frame.getRocketPanel().getDocument() == document) {
PreferencesDialog.showPreferences(frame, 5);
return;
}
}
}
});
});
return new GroupableAndSearchableComboBox<>(mm,
trans.get("MaterialPanel.MaterialComboBox.placeholder"), customMaterialButton, editMaterialsButton) {
@Override
public String getDisplayString(Material item) {
String baseText = item.toString();
if (item.isUserDefined()) {
baseText = "(ud) " + baseText;
}
return baseText;
}
};
}
}