Merge pull request #1508 from SiboVG/issue-1488
[#1488 & #1506] Focus most commonly edited parameter after preset selection + Highlight spinner text after focus gained
This commit is contained in:
commit
c02f9611e7
@ -715,7 +715,7 @@ compaddbuttons.Launchlug = Launch Lug
|
||||
compaddbuttons.RailButton = Rail Button
|
||||
compaddbuttons.InnerComponent = Inner Components
|
||||
compaddbuttons.Innertube = Inner Tube
|
||||
compaddbuttons.Coupler = Coupler
|
||||
compaddbuttons.Coupler = Tube Coupler
|
||||
compaddbuttons.Centeringring = Centering\nRing
|
||||
compaddbuttons.Bulkhead = Bulkhead
|
||||
compaddbuttons.Engineblock = Engine\nBlock
|
||||
|
@ -1,8 +1,14 @@
|
||||
package net.sf.openrocket.gui;
|
||||
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.DefaultFormatter;
|
||||
import javax.swing.text.DefaultFormatterFactory;
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
|
||||
/**
|
||||
* Editable editor for a JSpinner. Simply uses JSpinner.DefaultEditor, which has been made
|
||||
@ -22,6 +28,59 @@ public class SpinnerEditor extends JSpinner.DefaultEditor {
|
||||
DefaultFormatterFactory dff = (DefaultFormatterFactory) getTextField().getFormatterFactory();
|
||||
DefaultFormatter formatter = (DefaultFormatter) dff.getDefaultFormatter();
|
||||
formatter.setOverwriteMode(false);
|
||||
|
||||
|
||||
// Add listeners to select all the text when the field is focussed
|
||||
{
|
||||
getTextField().addFocusListener(new FocusListener() {
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
selectAllText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
}
|
||||
});
|
||||
|
||||
getTextField().addMouseListener(new MouseListener() {
|
||||
private boolean isFocussed = false; // Checks whether the text field was focussed when it was clicked upon
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
// If the text field was focussed when it was clicked upon instead of e.g. tab-switching to gain focus,
|
||||
// then the select all action from the focus listener is ignored (it is replaced by a cursor-click event).
|
||||
// So if we detect such a focus change, then redo the select all action.
|
||||
if (!isFocussed) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JTextField tf = (JTextField) e.getSource();
|
||||
tf.selectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
JTextField tf = (JTextField) e.getSource();
|
||||
isFocussed = tf.hasFocus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,5 +92,17 @@ public class SpinnerEditor extends JSpinner.DefaultEditor {
|
||||
this(spinner);
|
||||
getTextField().setColumns(cols);
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlights all the text in the text field.
|
||||
*/
|
||||
private void selectAllText() {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getTextField().selectAll();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import javax.swing.ComboBoxModel;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import net.sf.openrocket.database.ComponentPresetDatabase;
|
||||
import net.sf.openrocket.gui.configdialog.RocketComponentConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -90,6 +91,7 @@ public class PresetModel extends AbstractListModel implements ComboBoxModel, Com
|
||||
} else {
|
||||
document.addUndoPosition("Use Preset " + component.getComponentName());
|
||||
component.loadPreset((ComponentPreset) item);
|
||||
((RocketComponentConfig) parent).setFocusElement();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ import javax.swing.JDialog;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
import net.sf.openrocket.document.OpenRocketDocument;
|
||||
@ -23,6 +25,12 @@ import net.sf.openrocket.rocketcomponent.SymmetricComponent;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
import net.sf.openrocket.unit.UnitGroup;
|
||||
|
||||
import java.awt.event.FocusEvent;
|
||||
import java.awt.event.FocusListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class BodyTubeConfig extends RocketComponentConfig {
|
||||
|
||||
@ -43,6 +51,7 @@ public class BodyTubeConfig extends RocketComponentConfig {
|
||||
|
||||
JSpinner spin = new JSpinner(length.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(length), "growx");
|
||||
|
@ -64,7 +64,6 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
super(d, c, parent);
|
||||
|
||||
//// General and General properties
|
||||
JPanel rightPanel = new JPanel(new MigLayout());
|
||||
JPanel panel = new JPanel(new MigLayout("gap rel unrel", "[][65lp::][30lp::][]", ""));
|
||||
|
||||
DoubleModel m;
|
||||
@ -86,7 +85,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(od), "growx");
|
||||
panel.add(new BasicSlider(od.getSliderModel(0, 0.04, 0.2)), "w 100lp, wrap");
|
||||
panel.add(new BasicSlider(od.getSliderModel(0, 0.04, 0.2)), "wmin 100lp, growx, wrap");
|
||||
|
||||
if (od.isAutomaticAvailable()) {
|
||||
JCheckBox check = new JCheckBox(od.getAutomaticAction());
|
||||
@ -106,7 +105,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), "w 100lp, wrap");
|
||||
panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), "wmin 100lp, growx, wrap");
|
||||
|
||||
if (m.isAutomaticAvailable()) {
|
||||
JCheckBox check = new JCheckBox(m.getAutomaticAction());
|
||||
@ -127,7 +126,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
panel.add(new BasicSlider(m.getSliderModel(0, 0.01)), "w 100lp, wrap");
|
||||
panel.add(new BasicSlider(m.getSliderModel(0, 0.01)), "wmin 100lp, growx, wrap");
|
||||
|
||||
|
||||
//// Inner tube length
|
||||
@ -138,14 +137,15 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
panel.add(new BasicSlider(m.getSliderModel(0, 0.1, 1.0)), "w 100lp, wrap");
|
||||
panel.add(new BasicSlider(m.getSliderModel(0, 0.1, 1.0)), "wmin 100lp, growx, wrap");
|
||||
|
||||
//// Material
|
||||
panel.add(materialPanel(Material.Type.BULK),
|
||||
"spanx 3, growx, wrap 15lp");
|
||||
"spanx 4, growx, wrap 15lp");
|
||||
|
||||
|
||||
//// Right side of panel ----
|
||||
@ -174,7 +174,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
|
||||
panel2.add(new BasicSlider(m.getSliderModel(
|
||||
new DoubleModel(component.getParent(), "Length", -1.0, UnitGroup.UNITS_NONE),
|
||||
new DoubleModel(component.getParent(), "Length"))),
|
||||
"w 100lp, wrap");
|
||||
"wmin 100lp, growx, wrap");
|
||||
|
||||
|
||||
|
||||
|
@ -45,6 +45,7 @@ public class LaunchLugConfig extends RocketComponentConfig {
|
||||
|
||||
JSpinner spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
|
@ -138,6 +138,7 @@ public class MassComponentConfig extends RocketComponentConfig {
|
||||
m = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel2.add(spin, "growx");
|
||||
|
||||
panel2.add(new UnitSelector(m), "growx");
|
||||
|
@ -163,6 +163,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig {
|
||||
m = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
|
@ -92,6 +92,7 @@ public class RailButtonConfig extends RocketComponentConfig {
|
||||
DoubleModel offsetModel = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
JSpinner offsetSpinner = new JSpinner(offsetModel.getSpinnerModel());
|
||||
offsetSpinner.setEditor(new SpinnerEditor(offsetSpinner));
|
||||
focusElement = offsetSpinner;
|
||||
panel.add(offsetSpinner, "growx");
|
||||
panel.add(new UnitSelector(offsetModel), "growx");
|
||||
panel.add(new BasicSlider(offsetModel.getSliderModel(
|
||||
|
@ -20,6 +20,7 @@ import net.sf.openrocket.l10n.Translator;
|
||||
import net.sf.openrocket.material.Material;
|
||||
import net.sf.openrocket.rocketcomponent.EngineBlock;
|
||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||
import net.sf.openrocket.rocketcomponent.ThicknessRingComponent;
|
||||
import net.sf.openrocket.rocketcomponent.position.AxialMethod;
|
||||
import net.sf.openrocket.startup.Application;
|
||||
import net.sf.openrocket.unit.UnitGroup;
|
||||
@ -116,6 +117,9 @@ public class RingComponentConfig extends RocketComponentConfig {
|
||||
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
if (component instanceof ThicknessRingComponent) {
|
||||
focusElement = spin;
|
||||
}
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
@ -139,6 +143,9 @@ public class RingComponentConfig extends RocketComponentConfig {
|
||||
m = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
if (!(component instanceof ThicknessRingComponent)) {
|
||||
focusElement = spin;
|
||||
}
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
|
@ -22,6 +22,7 @@ import javax.swing.JSpinner;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
import net.sf.openrocket.document.OpenRocketDocument;
|
||||
@ -61,6 +62,7 @@ public class RocketComponentConfig extends JPanel {
|
||||
|
||||
private JComboBox<?> presetComboBox;
|
||||
private PresetModel presetModel;
|
||||
protected Component focusElement = null; // Element that will be focused on after a preset is selected
|
||||
|
||||
protected final JTextField componentNameField;
|
||||
protected JTextArea commentTextArea;
|
||||
@ -702,6 +704,24 @@ public class RocketComponentConfig extends JPanel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests focus for the focus element that should be active after a preset is selected.
|
||||
*/
|
||||
public void setFocusElement() {
|
||||
if (focusElement != null) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
if (focusElement instanceof JSpinner) {
|
||||
SpinnerEditor ed = (SpinnerEditor) ((JSpinner)focusElement).getEditor();
|
||||
ed.getTextField().requestFocusInWindow();
|
||||
} else {
|
||||
focusElement.requestFocusInWindow();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void register(Invalidatable model) {
|
||||
|
@ -80,6 +80,7 @@ public class ShockCordConfig extends RocketComponentConfig {
|
||||
m = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel2.add(spin, "growx");
|
||||
|
||||
panel2.add(new UnitSelector(m), "growx");
|
||||
|
@ -149,6 +149,7 @@ public class StreamerConfig extends RecoveryDeviceConfig {
|
||||
m = new DoubleModel(component, "AxialOffset", UnitGroup.UNITS_LENGTH);
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
|
@ -54,6 +54,7 @@ public class TubeFinSetConfig extends RocketComponentConfig {
|
||||
|
||||
spin = new JSpinner(m.getSpinnerModel());
|
||||
spin.setEditor(new SpinnerEditor(spin));
|
||||
focusElement = spin;
|
||||
panel.add(spin, "growx");
|
||||
|
||||
panel.add(new UnitSelector(m), "growx");
|
||||
|
Loading…
x
Reference in New Issue
Block a user