Fixes activation of improvement actions after reconnecting. Obsolete improvement actions are now removed when loading savegames etc.

This commit is contained in:
Stian Grenborgen 2023-04-23 09:26:40 +02:00
parent f250fd68ce
commit ba940440f8
6 changed files with 49 additions and 11 deletions

View File

@ -699,6 +699,10 @@ public final class FreeColClient {
public void addSpecificationActions(Specification specification) {
SwingUtilities.invokeLater(() -> {
actionManager.addSpecificationActions(specification);
// XXX: The actions are loaded asynchronously without a callback.
SwingUtilities.invokeLater(() -> {
getGUI().resetMapControls();
});
});
}

View File

@ -259,8 +259,8 @@ public final class Canvas extends JDesktopPane {
logger.info("Canvas resize from " + oldSize + " to " + size);
oldSize = size;
canvasMapViewer.changeSize(size);
final boolean add = removeMapControls();
if (add) {
if (removeMapControls()) {
freeColClient.getGUI().updateMapControls();
addMapControls();
}
updateFrameSizesAndPositions(size);

View File

@ -1429,6 +1429,18 @@ public class GUI extends FreeColClientHolder {
*/
public void closeMenus() {}
/**
* Resets the map controls in order to properly reference any
* newly recreated action.
*/
public void resetMapControls() {}
/**
* Resets the menu bar in order to properly reference any
* newly recreated action.
*/
public void resetMenuBar() {}
/**
* Update the menu bar.
*

View File

@ -741,8 +741,6 @@ public class SwingGUI extends GUI {
public void reconnectGUI(Unit active, Tile tile) {
this.canvas.requestFocusInWindow();
this.canvas.initializeInGame();
enableMapControls(getClientOptions()
.getBoolean(ClientOptions.DISPLAY_MAP_CONTROLS));
closeMenus();
clearGotoPath();
this.canvas.resetMenuBar();
@ -763,6 +761,10 @@ public class SwingGUI extends GUI {
changeView((Unit)null, false);
}
this.mapViewer.getMapViewerBounds().setFocus(tile);
enableMapControls(false);
enableMapControls(getClientOptions().getBoolean(ClientOptions.DISPLAY_MAP_CONTROLS));
refresh();
}
@ -975,11 +977,11 @@ public class SwingGUI extends GUI {
public void enableMapControls(boolean enable) {
if (this.mapControls == null) return;
if (enable) {
updateMapControls();
this.canvas.addMapControls();
} else {
this.canvas.removeMapControls();
}
updateMapControls();
}
/**
@ -1041,6 +1043,14 @@ public class SwingGUI extends GUI {
this.canvas.closeMenus();
}
/**
* {@inheritDoc}
*/
@Override
public void resetMenuBar() {
this.canvas.resetMenuBar();
}
/**
* {@inheritDoc}
*/
@ -1787,15 +1797,23 @@ public class SwingGUI extends GUI {
}
if (this.canvas != null) {
this.canvas.resetMenuBar();
if (this.canvas.removeMapControls()) {
this.canvas.addMapControls();
}
resetMapControls();
}
updateMapControls();
refresh();
}
/**
* {@inheritDoc}
*/
public void resetMapControls() {
if (this.canvas.removeMapControls()) {
updateMapControls();
this.canvas.addMapControls();
}
}
private int determineMainFontSizeUsingClientOptions(final int dpi) {
final int DEFAULT_DPI = 72;

View File

@ -185,6 +185,11 @@ public class ActionManager extends OptionGroup {
*/
public void addSpecificationActions(Specification spec) {
// Initialize ImprovementActions
for (Option<?> o : new ArrayList<>(getOptions())) {
if (o instanceof ImprovementAction) {
remove(o.getId());
}
}
for (TileImprovementType type : spec.getTileImprovementTypeList()) {
if (!type.isNatural()) {
add(new ImprovementAction(freeColClient, type));

View File

@ -370,8 +370,7 @@ public abstract class FreeColAction extends AbstractAction
* {@link #shouldBeEnabled}.
*/
public void update() {
boolean b = this.shouldBeEnabled();
if (this.isEnabled() != b) this.setEnabled(b);
this.setEnabled(this.shouldBeEnabled());
}