Search in sources :

Example 71 with Mounted

use of megamek.common.Mounted in project megameklab by MegaMek.

the class BuildView method mousePressed.

public void mousePressed(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
        JPopupMenu popup = new JPopupMenu();
        JMenuItem item;
        final int selectedRow = equipmentTable.rowAtPoint(e.getPoint());
        Mounted mount = (Mounted) equipmentTable.getModel().getValueAt(selectedRow, CriticalTableModel.EQUIPMENT);
        String[] locations;
        locations = getTank().getLocationNames();
        for (int location = 0; location < getTank().locations(); location++) {
            if (UnitUtil.isValidLocation(getTank(), mount.getType(), location)) {
                item = new JMenuItem("Add to " + locations[location]);
                final int loc = location;
                item.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        jMenuLoadComponent_actionPerformed(loc, selectedRow);
                    }
                });
                popup.add(item);
            }
        }
        popup.show(this, e.getX(), e.getY());
    }
}
Also used : ActionListener(java.awt.event.ActionListener) Mounted(megamek.common.Mounted) ActionEvent(java.awt.event.ActionEvent) JMenuItem(javax.swing.JMenuItem) JPopupMenu(javax.swing.JPopupMenu)

Example 72 with Mounted

use of megamek.common.Mounted in project megameklab by MegaMek.

the class BuildView method jMenuLoadComponent_actionPerformed.

private void jMenuLoadComponent_actionPerformed(int location, int selectedRow) {
    Mounted eq = (Mounted) equipmentTable.getModel().getValueAt(selectedRow, CriticalTableModel.EQUIPMENT);
    UnitUtil.changeMountStatus(getTank(), eq, location, -1, false);
    try {
        getTank().addEquipment(eq, location, false);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // go back up to grandparent build tab and fire a full refresh.
    ((BuildTab) getParent().getParent()).refreshAll();
}
Also used : BuildTab(megameklab.com.ui.Vehicle.tabs.BuildTab) Mounted(megamek.common.Mounted)

Example 73 with Mounted

use of megamek.common.Mounted in project megameklab by MegaMek.

the class CriticalView method refresh.

public void refresh() {
    leftPanel.removeAll();
    rightPanel.removeAll();
    bodyPanel.removeAll();
    frontPanel.removeAll();
    rearPanel.removeAll();
    turretPanel.removeAll();
    dualTurretPanel.removeAll();
    fullTurretPanel.removeAll();
    rearLeftPanel.removeAll();
    rearRightPanel.removeAll();
    this.remove(fullTurretPanel);
    if (getTank() instanceof VTOL) {
        if (getTank().hasNoTurret()) {
            turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rotor", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(turretPanel);
            this.add(fullTurretPanel);
        } else {
            dualTurretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(dualTurretPanel);
            turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rotor", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
            fullTurretPanel.add(turretPanel);
            this.add(fullTurretPanel);
        }
    } else if (!getTank().hasNoDualTurret()) {
        dualTurretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Front Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(dualTurretPanel);
        turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Rear Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(turretPanel);
        this.add(fullTurretPanel);
    } else if (!getTank().hasNoTurret()) {
        turretPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Turret", TitledBorder.TOP, TitledBorder.DEFAULT_POSITION));
        fullTurretPanel.add(turretPanel);
        this.add(fullTurretPanel);
    }
    synchronized (getTank()) {
        for (int location = 0; location < getTank().locations(); location++) {
            // JPanel locationPanel = new JPanel();
            Vector<String> critNames = new Vector<String>(1, 1);
            for (int slot = 0; slot < getTank().getNumberOfCriticals(location); slot++) {
                CriticalSlot cs = getTank().getCritical(location, slot);
                if (cs == null) {
                    continue;
                } else if (cs.getType() == CriticalSlot.TYPE_SYSTEM) {
                    critNames.add(getMech().getSystemName(cs.getIndex()));
                } else if (cs.getType() == CriticalSlot.TYPE_EQUIPMENT) {
                    try {
                        Mounted m = cs.getMount();
                        // Critical didn't get removed. Remove it now.
                        if (m == null) {
                            m = cs.getMount();
                            if (m == null) {
                                getTank().setCritical(location, slot, null);
                                continue;
                            }
                            cs.setMount(m);
                        }
                        StringBuffer critName = new StringBuffer(m.getName());
                        if (critName.length() > 25) {
                            critName.setLength(25);
                            critName.append("...");
                        }
                        if (m.isRearMounted()) {
                            critName.append(" (R)");
                        }
                        if (m.isSponsonTurretMounted()) {
                            critName.append(" (ST)");
                        }
                        if (m.isPintleTurretMounted()) {
                            critName.append(" (PT)");
                        }
                        critNames.add(critName.toString());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            if (critNames.size() == 0) {
                critNames.add(MtfFile.EMPTY);
            }
            DropTargetCriticalList<String> criticalSlotList = null;
            DropTargetCriticalList<String> dropTargetCriticalList = new DropTargetCriticalList<String>(critNames, eSource, refresh, showEmpty);
            criticalSlotList = dropTargetCriticalList;
            criticalSlotList.setVisibleRowCount(critNames.size());
            criticalSlotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            criticalSlotList.setFont(new Font("Arial", Font.PLAIN, 10));
            criticalSlotList.setName(Integer.toString(location));
            criticalSlotList.setBorder(BorderFactory.createEtchedBorder(Color.WHITE.brighter(), Color.BLACK.darker()));
            if (!(getTank()).isSuperHeavy()) {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_LEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_RIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_TURRET:
                        turretPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_TURRET_2:
                        dualTurretPanel.add(criticalSlotList);
                        break;
                }
            } else if (getTank() instanceof VTOL) {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_LEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_RIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case VTOL.LOC_ROTOR:
                        turretPanel.add(criticalSlotList);
                        break;
                }
            } else {
                switch(location) {
                    case Tank.LOC_FRONT:
                        frontPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_FRONTLEFT:
                        leftPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_FRONTRIGHT:
                        rightPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REARLEFT:
                        rearLeftPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REARRIGHT:
                        rearRightPanel.add(criticalSlotList);
                        break;
                    case Tank.LOC_BODY:
                        bodyPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_REAR:
                        rearPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_TURRET:
                        turretPanel.add(criticalSlotList);
                        break;
                    case SuperHeavyTank.LOC_TURRET_2:
                        dualTurretPanel.add(criticalSlotList);
                        break;
                }
            }
        }
        middlePanel2.setVisible(getTank().isSuperHeavy() && !(getTank() instanceof VTOL));
        frontPanel.repaint();
        bodyPanel.repaint();
        leftPanel.repaint();
        rightPanel.repaint();
        rearLeftPanel.repaint();
        rearRightPanel.repaint();
        rearPanel.repaint();
        turretPanel.repaint();
        dualTurretPanel.repaint();
    }
}
Also used : VTOL(megamek.common.VTOL) DropTargetCriticalList(megameklab.com.util.DropTargetCriticalList) CriticalSlot(megamek.common.CriticalSlot) Mounted(megamek.common.Mounted) Vector(java.util.Vector) Font(java.awt.Font)

Example 74 with Mounted

use of megamek.common.Mounted in project megameklab by MegaMek.

the class WeaponView method actionPerformed.

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals(LASERWEAPONADD_COMMAND)) {
        try {
            if (laserWeaponCombo.getSelectedIndex() > -1) {
                for (int index : laserWeaponCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subLaserWeaponList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(LASERAMMOADD_COMMAND) && (subLaserAmmoList.size() > 0)) {
        try {
            if (laserWeaponCombo.getSelectedIndex() > -1) {
                for (int index : laserAmmoCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subLaserAmmoList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(MISSILEWEAPONADD_COMMAND)) {
        try {
            if (missileWeaponCombo.getSelectedIndex() > -1) {
                for (int index : missileWeaponCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subMissileWeaponList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                    // for tracking. We do not need this in MLab
                    if (mount.getType().hasFlag(WeaponType.F_ONESHOT)) {
                        getTank().getEquipment().remove(getTank().getEquipment().size() - 1);
                        getTank().getAmmo().remove(getTank().getAmmo().size() - 1);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(MISSILEAMMOADD_COMMAND)) {
        try {
            if (missileAmmoCombo.getSelectedIndex() > -1) {
                for (int index : missileAmmoCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subMissileAmmoList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(BALLISTICWEAPONADD_COMMAND)) {
        try {
            if (ballisticWeaponCombo.getSelectedIndex() > -1) {
                for (int index : ballisticWeaponCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subBallisticWeaponList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                    // for tracking. We do not need this in MLab
                    if (mount.getType().hasFlag(WeaponType.F_ONESHOT)) {
                        getTank().getEquipment().remove(getTank().getEquipment().size() - 1);
                        getTank().getAmmo().remove(getTank().getAmmo().size() - 1);
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(BALLISTICAMMOADD_COMMAND)) {
        try {
            if (ballisticAmmoCombo.getSelectedIndex() > -1) {
                for (int index : ballisticAmmoCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subBallisticAmmoList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(ARTILLERYWEAPONADD_COMMAND)) {
        try {
            if (artilleryWeaponCombo.getSelectedIndex() > -1) {
                for (int index : artilleryWeaponCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subArtilleryWeaponList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(ARTILLERYAMMOADD_COMMAND)) {
        try {
            if (artilleryAmmoCombo.getSelectedIndex() > -1) {
                for (int index : artilleryAmmoCombo.getSelectedIndices()) {
                    Mounted mount = getTank().addEquipment(subArtilleryAmmoList.elementAt(index), Entity.LOC_NONE, false);
                    weaponList.addCrit(mount);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (e.getActionCommand().equals(REMOVE_COMMAND)) {
        int startRow = equipmentTable.getSelectedRow();
        int count = equipmentTable.getSelectedRowCount();
        for (; count > 0; count--) {
            if (startRow > -1) {
                weaponList.removeMounted(startRow);
                weaponList.removeCrit(startRow);
            }
        }
        refresh.refreshAll();
    } else if (e.getActionCommand().equals(REMOVEALL_COMMAND)) {
        removeAllWeapons();
    } else {
        return;
    }
    fireTableRefresh();
}
Also used : Mounted(megamek.common.Mounted)

Example 75 with Mounted

use of megamek.common.Mounted in project megameklab by MegaMek.

the class AeroBayTransferHandler method importData.

@Override
public boolean importData(TransferSupport support) {
    if (!support.isDrop()) {
        return false;
    }
    // Fields are equipmentNum, node child index, bay child index
    String[] source = null;
    List<Mounted> eqList = new ArrayList<>();
    try {
        source = ((String) support.getTransferable().getTransferData(DataFlavor.stringFlavor)).split(":");
        for (String field : source[0].split(",")) {
            int eqNum = Integer.parseInt(field);
            Mounted m = eSource.getEntity().getEquipment(eqNum);
            if (null != m) {
                eqList.add(m);
            }
        }
    } catch (Exception ex) {
        MegaMekLab.getLogger().log(AeroBayTransferHandler.class, // $NON-NLS-1$
        "importData(TransferSupport)", ex);
        return false;
    }
    if (eqList.isEmpty()) {
        return false;
    }
    if ((support.getComponent() instanceof BayWeaponCriticalTree)) {
        final BayWeaponCriticalTree tree = (BayWeaponCriticalTree) support.getComponent();
        if (eSource.getEntity().usesWeaponBays() && (eqList.size() == 1)) {
            // If it's a bay we move it and its entire contents. Otherwise we find the bay that was
            // dropped on and add it there. A weapon dropped on an illegal bay will create a new one
            // and non-bay equipment will be added at the top level regardless of the drop location.
            // Non-weapon bay equipment cannot be dropped on an illegal bay.
            final Mounted mount = eqList.get(0);
            if (mount.getType() instanceof BayWeapon) {
                tree.addBay(mount);
            } else if ((mount.getType() instanceof AmmoType) && (support.getUserDropAction() == AMMO_SINGLE)) {
                // Default action for ammo is to move a single slot. Holding the ctrl key when dropping
                // will create a AMMO_ALL command, which adds all the ammo of the type.
                tree.addAmmo(mount, ((AmmoType) mount.getType()).getShots(), ((JTree.DropLocation) support.getDropLocation()).getPath());
            } else {
                tree.addToArc(mount, ((JTree.DropLocation) support.getDropLocation()).getPath());
            }
        } else {
            // Small craft don't use bays.
            tree.addToLocation(eqList);
        }
    } else {
        // Target is unallocated bay table.
        for (Mounted mount : eqList) {
            if (mount.getType() instanceof AmmoType) {
                AmmoType at = (AmmoType) mount.getType();
                // Check whether we are moving one of multiple slots.
                if ((support.getUserDropAction() == AMMO_SINGLE) && (mount.getUsableShotsLeft() > at.getShots())) {
                    mount.setShotsLeft(mount.getUsableShotsLeft() - at.getShots());
                }
                Mounted addMount = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), at);
                if (null != addMount) {
                    if (support.getUserDropAction() == AMMO_SINGLE) {
                        addMount.setShotsLeft(addMount.getUsableShotsLeft() + at.getShots());
                    } else {
                        addMount.setShotsLeft(addMount.getUsableShotsLeft() + mount.getUsableShotsLeft());
                    }
                } else {
                    try {
                        Mounted m = eSource.getEntity().addEquipment(at, Entity.LOC_NONE);
                        if (support.getUserDropAction() == AMMO_ALL) {
                            m.setShotsLeft(mount.getUsableShotsLeft());
                        }
                    } catch (LocationFullException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } else {
                List<Mounted> toRemove;
                if (mount.getType() instanceof BayWeapon) {
                    toRemove = new ArrayList<>();
                    for (Integer num : mount.getBayWeapons()) {
                        toRemove.add(eSource.getEntity().getEquipment(num));
                    }
                    for (Integer num : mount.getBayAmmo()) {
                        toRemove.add(eSource.getEntity().getEquipment(num));
                    }
                } else {
                    toRemove = Collections.singletonList(mount);
                }
                for (Mounted m : toRemove) {
                    if (m.getType() instanceof AmmoType) {
                        Mounted aMount = UnitUtil.findUnallocatedAmmo(eSource.getEntity(), m.getType());
                        if (null != aMount) {
                            aMount.setShotsLeft(aMount.getUsableShotsLeft() + m.getUsableShotsLeft());
                            m.setShotsLeft(0);
                            continue;
                        }
                    }
                    UnitUtil.removeCriticals(eSource.getEntity(), m);
                    UnitUtil.changeMountStatus(eSource.getEntity(), m, Entity.LOC_NONE, Entity.LOC_NONE, false);
                    if ((mount.getType() instanceof WeaponType) && (m.getLinkedBy() != null)) {
                        UnitUtil.removeCriticals(eSource.getEntity(), m.getLinkedBy());
                        UnitUtil.changeMountStatus(eSource.getEntity(), m.getLinkedBy(), Entity.LOC_NONE, Entity.LOC_NONE, false);
                        m.getLinkedBy().setLinked(null);
                        m.setLinkedBy(null);
                    }
                }
                UnitUtil.compactCriticals(eSource.getEntity());
            }
            if (mount.getType() instanceof BayWeapon) {
                mount.getBayWeapons().clear();
                mount.getBayAmmo().clear();
                UnitUtil.removeMounted(eSource.getEntity(), mount);
            }
        }
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) LocationFullException(megamek.common.LocationFullException) IOException(java.io.IOException) AmmoType(megamek.common.AmmoType) LocationFullException(megamek.common.LocationFullException) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType)

Aggregations

Mounted (megamek.common.Mounted)162 MiscType (megamek.common.MiscType)50 LocationFullException (megamek.common.LocationFullException)39 AmmoType (megamek.common.AmmoType)36 EquipmentType (megamek.common.EquipmentType)34 CriticalSlot (megamek.common.CriticalSlot)31 ArrayList (java.util.ArrayList)30 WeaponType (megamek.common.WeaponType)29 Vector (java.util.Vector)25 EntityLoadingException (megamek.common.loaders.EntityLoadingException)21 Font (java.awt.Font)14 Entity (megamek.common.Entity)13 List (java.util.List)12 Dimension (java.awt.Dimension)11 JLabel (javax.swing.JLabel)11 JMenuItem (javax.swing.JMenuItem)11 JPopupMenu (javax.swing.JPopupMenu)11 BattleArmor (megamek.common.BattleArmor)11 ActionEvent (java.awt.event.ActionEvent)10 Weapon (megamek.common.weapons.Weapon)10