-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4888 from IllianiCBT/maintenance_partsReport
Added 'Part Quality Report' Dialog
- Loading branch information
Showing
4 changed files
with
198 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
MekHQ/src/mekhq/gui/dialog/reportDialogs/PartQualityReportDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved. | ||
* | ||
* This file is part of MekHQ. | ||
* | ||
* MekHQ 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 Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* MekHQ is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package mekhq.gui.dialog.reportDialogs; | ||
|
||
import mekhq.MekHQ; | ||
import mekhq.campaign.parts.Part; | ||
import mekhq.campaign.unit.Unit; | ||
import mekhq.utilities.ReportingUtilities; | ||
|
||
import javax.swing.*; | ||
import java.util.*; | ||
|
||
import static mekhq.campaign.parts.Part.QUALITY_A; | ||
import static mekhq.campaign.parts.Part.QUALITY_B; | ||
import static mekhq.campaign.parts.Part.QUALITY_E; | ||
import static mekhq.campaign.parts.Part.QUALITY_F; | ||
import static mekhq.utilities.ReportingUtilities.CLOSING_SPAN_TAG; | ||
|
||
/** | ||
* Represents a dialog for generating a part quality report. | ||
* Extends the {@link AbstractReportDialog} class. | ||
*/ | ||
public class PartQualityReportDialog extends AbstractReportDialog { | ||
//region Variable Declarations | ||
private final Unit unit; | ||
//endregion Variable Declarations | ||
|
||
//region Constructors | ||
/** | ||
* Constructs a new instance of {@link PartQualityReportDialog}. | ||
* | ||
* @param frame the parent {@link JFrame} | ||
* @param unit the unit for which the parts quality report is being generated | ||
*/ | ||
public PartQualityReportDialog(final JFrame frame, final Unit unit) { | ||
super(frame, "PartQualityReportDialog", "PartQualityReportDialog.title"); | ||
this.unit = unit; | ||
setTitle(String.format(resources.getString("PartQualityReportDialog.Unit.title"), | ||
unit.getName())); | ||
initialize(); | ||
} | ||
//endregion Constructors | ||
|
||
//region Getters | ||
/** | ||
* @return the unit associated with this object | ||
*/ | ||
public Unit getUnit() { | ||
return unit; | ||
} | ||
|
||
@Override | ||
protected JTextPane createTxtReport() { | ||
final JTextPane txtReport = new JTextPane(); | ||
txtReport.setContentType("text/html"); | ||
txtReport.setText(getPartsReport(getUnit())); | ||
txtReport.setName("txtReport"); | ||
txtReport.setEditable(false); | ||
txtReport.setCaretPosition(0); | ||
return txtReport; | ||
} | ||
//endregion Getters | ||
|
||
/** | ||
* Produces a Part Quality report for the given unit. The report includes each part's location, | ||
* name, and quality. | ||
* | ||
* @param unit The unit to generate a report for. | ||
* @return An HTML string displaying the status of each part in the unit. | ||
*/ | ||
private String getPartsReport(Unit unit) { | ||
// This map will hold part lists, keyed by the location on the unit. | ||
Map<String, List<Part>> reportMap = new HashMap<>(); | ||
|
||
// Iterate over parts, assigning each to its location in the map. | ||
for (Part part : unit.getParts()) { | ||
String location = part.getLocationName() != null ? part.getLocationName() : unit.getName(); | ||
reportMap.computeIfAbsent(location, k -> new ArrayList<>()).add(part); | ||
} | ||
|
||
// Create a sorted list of locations, excluding the unit's name. | ||
List<String> locations = new ArrayList<>(); | ||
for (String location : reportMap.keySet()) { | ||
if (!location.equals(unit.getName())) { | ||
locations.add(location); | ||
} | ||
} | ||
Collections.sort(locations); | ||
|
||
// Add the unit's name to the start of the sorted locations list. | ||
locations.add(0, unit.getName()); | ||
|
||
// Begin the HTML report. | ||
StringBuilder report = new StringBuilder("<html>"); | ||
|
||
// For each location, add reported details about that location's parts. | ||
for (String location : locations) { | ||
report.append("<b>"); | ||
if (location.equals(unit.getName())) { | ||
String colorCode = getColorCode(unit.getQuality()); | ||
|
||
// Add the location and its colored quality rating to the report. | ||
report.append("<span style=\"font-size: 18px;\">") | ||
.append(location) | ||
.append(" - "); | ||
report.append("<span style=\"color: ") | ||
.append(colorCode) | ||
.append(";\">") | ||
.append(unit.getQualityName()) | ||
.append("</span>"); | ||
report.append("</span>"); | ||
} else { | ||
report.append("<span style=\"font-size: 12px;\">").append(location).append("</span>"); | ||
} | ||
report.append("</b><br>"); | ||
|
||
// For each part in the current location, add it to the report. | ||
for (Part part : reportMap.get(location)) { | ||
report.append(part.getName()).append(" - "); | ||
|
||
int qualityLevel = part.getQuality(); | ||
String colorCode = getColorCode(qualityLevel); | ||
|
||
report.append(ReportingUtilities.spanOpeningWithCustomColor(colorCode)) | ||
.append(part.getQualityName()).append(CLOSING_SPAN_TAG).append("<br>"); | ||
} | ||
|
||
// Add a line break between locations. | ||
report.append("<br>"); | ||
} | ||
|
||
// Finish the HTML report. | ||
report.append("</html>"); | ||
|
||
return report.toString(); | ||
} | ||
|
||
/** | ||
* Returns the color code associated with the given quality level. | ||
* | ||
* @param qualityLevel The quality level for which to retrieve the color code. | ||
* @return The color code associated with the quality level. | ||
*/ | ||
private static String getColorCode(int qualityLevel) { | ||
return switch (qualityLevel) { | ||
case QUALITY_A, QUALITY_B -> MekHQ.getMHQOptions().getFontColorNegativeHexColor(); | ||
case QUALITY_E, QUALITY_F -> MekHQ.getMHQOptions().getFontColorPositiveHexColor(); | ||
default -> MekHQ.getMHQOptions().getFontColorWarningHexColor(); | ||
}; | ||
} | ||
} |