| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package org.leumasjaffe.charsheet.view.level;
- import javax.swing.JPanel;
- import org.leumasjaffe.charsheet.model.DDCharacter;
- import org.leumasjaffe.charsheet.model.DDCharacterClass;
- import com.fasterxml.jackson.core.JsonParseException;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.JsonMappingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
- import lombok.SneakyThrows;
- import java.awt.GridBagLayout;
- import javax.swing.JComboBox;
- import java.awt.GridBagConstraints;
- import javax.swing.JLabel;
- import java.awt.Insets;
- import java.io.IOException;
- import java.util.function.Consumer;
- import javax.swing.JButton;
- @SuppressWarnings("serial")
- class ChooseClassLevelUpDialog extends JPanel {
- public ChooseClassLevelUpDialog(DDCharacter chara, Consumer<LevelUpClassInfo> nexter) {
- GridBagLayout gridBagLayout = new GridBagLayout();
- gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
- gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
- gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
- gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
- setLayout(gridBagLayout);
-
-
- JLabel lblClass = new JLabel("Class:");
- GridBagConstraints gbc_lblClass = new GridBagConstraints();
- gbc_lblClass.insets = new Insets(0, 0, 5, 5);
- gbc_lblClass.anchor = GridBagConstraints.EAST;
- gbc_lblClass.gridx = 1;
- gbc_lblClass.gridy = 1;
- add(lblClass, gbc_lblClass);
- String[] choices = chara.getClasses().stream()
- .map(DDCharacterClass::getName).toArray(String[]::new);
- JComboBox<String> comboBox = new JComboBox<>(choices);
- comboBox.setEditable(true);
- GridBagConstraints gbc_comboBox = new GridBagConstraints();
- gbc_comboBox.insets = new Insets(0, 0, 5, 0);
- gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
- gbc_comboBox.gridx = 2;
- gbc_comboBox.gridy = 1;
- add(comboBox, gbc_comboBox);
-
- JPanel panel = new JPanel();
- GridBagConstraints gbc_panel = new GridBagConstraints();
- gbc_panel.gridwidth = 2;
- gbc_panel.insets = new Insets(0, 0, 0, 5);
- gbc_panel.fill = GridBagConstraints.BOTH;
- gbc_panel.gridx = 1;
- gbc_panel.gridy = 2;
- add(panel, gbc_panel);
- GridBagLayout gbl_panel = new GridBagLayout();
- gbl_panel.columnWidths = new int[]{0, 0, 0};
- gbl_panel.rowHeights = new int[]{0, 0};
- gbl_panel.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
- gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
- panel.setLayout(gbl_panel);
-
- JButton btnNext = new JButton("Next");
- GridBagConstraints gbc_btnNext = new GridBagConstraints();
- gbc_btnNext.gridx = 1;
- gbc_btnNext.gridy = 0;
- panel.add(btnNext, gbc_btnNext);
- btnNext.addActionListener(e -> {
- final String name = (String) comboBox.getSelectedItem();
- final LevelUpClassInfo info = chara.getClasses().stream()
- .filter(c -> c.getName().equals(name)).findFirst()
- .map(dch -> new LevelUpClassInfo(chara, clone(dch), dch.getLevel().value() + 1))
- .orElseGet(() -> new LevelUpClassInfo(chara, new DDCharacterClass(name)));
- nexter.accept(info);
- });
- }
- static ObjectMapper mapper = new ObjectMapper();
- static {
- mapper.registerModule(new Jdk8Module());
- }
-
- @SneakyThrows({JsonParseException.class, JsonMappingException.class, JsonProcessingException.class, IOException.class})
- private DDCharacterClass clone(DDCharacterClass dch) {
- final String data = mapper.writeValueAsString(dch);
- // System.out.println(data);
- return mapper.readValue(data, DDCharacterClass.class);
- }
- }
|