PrepareSpellsDialog.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.util.AbilityHelper;
  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. import javax.swing.ScrollPaneConstants;
  20. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  21. public class PrepareSpellsDialog extends JPanel {
  22. /**
  23. *
  24. */
  25. private static final long serialVersionUID = 1L;
  26. int[] ready = {0};
  27. int highestSpellLevel;
  28. public PrepareSpellsDialog(DDCharacter chara, DDCharacterClass dclass) {
  29. highestSpellLevel = dclass.getHighestSpellLevel();
  30. ready[0] = highestSpellLevel;
  31. GridBagLayout gridBagLayout = new GridBagLayout();
  32. gridBagLayout.columnWidths = new int[]{0, 0};
  33. gridBagLayout.rowHeights = new int[]{0, 0, 0};
  34. gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
  35. gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  36. setLayout(gridBagLayout);
  37. JPanel panel_1 = new JPanel();
  38. GridBagConstraints gbc_panel_1 = new GridBagConstraints();
  39. gbc_panel_1.insets = new Insets(0, 0, 5, 0);
  40. gbc_panel_1.fill = GridBagConstraints.BOTH;
  41. gbc_panel_1.gridx = 0;
  42. gbc_panel_1.gridy = 0;
  43. add(panel_1, gbc_panel_1);
  44. GridBagLayout gbl_panel_1 = new GridBagLayout();
  45. gbl_panel_1.columnWidths = new int[]{0, 0};
  46. gbl_panel_1.rowHeights = new int[]{0, 0};
  47. gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE};
  48. gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  49. panel_1.setLayout(gbl_panel_1);
  50. JButton btnPrepareTheseSpells = new JButton("Prepare These Spells");
  51. GridBagConstraints gbc_btnPrepareTheseSpells = new GridBagConstraints();
  52. gbc_btnPrepareTheseSpells.gridx = 0;
  53. gbc_btnPrepareTheseSpells.gridy = 0;
  54. panel_1.add(btnPrepareTheseSpells, gbc_btnPrepareTheseSpells);
  55. JScrollPane scrollPane = new JScrollPane();
  56. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  57. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  58. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  59. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  60. gbc_scrollPane.gridx = 0;
  61. gbc_scrollPane.gridy = 1;
  62. add(scrollPane, gbc_scrollPane);
  63. JPanel panel = new JPanel(new VerticalLayout(5));
  64. scrollPane.setViewportView(panel);
  65. final Ability.Scores score = AbilityHelper.get(chara, dclass);
  66. final Prepared spellBook = (Prepared) dclass.getSpellBook().get();
  67. List<SelectPreparedSpellsPanel> panels = new ArrayList<>();
  68. for (int i = 0; i < highestSpellLevel; ++i) {
  69. SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(i, dclass, score);
  70. panels.add(lvl);
  71. lvl.addPropertyChangeListener(SelectPreparedSpellsPanel.READY, e -> {
  72. if ((Boolean) e.getNewValue()) ++ready[0];
  73. else --ready[0];
  74. btnPrepareTheseSpells.setEnabled(ready[0] == highestSpellLevel);
  75. });
  76. panel.add(lvl);
  77. }
  78. btnPrepareTheseSpells.addActionListener(e -> {
  79. ((JDialog) this.getParent().getParent().getParent()).dispose();
  80. for (int i = 0; i < highestSpellLevel; ++i) {
  81. spellBook.prepareSpells(i, panels.get(i).getPrepared());
  82. }
  83. });
  84. }
  85. }