001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.geoimage;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007import java.awt.event.FocusListener;
008import java.awt.event.ItemListener;
009
010import javax.swing.JCheckBox;
011import javax.swing.JComponent;
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014import javax.swing.JSeparator;
015import javax.swing.JSpinner;
016import javax.swing.SpinnerNumberModel;
017import javax.swing.SwingConstants;
018import javax.swing.event.ChangeListener;
019
020import org.openstreetmap.josm.data.gpx.GpxImageDirectionPositionSettings;
021import org.openstreetmap.josm.tools.GBC;
022
023/**
024 * Panel allowing user to enter {@link GpxImageDirectionPositionSettings}.
025 * @since 18061
026 */
027public class ImageDirectionPositionPanel extends JPanel {
028
029    private final JCheckBox cChangeImageDirection = new JCheckBox();
030    private final JSpinner sOffsetDegrees = new JSpinner(new SpinnerNumberModel(0, -360, 360, 1));
031    private final JCheckBox cSetGpxTrackTag = new JCheckBox();
032
033    private final JSpinner sX = new JSpinner(new SpinnerNumberModel(0.0, -50.0, 50.0, 0.1));
034    private final JSpinner sY = new JSpinner(new SpinnerNumberModel(0.0, -50.0, 50.0, 0.1));
035    private final JSpinner sZ = new JSpinner(new SpinnerNumberModel(0.0, -20.0, 20.0, 0.1));
036
037    /**
038     * Constructs a new {@code ImageMetadataModificationPanel}
039     * @param changeDirectionText the text displayed for the change image direction combobox
040     */
041    protected ImageDirectionPositionPanel(String changeDirectionText, boolean hideGpxTrack) {
042        super(new GridBagLayout());
043
044        cChangeImageDirection.setText(changeDirectionText);
045        add(cChangeImageDirection, GBC.eol().insets(0, 0, 0, 5));
046        cChangeImageDirection.addActionListener(e -> sOffsetDegrees.setEnabled(!sOffsetDegrees.isEnabled()));
047        addSetting(tr("Offset angle in degrees:"), sOffsetDegrees);
048        sOffsetDegrees.setEnabled(false);
049        if (!hideGpxTrack) {
050            cChangeImageDirection.addActionListener(e -> cSetGpxTrackTag.setEnabled(!cSetGpxTrackTag.isEnabled()));
051            cSetGpxTrackTag.setText(tr("Set image course direction (from gpx track)"));
052            add(cSetGpxTrackTag, GBC.eol().insets(0, 0, 0, 5));
053            cSetGpxTrackTag.setEnabled(false);
054        }
055
056        add(new JSeparator(SwingConstants.HORIZONTAL),
057                GBC.eol().fill(GBC.HORIZONTAL).insets(0, 12, 0, 12));
058
059        add(new JLabel(tr("Shift image relative to the direction (in meters)")),
060                GBC.eol().insets(0, 0, 0, 5));
061        addSetting(tr("X:"), sX);
062        addSetting(tr("Y:"), sY);
063        addSetting(tr("Elevation:"), sZ);
064    }
065
066    /**
067     * Returns a new {@code ImageMetadataModificationPanel} in a GPX trace context.
068     * @return a new {@code ImageMetadataModificationPanel} in a GPX trace context
069     */
070    public static ImageDirectionPositionPanel forGpxTrace() {
071        return new ImageDirectionPositionPanel(tr("Set image direction towards the next GPX waypoint"), false);
072    }
073
074    /**
075     * Returns a new {@code ImageMetadataModificationPanel} in an image sequence context.
076     * @return a new {@code ImageMetadataModificationPanel} in an image sequence context
077     */
078    public static ImageDirectionPositionPanel forImageSequence() {
079        return new ImageDirectionPositionPanel(tr("Set image direction towards the next one"), true);
080    }
081
082    protected void addSetting(String text, JComponent component) {
083        add(new JLabel(text, JLabel.RIGHT), GBC.std().insets(15, 0, 5, 5).fill(GBC.HORIZONTAL).weight(0, 0));
084        add(component, GBC.std().fill(GBC.HORIZONTAL));
085        add(GBC.glue(1, 0), GBC.eol().fill(GBC.HORIZONTAL).weight(1, 0));
086    }
087
088    /**
089     * Returns the settings set by user.
090     * @return the settings set by user
091     */
092    public GpxImageDirectionPositionSettings getSettings() {
093        return new GpxImageDirectionPositionSettings(
094                cChangeImageDirection.isSelected(),
095                (Integer) sOffsetDegrees.getValue(),
096                cSetGpxTrackTag.isSelected(),
097                (Double) sX.getValue(),
098                (Double) sY.getValue(),
099                (Double) sZ.getValue());
100    }
101
102    /**
103     * Adds a focus listener on all spinners of this panel.
104     * @param focusListener focus listener to add
105     */
106    public void addFocusListenerOnComponent(FocusListener focusListener) {
107        sOffsetDegrees.addFocusListener(focusListener);
108        sX.addFocusListener(focusListener);
109        sY.addFocusListener(focusListener);
110        sZ.addFocusListener(focusListener);
111    }
112
113    /**
114     * Adds a change listener on all checkboxes of this panel.
115     * @param listener change listener to add
116     * @since 18078
117     */
118    public void addItemListenerOnComponents(ItemListener listener) {
119        cChangeImageDirection.addItemListener(listener);
120        cSetGpxTrackTag.addItemListener(listener);
121    }
122
123    /**
124     * Adds a change listener on all spinners of this panel.
125     * @param listener change listener to add
126     */
127    public void addChangeListenerOnComponents(ChangeListener listener) {
128        sOffsetDegrees.addChangeListener(listener);
129        sX.addChangeListener(listener);
130        sY.addChangeListener(listener);
131        sZ.addChangeListener(listener);
132    }
133}