8000 Fixes for issue #7004 - Purchase Unit Dialog Button Behavior by savanik · Pull Request #7005 · MegaMek/mekhq · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixes for issue #7004 - Purchase Unit Dialog Button Behavior #7005

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

Merged
merged 1 commit into from
May 16, 2025
Merged
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
109 changes: 62 additions & 47 deletions MekHQ/src/mekhq/gui/dialog/MekHQUnitSelectorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,40 @@
*/
package mekhq.gui.dialog;

import static mekhq.utilities.EntityUtilities.isUnsupportedEntity;
import static mekhq.utilities.ReportingUtilities.CLOSING_SPAN_TAG;
import static mekhq.utilities.ReportingUtilities.spanOpeningWithCustomColor;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.PatternSyntaxException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.RowFilter;

import megamek.client.ui.Messages;
import megamek.client.ui.advancedsearch.MekSearchFilter;
import megamek.client.ui.swing.UnitLoadingDialog;
import megamek.client.ui.swing.dialog.AbstractUnitSelectorDialog;
import megamek.common.*;
import megamek.common.Entity;
import megamek.common.EntityWeightClass;
import megamek.common.ITechnology;
import megamek.common.MekSummary;
import megamek.common.TargetRoll;
import megamek.common.TechConstants;
import megamek.common.UnitType;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
import mekhq.campaign.parts.enums.PartQuality;
import mekhq.campaign.unit.UnitOrder;
import mekhq.campaign.unit.UnitTechProgression;
import mekhq.utilities.MHQInternationalization;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.PatternSyntaxException;

import static mekhq.utilities.EntityUtilities.isUnsupportedEntity;
import static mekhq.utilities.ReportingUtilities.CLOSING_SPAN_TAG;
import static mekhq.utilities.ReportingUtilities.spanOpeningWithCustomColor;

public class MekHQUnitSelectorDialog extends AbstractUnitSelectorDialog {
//region Variable Declarations
private Campaign campaign;
Expand All @@ -59,8 +70,8 @@ public class MekHQUnitSelectorDialog extends AbstractUnitSelectorDialog {
private static final String TARGET_UNKNOWN = "--";
//endregion Variable Declarations

public MekHQUnitSelectorDialog(JFrame frame, UnitLoadingDialog unitLoadingDialog,
Campaign campaign, boolean addToCampaign) {
public MekHQUnitSelectorDialog(JFrame frame, UnitLoadingDialog unitLoadingDialog, Campaign campaign,
boolean addToCampaign) {
super(frame, unitLoadingDialog);
this.campaign = campaign;
this.addToCampaign = addToCampaign;
Expand All @@ -79,7 +90,8 @@ public void updateOptionValues() {
gameTechLevel = campaign.getCampaignOptions().getTechLevel();
eraBasedTechLevel = campaign.getCampaignOptions().isVariableTechLevel();

if (campaign.getCampaignOptions().isAllowClanPurchases() && campaign.getCampaignOptions().isAllowISPurchases()) {
if (campaign.getCampaignOptions().isAllowClanPurchases() &&
campaign.getCampaignOptions().isAllowISPurchases()) {
techLevelDisplayType = TECH_LEVEL_DISPLAY_IS_CLAN;
} else if (campaign.getCampaignOptions().isAllowClanPurchases()) {
techLevelDisplayType = TECH_LEVEL_DISPLAY_CLAN;
Expand All @@ -98,13 +110,15 @@ protected JPanel createButtonsPanel() {
buttonSelect = new JButton(Messages.getString("MekSelectorDialog.Buy", TARGET_UNKNOWN));
buttonSelect.setName("buttonBuy");
buttonSelect.addActionListener(this);
buttonSelect.setEnabled(false);
panelButtons.add(buttonSelect, new GridBagConstraints());

if (campaign.isGM()) {
// This is used as a GM add, the name is because of how it is used in MegaMek and MegaMekLab
buttonSelectClose = new JButton(Messages.getString("MekSelectorDialog.AddGM"));
buttonSelectClose.setName("buttonAddGM");
buttonSelectClose.addActionListener(this);
buttonSelectClose.setEnabled(false);
panelButtons.add(buttonSelectClose, new GridBagConstraints());
}

Expand Down Expand Up @@ -146,24 +160,23 @@ protected void select(boolean isGM) {

if (entity == null || isUnsupportedEntity(entity)) {
final ResourceBundle resources = ResourceBundle.getBundle("mekhq.resources.CampaignGUI",
MekHQ.getMHQOptions().getLocale());
MekHQ.getMHQOptions().getLocale());

String reason;
if (entity == null) {
reason = MHQInternationalization.getTextAt(resources.getBaseBundleName(),
"mekSelectorDialog.unsupported.null");
"mekSelectorDialog.unsupported.null");
} else if (entity.getUnitType() == UnitType.GUN_EMPLACEMENT) {
reason = MHQInternationalization.getTextAt(resources.getBaseBundleName(),
"mekSelectorDialog.unsupported.gunEmplacement");
"mekSelectorDialog.unsupported.gunEmplacement");
} else {
reason = MHQInternationalization.getTextAt(resources.getBaseBundleName(),
"mekSelectorDialog.unsupported.droneOs");
"mekSelectorDialog.unsupported.droneOs");
}

campaign.addReport(String.format(
reason,
spanOpeningWithCustomColor(MekHQ.getMHQOptions().getFontColorNegativeHexColor()),
CLOSING_SPAN_TAG));
campaign.addReport(String.format(reason,
spanOpeningWithCustomColor(MekHQ.getMHQOptions().getFontColorNegativeHexColor()),
CLOSING_SPAN_TAG));

dispose();
return;
Expand All @@ -185,8 +198,8 @@ protected void select(boolean isGM) {
//endregion Button Methods

/**
* We need to override this to add some MekHQ specific functionality, namely changing button
* names when the selected entity is chosen
* We need to override this to add some MekHQ specific functionality, namely changing button names when the selected
* entity is chosen
*
* @return selectedEntity, or null if there isn't one
*/
Expand All @@ -199,15 +212,16 @@ public Entity getSelectedEntity() {
buttonSelect.setEnabled(false);
buttonSelect.setText(Messages.getString("MekSelectorDialog.Buy", TARGET_UNKNOWN));
buttonSelect.setToolTipText(null);
buttonSelectClose.setEnabled(false);
}
} else {
selectedUnit = new UnitOrder(entity, campaign);
if (addToCampaign) {
buttonSelect.setEnabled(true);
final TargetRoll target = campaign.getTargetForAcquisition(selectedUnit);
buttonSelect.setText(Messages.getString("MekSelectorDialog.Buy",
target.getValueAsString()));
buttonSelect.setText(Messages.getString("MekSelectorDialog.Buy", target.getValueAsString()));
buttonSelect.setToolTipText(target.getDesc());
buttonSelectClose.setEnabled(true);
}
}

Expand Down Expand Up @@ -240,7 +254,7 @@ protected void filterUnits() {
final int nClass = comboWeight.getSelectedIndex();
final int nUnit = comboUnitType.getSelectedIndex() - 1;
final boolean checkSupportVee = Messages.getString("MekSelectorDialog.SupportVee")
.equals(comboUnitType.getSelectedItem());
.equals(comboUnitType.getSelectedItem());
// If the current expression doesn't parse, don't update.
try {
unitTypeFilter = new RowFilter<>() {
Expand All @@ -259,25 +273,26 @@ public boolean include(Entry<? extends MekTableModel, ? extends Integer> entry)
}

if (
/* year limits */
(!enableYearLimits || (mek.getYear() <= allowedYear))
/* Clan/IS limits */
&& (campaign.getCampaignOptions().isAllowClanPurchases() || !TechConstants.isClan(mek.getType()))
&& (campaign.getCampaignOptions().isAllowISPurchases() || TechConstants.isClan(mek.getType()))
/* Canon */
&& (!canonOnly || mek.isCanon())
/* Weight */
&& ((nClass == mek.getWeightClass()) || (nClass == EntityWeightClass.SIZE))
/* Technology Level */
&& ((null != tech) && campaign.isLegal(tech))
&& (techLevelMatch)
/* Support Vehicles */
&& ((nUnit == -1)
|| (!checkSupportVee && mek.getUnitType().equals(UnitType.getTypeName(nUnit)))
|| (checkSupportVee && mek.isSupport()))
/* Advanced Search */
&& ((searchFilter == null) || MekSearchFilter.isMatch(mek, searchFilter))
) {
/* year limits */
(!enableYearLimits || (mek.getYear() <= allowedYear))
/* Clan/IS limits */ &&
(campaign.getCampaignOptions().isAllowClanPurchases() ||
!TechConstants.isClan(mek.getType())) &&
(campaign.getCampaignOptions().isAllowISPurchases() ||
TechConstants.isClan(mek.getType()))
/* Canon */ &&
(!canonOnly || mek.isCanon())
/* Weight */ &&
((nClass == mek.getWeightClass()) || (nClass == EntityWeightClass.SIZE))
/* Technology Level */ &&
((null != tech) && campaign.isLegal(tech)) &&
(techLevelMatch)
/* Support Vehicles */ &&
((nUnit == -1) ||
(!checkSupportVee && mek.getUnitType().equals(UnitType.getTypeName(nUnit))) ||
(checkSupportVee && mek.isSupport()))
/* Advanced Search */ &&
((searchFilter == null) || MekSearchFilter.isMatch(mek, searchFilter))) {
if (!textFilter.getText().isBlank()) {
String text = textFilter.getText();
return mek.getName().toLowerCase().contains(text.toLowerCase());
Expand Down
Loading
0