Search in sources :

Example 1 with IEditableProperty

use of games.strategy.engine.data.properties.IEditableProperty in project triplea by triplea-game.

the class GameParser method parseEditableProperty.

private void parseEditableProperty(final Element property, final String name, final String defaultValue) throws GameParseException {
    // what type
    final List<Node> children = getNonTextNodes(property);
    if (children.size() != 1) {
        throw newGameParseException("Editable properties must have exactly 1 child specifying the type. Number of children found:" + children.size() + " for node:" + property.getNodeName());
    }
    final Element child = (Element) children.get(0);
    final String childName = child.getNodeName();
    final IEditableProperty editableProperty;
    if (childName.equals("boolean")) {
        editableProperty = new BooleanProperty(name, null, Boolean.valueOf(defaultValue));
    } else if (childName.equals("file")) {
        editableProperty = new FileProperty(name, null, defaultValue);
    } else if (childName.equals("list") || childName.equals("combo")) {
        final StringTokenizer tokenizer = new StringTokenizer(child.getAttribute("values"), ",");
        final Collection<String> values = new ArrayList<>();
        while (tokenizer.hasMoreElements()) {
            values.add(tokenizer.nextToken());
        }
        editableProperty = new ComboProperty<>(name, null, defaultValue, values);
    } else if (childName.equals("number")) {
        final int max = Integer.valueOf(child.getAttribute("max"));
        final int min = Integer.valueOf(child.getAttribute("min"));
        final int def = Integer.valueOf(defaultValue);
        editableProperty = new NumberProperty(name, null, max, min, def);
    } else if (childName.equals("color")) {
        // Parse the value as a hexidecimal number
        final int def = Integer.valueOf(defaultValue, 16);
        editableProperty = new ColorProperty(name, null, def);
    } else if (childName.equals("string")) {
        editableProperty = new StringProperty(name, null, defaultValue);
    } else {
        throw newGameParseException("Unrecognized property type:" + childName);
    }
    data.getProperties().addEditableProperty(editableProperty);
}
Also used : BooleanProperty(games.strategy.engine.data.properties.BooleanProperty) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) StringProperty(games.strategy.engine.data.properties.StringProperty) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty) StringTokenizer(java.util.StringTokenizer) FileProperty(games.strategy.engine.data.properties.FileProperty) NumberProperty(games.strategy.engine.data.properties.NumberProperty) ColorProperty(games.strategy.engine.data.properties.ColorProperty)

Example 2 with IEditableProperty

use of games.strategy.engine.data.properties.IEditableProperty in project triplea by triplea-game.

the class ChangeGameOptionsClientAction method actionPerformed.

@Override
public void actionPerformed(final ActionEvent e) {
    final byte[] oldBytes = serverRemote.getGameOptions();
    if (oldBytes == null || oldBytes.length == 0) {
        return;
    }
    try {
        final List<IEditableProperty> properties = GameProperties.readEditableProperties(oldBytes);
        final PropertiesUi pui = new PropertiesUi(properties, true);
        final JScrollPane scroll = new JScrollPane(pui);
        scroll.setBorder(null);
        scroll.getViewport().setBorder(null);
        final JOptionPane pane = new JOptionPane(scroll, JOptionPane.PLAIN_MESSAGE);
        final String ok = "OK";
        final String cancel = "Cancel";
        pane.setOptions(new Object[] { ok, cancel });
        final JDialog window = pane.createDialog(JOptionPane.getFrameForComponent(parent), "Map Options");
        window.setVisible(true);
        final Object buttonPressed = pane.getValue();
        if (buttonPressed != null && !buttonPressed.equals(cancel)) {
            // but it doesn't change the hosts, so we need to send it back to the host.
            try {
                serverRemote.changeToGameOptions(GameProperties.writeEditableProperties(properties));
            } catch (final IOException ex) {
                ClientLogger.logQuietly("Failed to write game properties", ex);
            }
        }
    } catch (final IOException | ClassCastException ex) {
        ClientLogger.logQuietly("Failed to read game properties", ex);
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) IOException(java.io.IOException) PropertiesUi(games.strategy.engine.data.properties.PropertiesUi) JOptionPane(javax.swing.JOptionPane) JDialog(javax.swing.JDialog) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty)

Example 3 with IEditableProperty

use of games.strategy.engine.data.properties.IEditableProperty in project triplea by triplea-game.

the class FileBackedGamePropertiesCache method cacheGameProperties.

/**
 * Caches the gameOptions stored in the game data, and associates with this game. only values that are serializable
 * (which they should all be) will be stored
 *
 * @param gameData
 *        the game which options you want to cache
 */
@Override
public void cacheGameProperties(final GameData gameData) {
    final Map<String, Object> serializableMap = new HashMap<>();
    for (final IEditableProperty property : gameData.getProperties().getEditableProperties()) {
        if (property.getValue() instanceof Serializable) {
            serializableMap.put(property.getName(), property.getValue());
        }
    }
    final File cache = getCacheFile(gameData);
    try {
        // create the directory if it doesn't already exists
        if (!cache.getParentFile().exists()) {
            cache.getParentFile().mkdirs();
        }
        try (OutputStream os = new FileOutputStream(cache);
            ObjectOutputStream out = new ObjectOutputStream(os)) {
            out.writeObject(serializableMap);
        }
    } catch (final IOException e) {
        ClientLogger.logQuietly("Failed to write game properties to cache: " + cache.getAbsolutePath(), e);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty)

Example 4 with IEditableProperty

use of games.strategy.engine.data.properties.IEditableProperty in project triplea by triplea-game.

the class GameSelectorPanel method selectGameOptions.

private void selectGameOptions() {
    // backup current game properties before showing dialog
    final Map<String, Object> currentPropertiesMap = new HashMap<>();
    for (final IEditableProperty property : model.getGameData().getProperties().getEditableProperties()) {
        currentPropertiesMap.put(property.getName(), property.getValue());
    }
    final PropertiesUi panel = new PropertiesUi(model.getGameData().getProperties(), true);
    final JScrollPane scroll = new JScrollPane(panel);
    scroll.setBorder(null);
    scroll.getViewport().setBorder(null);
    final JOptionPane pane = new JOptionPane(scroll, JOptionPane.PLAIN_MESSAGE);
    final String ok = "OK";
    final String cancel = "Cancel";
    final String makeDefault = "Make Default";
    final String reset = "Reset";
    pane.setOptions(new Object[] { ok, makeDefault, reset, cancel });
    final JDialog window = pane.createDialog(JOptionPane.getFrameForComponent(this), "Map Options");
    window.setVisible(true);
    final Object buttonPressed = pane.getValue();
    if (buttonPressed == null || buttonPressed.equals(cancel)) {
        // restore properties, if cancel was pressed, or window was closed
        for (final IEditableProperty property : model.getGameData().getProperties().getEditableProperties()) {
            property.setValue(currentPropertiesMap.get(property.getName()));
        }
    } else if (buttonPressed.equals(reset)) {
        if (!originalPropertiesMap.isEmpty()) {
            // restore properties, if cancel was pressed, or window was closed
            for (final IEditableProperty property : model.getGameData().getProperties().getEditableProperties()) {
                property.setValue(originalPropertiesMap.get(property.getName()));
            }
            selectGameOptions();
        }
    } else if (buttonPressed.equals(makeDefault)) {
        gamePropertiesCache.cacheGameProperties(model.getGameData());
    } else {
    // ok was clicked, and we have modified the properties already
    }
}
Also used : JScrollPane(javax.swing.JScrollPane) HashMap(java.util.HashMap) PropertiesUi(games.strategy.engine.data.properties.PropertiesUi) JOptionPane(javax.swing.JOptionPane) JDialog(javax.swing.JDialog) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty)

Example 5 with IEditableProperty

use of games.strategy.engine.data.properties.IEditableProperty in project triplea by triplea-game.

the class ViewMenu method addMapFontAndColorEditorMenu.

private void addMapFontAndColorEditorMenu() {
    final Action mapFontOptions = SwingAction.of("Edit Map Font and Color", e -> {
        final List<IEditableProperty> properties = new ArrayList<>();
        final NumberProperty fontsize = new NumberProperty("Font Size", null, 60, 0, MapImage.getPropertyMapFont().getSize());
        final ColorProperty territoryNameColor = new ColorProperty("Territory Name and PU Color", null, MapImage.getPropertyTerritoryNameAndPuAndCommentColor());
        final ColorProperty unitCountColor = new ColorProperty("Unit Count Color", null, MapImage.getPropertyUnitCountColor());
        final ColorProperty factoryDamageColor = new ColorProperty("Factory Damage Color", null, MapImage.getPropertyUnitFactoryDamageColor());
        final ColorProperty hitDamageColor = new ColorProperty("Hit Damage Color", null, MapImage.getPropertyUnitHitDamageColor());
        properties.add(fontsize);
        properties.add(territoryNameColor);
        properties.add(unitCountColor);
        properties.add(factoryDamageColor);
        properties.add(hitDamageColor);
        final PropertiesUi pui = new PropertiesUi(properties, true);
        final JPanel ui = new JPanel();
        ui.setLayout(new BorderLayout());
        ui.add(pui, BorderLayout.CENTER);
        ui.add(new JLabel("<html>Change the font and color of 'text' (not pictures) on the map. " + "<br /><em>(Some people encounter problems with the color picker, and this " + "<br />is a bug outside of triplea, located in the 'look and feel' that " + "<br />you are using. If you have an error come up, try switching to the " + "<br />basic 'look and feel', then setting the color, then switching back.)</em></html>"), BorderLayout.NORTH);
        final Object[] options = { "Set Properties", "Reset To Default", "Cancel" };
        final int result = JOptionPane.showOptionDialog(frame, ui, "Edit Map Font and Color", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, 2);
        if (result == 1) {
            MapImage.resetPropertyMapFont();
            MapImage.resetPropertyTerritoryNameAndPuAndCommentColor();
            MapImage.resetPropertyUnitCountColor();
            MapImage.resetPropertyUnitFactoryDamageColor();
            MapImage.resetPropertyUnitHitDamageColor();
            frame.getMapPanel().resetMap();
        } else if (result == 0) {
            MapImage.setPropertyMapFont(new Font("Ariel", Font.BOLD, fontsize.getValue()));
            MapImage.setPropertyTerritoryNameAndPuAndCommentColor((Color) territoryNameColor.getValue());
            MapImage.setPropertyUnitCountColor((Color) unitCountColor.getValue());
            MapImage.setPropertyUnitFactoryDamageColor((Color) factoryDamageColor.getValue());
            MapImage.setPropertyUnitHitDamageColor((Color) hitDamageColor.getValue());
            frame.getMapPanel().resetMap();
        }
    });
    add(mapFontOptions).setMnemonic(KeyEvent.VK_C);
}
Also used : JPanel(javax.swing.JPanel) AbstractAction(javax.swing.AbstractAction) SwingAction(games.strategy.ui.SwingAction) Action(javax.swing.Action) Color(java.awt.Color) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) PropertiesUi(games.strategy.engine.data.properties.PropertiesUi) Font(java.awt.Font) IEditableProperty(games.strategy.engine.data.properties.IEditableProperty) BorderLayout(java.awt.BorderLayout) NumberProperty(games.strategy.engine.data.properties.NumberProperty) ColorProperty(games.strategy.engine.data.properties.ColorProperty)

Aggregations

IEditableProperty (games.strategy.engine.data.properties.IEditableProperty)7 PropertiesUi (games.strategy.engine.data.properties.PropertiesUi)3 ColorProperty (games.strategy.engine.data.properties.ColorProperty)2 GameProperties (games.strategy.engine.data.properties.GameProperties)2 NumberProperty (games.strategy.engine.data.properties.NumberProperty)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JDialog (javax.swing.JDialog)2 JLabel (javax.swing.JLabel)2 JOptionPane (javax.swing.JOptionPane)2 JPanel (javax.swing.JPanel)2 JScrollPane (javax.swing.JScrollPane)2 GameData (games.strategy.engine.data.GameData)1 BooleanProperty (games.strategy.engine.data.properties.BooleanProperty)1 FileProperty (games.strategy.engine.data.properties.FileProperty)1 StringProperty (games.strategy.engine.data.properties.StringProperty)1 SwingAction (games.strategy.ui.SwingAction)1 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1