Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LGTM: Fixing the vast majority of open issues #1052

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions megameklab/src/megameklab/printing/PrintMech.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* MegaMekLab - Copyright (C) 2017 - The MegaMek Team
* MegaMekLab - Copyright (c) 2017-2022 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
Expand Down Expand Up @@ -34,6 +34,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -322,8 +323,7 @@ private boolean copyPipPattern(NodeList nl, String parentName) {
return null;
}
Document doc;
try {
InputStream is = new FileInputStream(f);
try (InputStream is = new FileInputStream(f)) {
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
final String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXDocumentFactory df = new SAXDocumentFactory(impl, parser);
Expand All @@ -332,13 +332,15 @@ private boolean copyPipPattern(NodeList nl, String parentName) {
LogManager.getLogger().error("Failed to open pip SVG file! Path: " + f.getName());
return null;
}
if (null == doc) {

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);
}
return doc.getElementsByTagName(SVGConstants.SVG_PATH_TAG);
}

// Mech armor and structure pips require special handling for rear armor and superheavy head armor/IS
@Override
protected void drawArmorStructurePips() {
Expand Down Expand Up @@ -693,7 +695,7 @@ private String formatCritName(CriticalSlot cs) {
name = mech.getCrew().getCrewType().getRoleName(mech.getCrewForCockpitSlot(Mech.LOC_HEAD, cs));
}
}
assert (name != null);
Objects.requireNonNull(name);
return name.replace("Standard ", "");
}
} else {
Expand Down
52 changes: 25 additions & 27 deletions megameklab/src/megameklab/printing/PrintRecordSheet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* MegaMekLab - Copyright (C) 2017 - The MegaMek Team
* MegaMekLab
* Copyright (c) 2017-2022 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
Expand Down Expand Up @@ -55,6 +56,7 @@
import java.io.*;
import java.net.URLConnection;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
Expand All @@ -79,7 +81,7 @@ public abstract class PrintRecordSheet implements Printable, IdConstants {
public final static String FILL_WHITE = "#ffffff";
/** Scale factor for record sheets with reference tables */
public final static double TABLE_RATIO = 0.8;

enum PipType {
CIRCLE, DIAMOND;

Expand Down Expand Up @@ -224,21 +226,21 @@ private void subColorElements() {
* Creates a {@link Document} from an svg image file
*
* @param filename The name of the SVG file
* @return The document object
* @return The document object
*/
static Document loadSVG(String dirName, String filename) {
static @Nullable Document loadSVG(String dirName, String filename) {
File f = new File(dirName, filename);
Document svgDocument = null;
try {
InputStream is = new FileInputStream(f);
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 e) {
LogManager.getLogger().error("", e);
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
}
if (null == svgDocument) {

if (svgDocument == null) {
LogManager.getLogger().error("Failed to open SVG file! Path: data/images/recordsheets/" + filename);
}
return svgDocument;
Expand All @@ -248,21 +250,16 @@ static Document loadSVG(String dirName, String filename) {
* Checks the <code>style</code> attribute of an {@link Element} for a given property and returns its
* value, or null if the property does not exist.
*
* @param element The element to check the property of
* @param element The element to check the property of
* @param property The name of the property
* @return The value of the property, or <code>null</code> if the property does not exist.
* @return The value of the property, or <code>null</code> if the property does not exist.
*/
static @Nullable
String parseStyle(Element element, String property) {
static @Nullable String parseStyle(Element element, String property) {
final String style = element.getAttributeNS(null, SVGConstants.SVG_STYLE_ATTRIBUTE);
if (null != style) {
for (String field : style.split(";")) {
if (field.startsWith(property + ":")) {
return field.substring(field.indexOf(":") + 1);
}
}
}
return null;
return Arrays.stream(style.split(";"))
.filter(field -> field.startsWith(property + ':'))
.findFirst()
.map(field -> field.substring(field.indexOf(':') + 1)).orElse(null);
}

/**
Expand Down Expand Up @@ -807,14 +804,15 @@ public static Rectangle2D getRectBBox(SVGRectElement rect) {
* @param bbox The bounding box for the image. The image will be scaled to fit.
* @param center Whether to center the image vertically and horizontally.
*/
public void embedImage(File imageFile, Element canvas, Rectangle2D bbox, boolean center) {
if (null == imageFile) {
public void embedImage(@Nullable File imageFile, Element canvas, Rectangle2D bbox, boolean center) {
if (imageFile == null) {
return;
}
try {
InputStream is = new BufferedInputStream(new FileInputStream(imageFile));
String mimeType = URLConnection.guessContentTypeFromStream(is);
String format = mimeType.substring(mimeType.indexOf("/") + 1);

try (InputStream fis = new FileInputStream(imageFile);
InputStream bis = new BufferedInputStream(fis)) {
String mimeType = URLConnection.guessContentTypeFromStream(bis);
String format = mimeType.substring(mimeType.indexOf('/') + 1);

RenderedImage fluffImage = ImageIO.read(imageFile);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Expand Down
61 changes: 31 additions & 30 deletions megameklab/src/megameklab/ui/fighterAero/ASBuildView.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,15 @@ private void loadEquipmentTable() {
}

private boolean isEngineHeatSink(Mounted mount) {
if ((mount.getLocation() == Entity.LOC_NONE) &&
UnitUtil.isHeatSink(mount) && (engineHeatSinkCount > 0)) {
if(mount.getType().hasFlag(MiscType.F_COMPACT_HEAT_SINK) &&
mount.getType().hasFlag(MiscType.F_DOUBLE_HEAT_SINK)) {
//only single compact HS should be used for engine sinks
return false;
}
if ((mount.getLocation() == Entity.LOC_NONE)
&& UnitUtil.isHeatSink(mount) && (engineHeatSinkCount > 0)
&& !(mount.getType().hasFlag(MiscType.F_COMPACT_HEAT_SINK)
&& mount.getType().hasFlag(MiscType.F_DOUBLE_HEAT_SINK))) {
engineHeatSinkCount--;
return engineHeatSinkCount >= 0;
return true;
} else {
return false;
}

return false;
}

public void refresh() {
Expand All @@ -217,9 +214,11 @@ public void refresh() {
}

private void removeAllListeners() {

}

private void addAllListeners() {

}

@Override
Expand Down Expand Up @@ -269,35 +268,40 @@ public void mousePressed(MouseEvent e) {

String[] locNames = getAero().getLocationNames();
// A list of the valid locations we can add the selected eq to
ArrayList<Integer> validLocs = new ArrayList<Integer>();
ArrayList<Integer> validLocs = new ArrayList<>();
// The number of possible locations, Aeros' have LOC_WINGS and LOC_FUSELAGE, which we
// want ot ignore for now, hence -2
int numLocs = getAero().locations() - 2;
// If it's a weapon, there are restrictions
if (eq.getType() instanceof WeaponType) {
int[] availSpace = TestAero.availableSpace(getAero());
int numWeapons[] = new int[availSpace.length];

for (Mounted m : getAero().getWeaponList()){
if (m.getLocation() != Aero.LOC_NONE){
numWeapons[m.getLocation()]++;
int[] availSpace = TestAero.availableSpace(getAero());

if (availSpace != null) {
int[] numWeapons = new int[availSpace.length];

for (Mounted m : getAero().getWeaponList()) {
if (m.getLocation() != Aero.LOC_NONE) {
numWeapons[m.getLocation()]++;
}
}
}
for (int loc = 0; loc < numLocs; loc++){
if ((numWeapons[loc]+1) < availSpace[loc]){
validLocs.add(loc);

for (int loc = 0; loc < numLocs; loc++) {
if ((numWeapons[loc] + 1) < availSpace[loc]) {
validLocs.add(loc);
}
}
}
// If it's not a weapon there are no space requirements
} else {
for (int loc = 0; loc < numLocs; loc++){
validLocs.add(loc);
for (int loc = 0; loc < numLocs; loc++) {
validLocs.add(loc);
}

if (!UnitUtil.isWeaponEnhancement(eq.getType())) {
validLocs.add(Aero.LOC_FUSELAGE);
}
}

// Add a menu item for each potential location
for (Integer location: validLocs) {
if (UnitUtil.isValidLocation(getAero(), eq.getType(), location)) {
Expand All @@ -321,7 +325,6 @@ public void actionPerformed(ActionEvent e) {
public void mouseReleased(MouseEvent e) {

}


/**
* When the user right-clicks on the equipment table, a context menu is
Expand All @@ -332,11 +335,9 @@ public void mouseReleased(MouseEvent e) {
* @param location
* @param selectedRow
*/
private void jMenuLoadComponent_actionPerformed(int location,
int selectedRow) {
Mounted eq = (Mounted)
equipmentTable.getModel().getValueAt(selectedRow,
CriticalTableModel.EQUIPMENT);
private void jMenuLoadComponent_actionPerformed(int location, int selectedRow) {
Mounted eq = (Mounted) equipmentTable.getModel().getValueAt(selectedRow,
CriticalTableModel.EQUIPMENT);
try {
getAero().addEquipment(eq, location, false);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021 - The MegaMek Team. All Rights Reserved.
* Copyright (c) 2008-2022 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
Expand Down Expand Up @@ -29,7 +29,6 @@
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -161,7 +160,7 @@ private void removeHeatSinks() {

private void removeSelectedEquipment(ActionEvent e) {
int[] selectedRows = loadoutTable.getSelectedRows();
for (Integer row : selectedRows) {
for (int row : selectedRows) {
loadoutModel.removeMounted(row);
}
loadoutModel.removeCrits(selectedRows);
Expand Down Expand Up @@ -203,5 +202,4 @@ private void refreshOtherTabs() {
refresh.refreshSummary();
}
}

}
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/ui/mek/BMBuildView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022 - The MegaMek Team. All Rights Reserved.
* Copyright (c) 2008-2022 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMekLab.
*
Expand Down Expand Up @@ -115,7 +115,7 @@ private boolean isEngineHeatSink(Mounted mount) {
&& mount.getType().hasFlag(MiscType.F_DOUBLE_HEAT_SINK))
&& !mount.getType().hasFlag(MiscType.F_IS_DOUBLE_HEAT_SINK_PROTOTYPE)) {
engineHeatSinkCount--;
return engineHeatSinkCount >= 0;
return true;
} else {
return false;
}
Expand Down
Loading