|
|
@@ -14,6 +14,7 @@ import org.leumasjaffe.charsheet.model.DDCharacter;
|
|
|
import org.leumasjaffe.charsheet.model.DDCharacterClass;
|
|
|
import org.leumasjaffe.charsheet.model.magic.DDSpell;
|
|
|
import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
|
|
|
+import org.leumasjaffe.charsheet.model.observable.IntValue;
|
|
|
import org.leumasjaffe.charsheet.util.AbilityHelper;
|
|
|
import org.leumasjaffe.event.SelectTableRowPopupMenuListener;
|
|
|
import org.leumasjaffe.format.StringHelper;
|
|
|
@@ -40,6 +41,8 @@ import javax.swing.JScrollPane;
|
|
|
@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
public class SelectSpellsPanel extends JPanel {
|
|
|
|
|
|
+ private static final String NONE = "<none>";
|
|
|
+
|
|
|
@AllArgsConstructor
|
|
|
private static class SelectSpellModel extends AbstractTableModel {
|
|
|
/**
|
|
|
@@ -83,23 +86,26 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
|
|
|
@Getter Collection<DDSpell> prepared;
|
|
|
boolean allowsDuplicates;
|
|
|
+ IntValue sharedValue;
|
|
|
SelectSpellModel modelPrepared, modelKnown;
|
|
|
|
|
|
public SelectSpellsPanel(Info info, int level, Collection<DDSpell> prepared, int toPrepare,
|
|
|
- Collection<DDSpell> avail, boolean allowsDuplicates) {
|
|
|
- this(info.chara, level, info.dclass, prepared, toPrepare, avail, allowsDuplicates);
|
|
|
+ Collection<DDSpell> avail, boolean allowsDuplicates, IntValue val) {
|
|
|
+ this(info.chara, level, info.dclass, prepared, toPrepare, avail, allowsDuplicates, val);
|
|
|
}
|
|
|
|
|
|
public SelectSpellsPanel(DDCharacter chara, int level,
|
|
|
DDCharacterClass dclass, Collection<DDSpell> prepared, int toPrepare,
|
|
|
- Collection<DDSpell> avail, boolean allowsDuplicates) {
|
|
|
+ Collection<DDSpell> avail, boolean allowsDuplicates, IntValue sharedValue) {
|
|
|
this.allowsDuplicates = allowsDuplicates;
|
|
|
+ this.sharedValue = sharedValue;
|
|
|
putClientProperty(READY, true);
|
|
|
final DDSpellbook spellBook = dclass.getSpellBook().get();
|
|
|
this.prepared = new ArrayList<>(prepared);
|
|
|
final List<DDSpell> known = new ArrayList<>(avail);
|
|
|
this.modelPrepared = new SelectSpellModel(createPrepareModel(prepared, toPrepare));
|
|
|
this.modelKnown = new SelectSpellModel(known.stream().map(DDSpell::getName).toArray());
|
|
|
+ sharedValue.value(sharedValue.value() - this.modelPrepared.data.length + countNone());
|
|
|
|
|
|
GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
gridBagLayout.columnWidths = new int[]{0, 40, 0, 0};
|
|
|
@@ -183,8 +189,9 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
panelDivider.add(button, gbc_button);
|
|
|
button.addActionListener(e -> {
|
|
|
final int row = tablePrepared.getSelectedRow();
|
|
|
- if (row != -1) {
|
|
|
- modelPrepared.setValueAt("<none>", row, 0);
|
|
|
+ if (row != -1 && !modelPrepared.data[row].equals(NONE)) {
|
|
|
+ sharedValue.value(sharedValue.value() + 1);
|
|
|
+ modelPrepared.setValueAt(NONE, row, 0);
|
|
|
}
|
|
|
tablePrepared.getSelectionModel().clearSelection();
|
|
|
tablePrepared.repaint();
|
|
|
@@ -204,10 +211,14 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
button_1.addActionListener(e -> {
|
|
|
final int[] rows = tableKnown.getSelectedRows();
|
|
|
final int[] orows = tablePrepared.getSelectedRows();
|
|
|
- if (orows.length >= rows.length) {
|
|
|
+ if (sharedValue.value() == 0) {
|
|
|
+ JOptionPane.showMessageDialog(this, "You have exceeded the shared limit on new spells",
|
|
|
+ "Error", JOptionPane.ERROR_MESSAGE);
|
|
|
+ } else if (orows.length >= rows.length) {
|
|
|
for (int i = 0; i < rows.length; ++i) {
|
|
|
if (wouldHaveIllegalDuplicate(rows[i])) continue;
|
|
|
modelPrepared.data[orows[i]] = modelKnown.data[rows[i]];
|
|
|
+ sharedValue.value(sharedValue.value() - 1);
|
|
|
}
|
|
|
} else if (orows.length == 0 && countNone() >= rows.length) {
|
|
|
replace(rows);
|
|
|
@@ -220,7 +231,7 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
tablePrepared.getSelectionModel().clearSelection();
|
|
|
tablePrepared.repaint();
|
|
|
|
|
|
- if (!(Boolean) getClientProperty(READY) && !Arrays.asList(modelPrepared.data).contains("<none>")) {
|
|
|
+ if (!(Boolean) getClientProperty(READY) && !Arrays.asList(modelPrepared.data).contains(NONE)) {
|
|
|
this.prepared.clear();
|
|
|
for (Object o : modelPrepared.data) {
|
|
|
this.prepared.add(DDSpell.fromString((String) o)); // TODO
|
|
|
@@ -230,6 +241,11 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ public SelectSpellsPanel(DDCharacter chara, int level, DDCharacterClass dclass,
|
|
|
+ Collection<DDSpell> prepared, Collection<DDSpell> avail) {
|
|
|
+ this(chara, level, dclass, prepared, 0, avail, true, new IntValue(-1));
|
|
|
+ }
|
|
|
+
|
|
|
private boolean wouldHaveIllegalDuplicate(int row) {
|
|
|
return !this.allowsDuplicates && Arrays.asList(modelPrepared.data).contains(modelKnown.data[row]);
|
|
|
}
|
|
|
@@ -244,7 +260,7 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
data[i++] = sp.getName();
|
|
|
}
|
|
|
for (; i < toPrepare; ++i) {
|
|
|
- data[i] = "<none>";
|
|
|
+ data[i] = NONE;
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
@@ -254,8 +270,9 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
for (int i = 0; i < rows.length; ++i) {
|
|
|
if (wouldHaveIllegalDuplicate(i)) continue;
|
|
|
for (int j = 0; j < modelPrepared.data.length; ++j) {
|
|
|
- if (!modelPrepared.data[j].equals("<none>")) continue;
|
|
|
+ if (!modelPrepared.data[j].equals(NONE)) continue;
|
|
|
modelPrepared.data[j] = modelKnown.data[i];
|
|
|
+ sharedValue.value(sharedValue.value() - 1);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -264,7 +281,7 @@ public class SelectSpellsPanel extends JPanel {
|
|
|
private int countNone() {
|
|
|
int cnt = 0;
|
|
|
for (Object o : modelPrepared.data) {
|
|
|
- if (o.equals("<none>")) ++cnt;
|
|
|
+ if (o.equals(NONE)) ++cnt;
|
|
|
}
|
|
|
return cnt;
|
|
|
}
|