Skip to content

Commit

Permalink
Feature login always on top (#371)
Browse files Browse the repository at this point in the history
* Encapsulated window and made helper for alwaysOnTop

* Reformat long lines

* Fix Codacy things

* Fix comments
  • Loading branch information
Wolfsblvt committed Sep 14, 2016
1 parent 0cbd34a commit a293b54
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 27 deletions.
45 changes: 37 additions & 8 deletions src/me/corriekay/pokegoutil/data/managers/AccountController.java
Expand Up @@ -27,6 +27,7 @@
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.helpers.Browser;
import me.corriekay.pokegoutil.utils.ui.Console;
import me.corriekay.pokegoutil.utils.windows.WindowStuffHelper;
import me.corriekay.pokegoutil.windows.PokemonGoMainWindow;
import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -111,7 +112,12 @@ public static void logOn() {
panel2.add(password, BorderLayout.CENTER);
final Object[] panel = {panel1, panel2,};

response = JOptionPane.showConfirmDialog(null, panel, "Login", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
response = JOptionPane.showConfirmDialog(
WindowStuffHelper.alwaysOnTopParent,
panel,
"Login",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
}

if (response == JOptionPane.CANCEL_OPTION) {
Expand All @@ -135,29 +141,43 @@ public static void logOn() {
}
} else if (response == JOptionPane.NO_OPTION) {
//Using Google, remove PTC infos
String googleAuthTitle = "Google Auth";
deleteLoginData(LoginType.PTC, true);
String authCode = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN, null);
boolean refresh = false;
if (authCode == null) {
//We need to get the auth code, as we do not have it yet.
UIManager.put("OptionPane.okButtonText", "Ok");
JOptionPane.showMessageDialog(null, "You will need to provide a google authentication key to log in. Press OK to continue.", "Google Auth", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(WindowStuffHelper.alwaysOnTopParent,
"You will need to provide a google authentication key to log in. Press OK to continue.",
googleAuthTitle,
JOptionPane.PLAIN_MESSAGE);

//We're gonna try to load the page using the users browser
JOptionPane.showMessageDialog(null, "A webpage should open up, please allow the permissions, and then copy the code into your clipboard. Press OK to continue", "Google Auth", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(WindowStuffHelper.alwaysOnTopParent,
"A webpage should open up, please allow the permissions, and then copy the code into your clipboard. Press OK to continue",
googleAuthTitle,
JOptionPane.PLAIN_MESSAGE);
final boolean success = Browser.openUrl(GoogleUserCredentialProvider.LOGIN_URL);

if (!success) {
// Okay, couldn't open it. We use the manual copy window
UIManager.put("OptionPane.cancelButtonText", "Copy To Clipboard");
if (JOptionPane.showConfirmDialog(null, "Please copy this link and paste it into a browser.\nThen, allow the permissions, and copy the code into your clipboard.", "Google Auth", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.CANCEL_OPTION) {
if (JOptionPane.showConfirmDialog(WindowStuffHelper.alwaysOnTopParent,
"Please copy this link and paste it into a browser.\nThen, allow the permissions, and copy the code into your clipboard.",
googleAuthTitle,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE) == JOptionPane.CANCEL_OPTION) {
final StringSelection ss = new StringSelection(GoogleUserCredentialProvider.LOGIN_URL);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
}
UIManager.put("OptionPane.cancelButtonText", "Cancel");
}
//The user should have the auth code now. Lets get it.
authCode = JOptionPane.showInputDialog(null, "Please provide the authentication code", "Google Auth", JOptionPane.PLAIN_MESSAGE);
authCode = JOptionPane.showInputDialog(WindowStuffHelper.alwaysOnTopParent,
"Please provide the authentication code",
googleAuthTitle,
JOptionPane.PLAIN_MESSAGE);
} else {
refresh = true;
}
Expand Down Expand Up @@ -218,15 +238,21 @@ private static void initOtherControllers(final PokemonGo go) {
}

private static void alertFailedLogin(final String message) {
JOptionPane.showMessageDialog(null, "Unfortunately, your login has failed. Reason: " + message + "\nPress OK to try again.", "Login Failed", JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(WindowStuffHelper.alwaysOnTopParent,
"Unfortunately, your login has failed. Reason: " + message + "\nPress OK to try again.",
"Login Failed",
JOptionPane.PLAIN_MESSAGE);
}

private static boolean checkSaveAuth() {
UIManager.put("OptionPane.noButtonText", "No");
UIManager.put("OptionPane.yesButtonText", "Yes");
UIManager.put("OptionPane.okButtonText", "Ok");
UIManager.put("OptionPane.cancelButtonText", "Cancel");
return JOptionPane.showConfirmDialog(null, "Do you wish to save the password/auth token?\nCaution: These are saved in plain-text.", "Save Authentication?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION;
return JOptionPane.showConfirmDialog(WindowStuffHelper.alwaysOnTopParent,
"Do you wish to save the password/auth token?\nCaution: These are saved in plain-text.", "Save Authentication?",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION;
}

private static LoginType checkSavedConfig() {
Expand Down Expand Up @@ -294,7 +320,10 @@ private static boolean checkForSavedCredentials() {
UIManager.put("OptionPane.yesButtonText", "Yes");
UIManager.put("OptionPane.okButtonText", "Ok");
UIManager.put("OptionPane.cancelButtonText", "Cancel");
return JOptionPane.showConfirmDialog(null, "You have saved login data for " + savedLogin.toString() + ". Want to login with that?", "Use Saved Login", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION;
return JOptionPane.showConfirmDialog(WindowStuffHelper.alwaysOnTopParent,
"You have saved login data for " + savedLogin.toString() + ". Want to login with that?",
"Use Saved Login",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/me/corriekay/pokegoutil/utils/Utilities.java
@@ -1,6 +1,7 @@
package me.corriekay.pokegoutil.utils;

import com.google.protobuf.InvalidProtocolBufferException;

import org.apache.commons.lang3.StringUtils;

import java.awt.*;
Expand All @@ -10,7 +11,7 @@
import java.util.concurrent.TimeUnit;

public final class Utilities {
/* Prevent initializing this class */
/** Prevent initializing this class. */
private Utilities() {
}

Expand Down Expand Up @@ -60,7 +61,7 @@ public static void sleep(int milliseconds) {

public static void sleepRandom(int minMilliseconds, int maxMilliseconds) {
try {
int randomInt = getRandom(minMilliseconds,maxMilliseconds);
int randomInt = getRandom(minMilliseconds, maxMilliseconds);
System.out.println("Waiting " + (randomInt / 1000.0F) + " seconds.");
TimeUnit.MILLISECONDS.sleep(randomInt);
} catch (InterruptedException e) {
Expand Down
4 changes: 3 additions & 1 deletion src/me/corriekay/pokegoutil/utils/helpers/Browser.java
Expand Up @@ -7,7 +7,9 @@
import java.net.URISyntaxException;

public final class Browser {
private Browser() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private Browser() {
}

/***
* Opens given URL in users default browser of his operating system.
Expand Down
Expand Up @@ -4,7 +4,9 @@
import java.util.Set;

public final class CollectionHelper {
private CollectionHelper() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private CollectionHelper() {
}

public static String joinStringArray(String[] args) {
return joinStringArray(args, "");
Expand Down
4 changes: 3 additions & 1 deletion src/me/corriekay/pokegoutil/utils/helpers/DateHelper.java
Expand Up @@ -7,7 +7,9 @@


public final class DateHelper {
private DateHelper() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private DateHelper() {
}

public static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
public static final String timeFormat = "HH:mm:ss";
Expand Down
5 changes: 4 additions & 1 deletion src/me/corriekay/pokegoutil/utils/helpers/FileHelper.java
Expand Up @@ -3,11 +3,14 @@

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.io.*;

public final class FileHelper {
private FileHelper() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private FileHelper() {
}

private static ClassLoader classLoader = FileHelper.class.getClassLoader();

Expand Down
Expand Up @@ -6,7 +6,9 @@
import javax.swing.table.TableColumn;

public final class JTableColumnPacker {
private JTableColumnPacker() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private JTableColumnPacker() {
}

public static void packColumn(JTable table, int vColIndex, int margin) {
DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
Expand Down
Expand Up @@ -7,12 +7,15 @@
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Objects;

public final class LDocumentListener {
private LDocumentListener() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private LDocumentListener() {
}

/**
* Installs a listener to receive notification when the text of any
Expand Down
5 changes: 4 additions & 1 deletion src/me/corriekay/pokegoutil/utils/helpers/UIHelper.java
@@ -1,10 +1,13 @@
package me.corriekay.pokegoutil.utils.helpers;

import javax.swing.*;

import java.awt.*;

public final class UIHelper {
private UIHelper() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private UIHelper() {
}

public static void setNativeLookAndFeel() {
try {
Expand Down
14 changes: 9 additions & 5 deletions src/me/corriekay/pokegoutil/utils/pokemon/PokemonUtils.java
@@ -1,15 +1,19 @@
package me.corriekay.pokegoutil.utils.pokemon;

import POGOProtos.Enums.PokemonMoveOuterClass.PokemonMove;

import com.pokegoapi.api.player.Team;
import com.pokegoapi.api.pokemon.*;

import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.Utilities;
import org.apache.commons.lang3.StringUtils;

public final class PokemonUtils {
private PokemonUtils() { /* Prevent initializing this class */ }
/** Prevent initializing this class. */
private PokemonUtils() {
}

/**
* Maximum duel ability - moveset only
Expand Down Expand Up @@ -119,7 +123,7 @@ private static double dpsForMove(Pokemon p, PokemonMove move, boolean primary) {
* Duel Ability is Tankiness * Gym Offense. A reasonable measure if you don't often/ever dodge,
* as then you can only attack for as long as you can stay positive on HP.
*
* @param p A Pokemon object
* @param p A Pokemon object
* @param useIV Use a pokemon's IV values in the calculations
* @return Rating of a Pokemon's overall attacking power considering damage, health & defense
* @link https://www.reddit.com/r/TheSilphRoad/comments/4vcobt/posthotfix_pokemon_go_full_moveset_rankings/
Expand All @@ -134,7 +138,7 @@ public static long duelAbility(Pokemon p, boolean useIV) {
* Gym Offense takes the better of No Weave/Weave Damage over 100s and multiplies by the
* Pokemon's base attack to arrive at a ranking of raw damage output.
*
* @param p A Pokemon object
* @param p A Pokemon object
* @param useIV Use a pokemon's IV values in the calculations
* @return Rating of a Pokemon's pure offensive ability over time considering move set
* @link https://www.reddit.com/r/TheSilphRoad/comments/4vcobt/posthotfix_pokemon_go_full_moveset_rankings/
Expand All @@ -151,7 +155,7 @@ public static double gymOffense(Pokemon p, boolean useIV) {
* Gym Defense takes the calculated Gym Weave Damage over 100s and multiplies by Tankiness
* to arrive at a ranking of how much damage a Pokemon will output when defending a gym.
*
* @param p A Pokemon object
* @param p A Pokemon object
* @param useIV Use a pokemon's IV values in the calculations
* @return Rating of a Pokemon's AI controlled gym defense over time considering move set
* @link https://www.reddit.com/r/TheSilphRoad/comments/4vcobt/posthotfix_pokemon_go_full_moveset_rankings/
Expand All @@ -171,7 +175,7 @@ public static long gymDefense(Pokemon p, boolean useIV) {
* <p>
* Used for duel ability & gym defense calculations
*
* @param p A Pokemon object
* @param p A Pokemon object
* @param useIV Use a pokemon's IV values in the calculations
* @return Rating of a Pokemon's tankiness :)
* @link https://www.reddit.com/r/TheSilphRoad/comments/4vcobt/posthotfix_pokemon_go_full_moveset_rankings/
Expand Down
18 changes: 18 additions & 0 deletions src/me/corriekay/pokegoutil/utils/windows/WindowStuffHelper.java
@@ -0,0 +1,18 @@
package me.corriekay.pokegoutil.utils.windows;

import javax.swing.*;

/**
* Helper class that provides useful tools for everything concerning windows.
*/
public final class WindowStuffHelper {
public static final JDialog alwaysOnTopParent = new JDialog();

static {
alwaysOnTopParent.setAlwaysOnTop(true);
}

/** Prevent initializing this class. */
private WindowStuffHelper() {
}
}
11 changes: 10 additions & 1 deletion src/me/corriekay/pokegoutil/windows/PokemonGoMainWindow.java
Expand Up @@ -22,7 +22,7 @@
@SuppressWarnings("serial")
public class PokemonGoMainWindow extends JFrame {

public static PokemonGoMainWindow window = null;
private static PokemonGoMainWindow window = null;
private final PokemonGo go;
private final PlayerProfile pp;
private final JTabbedPane tab = new JTabbedPane();
Expand Down Expand Up @@ -96,6 +96,15 @@ public void componentMoved(ComponentEvent e) {
updater.checkForNewVersion();
}

/**
* Returns the current instance of the PokemonGoMainWindow.
*
* @return The window.
*/
public static PokemonGoMainWindow getWindow() {
return window;
}

public PokemonGo getPoGo() {
return go;
}
Expand Down
8 changes: 4 additions & 4 deletions src/me/corriekay/pokegoutil/windows/PokemonTab.java
Expand Up @@ -102,9 +102,9 @@ public PokemonTab(final PokemonGo go) {
if (event.getSource() == pt.getSelectionModel() && pt.getRowSelectionAllowed()) {
final int selectedRows = pt.getSelectedRowCount();
if (selectedRows >= WHEN_TO_SHOW_SELECTION_TITLE) {
PokemonGoMainWindow.window.setTitle(selectedRows + " Pokémon selected");
PokemonGoMainWindow.getWindow().setTitle(selectedRows + " Pokémon selected");
} else {
PokemonGoMainWindow.window.refreshTitle();
PokemonGoMainWindow.getWindow().refreshTitle();
}
}
});
Expand Down Expand Up @@ -686,7 +686,7 @@ private void powerUpSelected() {
});
try {
go.getInventories().updateInventories(true);
PokemonGoMainWindow.window.refreshTitle();
PokemonGoMainWindow.getWindow().refreshTitle();
} catch (final Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -755,7 +755,7 @@ private void toggleFavorite() {
}
});
try {
PokemonGoMainWindow.window.refreshTitle();
PokemonGoMainWindow.getWindow().refreshTitle();
} catch (final Exception e) {
e.printStackTrace();
}
Expand Down

0 comments on commit a293b54

Please sign in to comment.