Merge pull request #944 from SiboVG/issue-879

[fixes #879] Added real-time color change appearance
This commit is contained in:
Joe Pfeiffer 2021-06-17 19:14:03 -06:00 committed by GitHub
commit e011ca634a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,6 +19,9 @@ import javax.swing.JSlider;
import javax.swing.JSpinner; import javax.swing.JSpinner;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.miginfocom.swing.MigLayout; import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.appearance.Appearance; import net.sf.openrocket.appearance.Appearance;
@ -101,34 +104,72 @@ public class AppearancePanel extends JPanel {
this.o = o; this.o = o;
} }
/**
Changes the color of the selected color to <color>
@param color: color to change the component to
*/
private void changeComponentColor(Color color) {
try {
final Method setMethod = o.getClass().getMethod(
"set" + valueName, net.sf.openrocket.util.Color.class);
if (color == null)
return;
try {
setMethod.invoke(o, ColorConversion
.fromAwtColor(color));
} catch (Throwable e1) {
Application.getExceptionHandler()
.handleErrorCondition(e1);
}
} catch (Throwable e1) {
Application.getExceptionHandler().handleErrorCondition(e1);
}
}
/**
* Change the component's preview color upon a change in color pick. If the user clicks 'OK' in the
* dialog window, the selected color is assigned to the component. If 'Cancel' was clicked, the component's
* color will be reverted to its initial color (color before the appearance editor opened).
*/
@Override @Override
public void actionPerformed(ActionEvent colorClickEvent) { public void actionPerformed(ActionEvent colorClickEvent) {
try { try {
final Method getMethod = o.getClass().getMethod( final Method getMethod = o.getClass().getMethod(
"get" + valueName); "get" + valueName);
final Method setMethod = o.getClass().getMethod(
"set" + valueName, net.sf.openrocket.util.Color.class);
net.sf.openrocket.util.Color c = (net.sf.openrocket.util.Color) getMethod net.sf.openrocket.util.Color c = (net.sf.openrocket.util.Color) getMethod
.invoke(o); .invoke(o);
Color awtColor = ColorConversion.toAwtColor(c); Color awtColor = ColorConversion.toAwtColor(c);
colorChooser.setColor(awtColor); colorChooser.setColor(awtColor);
// Bind a change of color selection to a change in the components color
ColorSelectionModel model = colorChooser.getSelectionModel();
ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
Color selected = colorChooser.getColor();
changeComponentColor(selected);
}
};
model.addChangeListener(changeListener);
JDialog d = JColorChooser.createDialog(AppearancePanel.this, JDialog d = JColorChooser.createDialog(AppearancePanel.this,
trans.get("RocketCompCfg.lbl.Choosecolor"), true, trans.get("RocketCompCfg.lbl.Choosecolor"), true,
colorChooser, new ActionListener() { colorChooser, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent okEvent) { public void actionPerformed(ActionEvent okEvent) {
Color selected = colorChooser.getColor(); changeComponentColor(colorChooser.getColor());
if (selected == null) // Unbind listener to avoid the current component's appearance to change with other components
return; model.removeChangeListener(changeListener);
try {
setMethod.invoke(o, ColorConversion
.fromAwtColor(selected));
} catch (Throwable e1) {
Application.getExceptionHandler()
.handleErrorCondition(e1);
}
} }
}, null); }, new ActionListener() {
@Override
public void actionPerformed(ActionEvent cancelEvent) {
changeComponentColor(awtColor);
// Unbind listener to avoid the current component's appearance to change with other components
model.removeChangeListener(changeListener);
}
});
d.setVisible(true); d.setVisible(true);
} catch (Throwable e1) { } catch (Throwable e1) {
Application.getExceptionHandler().handleErrorCondition(e1); Application.getExceptionHandler().handleErrorCondition(e1);
@ -136,6 +177,7 @@ public class AppearancePanel extends JPanel {
} }
} }
public AppearancePanel(final OpenRocketDocument document, public AppearancePanel(final OpenRocketDocument document,
final RocketComponent c) { final RocketComponent c) {
super(new MigLayout("fill", "[150][grow][150][grow]")); super(new MigLayout("fill", "[150][grow][150][grow]"));