0. 把鸽了万年的历史记录给弄好了

1. 修复清除历史记录后并未生效的问题
2. 其他的一些无伤大雅的修改
main
lensfrex 3 years ago
parent 68e152e95c
commit 5a3649f773
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 1
      Dogename/dogename.iml
  2. 5
      Dogename/src/main/java/me/lensferno/dogename/Main.java
  3. 4
      Dogename/src/main/java/me/lensferno/dogename/controllers/GushiciPaneController.java
  4. 94
      Dogename/src/main/java/me/lensferno/dogename/controllers/HistoryPaneController.java
  5. 4
      Dogename/src/main/java/me/lensferno/dogename/controllers/HitokotoPaneController.java
  6. 3
      Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java
  7. 4
      Dogename/src/main/java/me/lensferno/dogename/data/Data.java
  8. 3
      Dogename/src/main/java/me/lensferno/dogename/data/History.java
  9. 19
      Dogename/src/main/java/me/lensferno/dogename/sayings/Gushici.java
  10. 20
      Dogename/src/main/java/me/lensferno/dogename/sayings/Hitokoto.java
  11. 12
      Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java
  12. 19
      Dogename/src/main/resources/me/lensferno/dogename/FXMLs/HistoryPane.fxml

@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

@ -1,5 +1,7 @@
package me.lensferno.dogename; package me.lensferno.dogename;
import java.util.Properties;
import javafx.application.Application; import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
@ -23,7 +25,6 @@ public class Main extends Application {
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
try { try {
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
@ -82,8 +83,6 @@ public class Main extends Application {
} else { } else {
new Hitokoto().showHitokoto(mainInterfaceController.getRootPane(), mainInterfaceController.getTopBar(), mainInterfaceController.getMainConfig().isShowSaying()); new Hitokoto().showHitokoto(mainInterfaceController.getRootPane(), mainInterfaceController.getTopBar(), mainInterfaceController.getMainConfig().isShowSaying());
} }
} }
} }

@ -27,8 +27,8 @@ public class GushiciPaneController extends VBox {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
contentText.setText("“" + content + "”"); contentText.setText(String.format("“%s”", content));
contentInfo.setText("《" + title + "》" + "——" + author); contentInfo.setText(String.format("《%s》——%s", title, author));
contentType.setText(type); contentType.setText(type);
} }
} }

@ -10,15 +10,19 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import me.lensferno.dogename.data.History; import me.lensferno.dogename.data.History;
import me.lensferno.dogename.utils.DialogMaker; import me.lensferno.dogename.utils.DialogMaker;
import java.util.ArrayList;
public class HistoryPaneController extends VBox { public class HistoryPaneController extends VBox {
public static final ObservableList<String> shownHistoryList = FXCollections.observableArrayList();
History history; History history;
Pane rootPane;
int pointer = 0; public static final ObservableList<String> historyListCollection = FXCollections.observableArrayList();
private final Pane rootPane;
@FXML @FXML
private JFXListView<String> historyList; private JFXListView<String> historyList;
@ -31,6 +35,9 @@ public class HistoryPaneController extends VBox {
@FXML @FXML
private JFXButton nextBtn; private JFXButton nextBtn;
@FXML
private Text searchMessage;
public HistoryPaneController(History history, Pane rootPane) { public HistoryPaneController(History history, Pane rootPane) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/HistoryPane.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/me/lensferno/dogename/FXMLs/HistoryPane.fxml"));
loader.setRoot(this); loader.setRoot(this);
@ -40,82 +47,89 @@ public class HistoryPaneController extends VBox {
try { try {
loader.load(); loader.load();
shownHistoryList.setAll(history.getHistoryList()); historyListCollection.setAll(history.getHistoryList());
historyList.setItems(shownHistoryList); historyList.setItems(historyListCollection);
searchBar.textProperty().addListener((observable, oldValue, newValue) -> pointer = 0); searchBar.textProperty().addListener((observable, oldValue, newValue) -> searchIndex = 0);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void pointOutSearchResult(int pointer) { private void pointOutSearchResult(int pointer) {
historyList.scrollTo(pointer);
historyList.getSelectionModel().select(pointer); historyList.getSelectionModel().select(pointer);
historyList.getFocusModel().focus(pointer);
} }
private int searchIndex = 0;
@FXML @FXML
void upSearch(ActionEvent event) { void upSearch(ActionEvent event) {
searchMessage.textProperty().set("");
String searchText = searchBar.getText(); String searchText = searchBar.getText();
String[] historyArrayList = history.getHistoryList().toArray(new String[0]); if (searchText.isEmpty()) {
if (historyArrayList.length == 0) {
return; return;
} }
if (pointer > historyArrayList.length - 1 || pointer < 0) { ArrayList<String> historyArrayList = history.getHistoryList();
pointer = historyArrayList.length - 1; if (historyArrayList.isEmpty()) {
searchIndex = 0;
return;
} }
while (!historyArrayList[pointer].contains(searchText)) { // 逐个查找,直到searchIndex == 0
pointer--; while (searchIndex > 0 && !historyArrayList.get(searchIndex).contains(searchText)) {
if (pointer < 0) { searchIndex--;
pointer = historyArrayList.length - 1;
return;
}
}
pointOutSearchResult(pointer);
pointer--;
if (pointer < 0) {
pointer = historyArrayList.length - 1;
} }
pointOutSearchResult(searchIndex);
// 上移索引,避免原地停留
searchIndex--;
if (searchIndex < 0) {
searchIndex = historyArrayList.size() - 1;
searchMessage.textProperty().set("到达列表开头,再次点击将从列表尾部重新搜索");
}
} }
@FXML @FXML
void downSearch(ActionEvent event) { void downSearch(ActionEvent event) {
searchMessage.textProperty().set("");
String searchText = searchBar.getText(); String searchText = searchBar.getText();
String[] historyArrayList = history.getHistoryList().toArray(new String[0]); if (searchText.isEmpty()) {
if (historyArrayList.length == 0) {
return; return;
} }
if (pointer > historyArrayList.length - 1 || pointer < 0) { ArrayList<String> historyList = history.getHistoryList();
pointer = 0;
}
while (!historyArrayList[pointer].contains(searchText)) { if (historyList.isEmpty()) {
pointer++; searchIndex = 0;
if (pointer < historyArrayList.length - 1) { return;
pointer = 0;
return;
}
} }
pointOutSearchResult(pointer);
pointer++; // 逐个查找,直到最后一个元素
if (pointer < historyArrayList.length - 1) { while (searchIndex < historyList.size() - 1 && !historyList.get(searchIndex).contains(searchText)) {
pointer = 0; searchIndex++;
} }
pointOutSearchResult(searchIndex);
// 下移索引,避免原地停留
searchIndex++;
if (searchIndex >= historyList.size()) {
searchIndex = 0;
searchMessage.textProperty().set("到达列表尾部,再次点击将返回列表开头重新搜索");
}
} }
@FXML @FXML
void clearHistory() { void clearHistory() {
new DialogMaker(rootPane).createDialogWithOKAndCancel("且慢!", "真的要清除全部历史记录吗?", (e) -> { new DialogMaker(rootPane).createDialogWithOKAndCancel("且慢!", "真的要清除全部历史记录吗?", (e) -> {
this.history.clearHistory(); this.history.clearHistory();
pointer = 0; historyListCollection.clear();
searchIndex = 0;
}); });
} }

@ -27,8 +27,8 @@ public class HitokotoPaneController extends VBox {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
hitokotoContent.setText("『 " + hitokoto + " 』"); hitokotoContent.setText(String.format("『 %s 』", hitokoto));
contentInfo.setText("出自:" + from + "  |  作者:" + author + "  |  上传者:" + creator); contentInfo.setText(String.format("出自:%s  |  作者:%s  |  上传者:%s", from, author, creator));
contentType.setText(type); contentType.setText(type);
} }
} }

@ -131,7 +131,8 @@ public class SettingsPaneController extends VBox {
void equalBtnAction(ActionEvent event) { void equalBtnAction(ActionEvent event) {
if (!mainConfig.getPassSelectedResult()) { if (!mainConfig.getPassSelectedResult()) {
equalModeBtn.setSelected(false); equalModeBtn.setSelected(false);
new DialogMaker(rootPane).createMessageDialog("且慢", "无法在“概率均分”的模式下使用,如需使用请在“人人有份”模式下启用。"); new DialogMaker(rootPane).createMessageDialog("且慢",
"无法在“概率均分”的模式下使用,如需使用请在“人人有份”模式下启用。");
} }
} }

@ -80,7 +80,6 @@ public class Data {
sb.append(temp); sb.append(temp);
sb.append("\n"); sb.append("\n");
} }
nameList = new Gson().fromJson(sb.toString(), List.class); nameList = new Gson().fromJson(sb.toString(), List.class);
System.out.println("Imported list from:" + path.getPath()); System.out.println("Imported list from:" + path.getPath());
} catch (Exception e) { } catch (Exception e) {
@ -105,7 +104,6 @@ public class Data {
} }
nameList.clear(); nameList.clear();
nameList.addAll(tempList); nameList.addAll(tempList);
} }
public void add(String text) { public void add(String text) {
@ -246,7 +244,7 @@ public class Data {
public void readIgnoreList() { public void readIgnoreList() {
readNameIgnoreList(); readNameIgnoreList();
readNumberIgnoreList(); readNumberIgnoreList();
System.out.printf("There are %d names and %d numbers ignored", ignoreNameList.size(), ignoreNumberList.size()); System.out.printf("There are %d names and %d numbers ignored\n", ignoreNameList.size(), ignoreNumberList.size());
} }
private void readNameIgnoreList() { private void readNameIgnoreList() {

@ -7,7 +7,7 @@ import java.util.ArrayList;
public class History { public class History {
ArrayList<String> history; private ArrayList<String> history;
private final String HISTORY_FILE = FilePath.toSpecificPathForm("files/history.data"); private final String HISTORY_FILE = FilePath.toSpecificPathForm("files/history.data");
@ -70,6 +70,7 @@ public class History {
public void clearHistory() { public void clearHistory() {
this.history.clear(); this.history.clear();
writeHistory();
} }
} }

@ -13,8 +13,6 @@ public class Gushici {
private final String GUSHICI_API = "https://v1.jinrishici.com/all.json"; private final String GUSHICI_API = "https://v1.jinrishici.com/all.json";
String gushiciJSON = null;
private String getGushici() { private String getGushici() {
return NetworkUtil.getHtml(GUSHICI_API); return NetworkUtil.getHtml(GUSHICI_API);
} }
@ -23,18 +21,17 @@ public class Gushici {
new Thread(() -> { new Thread(() -> {
//gushiciJSON=getGushici(); String gushiciJSON = null;
String content, title, author, type;
if ((gushiciJSON = getGushici()) != null) { if ((gushiciJSON = getGushici()) != null) {
GushiciData gushiciData = new Gson().fromJson(gushiciJSON, GushiciData.class); GushiciData gushiciData = new Gson().fromJson(gushiciJSON, GushiciData.class);
content = gushiciData.getContent();
title = gushiciData.getTitle(); String content = gushiciData.getContent(),
author = gushiciData.getAuthor(); title = gushiciData.getTitle(),
type = gushiciData.getType(); author = gushiciData.getAuthor(),
type = gushiciData.getType();
Platform.runLater(() -> { Platform.runLater(() -> {
topBar.setText(content + " ——" + author + "《" + title + "》");
topBar.setText(String.format("%s ——%s 《%s》", content, author, title)); topBar.setText(String.format("%s ——%s 《%s》", content, author, title));
if (showOnDialog) { if (showOnDialog) {
GushiciPaneController gushiciPaneController = new GushiciPaneController(content, title, author, type); GushiciPaneController gushiciPaneController = new GushiciPaneController(content, title, author, type);
@ -42,7 +39,7 @@ public class Gushici {
} }
}); });
} }
}).start(); }, "GushiciThread").start();
} }

@ -11,11 +11,8 @@ import me.lensferno.dogename.utils.NetworkUtil;
public class Hitokoto { public class Hitokoto {
private final String HITOKOTO_API = "https://v1.hitokoto.cn/"; private final String HITOKOTO_API = "https://v1.hitokoto.cn/";
String hitokotoJSON = null;
private String getHitokoto() { private String getHitokoto() {
return NetworkUtil.getHtml(HITOKOTO_API); return NetworkUtil.getHtml(HITOKOTO_API);
} }
@ -24,18 +21,15 @@ public class Hitokoto {
new Thread(() -> { new Thread(() -> {
//hitokotoJSON = getHitokoto(); String hitokotoJSON = null;
String hitokoto, from, author, creator, type;
if ((hitokotoJSON = getHitokoto()) != null) { if ((hitokotoJSON = getHitokoto()) != null) {
HitokotoData hitokotoData = new Gson().fromJson(hitokotoJSON, HitokotoData.class); HitokotoData hitokotoData = new Gson().fromJson(hitokotoJSON, HitokotoData.class);
String hitokoto = hitokotoData.getHitokoto(),
hitokoto = hitokotoData.getHitokoto(); from = hitokotoData.getFrom(),
from = hitokotoData.getFrom(); author = hitokotoData.getAuthor(),
author = hitokotoData.getAuthor(); creator = hitokotoData.getCreator(),
creator = hitokotoData.getCreator(); type = hitokotoData.getType();
type = hitokotoData.getType();
Platform.runLater(() -> { Platform.runLater(() -> {
topBar.setText(String.format("《%s》:%s (%s)", from, hitokoto, author)); topBar.setText(String.format("《%s》:%s (%s)", from, hitokoto, author));
@ -45,7 +39,7 @@ public class Hitokoto {
} }
}); });
} }
}).start(); },"HitokotoThread").start();
} }
static class HitokotoData { static class HitokotoData {

@ -12,12 +12,12 @@ public final class Worker {
private final Random randomNumber = new Random(); private final Random randomNumber = new Random();
private final SimpleBooleanProperty stoppedIndicator = new SimpleBooleanProperty(true); private final SimpleBooleanProperty stoppedIndicator = new SimpleBooleanProperty(true);
private final MainConfig config; private final MainConfig config;
//挑选方法
private final int selectMethod = MainConfig.METHOD_NAME;
private final Data data; private final Data data;
private final History history; private final History history;
private final VoicePlayer voicePlayer; private final VoicePlayer voicePlayer;
//数值范围最大最小值 //数值范围最大最小值
private final int[] numberRange = new int[2]; private final int[] numberRange = new int[2];
private final int MIN_NUMBER = 0; private final int MIN_NUMBER = 0;
@ -25,9 +25,11 @@ public final class Worker {
private final Counter counter = new Counter(); private final Counter counter = new Counter();
private StringProperty[] labelTexts; private StringProperty[] labelTexts;
private int speed = 0; private int speed = 0;
//挑选次数和每一轮的挑选次数 //挑选次数和每一轮的挑选次数
private int maxTotalCount = MainConfig.DEFAULT_MAX_TOTAL_COUNT; private int maxTotalCount = MainConfig.DEFAULT_MAX_TOTAL_COUNT;
private int maxCycleCount = 0; private int maxCycleCount = 0;
//已经挑选了多少次 //已经挑选了多少次
private int totalCount = 0; private int totalCount = 0;
private int cycleCount = 0; private int cycleCount = 0;
@ -165,7 +167,7 @@ public final class Worker {
private int newResultLabelId = 0; private int newResultLabelId = 0;
protected void count() { private void count() {
newResultLabelId = resultLabelId; newResultLabelId = resultLabelId;
// 每一轮的计数到达最大时并且不强制继续这轮挑选时,重置这一轮的挑选,并指派新的显示label // 每一轮的计数到达最大时并且不强制继续这轮挑选时,重置这一轮的挑选,并指派新的显示label
@ -181,7 +183,7 @@ public final class Worker {
totalCount++; totalCount++;
} }
protected void resetCounter() { private void resetCounter() {
totalCount = 0; totalCount = 0;
cycleCount = 0; cycleCount = 0;
maxCycleCount = 0; maxCycleCount = 0;
@ -189,7 +191,7 @@ public final class Worker {
continueSelecting = false; continueSelecting = false;
} }
protected int getNewResultLabelId() { private int getNewResultLabelId() {
return newResultLabelId; return newResultLabelId;
} }
} }

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?> <?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.*?> <?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.layout.*?> <?import com.jfoenix.controls.JFXTextField?>
<?import com.jfoenix.controls.*?>
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.*?> <?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="395.0" prefWidth="287.0" spacing="5.0" type="VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="409.0" prefWidth="339.0" spacing="5.0" type="VBox" xmlns="http://javafx.com/javafx/8.0.321" xmlns:fx="http://javafx.com/fxml/1">
<children> <children>
<JFXListView fx:id="historyList" prefHeight="270.0" prefWidth="301.0" /> <JFXListView fx:id="historyList" prefHeight="270.0" prefWidth="301.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="历史记录超过2000条时会自动清空列表重新记录"> <Text strokeType="OUTSIDE" strokeWidth="0.0" text="历史记录超过2000条时会自动清空列表重新记录">
@ -17,7 +17,12 @@
<Font name="Microsoft YaHei" size="12.0" /> <Font name="Microsoft YaHei" size="12.0" />
</font> </font>
</Text> </Text>
<JFXTextField fx:id="searchBar" promptText="查找..."> <Text fx:id="searchMessage" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="312.240234375">
<font>
<Font name="Microsoft YaHei" size="12.0" />
</font>
</Text>
<JFXTextField fx:id="searchBar" promptText="输入查找内容...">
<font> <font>
<Font name="Microsoft YaHei" size="14.0" /> <Font name="Microsoft YaHei" size="14.0" />
</font> </font>

Loading…
Cancel
Save