Skip to content

Commit

Permalink
Fix filtering by user and improve the date filtering UI
Browse files Browse the repository at this point in the history
Filtering by user was broken and filtering by date has now a checkbox and disabled controls if the checkbox is unchecked.
  • Loading branch information
floscher committed Apr 10, 2017
1 parent 408a43a commit ffd8504
Showing 1 changed file with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openstreetmap.josm.plugins.mapillary.MapillaryImportedImage;
import org.openstreetmap.josm.plugins.mapillary.MapillaryLayer;
import org.openstreetmap.josm.plugins.mapillary.model.ImageDetection;
import org.openstreetmap.josm.plugins.mapillary.model.UserProfile;
import org.openstreetmap.josm.tools.ImageProvider;

/**
Expand All @@ -48,18 +49,19 @@ public class MapillaryFilterDialog extends ToggleDialog implements MapillaryData

private static MapillaryFilterDialog instance;

private static final String[] TIME_LIST = {tr("All"), tr("Years"), tr("Months"), tr("Days")};
private static final String[] TIME_LIST = {tr("Years"), tr("Months"), tr("Days")};

private final static long[] TIME_FACTOR = new long[]{
31_536_000_000L, // = 365 * 24 * 60 * 60 * 1000 = number of ms in a year
2_592_000_000L, // = 30 * 24 * 60 * 60 * 1000 = number of ms in a month
86_400_000 // = 24 * 60 * 60 * 1000 = number of ms in a day
};

private final JCheckBox filterByDateCheckbox;
/**
* Spinner to choose the range of dates.
*/
private final SpinnerNumberModel spinner;
private final SpinnerNumberModel spinnerModel;

private final JCheckBox imported = new JCheckBox(tr("Imported images"));
private final JCheckBox downloaded = new JCheckBox(new DownloadCheckBoxAction());
Expand Down Expand Up @@ -103,12 +105,21 @@ private MapillaryFilterDialog() {

JPanel fromPanel = new JPanel();
fromPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
fromPanel.add(new JLabel(tr("Not older than: ")));
this.spinner = new SpinnerNumberModel(1, 0, 10000, 1);
fromPanel.add(new JSpinner(this.spinner));
this.time = new JComboBox<>(TIME_LIST);
filterByDateCheckbox = new JCheckBox(tr("Not older than: "));
fromPanel.add(filterByDateCheckbox);
this.spinnerModel = new SpinnerNumberModel(1.0, 0, 10000, .1);
JSpinner spinner = new JSpinner(spinnerModel);
spinner.setEnabled(false);
fromPanel.add(spinner);
time = new JComboBox<>(TIME_LIST);
time.setEnabled(false);
fromPanel.add(this.time);

filterByDateCheckbox.addItemListener(itemE -> {
spinner.setEnabled(filterByDateCheckbox.isSelected());
time.setEnabled(filterByDateCheckbox.isSelected());
});

JPanel userSearchPanel = new JPanel();
userSearchPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
this.user = new DisableShortcutsOnFocusGainedTextField(10);
Expand Down Expand Up @@ -170,7 +181,7 @@ public void reset() {
this.onlySigns.setSelected(false);
this.user.setText("");
this.time.setSelectedItem(TIME_LIST[0]);
this.spinner.setValue(1);
this.spinnerModel.setValue(1);
refresh();
}

Expand All @@ -180,17 +191,32 @@ public void reset() {
public synchronized void refresh() {
boolean imported = this.imported.isSelected();
boolean downloaded = this.downloaded.isSelected();
boolean timeFilter = filterByDateCheckbox.isSelected();
boolean onlySigns = this.onlySigns.isSelected();

// This predicate returns true is the image should be made invisible
Predicate<MapillaryAbstractImage> p =
img ->
(img instanceof MapillaryImportedImage && !imported) ||
(img instanceof MapillaryImage &&
(!downloaded ||
(onlySigns && (((MapillaryImage) img).getDetections().isEmpty() || !checkSigns((MapillaryImage) img))) ||
(!"".equals(user.getText()) && !this.user.getText().equals(((MapillaryImage) img).getUser())))) ||
checkValidTime(img);
img -> {
if (timeFilter && checkValidTime(img)) {
return true;
}
if (!imported && img instanceof MapillaryImportedImage) {
return true;
}
if (img instanceof MapillaryImage) {
if (!downloaded) {
return true;
}
if (onlySigns && (((MapillaryImage) img).getDetections().isEmpty() || !checkSigns((MapillaryImage) img))) {
return true;
}
UserProfile userProfile = ((MapillaryImage) img).getUser();
if (!"".equals(user.getText()) && (userProfile == null || !user.getText().equals(userProfile.getUsername()))) {
return true;
}
}
return false;
};

MapillaryLayer.getInstance().getData().getImages().parallelStream().forEach(img -> img.setVisible(!p.test(img)));

Expand All @@ -199,9 +225,9 @@ public synchronized void refresh() {

private boolean checkValidTime(MapillaryAbstractImage img) {
Long currentTime = currentTime();
for (int i = 1; i <= 3; i++) {
for (int i = 0; i < 3; i++) {
if (TIME_LIST[i].equals(time.getSelectedItem()) &&
img.getCapturedAt() < currentTime - ((Integer) spinner.getValue()).longValue() * TIME_FACTOR[i - 1]) {
img.getCapturedAt() < currentTime - spinnerModel.getNumber().doubleValue() * TIME_FACTOR[i]) {
return true;
}
}
Expand Down

0 comments on commit ffd8504

Please sign in to comment.