Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed May 31, 2024
1 parent 458baf8 commit 66b4a50
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.MechView;
import megamek.common.MechView.ViewFormatting;
import megamek.common.ViewFormatting;
import megamek.common.annotations.Nullable;

import javax.swing.*;
Expand Down Expand Up @@ -63,8 +63,8 @@ public ConfigurableMechViewPanel(@Nullable Entity entity) {
fontChooser.addActionListener(ev -> updateFont());
fontChooser.setSelectedItem(GUIPreferences.getInstance().getSummaryFont());

copyHtmlButton.addActionListener(ev -> copyToClipboard(ViewFormatting.Html));
copyTextButton.addActionListener(ev -> copyToClipboard(ViewFormatting.None));
copyHtmlButton.addActionListener(ev -> copyToClipboard(ViewFormatting.HTML));
copyTextButton.addActionListener(ev -> copyToClipboard(ViewFormatting.NONE));
// todo: create a copyDiscordButton
// The implementer of the Discord export cared only about the MML UI.

Expand Down
6 changes: 2 additions & 4 deletions megamek/src/megamek/client/ui/panes/EntityViewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import megamek.client.ui.swing.alphaStrike.ConfigurableASCardPanel;
import megamek.client.ui.swing.calculationReport.FlexibleCalculationReport;
import megamek.common.Entity;
import megamek.common.GunEmplacement;
import megamek.common.MechView;
import megamek.common.MechView.ViewFormatting;
import megamek.common.ViewFormatting;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.AlphaStrikeElement;
import megamek.common.alphaStrike.conversion.ASConverter;
Expand Down Expand Up @@ -88,7 +86,7 @@ public void updateDisplayedEntity(final @Nullable Entity entity, @Nullable ASCar
if (entity == null) {
troPanel.reset();
} else {
troPanel.setMech(entity, TROView.createView(entity, ViewFormatting.Html));
troPanel.setMech(entity, TROView.createView(entity, ViewFormatting.HTML));
}
summaryPanel.setEntity(entity);
cardPanel.setASElement(ASConverter.canConvert(entity) ? asUnit : null);
Expand Down
12 changes: 5 additions & 7 deletions megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import megamek.client.ui.swing.calculationReport.CalculationReport;
import megamek.client.ui.swing.calculationReport.DummyCalculationReport;
import megamek.codeUtilities.StringUtility;
import megamek.common.MechView.ViewFormatting;
import megamek.common.MovePath.MoveStepType;
import megamek.common.actions.*;
import megamek.common.annotations.Nullable;
Expand All @@ -38,8 +37,7 @@
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.planetaryconditions.Wind;
import megamek.common.preference.PreferenceManager;
import megamek.common.util.DiscordExportUtil;
import megamek.common.util.DiscordExportUtil.DiscordFormat;
import megamek.common.util.DiscordFormat;
import megamek.common.weapons.*;
import megamek.common.weapons.bayweapons.AR10BayWeapon;
import megamek.common.weapons.bayweapons.BayWeapon;
Expand Down Expand Up @@ -8900,7 +8898,7 @@ public void resetTransporter() {

@Override
public String getUnusedString() {
return getUnusedString(ViewFormatting.None);
return getUnusedString(ViewFormatting.NONE);
}

@Override
Expand Down Expand Up @@ -8945,11 +8943,11 @@ public String getUnusedString(ViewFormatting formatting) {
if ((next instanceof DockingCollar) && ((DockingCollar) next).isDamaged()) {
continue;
}
if (formatting == ViewFormatting.Html && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
if (formatting == ViewFormatting.HTML && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
result.append("<font color='red'>")
.append(next.getUnusedString())
.append("</font>");
} else if (formatting == ViewFormatting.Discord && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
} else if (formatting == ViewFormatting.DISCORD && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
result.append(DiscordFormat.RED.format())
.append(next.getUnusedString())
.append(DiscordFormat.RESET.format());
Expand All @@ -8966,7 +8964,7 @@ public String getUnusedString(ViewFormatting formatting) {
}
// Add a newline character between strings.
if (iter.hasMoreElements()) {
if (formatting == ViewFormatting.Html) {
if (formatting == ViewFormatting.HTML) {
result.append("<br>");
} else {
result.append("\n");
Expand Down
55 changes: 25 additions & 30 deletions megamek/src/megamek/common/MechView.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import megamek.common.eras.Era;
import megamek.common.eras.Eras;
import megamek.common.options.*;
import megamek.common.util.DiscordExportUtil.DiscordFormat;
import megamek.common.util.DiscordFormat;
import megamek.common.verifier.*;
import megamek.common.weapons.bayweapons.BayWeapon;
import megamek.common.weapons.infantry.InfantryWeapon;
Expand All @@ -45,11 +45,6 @@
* @since January 20, 2003
*/
public class MechView {
public static enum ViewFormatting {
Html,
None,
Discord
}

/**
* Provides common interface for various ways to present data that can be formatted
Expand Down Expand Up @@ -101,7 +96,7 @@ interface ViewElement {
* @param showDetail If true, shows individual weapons that make up weapon bays.
*/
public MechView(Entity entity, boolean showDetail) {
this(entity, showDetail, false, ViewFormatting.Html);
this(entity, showDetail, false, ViewFormatting.HTML);
}

/**
Expand All @@ -114,7 +109,7 @@ public MechView(Entity entity, boolean showDetail) {
* equipment-only cost for conventional infantry for MekHQ.
*/
public MechView(Entity entity, boolean showDetail, boolean useAlternateCost) {
this(entity, showDetail, useAlternateCost, ViewFormatting.Html);
this(entity, showDetail, useAlternateCost, ViewFormatting.HTML);
}

/**
Expand Down Expand Up @@ -585,13 +580,13 @@ private String eraText(int startYear, int endYear) {
private String getReadout(List<ViewElement> section) {
Function<ViewElement,String> mapper;
switch (formatting) {
case Html:
case HTML:
mapper = ViewElement::toHTML;
break;
case None:
case NONE:
mapper = ViewElement::toPlainText;
break;
case Discord:
case DISCORD:
mapper = ViewElement::toDiscord;
break;
default:
Expand Down Expand Up @@ -639,7 +634,7 @@ public String getMechReadoutLoadout() {
* @return The data from the fluff section.
*/
public String getMechReadoutFluff() {
if (formatting == ViewFormatting.Discord) {
if (formatting == ViewFormatting.DISCORD) {
return "";
}
return getReadout(sFluff);
Expand All @@ -661,12 +656,12 @@ public String getMechReadout(@Nullable String fontName) {
String preStart = "";
String preEnd = "";

if (formatting == ViewFormatting.Html && (fontName != null)) {
if (formatting == ViewFormatting.HTML && (fontName != null)) {
docStart = "<div style=\"font-family:" + fontName + ";\">";
docEnd = "</div>";
preStart = "<PRE style=\"font-family:" + fontName + ";\">";
preEnd = "</PRE>";
} else if (formatting == ViewFormatting.Discord) {
} else if (formatting == ViewFormatting.DISCORD) {
docStart = "```ansi\n";
docEnd = "```";
}
Expand Down Expand Up @@ -1118,7 +1113,7 @@ private List<ViewElement> getMisc() {
TableElement transportTable = new TableElement(1);
transportTable.setColNames(Messages.getString("MechView.CarryingCapacity"));
transportTable.setJustification(TableElement.JUSTIFIED_LEFT);
String separator = formatting == ViewFormatting.Html ? "<br>" : "\n";
String separator = formatting == ViewFormatting.HTML ? "<br>" : "\n";
String[] transportersLines = transportersString.split(separator);
for (String line : transportersLines) {
transportTable.addRow(line);
Expand Down Expand Up @@ -1179,19 +1174,19 @@ private static String renderArmor(int nArmor, int origArmor, ViewFormatting form
String cautionEnd;

switch (formatting) {
case Html:
case HTML:
warnBegin = "<FONT " + UIUtil.colorString(GUIPreferences.getInstance().getWarningColor()) + '>';
warnEnd = "</FONT>";
cautionBegin = "<FONT " + UIUtil.colorString(GUIPreferences.getInstance().getCautionColor()) + '>';
cautionEnd = "</FONT>";
break;
case None:
case NONE:
warnBegin = "";
warnEnd = "";
cautionBegin = "";
cautionEnd = "";
break;
case Discord:
case DISCORD:
warnBegin = DiscordFormat.RED.format();
warnEnd = DiscordFormat.RESET.format();
cautionBegin = DiscordFormat.YELLOW.format();
Expand Down Expand Up @@ -1634,11 +1629,11 @@ public String toDiscord() {
*/
private String warningStart() {
switch (formatting) {
case Html:
case HTML:
return "<font color=\"red\">";
case None:
case NONE:
return "*";
case Discord:
case DISCORD:
return DiscordFormat.RED.format();
default:
throw new IllegalStateException("Impossible");
Expand All @@ -1651,11 +1646,11 @@ private String warningStart() {
*/
private String warningEnd() {
switch (formatting) {
case Html:
case HTML:
return "</font>";
case None:
case NONE:
return "*";
case Discord:
case DISCORD:
return DiscordFormat.RESET.format();
default:
throw new IllegalStateException("Impossible");
Expand All @@ -1669,11 +1664,11 @@ private String warningEnd() {
*/
private String italicsStart() {
switch (formatting) {
case Html:
case HTML:
return "<i>";
case None:
case NONE:
return "";
case Discord:
case DISCORD:
return DiscordFormat.UNDERLINE.format();
default:
throw new IllegalStateException("Impossible");
Expand All @@ -1686,11 +1681,11 @@ private String italicsStart() {
*/
private String italicsEnd() {
switch (formatting) {
case Html:
case HTML:
return "</i>";
case None:
case NONE:
return "";
case Discord:
case DISCORD:
return DiscordFormat.RESET.format();
default:
throw new IllegalStateException("Impossible");
Expand Down
24 changes: 24 additions & 0 deletions megamek/src/megamek/common/ViewFormatting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek 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.
*
* MegaMek 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 MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/

package megamek.common;

public enum ViewFormatting {
HTML, NONE, DISCORD
}
6 changes: 3 additions & 3 deletions megamek/src/megamek/common/templates/TROView.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TROView {
protected TROView() {
}

public static TROView createView(Entity entity, MechView.ViewFormatting formatting) {
public static TROView createView(Entity entity, ViewFormatting formatting) {
TROView view;
if (entity.hasETypeFlag(Entity.ETYPE_MECH)) {
view = new MechTROView((Mech) entity);
Expand All @@ -77,10 +77,10 @@ public static TROView createView(Entity entity, MechView.ViewFormatting formatti
} else {
view = new TROView();
}
if (null != view.getTemplateFileName(formatting == MechView.ViewFormatting.Html)) {
if (null != view.getTemplateFileName(formatting == ViewFormatting.HTML)) {
try {
view.template = TemplateConfiguration.getInstance()
.getTemplate("tro/" + view.getTemplateFileName(formatting == MechView.ViewFormatting.Html));
.getTemplate("tro/" + view.getTemplateFileName(formatting == ViewFormatting.HTML));
} catch (final IOException e) {
LogManager.getLogger().error("", e);
}
Expand Down
32 changes: 0 additions & 32 deletions megamek/src/megamek/common/util/DiscordExportUtil.java

This file was deleted.

37 changes: 37 additions & 0 deletions megamek/src/megamek/common/util/DiscordFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
* MegaMek 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.
*
* MegaMek 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 MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.common.util;

public enum DiscordFormat {
GRAY(30), RED(31), GREEN(32), YELLOW(33), BLUE(34), PINK(35), CYAN(36), WHITE(37),

BOLD(1), UNDERLINE(4),

RESET(0);

private final int code;

DiscordFormat(int code) {
this.code = code;
}

public String format() {
return "\u001b[" + code + 'm';
}
}

0 comments on commit 66b4a50

Please sign in to comment.