Search in sources :

Example 1 with BAASBMDropTargetCriticalList

use of megameklab.ui.util.BAASBMDropTargetCriticalList in project megameklab by MegaMek.

the class BMCriticalTransferHandler method importData.

@Override
public boolean importData(TransferSupport info) {
    if (!info.isDrop() || !(getUnit() instanceof Mech)) {
        return false;
    }
    if (info.getComponent() instanceof BAASBMDropTargetCriticalList) {
        BAASBMDropTargetCriticalList<?> list = (BAASBMDropTargetCriticalList<?>) info.getComponent();
        location = Integer.parseInt(list.getName());
        Transferable t = info.getTransferable();
        int slotNumber = list.getDropLocation().getIndex();
        if ((slotNumber < 0) || (slotNumber >= getUnit().getNumberOfCriticals(location))) {
            return false;
        }
        try {
            Mounted eq = getUnit().getEquipment(Integer.parseInt((String) t.getTransferData(DataFlavor.stringFlavor)));
            // If this equipment is already mounted, clear the criticals it's mounted in
            if (eq.getLocation() != Entity.LOC_NONE || eq.getSecondLocation() != Entity.LOC_NONE) {
                UnitUtil.removeCriticals(getUnit(), eq);
                UnitUtil.changeMountStatus(getUnit(), eq, Entity.LOC_NONE, -1, false);
            } else {
                eq.setOmniPodMounted(UnitUtil.canPodMount(getUnit(), eq));
            }
            StringBuffer errors = new StringBuffer();
            if (!TestEntity.isValidLocation(getUnit(), eq.getType(), location, errors)) {
                JOptionPane.showMessageDialog(null, eq.getName() + " can't be placed in " + getUnit().getLocationAbbr(location) + ":\n" + errors, "Invalid Location", JOptionPane.INFORMATION_MESSAGE);
                doRefresh();
                return false;
            }
            // superheavies can put 2 ammobins or heatsinks in one crit
            if ((getUnit() instanceof Mech) && getUnit().isSuperHeavy()) {
                CriticalSlot cs = getUnit().getCritical(location, slotNumber);
                if ((cs != null) && (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) && (cs.getMount2() == null)) {
                    EquipmentType etype = cs.getMount().getType();
                    EquipmentType etype2 = eq.getType();
                    boolean canDouble = false;
                    if ((etype instanceof AmmoType) && (etype2 instanceof AmmoType)) {
                        canDouble = (((AmmoType) etype).getAmmoType() == ((AmmoType) etype2).getAmmoType()) && (((AmmoType) etype).getRackSize() == ((AmmoType) etype2).getRackSize());
                    } else if (etype.equals(etype2) && UnitUtil.isHeatSink(etype)) {
                        canDouble = etype.getCriticals(getUnit()) == 1;
                    }
                    if (canDouble) {
                        cs.setMount2(eq);
                        changeMountStatus(eq, location);
                        return true;
                    }
                }
            }
            return addEquipmentMech((Mech) getUnit(), eq, slotNumber);
        } catch (LocationFullException lfe) {
            JOptionPane.showMessageDialog(null, lfe.getMessage(), "Location Full", JOptionPane.INFORMATION_MESSAGE);
            doRefresh();
            return false;
        } catch (Exception ex) {
            LogManager.getLogger().error("", ex);
        }
        return true;
    }
    return false;
}
Also used : Transferable(java.awt.datatransfer.Transferable) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) BAASBMDropTargetCriticalList(megameklab.ui.util.BAASBMDropTargetCriticalList)

Example 2 with BAASBMDropTargetCriticalList

use of megameklab.ui.util.BAASBMDropTargetCriticalList in project megameklab by MegaMek.

the class BACriticalView method refresh.

public void refresh() {
    critSuit = new BACriticalSuit(getBattleArmor());
    leftArmPanel.removeAll();
    rightArmPanel.removeAll();
    bodyPanel.removeAll();
    turretPanel.removeAll();
    int[] numAPWeapons = new int[BattleArmor.MOUNT_NUM_LOCS];
    int[] numAMWeapons = new int[BattleArmor.MOUNT_NUM_LOCS];
    for (Mounted m : getBattleArmor().getEquipment()) {
        if ((m.getLocation() == BattleArmor.LOC_SQUAD) || (m.getLocation() == trooper)) {
            critSuit.addMounted(m.getBaMountLoc(), m);
            // Weapons mounted in a quad turret count against the body limits
            int useLoc = m.getBaMountLoc();
            if (useLoc == BattleArmor.MOUNT_LOC_TURRET) {
                useLoc = BattleArmor.MOUNT_LOC_BODY;
            }
            if (useLoc != BattleArmor.MOUNT_LOC_NONE) {
                if ((m.getType() instanceof WeaponType) && !(m.getType() instanceof InfantryWeapon)) {
                    numAMWeapons[useLoc]++;
                }
                if (m.getType().hasFlag(MiscType.F_AP_MOUNT)) {
                    numAPWeapons[useLoc]++;
                }
            }
        }
    }
    synchronized (getBattleArmor()) {
        for (int location = 0; location < critSuit.locations(); location++) {
            Vector<String> critNames = new Vector<>(1, 1);
            for (int slot = 0; slot < critSuit.getNumCriticals(location); slot++) {
                CriticalSlot cs = critSuit.getCritical(location, slot);
                if (cs == null) {
                    if (showEmpty) {
                        critNames.add(CritCellUtil.EMPTY_CRITCELL_TEXT);
                    }
                } else if (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) {
                    Mounted m = cs.getMount();
                    if (m == null) {
                        if (showEmpty) {
                            critNames.add(CritCellUtil.EMPTY_CRITCELL_TEXT);
                        }
                    } else {
                        critNames.add(m.getName() + ":" + slot + ":" + getBattleArmor().getEquipmentNum(m));
                    }
                }
            }
            BAASBMDropTargetCriticalList<String> criticalSlotList = new BAASBMDropTargetCriticalList<>(critNames, eSource, refresh, showEmpty, this);
            criticalSlotList.setVisibleRowCount(critNames.size());
            criticalSlotList.setAlignmentX(JComponent.CENTER_ALIGNMENT);
            criticalSlotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            criticalSlotList.setName(location + ":" + trooper);
            criticalSlotList.setBorder(BorderFactory.createLineBorder(CritCellUtil.CRITCELL_BORDER_COLOR));
            switch(location) {
                case BattleArmor.MOUNT_LOC_LARM:
                    leftArmPanel.add(criticalSlotList);
                    break;
                case BattleArmor.MOUNT_LOC_RARM:
                    rightArmPanel.add(criticalSlotList);
                    break;
                case BattleArmor.MOUNT_LOC_BODY:
                    bodyPanel.add(criticalSlotList);
                    break;
                case BattleArmor.MOUNT_LOC_TURRET:
                    turretPanel.add(criticalSlotList);
                    break;
            }
        }
        String[] amTxt = new String[BattleArmor.MOUNT_NUM_LOCS];
        String[] apTxt = new String[BattleArmor.MOUNT_NUM_LOCS];
        for (int loc = 0; loc < BattleArmor.MOUNT_NUM_LOCS; loc++) {
            amTxt[loc] = "Anti-Mech Weapons: " + numAMWeapons[loc] + "/" + getBattleArmor().getNumAllowedAntiMechWeapons(loc);
            apTxt[loc] = "Anti-Personnel Weapons: " + numAPWeapons[loc] + "/" + getBattleArmor().getNumAllowedAntiPersonnelWeapons(loc, trooper);
            if (numAMWeapons[loc] > getBattleArmor().getNumAllowedAntiMechWeapons(loc)) {
                amTxt[loc] = "<html><font color='C00000'>" + amTxt[loc] + "</font></html>";
            }
            if (numAPWeapons[loc] > getBattleArmor().getNumAllowedAntiMechWeapons(loc)) {
                apTxt[loc] = "<html><font color='C00000'>" + apTxt[loc] + "</font></html>";
            }
        }
        leftArmPanel.add(makeLabel(amTxt[BattleArmor.MOUNT_LOC_LARM]));
        leftArmPanel.add(makeLabel(apTxt[BattleArmor.MOUNT_LOC_LARM]));
        rightArmPanel.add(makeLabel(amTxt[BattleArmor.MOUNT_LOC_RARM]));
        rightArmPanel.add(makeLabel(apTxt[BattleArmor.MOUNT_LOC_RARM]));
        bodyPanel.add(makeLabel(amTxt[BattleArmor.MOUNT_LOC_BODY]));
        bodyPanel.add(makeLabel(apTxt[BattleArmor.MOUNT_LOC_BODY]));
        // Hide the arm panels if we are a quad
        boolean isQuad = getBattleArmor().getChassisType() == BattleArmor.CHASSIS_TYPE_QUAD;
        leftArmPanel.setVisible(!isQuad);
        rightArmPanel.setVisible(!isQuad);
        turretPanel.setVisible(isQuad && (getBattleArmor().getTurretCapacity() > 0));
        EntityVerifier entityVerifier = EntityVerifier.getInstance(new File("data/mechfiles/UnitVerifierOptions.xml"));
        TestBattleArmor testBA = new TestBattleArmor(getBattleArmor(), entityVerifier.baOption, null);
        String weightTxt = "Weight: " + String.format("%1$.3f", testBA.calculateWeight(trooper)) + "/" + getBattleArmor().getTrooperWeight();
        if (testBA.calculateWeight(trooper) > getBattleArmor().getTrooperWeight()) {
            weightTxt = "<html><font color='C00000'>" + weightTxt + "</font></html>";
        }
        weightLabel.setText(weightTxt);
        validate();
    }
}
Also used : EntityVerifier(megamek.common.verifier.EntityVerifier) TestBattleArmor(megamek.common.verifier.TestBattleArmor) BAASBMDropTargetCriticalList(megameklab.ui.util.BAASBMDropTargetCriticalList) Vector(java.util.Vector) File(java.io.File) InfantryWeapon(megamek.common.weapons.infantry.InfantryWeapon)

Aggregations

BAASBMDropTargetCriticalList (megameklab.ui.util.BAASBMDropTargetCriticalList)2 Transferable (java.awt.datatransfer.Transferable)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 File (java.io.File)1 IOException (java.io.IOException)1 Vector (java.util.Vector)1 EntityVerifier (megamek.common.verifier.EntityVerifier)1 TestBattleArmor (megamek.common.verifier.TestBattleArmor)1 InfantryWeapon (megamek.common.weapons.infantry.InfantryWeapon)1