Search in sources :

Example 1 with Nullable

use of megamek.common.annotations.Nullable in project megameklab by MegaMek.

the class BayWeaponCriticalTree method addToBay.

/**
 * Adds an equipment mount to a bay. Changes the equipment mount's location and updates the bay's
 * weapon or ammo list if necessary.
 *
 * @param bay
 * @param eq
 */
public void addToBay(@Nullable Mounted bay, Mounted eq) {
    // Check that we have a bay
    if ((null == bay) || !canTakeEquipment(bay, eq)) {
        if (eq.getType() instanceof WeaponType) {
            EquipmentType bayType = ((WeaponType) eq.getType()).getBayType();
            addToNewBay(bayType, eq);
        } else {
            addToLocation(eq);
        }
        return;
    } else if (eq.getType() instanceof MiscType) {
        if (eq.getType().hasFlag(MiscType.F_ARTEMIS) || eq.getType().hasFlag(MiscType.F_ARTEMIS_PROTO) || eq.getType().hasFlag(MiscType.F_ARTEMIS_V)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                final WeaponType wtype = (WeaponType) weapon.getType();
                if ((weapon.getLinkedBy() == null) && ((wtype.getAmmoType() == AmmoType.T_LRM) || (wtype.getAmmoType() == AmmoType.T_SRM) || (wtype.getAmmoType() == AmmoType.T_MML) || (wtype.getAmmoType() == AmmoType.T_NLRM))) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        } else if (eq.getType().hasFlag(MiscType.F_APOLLO) || eq.getType().hasFlag(MiscType.F_PPC_CAPACITOR)) {
            for (Integer eqNum : bay.getBayWeapons()) {
                final Mounted weapon = eSource.getEntity().getEquipment(eqNum);
                if (weapon.getLinkedBy() == null) {
                    moveToArc(eq);
                    eq.setLinked(weapon);
                    break;
                }
            }
        }
    } else {
        // there.
        if (eq.getType() instanceof AmmoType) {
            Optional<Mounted> addMount = bay.getBayAmmo().stream().map(n -> eSource.getEntity().getEquipment(n)).filter(m -> m.getType() == eq.getType()).findFirst();
            if (addMount.isPresent()) {
                addMount.get().setShotsLeft(addMount.get().getBaseShotsLeft() + eq.getBaseShotsLeft());
                UnitUtil.removeMounted(eSource.getEntity(), eq);
                refresh.refreshEquipment();
                refresh.refreshBuild();
                refresh.refreshPreview();
                refresh.refreshStatus();
                refresh.refreshSummary();
                return;
            }
        }
        EquipmentNode bayNode = null;
        for (Enumeration<?> e = ((TreeNode) model.getRoot()).children(); e.hasMoreElements(); ) {
            final EquipmentNode node = (EquipmentNode) e.nextElement();
            if (node.getMounted() == bay) {
                bayNode = node;
                break;
            }
        }
        if (null != bayNode) {
            moveToArc(eq);
            EquipmentNode eqNode = new EquipmentNode(eq);
            model.insertNodeInto(eqNode, bayNode, bayNode.getChildCount());
            eqNode.setParent(bayNode);
            if (eq.getType() instanceof WeaponType) {
                bay.addWeaponToBay(eSource.getEntity().getEquipmentNum(eq));
                if (eq.getLinkedBy() != null) {
                    moveToArc(eq.getLinkedBy());
                }
            } else if (eq.getType() instanceof AmmoType) {
                bay.addAmmoToBay(eSource.getEntity().getEquipmentNum(eq));
            }
        } else {
            // $NON-NLS-1$
            MegaMekLab.getLogger().log(// $NON-NLS-1$
            BayWeaponCriticalTree.class, // $NON-NLS-1$
            "addToBay(Mounted,Mounted)", LogLevel.DEBUG, // $NON-NLS-1$
            bay.getName() + "[" + eSource.getEntity().getEquipmentNum(bay) + "] not found in " + // $NON-NLS-1$
            getLocationName());
        }
    }
    refresh.refreshEquipment();
    refresh.refreshBuild();
    refresh.refreshPreview();
    refresh.refreshStatus();
    refresh.refreshSummary();
}
Also used : AmmoType(megamek.common.AmmoType) Color(java.awt.Color) Enumeration(java.util.Enumeration) EntitySource(megameklab.com.ui.EntitySource) Vector(java.util.Vector) MouseAdapter(java.awt.event.MouseAdapter) SmallCraft(megamek.common.SmallCraft) TestAero(megamek.common.verifier.TestAero) MouseListener(java.awt.event.MouseListener) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) ToolTipManager(javax.swing.ToolTipManager) TreePath(javax.swing.tree.TreePath) LocationFullException(megamek.common.LocationFullException) Set(java.util.Set) JMenu(javax.swing.JMenu) Component(java.awt.Component) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) Dimension(java.awt.Dimension) List(java.util.List) WeaponType(megamek.common.WeaponType) Graphics(java.awt.Graphics) MiscType(megamek.common.MiscType) Optional(java.util.Optional) Entity(megamek.common.Entity) RefreshListener(megameklab.com.util.RefreshListener) InputEvent(java.awt.event.InputEvent) Rectangle(java.awt.Rectangle) LogLevel(megamek.common.logging.LogLevel) TreeNode(javax.swing.tree.TreeNode) ArrayList(java.util.ArrayList) PPCBayWeapon(megamek.common.weapons.bayweapons.PPCBayWeapon) HashSet(java.util.HashSet) EquipmentType(megamek.common.EquipmentType) JMenuItem(javax.swing.JMenuItem) MegaMekLab(megameklab.com.MegaMekLab) CConfig(megameklab.com.util.CConfig) Mounted(megamek.common.Mounted) UnitUtil(megameklab.com.util.UnitUtil) DefaultTreeCellRenderer(javax.swing.tree.DefaultTreeCellRenderer) BayWeapon(megamek.common.weapons.bayweapons.BayWeapon) AmmoType(megamek.common.AmmoType) JPopupMenu(javax.swing.JPopupMenu) Nullable(megamek.common.annotations.Nullable) TreeSelectionModel(javax.swing.tree.TreeSelectionModel) JTree(javax.swing.JTree) MouseEvent(java.awt.event.MouseEvent) JLabel(javax.swing.JLabel) StringJoiner(java.util.StringJoiner) PPCWeapon(megamek.common.weapons.ppc.PPCWeapon) Collections(java.util.Collections) Enumeration(java.util.Enumeration) Optional(java.util.Optional) Mounted(megamek.common.Mounted) WeaponType(megamek.common.WeaponType) MiscType(megamek.common.MiscType) EquipmentType(megamek.common.EquipmentType)

Example 2 with Nullable

use of megamek.common.annotations.Nullable in project megameklab by MegaMek.

the class PrintMech method loadPipSVG.

@Nullable
private NodeList loadPipSVG(String filename) {
    File f = new File(filename);
    if (!f.exists()) {
        return null;
    }
    Document doc;
    try (InputStream is = new FileInputStream(f)) {
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        final String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXDocumentFactory df = new SAXDocumentFactory(impl, parser);
        doc = df.createDocument(f.toURI().toASCIIString(), is);
    } catch (Exception e) {
        LogManager.getLogger().error("Failed to open pip SVG file! Path: " + f.getName());
        return null;
    }
    if (doc == null) {
        LogManager.getLogger().error("Failed to open pip SVG file! Path: " + f.getName());
        return null;
    } else {
        return doc.getElementsByTagName(SVGConstants.SVG_PATH_TAG);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) SAXDocumentFactory(org.apache.batik.dom.util.SAXDocumentFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(megamek.common.annotations.Nullable)

Example 3 with Nullable

use of megamek.common.annotations.Nullable in project megameklab by MegaMek.

the class PrintRecordSheet method loadSVG.

/**
 * Creates a {@link Document} from an svg image file
 *
 * @param filename The name of the SVG file
 * @return The document object
 */
@Nullable
static Document loadSVG(String dirName, String filename) {
    File f = new File(dirName, filename);
    Document svgDocument = null;
    try (InputStream is = new FileInputStream(f)) {
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        final String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXDocumentFactory df = new SAXDocumentFactory(impl, parser);
        svgDocument = df.createDocument(f.toURI().toASCIIString(), is);
    } catch (Exception ex) {
        LogManager.getLogger().error("", ex);
    }
    if (svgDocument == null) {
        LogManager.getLogger().error("Failed to open SVG file! Path: data/images/recordsheets/" + filename);
    }
    return svgDocument;
}
Also used : DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) SAXDocumentFactory(org.apache.batik.dom.util.SAXDocumentFactory) SVGDocument(org.w3c.dom.svg.SVGDocument) Document(org.w3c.dom.Document) ConfigurationException(org.apache.fop.configuration.ConfigurationException) TranscoderException(org.apache.batik.transcoder.TranscoderException) SAXException(org.xml.sax.SAXException) Nullable(megamek.common.annotations.Nullable)

Aggregations

Nullable (megamek.common.annotations.Nullable)3 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)2 SAXDocumentFactory (org.apache.batik.dom.util.SAXDocumentFactory)2 Color (java.awt.Color)1 Component (java.awt.Component)1 Dimension (java.awt.Dimension)1 Graphics (java.awt.Graphics)1 Rectangle (java.awt.Rectangle)1 InputEvent (java.awt.event.InputEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 MouseListener (java.awt.event.MouseListener)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 List (java.util.List)1