Skip to content

Commit

Permalink
resolves #9 - makes the Pokemon used configurable
Browse files Browse the repository at this point in the history
also adds an Easter egg - MissingNo. is returned
used if all Pokemon are turned off
  • Loading branch information
kagof committed Sep 22, 2020
1 parent b972d2a commit 9d7f385
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 20 deletions.
37 changes: 27 additions & 10 deletions src/main/java/com/kagof/intellij/plugins/pokeprogress/Pokemon.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.kagof.intellij.plugins.pokeprogress;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.swing.Icon;

Expand Down Expand Up @@ -63,33 +66,41 @@ public enum Pokemon {
INTELLEON(818, "intelleon", -16, -6, PokemonType.WATER),
WOOLOO(831, "wooloo", -12, -5, PokemonType.NORMAL),
ZACIAN(888, "zacian", -10, -6, PokemonType.FAIRY, PokemonType.STEEL),
ZAMAZENTA(889, "zamazenta", -7, -7, PokemonType.FIGHTING, PokemonType.STEEL);
ZAMAZENTA(889, "zamazenta", -7, -7, PokemonType.FIGHTING, PokemonType.STEEL),

// Secret
MISSINGNO("???", "missingNo.", -16, -7, true, PokemonType.NORMAL);

// For convenience's sake, these can be used when testing positioning & sizing of new sprites
static final boolean DEBUGGING = false;
private static final Pokemon TARGET = null;
static final Pokemon TARGET = null;

private static final String RESOURCE_PATH = "/com/kagof/intellij/plugins/pokeprogress/sprites/";
private static final Random RANDOM = new Random();

public static final Map<String, Pokemon> DEFAULT_POKEMON = Arrays.stream(values()).filter(p -> !p.secret)
.collect(Collectors.toMap(Pokemon::getNumber, Function.identity()));

private final Supplier<Icon> icon;
private final Supplier<Icon> iconR;

private final List<PokemonType> types;

private final String name;

private final int number;
private final String number;

private final int xShift;
private final int yShift;
private final boolean secret;

@SuppressWarnings("ConstantConditions")
public static Pokemon randomPokemon() {
return TARGET == null ? Pokemon.values()[RANDOM.nextInt(Pokemon.values().length)] : TARGET;
public static Pokemon getByNumber(String number) {
return DEFAULT_POKEMON.get(number);
}

Pokemon(final int number, final String name, final int xShift, final int yShift, final PokemonType... types) {
this("" + number, name, xShift, yShift, false, types);
}

Pokemon(final String number, final String name, final int xShift, final int yShift, final boolean secret, final PokemonType... types) {
if (types == null || types.length < 1) {
throw new IllegalArgumentException("configuration for " + name + " invalid");
}
Expand All @@ -102,6 +113,8 @@ public static Pokemon randomPokemon() {

this.icon = () -> IconLoader.getIcon(RESOURCE_PATH + name + ".gif");
this.iconR = () -> IconLoader.getIcon(RESOURCE_PATH + name + "_r.gif");

this.secret = secret;
}

public List<PokemonType> getTypes() {
Expand All @@ -128,10 +141,14 @@ public String getName() {
return name;
}

public int getNumber() {
public String getNumber() {
return number;
}

public boolean isSecret() {
return secret;
}

public String getNameWithNumber() {
return StringUtil.capitalizeWords(name, true) + " (#" + number + ")";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kagof.intellij.plugins.pokeprogress;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

import com.kagof.intellij.plugins.pokeprogress.configuration.PokemonProgressState;

public class PokemonPicker {
private static final Random RANDOM = new Random();

@SuppressWarnings("ConstantConditions")
public static Pokemon get() {
if (Pokemon.TARGET != null) {
return Pokemon.TARGET;
}

final List<String> enabledPokemonNumbers = Optional.ofNullable(PokemonProgressState.getInstance())
.map(PokemonPicker::getEnabledPokemonNumbers)
.orElse(null);
if (enabledPokemonNumbers == null || enabledPokemonNumbers.isEmpty()) {
return Pokemon.MISSINGNO;
}
return Pokemon.getByNumber(enabledPokemonNumbers.get(RANDOM.nextInt(enabledPokemonNumbers.size())));
}

private static List<String> getEnabledPokemonNumbers(final PokemonProgressState state) {
return state.pokemonNumbersEnabled.entrySet().stream().filter(Map.Entry::getValue).map(Map.Entry::getKey).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PokemonProgressBarUi(final Pokemon pokemon) {
@SuppressWarnings({"MethodOverridesStaticMethodOfSuperclass", "UnusedDeclaration"})
public static ComponentUI createUI(JComponent c) {
c.setBorder(JBUI.Borders.empty().asUIResource());
return new PokemonProgressBarUi(Pokemon.randomPokemon());
return new PokemonProgressBarUi(PokemonPicker.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.kagof.intellij.plugins.pokeprogress.configuration;

import javax.swing.JComponent;

import org.jetbrains.annotations.Nullable;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.NlsContexts;

public class PokemonProgressConfigurable implements Configurable {
private PokemonProgressConfigurationComponent component;

@Override
@NlsContexts.ConfigurableName
public String getDisplayName() {
return "Pokémon Progress";
}

@Override
public @Nullable JComponent createComponent() {
component = new PokemonProgressConfigurationComponent();
return component.getPanel();
}

@Override
public boolean isModified() {
PokemonProgressState state = PokemonProgressState.getInstance();
return !state.pokemonNumbersEnabled.equals(component.getEnabledNumberMap());
}

@Override
public void apply() throws ConfigurationException {
PokemonProgressState state = PokemonProgressState.getInstance();
state.pokemonNumbersEnabled = component.getEnabledNumberMap();
}

@Override
public void reset() {
PokemonProgressState state = PokemonProgressState.getInstance();
component.updateUi(state);
}

@Override
public void disposeUIResources() {
component = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.kagof.intellij.plugins.pokeprogress.configuration;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.roots.ScalableIconComponent;
import com.intellij.util.ui.FormBuilder;
import com.kagof.intellij.plugins.pokeprogress.Pokemon;

public class PokemonProgressConfigurationComponent {
private JPanel mainPanel;
private final Map<String, JBCheckBox> checkboxes = new HashMap<>();
private final JButton selectAll = new JButton("Select all");
private final JButton deselectAll = new JButton("Deselect all");
private final JLabel label = new JLabel("?/? selected");
private final AtomicInteger numSelected = new AtomicInteger(0);


public PokemonProgressConfigurationComponent() {
createUi();
}

void createUi() {
final FormBuilder formBuilder = FormBuilder.createFormBuilder();
selectAll.addActionListener(a -> {
if (a.getID() == ActionEvent.ACTION_PERFORMED) {
checkboxes.values().forEach(c -> c.setSelected(true));
}
});
deselectAll.addActionListener(a -> {
if (a.getID() == ActionEvent.ACTION_PERFORMED) {
checkboxes.values().forEach(c -> c.setSelected(false));
}
});
formBuilder.addComponent(label);
formBuilder.addComponent(selectAll);
formBuilder.addComponent(deselectAll);

Pokemon.DEFAULT_POKEMON.values().stream().sorted().forEach(pokemon -> {
final JBCheckBox checkBox = new JBCheckBox(pokemon.getNameWithNumber(), true);
checkBox.addItemListener(c -> {
if (c.getStateChange() == ItemEvent.SELECTED) {
numSelected.incrementAndGet();
} else if (c.getStateChange() == ItemEvent.DESELECTED){
numSelected.decrementAndGet();
}
refreshSelectAllButtons();
});
formBuilder.addLabeledComponent(new ScalableIconComponent(pokemon.getIcon()), checkBox);
checkboxes.put(pokemon.getNumber(), checkBox);
numSelected.incrementAndGet();
});
refreshSelectAllButtons();
mainPanel = formBuilder.getPanel();
}

void updateUi(final PokemonProgressState state) {
if (state != null) {
state.pokemonNumbersEnabled
.forEach((pokemon, enabled) -> checkboxes.computeIfPresent(pokemon, (p, check) -> {
check.setSelected(enabled);
return check;
}));
}
}

public JPanel getPanel() {
return mainPanel;
}

public Map<String, Boolean> getEnabledNumberMap() {
return checkboxes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().isSelected()));
}

private void refreshSelectAllButtons() {
final int i = numSelected.get();
deselectAll.setEnabled(i > 0);
selectAll.setEnabled(i < checkboxes.size());
label.setText(i + "/" + checkboxes.size() + " selected");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.kagof.intellij.plugins.pokeprogress.configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.intellij.util.xmlb.annotations.Transient;
import com.kagof.intellij.plugins.pokeprogress.Pokemon;

@State(
name = "com.kagof.intellij.plugins.pokeprogress.configuration.PokemonProgressState",
storages = {@Storage("PokemonProgress.xml")}
)
public class PokemonProgressState implements PersistentStateComponent<PokemonProgressState> {

public Map<String, Boolean> pokemonNumbersEnabled = Pokemon.DEFAULT_POKEMON.keySet().stream().collect(Collectors.toMap(Function.identity(), p -> true));

public static PokemonProgressState getInstance() {
return ServiceManager.getService(PokemonProgressState.class);
}

@Override
public PokemonProgressState getState() {
return this;
}

@Override
public void loadState(@NotNull final PokemonProgressState state) {
XmlSerializerUtil.copyBean(state, this);
}

}
6 changes: 4 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
<depends>com.intellij.modules.platform</depends>

<extensions defaultExtensionNs="com.intellij">

<applicationConfigurable parentId="appearance" instance="com.kagof.intellij.plugins.pokeprogress.configuration.PokemonProgressConfigurable"
id="org.intellij.sdk.settings.AppSettingsConfigurable"
displayName="Pokémon Progress"/>
<applicationService serviceImplementation="com.kagof.intellij.plugins.pokeprogress.configuration.PokemonProgressState"/>
</extensions>
<applicationListeners>
<listener class="com.kagof.intellij.plugins.pokeprogress.PokemonProgressListener"
topic="com.intellij.ide.ui.LafManagerListener"/>
<listener class="com.kagof.intellij.plugins.pokeprogress.PokemonProgressListener"
topic="com.intellij.ide.plugins.DynamicPluginListener"/>
</applicationListeners>

<actions>
<!-- Add your actions here -->
</actions>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class DocumentationGenerator {
@Test
public void printForReadme() {
for (final Pokemon pokemon : Pokemon.values()) {
System.out.println(getReadmeString(pokemon));
if (!pokemon.isSecret()) {
System.out.println(getReadmeString(pokemon));
}
}
}

Expand All @@ -18,14 +20,15 @@ public void printForPluginXml() {
final StringBuilder stringBuilder = new StringBuilder();
int i = 0;
for (final Pokemon pokemon : Pokemon.values()) {
if (i == 0) {
stringBuilder.append("<br>\n ");
if (!pokemon.isSecret()) {
if (i == 0) {
stringBuilder.append("<br>\n ");
}
i = (i + 1) % 10;
stringBuilder.append(getPluginXmlString(pokemon));
}
i = (i + 1) % 10;
stringBuilder.append(getPluginXmlString(pokemon));
}
System.out
.println(stringBuilder.toString().trim());
System.out.println(stringBuilder.toString().trim());
}

private String getReadmeString(final Pokemon pokemon) {
Expand Down

0 comments on commit 9d7f385

Please sign in to comment.