|
|
@@ -5,15 +5,57 @@ import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
|
|
|
import javax.swing.JPanel;
|
|
|
+import javax.swing.JTable;
|
|
|
+import javax.swing.ListSelectionModel;
|
|
|
|
|
|
import org.leumasjaffe.charsheet.model.magic.DDSpell;
|
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.Getter;
|
|
|
import lombok.experimental.FieldDefaults;
|
|
|
+import java.awt.GridBagLayout;
|
|
|
+import java.awt.GridBagConstraints;
|
|
|
+import java.awt.Insets;
|
|
|
+import javax.swing.border.BevelBorder;
|
|
|
+import javax.swing.border.SoftBevelBorder;
|
|
|
+import javax.swing.table.AbstractTableModel;
|
|
|
+import javax.swing.JButton;
|
|
|
|
|
|
@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
|
|
|
class SelectPreparedSpellsPanel extends JPanel {
|
|
|
+
|
|
|
+ @AllArgsConstructor
|
|
|
+ private static class SelectSpellModel extends AbstractTableModel {
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private static final long serialVersionUID = 1L;
|
|
|
+
|
|
|
+ final Object[] data;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getRowCount() {
|
|
|
+ return data.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getColumnCount() {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object getValueAt(int rowIndex, int columnIndex) {
|
|
|
+ if (columnIndex != 0) { throw new IllegalArgumentException("There is only 1 column"); }
|
|
|
+ return data[rowIndex];
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
|
|
+ if (columnIndex != 0) { throw new IllegalArgumentException("There is only 1 column"); }
|
|
|
+ data[rowIndex] = aValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
@@ -21,9 +63,116 @@ class SelectPreparedSpellsPanel extends JPanel {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
@Getter List<DDSpell> prepared;
|
|
|
+
|
|
|
+ SelectSpellModel modelPrepared, modelKnown;
|
|
|
|
|
|
public SelectPreparedSpellsPanel(List<DDSpell> prepared,
|
|
|
Collection<DDSpell> known) {
|
|
|
this.prepared = new ArrayList<>(prepared);
|
|
|
+ this.modelPrepared = new SelectSpellModel(prepared.stream().map(DDSpell::getName).toArray());
|
|
|
+ this.modelKnown = new SelectSpellModel(known.stream().map(DDSpell::getName).toArray());
|
|
|
+
|
|
|
+ GridBagLayout gridBagLayout = new GridBagLayout();
|
|
|
+ gridBagLayout.columnWidths = new int[]{0, 40, 0, 0};
|
|
|
+ gridBagLayout.rowHeights = new int[]{20, 0, 0};
|
|
|
+ gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
|
|
|
+ gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
|
|
+ setLayout(gridBagLayout);
|
|
|
+
|
|
|
+ JPanel panel = new JPanel();
|
|
|
+ GridBagConstraints gbc_panel = new GridBagConstraints();
|
|
|
+ gbc_panel.gridwidth = 3;
|
|
|
+ gbc_panel.insets = new Insets(0, 0, 5, 5);
|
|
|
+ gbc_panel.fill = GridBagConstraints.BOTH;
|
|
|
+ gbc_panel.gridx = 0;
|
|
|
+ gbc_panel.gridy = 0;
|
|
|
+ add(panel, gbc_panel);
|
|
|
+
|
|
|
+ JTable tablePrepared = new JTable(modelPrepared);
|
|
|
+ tablePrepared.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|
|
+ tablePrepared.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
|
|
|
+ GridBagConstraints gbc_panelPrepared = new GridBagConstraints();
|
|
|
+ gbc_panelPrepared.insets = new Insets(0, 0, 0, 5);
|
|
|
+ gbc_panelPrepared.fill = GridBagConstraints.BOTH;
|
|
|
+ gbc_panelPrepared.gridx = 0;
|
|
|
+ gbc_panelPrepared.gridy = 1;
|
|
|
+ add(tablePrepared, gbc_panelPrepared);
|
|
|
+
|
|
|
+ JPanel panelDivider = new JPanel();
|
|
|
+ GridBagConstraints gbc_panelDivider = new GridBagConstraints();
|
|
|
+ gbc_panelDivider.insets = new Insets(0, 0, 0, 5);
|
|
|
+ gbc_panelDivider.fill = GridBagConstraints.BOTH;
|
|
|
+ gbc_panelDivider.gridx = 1;
|
|
|
+ gbc_panelDivider.gridy = 1;
|
|
|
+ add(panelDivider, gbc_panelDivider);
|
|
|
+ GridBagLayout gbl_panelDivider = new GridBagLayout();
|
|
|
+ gbl_panelDivider.columnWidths = new int[]{0, 0};
|
|
|
+ gbl_panelDivider.rowHeights = new int[]{0, 0, 0, 0, 0};
|
|
|
+ gbl_panelDivider.columnWeights = new double[]{0.0, Double.MIN_VALUE};
|
|
|
+ gbl_panelDivider.rowWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
|
|
|
+ panelDivider.setLayout(gbl_panelDivider);
|
|
|
+
|
|
|
+ JTable tableKnown = new JTable(modelKnown);
|
|
|
+ tableKnown.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|
|
+ tableKnown.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
|
|
|
+ GridBagConstraints gbc_panelKnown = new GridBagConstraints();
|
|
|
+ gbc_panelKnown.fill = GridBagConstraints.BOTH;
|
|
|
+ gbc_panelKnown.gridx = 2;
|
|
|
+ gbc_panelKnown.gridy = 1;
|
|
|
+ add(tableKnown, gbc_panelKnown);
|
|
|
+
|
|
|
+ JButton button = new JButton(">>");
|
|
|
+ button.setMargin(new Insets(2, 8, 2, 8));
|
|
|
+ GridBagConstraints gbc_button = new GridBagConstraints();
|
|
|
+ gbc_button.insets = new Insets(0, 0, 5, 0);
|
|
|
+ gbc_button.gridx = 0;
|
|
|
+ gbc_button.gridy = 1;
|
|
|
+ panelDivider.add(button, gbc_button);
|
|
|
+ button.addActionListener(e -> {
|
|
|
+ final int row = tablePrepared.getSelectedRow();
|
|
|
+ if (row != -1) {
|
|
|
+ modelPrepared.setValueAt("<none>", row, 0);
|
|
|
+ }
|
|
|
+ tablePrepared.repaint();
|
|
|
+ });
|
|
|
+
|
|
|
+ JButton button_1 = new JButton("<<");
|
|
|
+ button_1.setMargin(new Insets(2, 8, 2, 8));
|
|
|
+ GridBagConstraints gbc_button_1 = new GridBagConstraints();
|
|
|
+ gbc_button_1.insets = new Insets(0, 0, 5, 0);
|
|
|
+ gbc_button_1.gridx = 0;
|
|
|
+ gbc_button_1.gridy = 2;
|
|
|
+ panelDivider.add(button_1, gbc_button_1);
|
|
|
+ button_1.addActionListener(e -> {
|
|
|
+ final int[] rows = tableKnown.getSelectedRows();
|
|
|
+ final int[] orows = tablePrepared.getSelectedRows();
|
|
|
+ if (orows.length >= rows.length) {
|
|
|
+ for (int i = 0; i < rows.length; ++i) {
|
|
|
+ modelPrepared.data[orows[i]] = modelKnown.data[rows[i]];
|
|
|
+ }
|
|
|
+ } else if (orows.length == 0 && countNone() >= rows.length) {
|
|
|
+ replace(rows);
|
|
|
+ } else {
|
|
|
+ // Error?
|
|
|
+ }
|
|
|
+ tablePrepared.repaint();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void replace(int[] rows) {
|
|
|
+ for (int i = 0; i < rows.length; ++i) {
|
|
|
+ for (int j = 0; j < modelPrepared.data.length; ++j) {
|
|
|
+ if (!modelPrepared.data[j].equals("<none>")) continue;
|
|
|
+ modelPrepared.data[j] = modelKnown.data[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int countNone() {
|
|
|
+ int cnt = 0;
|
|
|
+ for (Object o : modelPrepared.data) {
|
|
|
+ if (o.equals("<none>")) ++cnt;
|
|
|
+ }
|
|
|
+ return cnt;
|
|
|
}
|
|
|
}
|