Improved AccountListCell

This commit is contained in:
Bixilon 2020-12-23 18:18:33 +01:00
parent 2fa7fb1225
commit 4b17fade93
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
14 changed files with 162 additions and 88 deletions

View File

@ -23,10 +23,11 @@ import de.bixilon.minosoft.data.assets.AssetsManager;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.minecraft.MinecraftLocaleManager;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.gui.main.AccountListCell;
import de.bixilon.minosoft.gui.main.GUITools;
import de.bixilon.minosoft.gui.main.Launcher;
import de.bixilon.minosoft.gui.main.ServerListCell;
import de.bixilon.minosoft.gui.main.StartProgressWindow;
import de.bixilon.minosoft.gui.main.cells.AccountListCell;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.logging.LogLevels;
import de.bixilon.minosoft.modding.event.EventManager;
@ -190,25 +191,28 @@ public final class Minosoft {
}
}
public static void selectAccount(Account account) {
public static boolean selectAccount(Account account) {
if (account == null) {
config.putString(ConfigurationPaths.StringPaths.ACCOUNT_SELECTED, "");
config.saveToFile();
return;
return false;
}
if (!account.select()) {
account.logout();
AccountListCell.MOJANG_ACCOUNT_LIST_VIEW.getItems().remove(account);
config.removeAccount(account);
if (account.select()) {
config.putAccount(account);
config.selectAccount(account);
config.saveToFile();
return;
}
config.putAccount(account);
config.selectAccount(account);
if (Launcher.getMainWindow() != null) {
Launcher.getMainWindow().selectAccount(account);
if (Launcher.getMainWindow() != null) {
Launcher.getMainWindow().selectAccount(account);
}
AccountListCell.ACCOUNT_LIST_VIEW.refresh();
ServerListCell.SERVER_LIST_VIEW.refresh();
return true;
}
account.logout();
AccountListCell.ACCOUNT_LIST_VIEW.getItems().remove(account);
config.removeAccount(account);
config.saveToFile();
return false;
}
public static Configuration getConfig() {

View File

@ -71,4 +71,9 @@ public abstract class Account {
Minosoft.getConfig().putAccount(this);
Minosoft.getConfig().saveToFile();
}
@Override
public String toString() {
return getId();
}
}

View File

@ -110,11 +110,6 @@ public class MojangAccount extends Account {
}
@Override
public String toString() {
return getId();
}
public boolean needsRefresh() {
return this.needsRefresh;
}

View File

@ -42,7 +42,8 @@ public enum Strings {
SESSIONS_DIALOG_TITLE,
ACCOUNTS_ACTION_SELECT,
ACCOUNTS_ACTION_DELETE,
ACCOUNTS_ACTION_INFO,
ACCOUNTS_ACTION_LOGOUT,
SERVER_ACTION_CONNECT,
SERVER_ACTION_SHOW_INFO,

View File

@ -17,6 +17,7 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.gui.main.cells.AccountListCell;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
@ -35,11 +36,11 @@ public class AccountWindow implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
AccountListCell.MOJANG_ACCOUNT_LIST_VIEW.setCellFactory((lv) -> AccountListCell.newInstance());
AccountListCell.ACCOUNT_LIST_VIEW.setCellFactory((lv) -> AccountListCell.newInstance());
ObservableList<Account> accounts = FXCollections.observableArrayList(Minosoft.getConfig().getAccounts().values());
AccountListCell.MOJANG_ACCOUNT_LIST_VIEW.setItems(accounts);
this.accountPane.setCenter(AccountListCell.MOJANG_ACCOUNT_LIST_VIEW);
AccountListCell.ACCOUNT_LIST_VIEW.setItems(accounts);
this.accountPane.setCenter(AccountListCell.ACCOUNT_LIST_VIEW);
this.menuAddMojangAccount.setText(LocaleManager.translate(Strings.ACCOUNT_MODAL_MENU_ADD_MOJANG_ACCOUNT));
this.menuAddOfflineAccount.setText(LocaleManager.translate(Strings.ACCOUNT_MODAL_MENU_ADD_OFFLINE_ACCOUNT));

View File

@ -104,12 +104,14 @@ public class ServerListCell extends ListCell<Server> implements Initializable {
protected void updateItem(Server server, boolean empty) {
super.updateItem(server, empty);
this.root.setVisible(!empty);
this.root.setVisible(server != null || !empty);
if (empty) {
resetCell();
return;
}
if (server == null) {
resetCell();
return;
}

View File

@ -11,32 +11,40 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.gui.main;
package de.bixilon.minosoft.gui.main.cells;
import com.jfoenix.controls.JFXButton;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.gui.main.GUITools;
import de.bixilon.minosoft.logging.Log;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class AccountListCell extends ListCell<Account> implements Initializable {
public static final ListView<Account> MOJANG_ACCOUNT_LIST_VIEW = new ListView<>();
public static final ListView<Account> ACCOUNT_LIST_VIEW = new ListView<>();
public HBox hBox;
public ImageView head;
public Label username;
public Label type;
public JFXButton selectIcon;
public JFXButton infoIcon;
public JFXButton logoutIcon;
public MenuButton optionsMenu;
public Label playerName;
public MenuItem optionsSelect;
public Label email;
public MenuItem optionsDelete;
public AnchorPane root;
private Account account;
@ -54,28 +62,27 @@ public class AccountListCell extends ListCell<Account> implements Initializable
@Override
public void initialize(URL url, ResourceBundle rb) {
updateSelected(false);
setGraphic(this.root);
setGraphic(this.hBox);
// change locale
this.optionsSelect.setText(LocaleManager.translate(Strings.ACCOUNTS_ACTION_SELECT));
this.optionsDelete.setText(LocaleManager.translate(Strings.ACCOUNTS_ACTION_DELETE));
this.selectIcon.setText(LocaleManager.translate(Strings.ACCOUNTS_ACTION_SELECT));
this.infoIcon.setText(LocaleManager.translate(Strings.ACCOUNTS_ACTION_INFO));
this.logoutIcon.setText(LocaleManager.translate(Strings.ACCOUNTS_ACTION_LOGOUT));
}
public AnchorPane getRoot() {
return this.root;
}
@Override
protected void updateItem(Account account, boolean empty) {
super.updateItem(account, empty);
this.root.setVisible(!empty);
this.hBox.setVisible(account != null || !empty);
if (empty) {
resetCell();
return;
}
if (account == null) {
resetCell();
return;
}
@ -83,20 +90,34 @@ public class AccountListCell extends ListCell<Account> implements Initializable
return;
}
resetCell();
if (Minosoft.getConfig().getSelectedAccount() == account) {
this.hBox.getStyleClass().add("list-cell-selected");
this.selectIcon.setDisable(true);
}
// ToDo: Set head
this.account = account;
this.playerName.setText(account.getUsername());
// this.email.setText(account.getEmail());
if (Minosoft.getConfig().getSelectedAccount() == account) {
setStyle("-fx-background-color: darkseagreen;");
this.optionsSelect.setDisable(true);
}
this.username.setText(account.getUsername());
this.type.setText(account.getClass().getSimpleName());
}
private void resetCell() {
// clear all cells
setStyle(null);
this.optionsSelect.setDisable(false);
this.hBox.getStyleClass().remove("list-cell-selected");
this.selectIcon.setDisable(false);
this.head.setImage(GUITools.MINOSOFT_LOGO);
}
public void select() {
Minosoft.selectAccount(this.account);
if (Minosoft.getConfig().getSelectedAccount() == this.account) {
// ToDo: Why isn't his working correct?
this.hBox.getStyleClass().add("list-cell-selected");
this.selectIcon.setDisable(true);
}
}
public void logout() {
@ -109,26 +130,25 @@ public class AccountListCell extends ListCell<Account> implements Initializable
} else {
Minosoft.selectAccount(Minosoft.getConfig().getAccounts().values().iterator().next());
}
MOJANG_ACCOUNT_LIST_VIEW.refresh();
ACCOUNT_LIST_VIEW.refresh();
}
Log.info(String.format("Deleted account (id=%s, username=%s)", this.account.getId(), this.account.getUsername()));
MOJANG_ACCOUNT_LIST_VIEW.getItems().remove(this.account);
Log.info(String.format("Deleted account (type=%s, id=%s, username=%s)", this.account.getClass().getSimpleName(), this.account.getId(), this.account.getUsername()));
ACCOUNT_LIST_VIEW.getItems().remove(this.account);
}
public void info() {
// ToDo
}
public void clicked(MouseEvent e) {
switch (e.getButton()) {
case PRIMARY -> {
if (e.getClickCount() == 2) {
select();
}
}
case SECONDARY -> this.optionsMenu.fire();
if (e.getButton() != MouseButton.PRIMARY) {
return;
}
if (e.getClickCount() != 2) {
return;
}
select();
}
public void select() {
Minosoft.selectAccount(this.account);
MOJANG_ACCOUNT_LIST_VIEW.refresh();
ServerListCell.SERVER_LIST_VIEW.refresh();
}
}

View File

@ -20,7 +20,7 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.accounts.MojangAccount;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.gui.main.AccountListCell;
import de.bixilon.minosoft.gui.main.cells.AccountListCell;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.util.mojang.api.MojangAuthentication;
import de.bixilon.minosoft.util.mojang.api.exceptions.AuthenticationException;
@ -91,7 +91,7 @@ public class MojangLoginController implements Initializable {
account.saveToConfig();
Log.info(String.format("Added and saved account (type=mojang, username=%s, email=%s, uuid=%s)", account.getUsername(), account.getEmail(), account.getUUID()));
Platform.runLater(() -> {
AccountListCell.MOJANG_ACCOUNT_LIST_VIEW.getItems().add(account);
AccountListCell.ACCOUNT_LIST_VIEW.getItems().add(account);
close();
});
if (Minosoft.getConfig().getSelectedAccount() == null) {

View File

@ -19,7 +19,7 @@ import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.data.accounts.OfflineAccount;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.gui.main.AccountListCell;
import de.bixilon.minosoft.gui.main.cells.AccountListCell;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.util.Util;
@ -85,7 +85,7 @@ public class OfflineLoginController implements Initializable {
account.saveToConfig();
Log.info(String.format("Added and saved account (type=offline, username=%s, uuid=%s)", account.getUsername(), account.getUUID()));
Platform.runLater(() -> {
AccountListCell.MOJANG_ACCOUNT_LIST_VIEW.getItems().add(account);
AccountListCell.ACCOUNT_LIST_VIEW.getItems().add(account);
close();
});
if (Minosoft.getConfig().getSelectedAccount() == null) {

View File

@ -27,7 +27,8 @@
"SERVER_INFO_SERVER_MODDED_MOD_LIST": "Mod liste",
"SESSIONS_DIALOG_TITLE": "Verbindungen - {0} - Minosoft",
"ACCOUNTS_ACTION_SELECT": "Auswählen",
"ACCOUNTS_ACTION_DELETE": "Löschen",
"ACCOUNTS_ACTION_INFO": "Info",
"ACCOUNTS_ACTION_LOGOUT": "Abmelden",
"SERVER_ACTION_CONNECT": "Verbinden",
"SERVER_ACTION_SHOW_INFO": "Infos",
"SERVER_ACTION_EDIT": "Bearbeiten",

View File

@ -28,7 +28,8 @@
"SERVER_INFO_SERVER_MODDED_MOD_LIST": "Mod list",
"SESSIONS_DIALOG_TITLE": "Sessions - {0} - Minosoft",
"ACCOUNTS_ACTION_SELECT": "Select",
"ACCOUNTS_ACTION_DELETE": "Delete",
"ACCOUNTS_ACTION_INFO": "Info",
"ACCOUNTS_ACTION_LOGOUT": "Logout",
"SERVER_ACTION_CONNECT": "Connect",
"SERVER_ACTION_SHOW_INFO": "Info",
"SERVER_ACTION_EDIT": "Edit",

View File

@ -21,8 +21,8 @@
</Menu>
</MenuBar>
<AnchorPane VBox.vgrow="ALWAYS">
<ScrollPane fitToHeight="true" fitToWidth="true" layoutX="14.0" layoutY="14.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<BorderPane fx:id="accountPane" />
<ScrollPane fitToHeight="true" fitToWidth="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<BorderPane fx:id="accountPane"/>
</ScrollPane>
</AnchorPane>
</VBox>

View File

@ -11,20 +11,56 @@
~
~ This software is not affiliated with Mojang AB, the original developer of Minecraft.
-->
<?import javafx.scene.control.*?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:id="root" maxHeight="-Infinity" maxWidth="500.0" minHeight="-Infinity" minWidth="500.0" onMouseClicked="#clicked" prefWidth="500.0" fx:controller="de.bixilon.minosoft.gui.main.AccountListCell">
<Label fx:id="playerName" layoutX="111.0" layoutY="14.0" maxWidth="200.0" minWidth="10.0" text="#Player name#" underline="true" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0">
<font>
<Font size="17.0" />
</font>
</Label>
<MenuButton fx:id="optionsMenu" layoutX="389.0" layoutY="81.0" mnemonicParsing="false" text="⋮" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0">
<items>
<MenuItem fx:id="optionsSelect" mnemonicParsing="false" onAction="#select" text="-Select-" />
<MenuItem fx:id="optionsDelete" mnemonicParsing="false" onAction="#logout" style="-fx-text-fill: red" text="-Delete-"/>
</items>
</MenuButton>
<Label fx:id="email" layoutX="121.0" layoutY="24.0" maxWidth="200.0" minWidth="10.0" text="#Email#" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="30.0" />
</AnchorPane>
<HBox xmlns:fx="http://javafx.com/fxml/1" onMouseClicked="#clicked" fx:id="hBox" maxHeight="50" maxWidth="Infinity" minHeight="50" minWidth="-Infinity" prefHeight="50.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="de.bixilon.minosoft.gui.main.cells.AccountListCell">
<GridPane maxHeight="Infinity" maxWidth="Infinity" HBox.hgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="NEVER" prefWidth="40.0"/>
<ColumnConstraints hgrow="ALWAYS" minWidth="50.0" prefWidth="160.0"/>
<ColumnConstraints hgrow="NEVER" minWidth="30.0"/>
<ColumnConstraints hgrow="NEVER" minWidth="30.0"/>
<ColumnConstraints hgrow="NEVER" minWidth="30.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="40.0" vgrow="NEVER"/>
</rowConstraints>
<ImageView fx:id="head" fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true"/>
<GridPane GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.vgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0"/>
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="20" minHeight="20.0" prefHeight="20.0" vgrow="NEVER"/>
<RowConstraints maxHeight="20" minHeight="20.0" prefHeight="20.0" vgrow="NEVER"/>
</rowConstraints>
<Label fx:id="username" text="Bixilon"/>
<Label fx:id="type" styleClass="account-type" text="Offline" GridPane.rowIndex="1"/>
<GridPane.margin>
<Insets left="10.0"/>
</GridPane.margin>
</GridPane>
<JFXButton fx:id="selectIcon" onAction="#select" text="SELECT" GridPane.columnIndex="2">
<GridPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</GridPane.margin>
</JFXButton>
<JFXButton fx:id="infoIcon" disable="true" onAction="#info" text="INFO" GridPane.columnIndex="3">
<GridPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</GridPane.margin>
</JFXButton>
<JFXButton fx:id="logoutIcon" onAction="#logout" text="LOGOUT" GridPane.columnIndex="4">
<GridPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0"/>
</GridPane.margin>
</JFXButton>
</GridPane>
<padding>
<Insets bottom="5.0" left="10.0" right="10.0" top="5.0"/>
</padding>
</HBox>

View File

@ -174,6 +174,10 @@
-fx-background-color: darkseagreen;
}
.list-cell-selected {
-fx-background-color: darkseagreen;
}
.ping-5-bars {
-fx-text-fill: greenyellow;
}
@ -205,3 +209,7 @@
.error {
-fx-text-fill: red;
}
.account-type {
-fx-text-fill: -secondary-light-light-color;
}