Skip to content

Commit

Permalink
feat: add "Set Cave" button to Survey Table in edit mode
Browse files Browse the repository at this point in the history
fix #49
  • Loading branch information
jedwards1211 committed Jan 30, 2019
1 parent f788954 commit b59ffe9
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 3 deletions.
11 changes: 11 additions & 0 deletions breakout-main/src/main/java/org/breakout/BreakoutMainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,8 @@ public void run() {
ImportWallsAction importWallsAction = new ImportWallsAction(this);

ExportImageAction exportImageAction = new ExportImageAction(this);

SetCaveOnRowsAction setCaveOnRowsAction = new SetCaveOnRowsAction(this);

final WeakHashMap<Animation, Object> protectedAnimations = new WeakHashMap<>();

Expand Down Expand Up @@ -1441,6 +1443,7 @@ public void onLayoutChanged(Container target) {
rebuild3dModel.run();
}
});
surveyDrawer.setCaveButton().setAction(setCaveOnRowsAction);

surveyDrawer.addTo(mainPanel);

Expand Down Expand Up @@ -2318,6 +2321,10 @@ public JoglScene getScene() {
return scene;
}

public SurveyTable getSurveyTable() {
return surveyDrawer.table();
}

protected Stream<ShotKey> getSelectedShotsFromTable() {
SurveyTableModel model = surveyDrawer.table().getModel();
ListSelectionModel selModel = surveyDrawer.table().getModelSelectionModel();
Expand Down Expand Up @@ -2366,6 +2373,10 @@ public JoglViewSettings getViewSettings() {
return renderer.getViewSettings();
}

public TaskService sortTaskService() {
return sortTaskService;
}

public TaskService ioTaskService() {
return ioTaskService;
}
Expand Down
106 changes: 106 additions & 0 deletions breakout-main/src/main/java/org/breakout/SetCaveOnRowsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*******************************************************************************
* Breakout Cave Survey Visualizer
*
* Copyright (C) 2014 James Edwards
*
* jedwards8 at fastmail dot fm
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*******************************************************************************/
package org.breakout;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Function;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

import org.andork.awt.I18n.Localizer;
import org.andork.swing.OnEDT;
import org.breakout.model.SurveyTableModel;
import org.breakout.model.raw.SurveyTrip;

public class SetCaveOnRowsAction extends AbstractAction {
/**
*
*/
private static final long serialVersionUID = -8899030390228292424L;

BreakoutMainView mainView;

public SetCaveOnRowsAction(final BreakoutMainView mainView) {
super();
this.mainView = mainView;
OnEDT.onEDT(() -> {
Localizer localizer = mainView.getI18n().forClass(SetCaveOnRowsAction.this.getClass());
localizer.setName(SetCaveOnRowsAction.this, "name");
});
}

@Override
public void actionPerformed(ActionEvent e) {
final String caveName = JOptionPane.showInputDialog(mainView.getMainPanel(), "Enter new cave name:", "Set Cave",
JOptionPane.QUESTION_MESSAGE);
final SurveyTableModel model = mainView.getSurveyTable().getModel();
final ListSelectionModel selModel = mainView.getSurveyTable().getModelSelectionModel();

final IdentityHashMap<SurveyTrip, SurveyTrip> newTrips = new IdentityHashMap<>();
final IdentityHashMap<SurveyTrip, Boolean> isWholeTripSelected = new IdentityHashMap<>();

final Function<SurveyTrip, SurveyTrip> updateTrip = trip -> {
if (caveName.equals(trip.getCave())) return trip;
SurveyTrip updated = newTrips.get(trip);
if (updated == null) {
updated = trip.setCave(caveName);
newTrips.put(trip, updated);
}
return updated;
};

for (int i = 0; i < model.getRowCount(); i++) {
SurveyTrip trip = model.getRow(i).getTrip();
if (!selModel.isSelectionEmpty() && !selModel.isSelectedIndex(i)) {
isWholeTripSelected.put(trip, false);
}
else if (!isWholeTripSelected.containsKey(trip)) {
isWholeTripSelected.put(trip, true);
}
}

for (int i = 0; i < model.getRowCount(); i++) {
if (selModel.isSelectionEmpty() || selModel.isSelectedIndex(i)) {
model.setRow(i, model.getRow(i).withMutations(mrow -> {
if (Boolean.TRUE.equals(isWholeTripSelected.get(mrow.getTrip()))) {
mrow.updateTrip(updateTrip);
mrow.setOverrideFromCave(null);
mrow.setOverrideToCave(null);
} else {
mrow.setOverrideFromCave(caveName);
mrow.setOverrideToCave(caveName);
}
}));
}
}

model.updateRows(row -> {
SurveyTrip newTrip = newTrips.get(row.getTrip());
return newTrip != null ? row.setTrip(newTrip) : row;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
###############################################################################
# Breakout Cave Survey Visualizer
#
# Copyright (C) 2014 James Edwards
#
# jedwards8 at fastmail dot fm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
###############################################################################
name=Set Cave...
19 changes: 16 additions & 3 deletions breakout-main/src/main/java/org/breakout/SurveyDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Consumer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
Expand Down Expand Up @@ -62,6 +63,8 @@ public class SurveyDrawer extends Drawer {
JRadioButton shotsButton;
JRadioButton nevButton;
JRadioButton tripButton;

JButton setCaveButton;

JToggleButton editButton;

Expand All @@ -84,6 +87,9 @@ public SurveyDrawer(Consumer<Runnable> sortRunner) {
surveyTableSetup = new DefaultAnnotatingJTableSetup(surveyTable, sortRunner);
((AnnotatingTableRowSorter<SurveyTableModel>) surveyTableSetup.table.getAnnotatingRowSorter())
.setModelCopier(new SurveyTableModelCopier());

setCaveButton = new JButton("Set Cave...");
setCaveButton.setVisible(false);

shotsButton = new JRadioButton("Shots");
nevButton = new JRadioButton("NEV");
Expand All @@ -97,10 +103,12 @@ public SurveyDrawer(Consumer<Runnable> sortRunner) {
editButton.setMargin(new Insets(0, 0, 0, 0));
editButton.addItemListener(e -> {
SurveyTableModel model = surveyTable.getModel();
boolean editing = e.getStateChange() == ItemEvent.SELECTED;
if (model != null) {
model.setEditable(e.getStateChange() == ItemEvent.SELECTED);
model.setEditable(editing);
}
editButton.setText(e.getStateChange() == ItemEvent.SELECTED ? "Done" : "Edit");
setCaveButton.setVisible(editing);
editButton.setText(editing ? "Done" : "Edit");
});

delegate().dockingSide(Side.BOTTOM);
Expand All @@ -114,8 +122,9 @@ public SurveyDrawer(Consumer<Runnable> sortRunner) {
gbw.put(searchField).rightOfLast().fillboth(1.0, 0.0);
gbw.put(highlightButton).rightOfLast().west().insets(2, 5, 0, 0);
gbw.put(filterButton).rightOfLast().west().insets(2, 5, 0, 0);
gbw.put(shotsButton).rightOfLast().filly(0.0);
gbw.put(setCaveButton).rightOfLast().filly(0.0);
gbw.put(new JSeparator(SwingConstants.VERTICAL)).rightOfLast().filly(0.0);
gbw.put(shotsButton).rightOfLast().filly(0.0);
gbw.put(nevButton).rightOfLast().filly(0.0);
gbw.put(tripButton).rightOfLast().filly(0.0);
gbw.put(editButton).rightOfLast().filly(0.0);
Expand Down Expand Up @@ -161,6 +170,10 @@ public JRadioButton filterButton() {
public JToggleButton editButton() {
return editButton;
}

public JButton setCaveButton() {
return setCaveButton;
}

public SurveyTable table() {
return surveyTable;
Expand Down

0 comments on commit b59ffe9

Please sign in to comment.