Selaa lähdekoodia

Move responsibility of fetching of available spells, tuple data, and allowsDuplicates boolean into SpellPicker

Sam Jaffe 8 vuotta sitten
vanhempi
commit
8714d283da

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

@@ -70,13 +70,13 @@ class LevelUpSpellPanel extends JPanel {
 		final IntValue val = new IntValue(getSharedAllowedSlots(info));
 		final Map<Integer, Integer> spells = getNewSpells(val);
 		final int sharedSlots = val.value();
+				
 		BoolGate.Meta gate = readyCount.createSubGate(newHighestSpellLevel);
 		for (int i = 0; i < newHighestSpellLevel; ++i) {
 			if (spells.get(i) < 0) { gate.set(i, true); panels.add(null); continue; }
 			++spellLevelsGrown;
-			SelectSpellsPanel lvl = new SelectSpellsPanel(info, gate.handle(i), i,
-					createArray(Math.max(spells.get(i), sharedSlots)), 
-					pick.getAvailableSpells(i), pick.allowsDuplicates(), val);
+			SelectSpellsPanel lvl = new SelectSpellsPanel(pick, gate.handle(i), i,
+					createArray(Math.max(spells.get(i), sharedSlots)), val);
 			panels.add(lvl);
 			panel.add(lvl);
 		}

+ 10 - 2
src/main/lombok/org/leumasjaffe/charsheet/view/magic/PrepareSpellsDialog.java

@@ -3,11 +3,15 @@ package org.leumasjaffe.charsheet.view.magic;
 import javax.swing.JPanel;
 
 import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
+import org.leumasjaffe.charsheet.controller.magic.PrepareSpellPicker;
+import org.leumasjaffe.charsheet.controller.magic.SpellPicker;
 import org.leumasjaffe.charsheet.model.DDCharacter;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
 import org.leumasjaffe.charsheet.model.magic.impl.Prepared;
 import org.leumasjaffe.charsheet.model.observable.BoolGate;
+import org.leumasjaffe.charsheet.model.observable.IntValue;
 import org.leumasjaffe.observer.ObservableListener;
 import org.leumasjaffe.observer.ObserverDispatch;
 
@@ -77,11 +81,15 @@ public class PrepareSpellsDialog extends JPanel {
 		JPanel panel = new JPanel(new VerticalLayout(5));
 		scrollPane.setViewportView(panel);
 		
-		List<SelectPreparedSpellsPanel> panels = new ArrayList<>();
+		List<SelectSpellsPanel> panels = new ArrayList<>();
 		final BoolGate gate = new BoolGate(highestSpellLevel);
 		allReady = gate.makeListener(btnPrepareTheseSpells::setEnabled);
+		ChooseSpellTuple tup = new ChooseSpellTuple(chara, dclass, spellBook);
+		SpellPicker pick = new PrepareSpellPicker(tup);
 		for (int i = 0; i < highestSpellLevel; ++i) {
-			SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(chara, dclass, gate.handle(i), i, (Prepared) spellBook);
+			SelectSpellsPanel lvl = new SelectSpellsPanel(pick, gate.handle(i), i,
+					((Prepared) spellBook).getSpellsPreparedPreviouslyForLevel(i),
+					new IntValue(-1));
 			panels.add(lvl);
 			panel.add(lvl);
 		}

+ 0 - 23
src/main/lombok/org/leumasjaffe/charsheet/view/magic/SelectPreparedSpellsPanel.java

@@ -1,23 +0,0 @@
-package org.leumasjaffe.charsheet.view.magic;
-
-import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
-import org.leumasjaffe.charsheet.model.DDCharacter;
-import org.leumasjaffe.charsheet.model.DDCharacterClass;
-import org.leumasjaffe.charsheet.model.magic.impl.Prepared;
-import org.leumasjaffe.charsheet.model.observable.BoolGate;
-
-import lombok.AccessLevel;
-import lombok.experimental.FieldDefaults;
-
-@SuppressWarnings("serial")
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-class SelectPreparedSpellsPanel extends SelectSpellsPanel {
-	
-	public SelectPreparedSpellsPanel(DDCharacter chara, DDCharacterClass dclass, BoolGate.Handle gate, int level,
-			Prepared prep) {
-		super(new ChooseSpellTuple(chara, dclass, prep), gate, level, 
-				prep.getSpellsPreparedPreviouslyForLevel(level),
-				prep.spellsKnownAtLevel(level));
-	}
-
-}

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

@@ -10,7 +10,7 @@ import javax.swing.JPopupMenu;
 import javax.swing.JTable;
 import javax.swing.ListSelectionModel;
 
-import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
+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.IntValue;
@@ -80,13 +80,12 @@ public class SelectSpellsPanel extends JPanel {
 	IntValue sharedValue;
 	SelectSpellModel modelPrepared, modelKnown;
 	
-	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level, 
-			Collection<DDSpell> prepared, Collection<DDSpell> avail,
-			boolean allowsDuplicates, IntValue sharedValue) {
-		this.allowsDuplicates = allowsDuplicates;
+	public SelectSpellsPanel(SpellPicker pick, BoolGate.Handle gate, int level, 
+			Collection<DDSpell> prepared, IntValue sharedValue) {
+		this.allowsDuplicates = pick.allowsDuplicates();
 		this.sharedValue = sharedValue;
 		this.prepared = new ArrayList<>(prepared);
-		final List<DDSpell> known = new ArrayList<>(avail);
+		final List<DDSpell> known = new ArrayList<>(pick.getAvailableSpells(level));
 		this.modelPrepared = new SelectSpellModel(createModel(prepared));
 		this.modelKnown = new SelectSpellModel(createModel(known));
 		gate.set(countNone() == 0);
@@ -99,7 +98,7 @@ public class SelectSpellsPanel extends JPanel {
 		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
 		setLayout(gridBagLayout);
 		
-		JPanel panel = new ChooseSpellsPerDayHeader(level, info.spellBook, info.ability());
+		JPanel panel = new ChooseSpellsPerDayHeader(level, pick.getInfo().spellBook, pick.getInfo().ability());
 		GridBagConstraints gbc_panel = new GridBagConstraints();
 		gbc_panel.gridwidth = 3;
 		gbc_panel.insets = new Insets(0, 0, 5, 5);
@@ -157,8 +156,8 @@ public class SelectSpellsPanel extends JPanel {
 		JMenuItem mntmInfo = new JMenuItem("Info");
 		mntmInfo.addActionListener( e -> {
 			DDSpell spell = known.get(tableKnown.getSelectedRow());
-			JFrame frame = new JFrame(spell.getName() +  " (" + info.dclass.getName() + " " + level + ")");
-			frame.add(new SpellInfoPanel(info.chara, info.dclass, spell));
+			JFrame frame = new JFrame(spell.getName() +  " (" + pick.getInfo().dclass.getName() + " " + level + ")");
+			frame.add(new SpellInfoPanel(pick.getInfo().chara, pick.getInfo().dclass, spell));
 			frame.pack();
 			frame.setVisible(true);
 		});
@@ -225,11 +224,6 @@ public class SelectSpellsPanel extends JPanel {
 		});
 	}
 
-	public SelectSpellsPanel(ChooseSpellTuple info, BoolGate.Handle gate, int level,
-			Collection<DDSpell> prepared, Collection<DDSpell> avail) {
-		this(info, gate, level, prepared, avail, true, new IntValue(-1));
-	}
-
 	private boolean wouldHaveIllegalDuplicate(int row) {
 		return !this.allowsDuplicates && Arrays.asList(modelPrepared.data).contains(modelKnown.data[row]);
 	}