Ver Fonte

Creating a handle wrapper, so that BoolGate objects don't need to extend beyond their creation scope.

Sam Jaffe há 8 anos atrás
pai
commit
2dbc1e334b

+ 19 - 0
src/main/lombok/org/leumasjaffe/charsheet/model/observable/BoolGate.java

@@ -3,8 +3,10 @@ package org.leumasjaffe.charsheet.model.observable;
 import java.util.Arrays;
 
 import org.leumasjaffe.observer.Observable;
+import org.leumasjaffe.observer.ObserverDispatch;
 
 import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
 import lombok.experimental.FieldDefaults;
 
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
@@ -12,6 +14,19 @@ public class BoolGate extends Observable {
 	boolean[] data;
 	int size;
 	
+	@AllArgsConstructor
+	@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+	public class Handle {
+		int index;
+		public boolean get() { return BoolGate.this.get(index); }
+		public void set(Object source, boolean bool) {
+			if (bool != get()) {
+				BoolGate.this.set(index, bool);
+				ObserverDispatch.notifySubscribers(BoolGate.this, source);
+			}
+		}
+	}
+	
 	public BoolGate(int dim) {
 		size = dim;
 		data = new boolean[dim];
@@ -25,6 +40,10 @@ public class BoolGate extends Observable {
 		return true;
 	}
 	
+	public Handle handle(int idx) {
+		return this.new Handle(idx);
+	}
+	
 	public boolean get(int idx) {
 		return data[idx];
 	}

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

@@ -70,7 +70,7 @@ class LevelUpSpellPanel extends JPanel {
 	ObservableListener<LevelUpSpellPanel, BoolGate> allReady;
 
 	public LevelUpSpellPanel(SpellPicker pick, SelectSpellsPanel.Info info, 
-			BoolGate readyCount, int index) {
+			BoolGate.Handle readyCount) {
 		this.pick = pick;
 		this.info = info;
 		this.toLevel = info.dclass.getLevel().value();
@@ -103,7 +103,7 @@ class LevelUpSpellPanel extends JPanel {
 		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, i,
+			SelectSpellsPanel lvl = new SelectSpellsPanel(info, gate.handle(i), i,
 					new LinkedHashSet<>(), Math.max(spells.get(i), sharedSlots), 
 					pick.getAvailableSpells(info, i),
 					pick != SpellPickType.LEARN, val);
@@ -111,8 +111,7 @@ class LevelUpSpellPanel extends JPanel {
 			panel.add(lvl);
 		}
 		allReady = new ObservableListener<>(this, (c, v) -> {
-			readyCount.set(index, v.all());
-			ObserverDispatch.notifySubscribers(readyCount, this);
+			readyCount.set(this, v.all());
 		});
 		allReady.setObserved(gate);
 	}

+ 6 - 4
src/main/lombok/org/leumasjaffe/charsheet/view/level/UpdateClassWithLevelPanel.java

@@ -36,7 +36,9 @@ import javax.swing.JLabel;
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 class UpdateClassWithLevelPanel extends JPanel {
+	static int CHOOSE_SKILL_INDEX = 0;
 	static int LEARN_SPELL_INDEX = 1;
+	static int PREPARE_SPELL_INDEX = 2;
 
 	BoolGate readyCount = new BoolGate(3);
 	ObservableListener<Consumer<Boolean>, BoolGate> listener;
@@ -70,7 +72,7 @@ class UpdateClassWithLevelPanel extends JPanel {
 
 		JPanel skills = new SkillLevelUpPanel(info.ddCharacter, info.ddClass) {
 			@Override public void setIsReady(boolean b) {
-				readyCount.set(0, b);
+				readyCount.set(CHOOSE_SKILL_INDEX, b);
 				ObserverDispatch.notifySubscribers(readyCount, null);
 			}
 		};
@@ -82,7 +84,7 @@ class UpdateClassWithLevelPanel extends JPanel {
 			if (sb.learnsSpells()) {
 				LevelUpSpellPanel spells = new LevelUpSpellPanel(LEARN,
 						new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-						readyCount, LEARN_SPELL_INDEX);
+						readyCount.handle(LEARN_SPELL_INDEX));
 				tabbedPane.addTab("Learn Spells", null, spells, null);
 				if (sb.preparesSpells()) {
 					learnAndPrepareListener = new ObservableListener<>(this, (c, v) -> {
@@ -99,7 +101,7 @@ class UpdateClassWithLevelPanel extends JPanel {
 			else if (sb.preparesSpells()) {
 				LevelUpSpellPanel spells = new LevelUpSpellPanel(PREPARE,
 						new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-						readyCount, 2);
+						readyCount.handle(PREPARE_SPELL_INDEX));
 				tabbedPane.addTab("Prepare Spells", null, spells, null);
 			}
 		});
@@ -156,7 +158,7 @@ class UpdateClassWithLevelPanel extends JPanel {
 
 		learnSpells = new LevelUpSpellPanel(pick,
 				new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-				readyCount, 2);
+				readyCount.handle(PREPARE_SPELL_INDEX));
 		tabbedPane.addTab("Prepare Spells", null, learnSpells, null);
 	}
 	

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

@@ -82,7 +82,7 @@ public class PrepareSpellsDialog extends JPanel {
 		});
 		allReady.setObserved(gate);
 		for (int i = 0; i < highestSpellLevel; ++i) {
-			SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(chara, dclass, gate, i);
+			SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(chara, dclass, gate.handle(i), i);
 			panels.add(lvl);
 			panel.add(lvl);
 		}

+ 3 - 2
src/main/lombok/org/leumasjaffe/charsheet/view/magic/SelectPreparedSpellsPanel.java

@@ -12,13 +12,14 @@ import lombok.experimental.FieldDefaults;
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 class SelectPreparedSpellsPanel extends SelectSpellsPanel {
 	
-	private SelectPreparedSpellsPanel(DDCharacter chara, DDCharacterClass dclass, BoolGate gate, int level, Prepared prep) {
+	private SelectPreparedSpellsPanel(DDCharacter chara, DDCharacterClass dclass, 
+			BoolGate.Handle gate, int level, Prepared prep) {
 		super(new SelectSpellsPanel.Info(chara, dclass), gate, level,
 				prep.getSpellsPreparedPreviouslyForLevel(level),
 				prep.spellsKnownAtLevel(level));
 	}
 
-	public SelectPreparedSpellsPanel(DDCharacter chara, DDCharacterClass dclass, BoolGate gate, int level) {
+	public SelectPreparedSpellsPanel(DDCharacter chara, DDCharacterClass dclass, BoolGate.Handle gate, int level) {
 		this(chara, dclass, gate, level, ((Prepared) dclass.getSpellBook().get()));
 	}
 

+ 4 - 6
src/main/lombok/org/leumasjaffe/charsheet/view/magic/SelectSpellsPanel.java

@@ -19,7 +19,6 @@ import org.leumasjaffe.charsheet.model.observable.IntValue;
 import org.leumasjaffe.charsheet.util.AbilityHelper;
 import org.leumasjaffe.event.SelectTableRowPopupMenuListener;
 import org.leumasjaffe.format.StringHelper;
-import org.leumasjaffe.observer.ObserverDispatch;
 
 import lombok.AccessLevel;
 import lombok.AllArgsConstructor;
@@ -91,7 +90,7 @@ public class SelectSpellsPanel extends JPanel {
 	IntValue sharedValue;
 	SelectSpellModel modelPrepared, modelKnown;
 	
-	public SelectSpellsPanel(Info info, BoolGate gate, int level, Collection<DDSpell> prepared, int toPrepare,
+	public SelectSpellsPanel(Info info, BoolGate.Handle gate, int level, Collection<DDSpell> prepared, int toPrepare,
 			Collection<DDSpell> avail, boolean allowsDuplicates, IntValue sharedValue) {
 		this.allowsDuplicates = allowsDuplicates;
 		this.sharedValue = sharedValue;
@@ -228,18 +227,17 @@ public class SelectSpellsPanel extends JPanel {
 			tablePrepared.getSelectionModel().clearSelection();
 			tablePrepared.repaint();
 			
-			if (!gate.get(level) && !Arrays.asList(modelPrepared.data).contains(NONE)) {
+			if (!gate.get() && !Arrays.asList(modelPrepared.data).contains(NONE)) {
 				this.prepared.clear();
 				for (Object o : modelPrepared.data) {
 					this.prepared.add(DDSpell.fromString((String) o)); // TODO
 				}
-				gate.set(level, true);
-				ObserverDispatch.notifySubscribers(gate, this);
+				gate.set(this, true);
 			}
 		});
 	}
 
-	public SelectSpellsPanel(Info info, BoolGate gate, int level,
+	public SelectSpellsPanel(Info info, BoolGate.Handle gate, int level,
 			Collection<DDSpell> prepared, Collection<DDSpell> avail) {
 		this(info, gate, level, prepared, 0, avail, true, new IntValue(-1));
 	}