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. 92
      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. 17
      Dogename/src/main/java/me/lensferno/dogename/sayings/Gushici.java
  10. 18
      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$">
<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/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

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

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

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

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

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

@ -80,7 +80,6 @@ public class Data {
sb.append(temp);
sb.append("\n");
}
nameList = new Gson().fromJson(sb.toString(), List.class);
System.out.println("Imported list from:" + path.getPath());
} catch (Exception e) {
@ -105,7 +104,6 @@ public class Data {
}
nameList.clear();
nameList.addAll(tempList);
}
public void add(String text) {
@ -246,7 +244,7 @@ public class Data {
public void readIgnoreList() {
readNameIgnoreList();
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() {

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

@ -13,8 +13,6 @@ public class Gushici {
private final String GUSHICI_API = "https://v1.jinrishici.com/all.json";
String gushiciJSON = null;
private String getGushici() {
return NetworkUtil.getHtml(GUSHICI_API);
}
@ -23,18 +21,17 @@ public class Gushici {
new Thread(() -> {
//gushiciJSON=getGushici();
String content, title, author, type;
String gushiciJSON = null;
if ((gushiciJSON = getGushici()) != null) {
GushiciData gushiciData = new Gson().fromJson(gushiciJSON, GushiciData.class);
content = gushiciData.getContent();
title = gushiciData.getTitle();
author = gushiciData.getAuthor();
String content = gushiciData.getContent(),
title = gushiciData.getTitle(),
author = gushiciData.getAuthor(),
type = gushiciData.getType();
Platform.runLater(() -> {
topBar.setText(content + " ——" + author + "《" + title + "》");
topBar.setText(String.format("%s ——%s 《%s》", content, author, title));
if (showOnDialog) {
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 {
private final String HITOKOTO_API = "https://v1.hitokoto.cn/";
String hitokotoJSON = null;
private String getHitokoto() {
return NetworkUtil.getHtml(HITOKOTO_API);
}
@ -24,17 +21,14 @@ public class Hitokoto {
new Thread(() -> {
//hitokotoJSON = getHitokoto();
String hitokoto, from, author, creator, type;
String hitokotoJSON = null;
if ((hitokotoJSON = getHitokoto()) != null) {
HitokotoData hitokotoData = new Gson().fromJson(hitokotoJSON, HitokotoData.class);
hitokoto = hitokotoData.getHitokoto();
from = hitokotoData.getFrom();
author = hitokotoData.getAuthor();
creator = hitokotoData.getCreator();
String hitokoto = hitokotoData.getHitokoto(),
from = hitokotoData.getFrom(),
author = hitokotoData.getAuthor(),
creator = hitokotoData.getCreator(),
type = hitokotoData.getType();
Platform.runLater(() -> {
@ -45,7 +39,7 @@ public class Hitokoto {
}
});
}
}).start();
},"HitokotoThread").start();
}
static class HitokotoData {

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

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import com.jfoenix.controls.*?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.HBox?>
<?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>
<JFXListView fx:id="historyList" prefHeight="270.0" prefWidth="301.0" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="历史记录超过2000条时会自动清空列表重新记录">
@ -17,7 +17,12 @@
<Font name="Microsoft YaHei" size="12.0" />
</font>
</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 name="Microsoft YaHei" size="14.0" />
</font>

Loading…
Cancel
Save