PrepareSpellsDialog.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package org.leumasjaffe.charsheet.view.magic;
  2. import javax.swing.JPanel;
  3. import org.jdesktop.swingx.VerticalLayout;
  4. import org.leumasjaffe.charsheet.model.Ability;
  5. import org.leumasjaffe.charsheet.model.DDCharacter;
  6. import org.leumasjaffe.charsheet.model.DDCharacterClass;
  7. import org.leumasjaffe.charsheet.model.magic.impl.Prepared;
  8. import org.leumasjaffe.charsheet.model.observable.IntValue;
  9. import lombok.AccessLevel;
  10. import lombok.experimental.FieldDefaults;
  11. import java.awt.GridBagLayout;
  12. import javax.swing.JScrollPane;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.Insets;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import javax.swing.JButton;
  18. import javax.swing.JDialog;
  19. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  20. public class PrepareSpellsDialog extends JPanel {
  21. /**
  22. *
  23. */
  24. private static final long serialVersionUID = 1L;
  25. int[] ready = {0};
  26. int highestSpellLevel;
  27. public PrepareSpellsDialog(DDCharacter chara, DDCharacterClass dclass) {
  28. highestSpellLevel = dclass.getHighestSpellLevel();
  29. ready[0] = highestSpellLevel;
  30. GridBagLayout gridBagLayout = new GridBagLayout();
  31. gridBagLayout.columnWidths = new int[]{0, 0};
  32. gridBagLayout.rowHeights = new int[]{0, 0, 0};
  33. gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
  34. gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  35. setLayout(gridBagLayout);
  36. JPanel panel_1 = new JPanel();
  37. GridBagConstraints gbc_panel_1 = new GridBagConstraints();
  38. gbc_panel_1.insets = new Insets(0, 0, 5, 0);
  39. gbc_panel_1.fill = GridBagConstraints.BOTH;
  40. gbc_panel_1.gridx = 0;
  41. gbc_panel_1.gridy = 0;
  42. add(panel_1, gbc_panel_1);
  43. GridBagLayout gbl_panel_1 = new GridBagLayout();
  44. gbl_panel_1.columnWidths = new int[]{0, 0};
  45. gbl_panel_1.rowHeights = new int[]{0, 0};
  46. gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE};
  47. gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  48. panel_1.setLayout(gbl_panel_1);
  49. JButton btnPrepareTheseSpells = new JButton("Prepare These Spells");
  50. GridBagConstraints gbc_btnPrepareTheseSpells = new GridBagConstraints();
  51. gbc_btnPrepareTheseSpells.gridx = 0;
  52. gbc_btnPrepareTheseSpells.gridy = 0;
  53. panel_1.add(btnPrepareTheseSpells, gbc_btnPrepareTheseSpells);
  54. JScrollPane scrollPane = new JScrollPane();
  55. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  56. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  57. gbc_scrollPane.gridx = 0;
  58. gbc_scrollPane.gridy = 1;
  59. add(scrollPane, gbc_scrollPane);
  60. JPanel panel = new JPanel(new VerticalLayout(5));
  61. scrollPane.setViewportView(panel);
  62. final IntValue value = Ability.fields.get(dclass.getProto().getSpells().get().getAbility()).apply(chara.getAbilities().getBase());
  63. final Prepared spellBook = (Prepared) dclass.getSpellBook().get();
  64. List<SelectPreparedSpellsPanel> panels = new ArrayList<>();
  65. for (int i = 0; i < highestSpellLevel; ++i) {
  66. SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(i, dclass, value);
  67. panels.add(lvl);
  68. lvl.addPropertyChangeListener(SelectPreparedSpellsPanel.READY, e -> {
  69. if ((Boolean) e.getNewValue()) ++ready[0];
  70. else --ready[0];
  71. btnPrepareTheseSpells.setEnabled(ready[0] == highestSpellLevel);
  72. });
  73. panel.add(lvl);
  74. }
  75. btnPrepareTheseSpells.addActionListener(e -> {
  76. ((JDialog) this.getParent().getParent().getParent()).dispose();
  77. for (int i = 0; i < highestSpellLevel; ++i) {
  78. spellBook.prepareSpells(i, panels.get(i).getPrepared());
  79. }
  80. });
  81. }
  82. }