source: josm/trunk/src/org/openstreetmap/josm/gui/layer/gpx/DateFilterPanel.java @ 12841

Last change on this file since 12841 was 12841, checked in by bastiK, 6 weeks ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.gpx;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionListener;
9import java.time.ZoneId;
10import java.time.ZonedDateTime;
11import java.util.Date;
12
13import javax.swing.JCheckBox;
14import javax.swing.JPanel;
15import javax.swing.Timer;
16import javax.swing.event.ChangeListener;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.layer.GpxLayer;
20import org.openstreetmap.josm.gui.widgets.DateEditorWithSlider;
21import org.openstreetmap.josm.tools.GBC;
22
23/**
24 * A panel that allows the user to input a date range he wants to filter the GPX data for.
25 */
26public class DateFilterPanel extends JPanel {
27    private final DateEditorWithSlider dateFrom = new DateEditorWithSlider(tr("From"));
28    private final DateEditorWithSlider dateTo = new DateEditorWithSlider(tr("To"));
29    private final JCheckBox noTimestampCb = new JCheckBox(tr("No timestamp"));
30    private final transient GpxLayer layer;
31
32    private transient ActionListener filterAppliedListener;
33
34    private final String prefDate0;
35    private final String prefDateMin;
36    private final String prefDateMax;
37
38    /**
39     * Create the panel to filter tracks on GPX layer @param layer by date
40     * Preferences will be stored in @param preferencePrefix
41     * If @param enabled = true, then the panel is created as active and filtering occurs immediately.
42     * @param layer GPX layer
43     * @param preferencePrefix preference prefix
44     * @param enabled panel initial enabled state
45     */
46    public DateFilterPanel(GpxLayer layer, String preferencePrefix, boolean enabled) {
47        super(new GridBagLayout());
48        prefDate0 = preferencePrefix+".showzerotimestamp";
49        prefDateMin = preferencePrefix+".mintime";
50        prefDateMax = preferencePrefix+".maxtime";
51        this.layer = layer;
52
53        final Date startTime, endTime;
54        Date[] bounds = layer.data.getMinMaxTimeForAllTracks();
55        startTime = (bounds.length == 0) ? Date.from(ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant()) : bounds[0];
56        endTime = (bounds.length == 0) ? new Date() : bounds[1];
57
58        dateFrom.setDate(startTime);
59        dateTo.setDate(endTime);
60        dateFrom.setRange(startTime, endTime);
61        dateTo.setRange(startTime, endTime);
62
63        add(noTimestampCb, GBC.std().grid(1, 1).insets(0, 0, 5, 0));
64        add(dateFrom, GBC.std().grid(2, 1).fill(GBC.HORIZONTAL));
65        add(dateTo, GBC.eol().grid(3, 1).fill(GBC.HORIZONTAL));
66
67        setEnabled(enabled);
68
69        ChangeListener changeListener = e -> {
70            if (isEnabled()) applyFilterWithDelay();
71        };
72
73        dateFrom.addDateListener(changeListener);
74        dateTo.addDateListener(changeListener);
75        noTimestampCb.addChangeListener(changeListener);
76    }
77
78    private final Timer t = new Timer(200, e -> applyFilter());
79
80    /**
81     * Do filtering but little bit later (to reduce cpu load)
82     */
83    public void applyFilterWithDelay() {
84        if (t.isRunning()) {
85            t.restart();
86        } else {
87            t.start();
88        }
89    }
90
91    /**
92     * Applies the filter that was input by the user to the GPX track
93     */
94    public void applyFilter() {
95        t.stop();
96        filterTracksByDate();
97        if (filterAppliedListener != null)
98           filterAppliedListener.actionPerformed(null);
99    }
100
101    /**
102     * Called by other components when it is correct time to save date filtering parameters
103     */
104    public void saveInPrefs() {
105        Main.pref.putLong(prefDateMin, dateFrom.getDate().getTime());
106        Main.pref.putLong(prefDateMax, dateTo.getDate().getTime());
107        Main.pref.putBoolean(prefDate0, noTimestampCb.isSelected());
108    }
109
110    /**
111     * If possible, load date ragne and "zero timestamp" option from preferences
112     * Called by other components when it is needed.
113     */
114    public void loadFromPrefs() {
115        long t1 = Main.pref.getLong(prefDateMin, 0);
116        if (t1 != 0) dateFrom.setDate(new Date(t1));
117        long t2 = Main.pref.getLong(prefDateMax, 0);
118        if (t2 != 0) dateTo.setDate(new Date(t2));
119        noTimestampCb.setSelected(Main.pref.getBoolean(prefDate0, false));
120    }
121
122    /**
123     * Sets a listener that should be called after the filter was applied
124     * @param filterAppliedListener The listener to call
125     */
126    public void setFilterAppliedListener(ActionListener filterAppliedListener) {
127        this.filterAppliedListener = filterAppliedListener;
128    }
129
130    private void filterTracksByDate() {
131        Date from = dateFrom.getDate();
132        Date to = dateTo.getDate();
133        layer.filterTracksByDate(from, to, noTimestampCb.isSelected());
134    }
135
136    @Override
137    public final void setEnabled(boolean enabled) {
138        super.setEnabled(enabled);
139        for (Component c: getComponents()) {
140            c.setEnabled(enabled);
141        }
142    }
143}
Note: See TracBrowser for help on using the repository browser.