| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package org.leumasjaffe.charsheet.controller;
- import static org.leumasjaffe.charsheet.model.inventory.EquipmentSlot.*;
- import javax.swing.JOptionPane;
- import org.leumasjaffe.charsheet.model.inventory.DDEquipment;
- import org.leumasjaffe.charsheet.model.inventory.DDItem;
- import org.leumasjaffe.charsheet.model.inventory.EquipmentSlot;
- import org.leumasjaffe.charsheet.view.dialog.EquipItemDialog;
- import org.leumasjaffe.observer.ObserverDispatch;
- import lombok.RequiredArgsConstructor;
- public class EquipItemController {
- public static void accept(final DDEquipment inv, final DDItem item) {
- if (item.getUnequippedCount() == 0) { return; }
- if (inv.canEquip(item) || new Helper(inv).getReplaceItem(item.getSlot())) {
- item.adjustCounEquipped(+1);
- inv.equipNext(item);
- ObserverDispatch.notifySubscribers(inv, null);
- }
- }
- @RequiredArgsConstructor
- private static class Helper {
- private static final String QUERY_REPLACE = "Replace Equipped Item";
- private static final String REPLACE_EITHER = "Which of the following items would you like to replace?";
- private static final String REPLACE_BOTH = "Do you want to replace both of the following items?";
- private static final String REPLACE_ONE = "Do you want to replace the following item?";
- final DDEquipment inv;
-
- private boolean getReplaceItem(final EquipmentSlot slot) {
- switch (slot) {
- case TWO_HANDS: return selectToReplaceAllOf(TWO_HANDS, MAIN_HAND, OFF_HAND);
- case ONE_HAND: return selectToReplaceOneOf(MAIN_HAND, OFF_HAND);
- case RING: return selectToReplaceOneOf(RING1, RING2);
- default: return selectToReplace(slot);
- }
- }
- private boolean selectToReplaceAllOf(final EquipmentSlot base,
- final EquipmentSlot slot1, final EquipmentSlot slot2) {
- if (inv.get(slot1).getSlot() == base) {
- return selectToReplace(slot1);
- } else if (JOptionPane.showConfirmDialog(null, createDialogTwoSlots(REPLACE_BOTH, slot1, slot2),
- QUERY_REPLACE, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
- doUnequip(slot1);
- doUnequip(slot2);
- return true;
- }
- return false;
- }
- private boolean selectToReplaceOneOf(final EquipmentSlot slot1,
- final EquipmentSlot slot2) {
- final int choice = JOptionPane.showOptionDialog(null, createDialogTwoSlots(REPLACE_EITHER, slot1, slot2),
- QUERY_REPLACE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
- new String[] {"Cancel", slot1.toString(), slot2.toString()}, null);
- if (choice == JOptionPane.YES_OPTION) {
- doUnequip(slot1);
- return true;
- } else if (choice == JOptionPane.NO_OPTION) {
- doUnequip(slot2);
- return true;
- }
- return false;
- }
- private boolean selectToReplace(final EquipmentSlot slot) {
- if (JOptionPane.showConfirmDialog(null, createDialogOneSlot(slot), QUERY_REPLACE, JOptionPane.YES_NO_OPTION)
- == JOptionPane.YES_OPTION) {
- doUnequip(slot);
- return true;
- }
- return false;
- }
- private void doUnequip(final EquipmentSlot slot) {
- inv.get(slot).adjustCounEquipped(-1);
- inv.unequip(slot);
- }
- private EquipItemDialog createDialogOneSlot(final EquipmentSlot slot) {
- return new EquipItemDialog(REPLACE_ONE, slot, inv.get(slot));
- }
-
- private EquipItemDialog createDialogTwoSlots(final String message,
- final EquipmentSlot slot1, final EquipmentSlot slot2) {
- return new EquipItemDialog(message,
- new EquipItemDialog.Tuple(slot1, inv.get(slot1)),
- new EquipItemDialog.Tuple(slot2, inv.get(slot2)));
- }
- }
- }
|