Преглед изворни кода

Simplifying SelectSpellsPanel functionality by moving the 'toPrepare' minimum size rule to the only place that actually needs it.

Sam Jaffe пре 8 година
родитељ
комит
f6c5f68922

+ 9 - 3
src/main/lombok/org/leumasjaffe/charsheet/view/level/LevelUpSpellPanel.java

@@ -4,8 +4,8 @@ import javax.swing.JPanel;
 
 
 import org.jdesktop.swingx.VerticalLayout;
 import org.jdesktop.swingx.VerticalLayout;
 import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
 import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
-import org.leumasjaffe.charsheet.controller.magic.LearnSpellPicker;
 import org.leumasjaffe.charsheet.controller.magic.SpellPicker;
 import org.leumasjaffe.charsheet.controller.magic.SpellPicker;
+import org.leumasjaffe.charsheet.model.magic.DDSpell;
 import org.leumasjaffe.charsheet.model.observable.BoolGate;
 import org.leumasjaffe.charsheet.model.observable.BoolGate;
 import org.leumasjaffe.charsheet.model.observable.IntValue;
 import org.leumasjaffe.charsheet.model.observable.IntValue;
 import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel;
 import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel;
@@ -19,10 +19,12 @@ import lombok.experimental.NonFinal;
 
 
 import java.awt.GridBagLayout;
 import java.awt.GridBagLayout;
 import java.util.ArrayList;
 import java.util.ArrayList;
-import java.util.LinkedHashSet;
+import java.util.Collection;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.TreeMap;
 import java.util.TreeMap;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 
 import javax.swing.JScrollPane;
 import javax.swing.JScrollPane;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagConstraints;
@@ -73,7 +75,7 @@ class LevelUpSpellPanel extends JPanel {
 			if (spells.get(i) < 0) { gate.set(i, true); panels.add(null); continue; }
 			if (spells.get(i) < 0) { gate.set(i, true); panels.add(null); continue; }
 			++spellLevelsGrown;
 			++spellLevelsGrown;
 			SelectSpellsPanel lvl = new SelectSpellsPanel(info, gate.handle(i), i,
 			SelectSpellsPanel lvl = new SelectSpellsPanel(info, gate.handle(i), i,
-					new LinkedHashSet<>(), Math.max(spells.get(i), sharedSlots), 
+					createArray(Math.max(spells.get(i), sharedSlots)), 
 					pick.getAvailableSpells(i), pick.allowsDuplicates(), val);
 					pick.getAvailableSpells(i), pick.allowsDuplicates(), val);
 			panels.add(lvl);
 			panels.add(lvl);
 			panel.add(lvl);
 			panel.add(lvl);
@@ -81,6 +83,10 @@ class LevelUpSpellPanel extends JPanel {
 		allReady = gate.makeListener();
 		allReady = gate.makeListener();
 	}
 	}
 	
 	
+	private Collection<DDSpell> createArray(int max) {
+		return IntStream.range(0, max).<DDSpell>mapToObj(i -> null).collect(Collectors.toList());
+	}
+
 	private Map<Integer, Integer> getNewSpells(IntValue sharedSpellCountLimit) {
 	private Map<Integer, Integer> getNewSpells(IntValue sharedSpellCountLimit) {
 		final Map<Integer, Integer> map = new TreeMap<>();
 		final Map<Integer, Integer> map = new TreeMap<>();
 		final List<Integer> spellsAtPreviousLevel = pick.getSpellCounts(toLevel-1);
 		final List<Integer> spellsAtPreviousLevel = pick.getSpellCounts(toLevel-1);

+ 8 - 20
src/main/lombok/org/leumasjaffe/charsheet/view/magic/SelectSpellsPanel.java

@@ -39,7 +39,7 @@ import javax.swing.JScrollPane;
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 public class SelectSpellsPanel extends JPanel {
 public class SelectSpellsPanel extends JPanel {
 	
 	
-	private static final String NONE = "<none>";
+	static String NONE = "<none>";
 
 
 	@AllArgsConstructor
 	@AllArgsConstructor
 	protected static class SelectSpellModel extends AbstractTableModel {
 	protected static class SelectSpellModel extends AbstractTableModel {
@@ -81,14 +81,14 @@ public class SelectSpellsPanel extends JPanel {
 	SelectSpellModel modelPrepared, modelKnown;
 	SelectSpellModel modelPrepared, modelKnown;
 	
 	
 	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level, 
 	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level, 
-			Collection<DDSpell> prepared, int toPrepare,
-			Collection<DDSpell> avail, boolean allowsDuplicates, IntValue sharedValue) {
+			Collection<DDSpell> prepared, Collection<DDSpell> avail,
+			boolean allowsDuplicates, IntValue sharedValue) {
 		this.allowsDuplicates = allowsDuplicates;
 		this.allowsDuplicates = allowsDuplicates;
 		this.sharedValue = sharedValue;
 		this.sharedValue = sharedValue;
 		this.prepared = new ArrayList<>(prepared);
 		this.prepared = new ArrayList<>(prepared);
 		final List<DDSpell> known = new ArrayList<>(avail);
 		final List<DDSpell> known = new ArrayList<>(avail);
-		this.modelPrepared = new SelectSpellModel(createPrepareModel(prepared, toPrepare));
-		this.modelKnown = new SelectSpellModel(known.stream().map(DDSpell::getName).toArray());
+		this.modelPrepared = new SelectSpellModel(createModel(prepared));
+		this.modelKnown = new SelectSpellModel(createModel(known));
 		gate.set(countNone() == 0);
 		gate.set(countNone() == 0);
 		sharedValue.value(sharedValue.value() - this.modelPrepared.data.length + countNone());
 		sharedValue.value(sharedValue.value() - this.modelPrepared.data.length + countNone());
 		
 		
@@ -227,27 +227,15 @@ public class SelectSpellsPanel extends JPanel {
 
 
 	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level,
 	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level,
 			Collection<DDSpell> prepared, Collection<DDSpell> avail) {
 			Collection<DDSpell> prepared, Collection<DDSpell> avail) {
-		this(info, gate, level, prepared, 0, avail, true, new IntValue(-1));
+		this(info, gate, level, prepared, avail, true, new IntValue(-1));
 	}
 	}
 
 
 	private boolean wouldHaveIllegalDuplicate(int row) {
 	private boolean wouldHaveIllegalDuplicate(int row) {
 		return !this.allowsDuplicates && Arrays.asList(modelPrepared.data).contains(modelKnown.data[row]);
 		return !this.allowsDuplicates && Arrays.asList(modelPrepared.data).contains(modelKnown.data[row]);
 	}
 	}
 
 
-	private String[] createPrepareModel(Collection<DDSpell> prepared, int toPrepare) {
-		if (toPrepare <= prepared.size()) {
-			return prepared.stream().map(DDSpell::getName).toArray(String[]::new);
-		} else {
-			String[] data = new String[toPrepare];
-			int i = 0;
-			for (DDSpell sp : prepared) {
-				data[i++] = sp.getName();
-			}
-			for (; i < toPrepare; ++i) {
-				data[i] = NONE;
-			}
-			return data;
-		}
+	private String[] createModel(Collection<DDSpell> prepared) {
+		return prepared.stream().map(s -> s == null ? NONE : s.getName()).toArray(String[]::new);
 	}
 	}
 
 
 	private void replace(int[] rows) {
 	private void replace(int[] rows) {