diff --git a/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java b/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java new file mode 100644 index 000000000..dfef843f6 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/adaptors/DecalModel.java @@ -0,0 +1,86 @@ +package net.sf.openrocket.gui.adaptors; + +import java.awt.Component; +import java.io.File; + +import javax.swing.AbstractListModel; +import javax.swing.ComboBoxModel; +import javax.swing.JFileChooser; +import javax.swing.SwingUtilities; + +import net.sf.openrocket.appearance.AppearanceBuilder; +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.l10n.Translator; +import net.sf.openrocket.startup.Application; + +public class DecalModel extends AbstractListModel implements ComboBoxModel { + + private static final Translator trans = Application.getTranslator(); + + private static final String NONE_SELECTED = trans.get("lbl.select"); + private static final String SELECT_FILE = trans.get("lbl.choose"); + + private final Component parent; + private final AppearanceBuilder ab; + + private File lastImageDir = null; + + private String[] decals; + + public DecalModel(Component parent, OpenRocketDocument document, AppearanceBuilder ab) { + this.parent = parent; + this.ab = ab; + decals = document.getDecalList().toArray( new String[0] ); + } + + @Override + public int getSize() { + return decals.length + 2; + } + + @Override + public Object getElementAt(int index) { + if (index == 0) { + return NONE_SELECTED; + } + if (index == getSize() - 1) { + return SELECT_FILE; + } + return decals[index-1]; + } + + @Override + public void setSelectedItem(Object item) { + + if (item == null || item.equals(NONE_SELECTED)) { + ab.setImage(null); + } else if (item.equals(SELECT_FILE)) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + File current = lastImageDir; + lastImageDir = current; + + JFileChooser fc = new JFileChooser(current); + int action = fc.showOpenDialog(SwingUtilities.getWindowAncestor(parent)); + if ( action == JFileChooser.APPROVE_OPTION) { + setSelectedItem(fc.getSelectedFile().getAbsolutePath()); + } + } + }); + } else { + ab.setImage( (String) item); + } + } + + @Override + public Object getSelectedItem() { + String name = ab.getImage(); + if (name == null) { + return NONE_SELECTED; + } else { + return name; + } + } + +} diff --git a/core/src/net/sf/openrocket/gui/configdialog/AppearancePanel.java b/core/src/net/sf/openrocket/gui/configdialog/AppearancePanel.java index 0a6dc27a4..d5b2a1f19 100644 --- a/core/src/net/sf/openrocket/gui/configdialog/AppearancePanel.java +++ b/core/src/net/sf/openrocket/gui/configdialog/AppearancePanel.java @@ -3,7 +3,6 @@ package net.sf.openrocket.gui.configdialog; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.File; import java.lang.reflect.Method; import java.util.EventObject; @@ -12,19 +11,19 @@ import javax.swing.JCheckBox; import javax.swing.JColorChooser; import javax.swing.JComboBox; import javax.swing.JDialog; -import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSeparator; import javax.swing.JSpinner; -import javax.swing.JTextField; import javax.swing.SwingConstants; import net.miginfocom.swing.MigLayout; import net.sf.openrocket.appearance.AppearanceBuilder; import net.sf.openrocket.appearance.Decal.EdgeMode; +import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.gui.SpinnerEditor; import net.sf.openrocket.gui.adaptors.BooleanModel; +import net.sf.openrocket.gui.adaptors.DecalModel; import net.sf.openrocket.gui.adaptors.DoubleModel; import net.sf.openrocket.gui.adaptors.EnumModel; import net.sf.openrocket.gui.adaptors.IntegerModel; @@ -47,8 +46,6 @@ public class AppearancePanel extends JPanel { private AppearanceBuilder ab; - private static File lastImageDir = null; - private static final JColorChooser colorChooser = new JColorChooser(); private class ColorActionListener implements ActionListener { @@ -89,7 +86,7 @@ public class AppearancePanel extends JPanel { } } - public AppearancePanel(final RocketComponent c) { + public AppearancePanel(final OpenRocketDocument document, final RocketComponent c) { super(new MigLayout("fill", "[150][grow][150][grow]")); ab = new AppearanceBuilder(c.getAppearance()); @@ -103,8 +100,7 @@ public class AppearancePanel extends JPanel { final JButton ambientColorButton = new JButton(new ColorIcon(ab.getAmbient())); final JButton specularColorButton = new JButton(new ColorIcon(ab.getSpecular())); - final JTextField uriTextField = new JTextField(ab.getImage() != null ? ab.getImage().toString() : ""); - uriTextField.setEditable(false); + final JComboBox textureDropDown = new JComboBox( new DecalModel(this,document,ab));; ab.addChangeListener(new StateChangeListener() { @Override @@ -114,7 +110,6 @@ public class AppearancePanel extends JPanel { ambientColorButton.setIcon(new ColorIcon(ab.getAmbient())); specularColorButton.setIcon(new ColorIcon(ab.getSpecular())); c.setAppearance(ab.getAppearance()); - uriTextField.setText(ab.getImage() != null ? ab.getImage().toString() : ""); } }); @@ -220,27 +215,11 @@ public class AppearancePanel extends JPanel { } {// Texture File - JButton choose; add(new JLabel(trans.get("AppearanceCfg.lbl.Texture"))); JPanel p = new JPanel(new MigLayout("fill, ins 0", "[grow][]")); - p.add(uriTextField, "grow"); - p.add(choose = new JButton(trans.get("AppearanceCfg.lbl.Choose"))); - mDefault.addEnableComponent(uriTextField, false); - mDefault.addEnableComponent(choose, false); + mDefault.addEnableComponent(textureDropDown, false); + p.add(textureDropDown, "grow"); add(p, "span 3, growx, wrap"); - choose.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - File current = lastImageDir; - lastImageDir = current; - - JFileChooser fc = new JFileChooser(current); - if (fc.showOpenDialog(AppearancePanel.this) == JFileChooser.APPROVE_OPTION) { - ab.setImage(fc.getSelectedFile().getAbsolutePath()); - } - - } - }); } { // Diffuse @@ -344,5 +323,5 @@ public class AppearancePanel extends JPanel { } } - + } diff --git a/core/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java b/core/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java index a24deef90..2e8a2a59d 100644 --- a/core/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java +++ b/core/src/net/sf/openrocket/gui/configdialog/RocketComponentConfig.java @@ -108,7 +108,7 @@ public class RocketComponentConfig extends JPanel { if (component.isMassive()) //// Appearance options - tabbedPane.addTab("Appearance", null, new AppearancePanel(component), + tabbedPane.addTab("Appearance", null, new AppearancePanel(document,component), "Appearance Tool Tip"); //// Comment and Specify a comment for the component