1+ package i18nupdatemod .core ;
2+
3+ import i18nupdatemod .I18nUpdateMod ;
4+ import i18nupdatemod .entity .LoadStage ;
5+
6+ import javax .swing .*;
7+ import java .awt .*;
8+ import java .awt .event .ActionEvent ;
9+ import java .awt .event .WindowAdapter ;
10+ import java .awt .event .WindowEvent ;
11+ import java .net .URL ;
12+
13+ public class LoadDetailUI {
14+ private static volatile LoadDetailUI instance ;
15+ private final JFrame frame ;
16+ private final JProgressBar statusBar ;
17+ private final JTextArea logArea ;
18+ private final boolean useGUI ;
19+
20+ private LoadDetailUI () {
21+ useGUI = Boolean .parseBoolean (System .getProperty ("java.awt.headless" ));
22+
23+ frame = new JFrame ();
24+ frame .setTitle ("I18nUpdateMod-资源包下载进度" );
25+ frame .setDefaultCloseOperation (JFrame .DO_NOTHING_ON_CLOSE );
26+ frame .setSize (854 , 480 );
27+ frame .setLocationRelativeTo (null );
28+ frame .setLayout (new BorderLayout ());
29+ frame .addWindowListener (new WindowAdapter () {
30+ @ Override
31+ public void windowClosing (WindowEvent e ) {
32+ shutdown ();
33+ }
34+ });
35+
36+
37+ URL iconURL = getClass ().getResource ("/icons/CFPA.png" );
38+ if (iconURL != null ) {
39+ Image icon = Toolkit .getDefaultToolkit ().getImage (iconURL );
40+ frame .setIconImage (icon );
41+ }
42+
43+ // 主面板
44+ JPanel panel = new JPanel ();
45+ panel .setBackground (new Color (220 , 220 , 220 ));
46+ panel .setLayout (new BoxLayout (panel , BoxLayout .Y_AXIS ));
47+ panel .setBorder (BorderFactory .createEmptyBorder (15 , 15 , 15 , 15 ));
48+
49+ // 状态栏
50+ statusBar = new JProgressBar ();
51+ statusBar .setString ("正在合并资源包" );
52+ statusBar .setStringPainted (true );
53+ statusBar .setMaximum (LoadStage .values ().length - 1 );
54+ statusBar .setValue (100 );
55+ statusBar .setForeground (new Color (102 , 255 , 102 ));
56+ panel .add (statusBar );
57+ panel .add (Box .createVerticalStrut (10 ));
58+
59+ // 日志输出区
60+ logArea = new JTextArea (6 , 30 );
61+ logArea .setEditable (false );
62+ logArea .setFont (new Font ("Monospaced" , Font .PLAIN , 13 ));
63+ JScrollPane scrollPane = new JScrollPane (logArea );
64+ scrollPane .setBorder (BorderFactory .createLineBorder (Color .GRAY ));
65+ panel .add (scrollPane );
66+ panel .add (Box .createVerticalStrut (10 ));
67+
68+ // 提示文字
69+ JLabel tip = new JLabel ("如遇到进度卡住,可以点击下方按钮/关闭此窗口停止此次下载。" );
70+ tip .setAlignmentX (Component .CENTER_ALIGNMENT );
71+ panel .add (tip );
72+ panel .add (Box .createVerticalStrut (10 ));
73+
74+ // 停止按钮
75+ JButton stopButton = new JButton ("停止此次下载" );
76+ stopButton .setFocusPainted (false );
77+ stopButton .setBackground (Color .WHITE );
78+ stopButton .setAlignmentX (Component .CENTER_ALIGNMENT );
79+ stopButton .addActionListener ((ActionEvent e ) -> shutdown ());
80+ panel .add (stopButton );
81+
82+ frame .add (panel , BorderLayout .CENTER );
83+ }
84+
85+ public static LoadDetailUI getInstance () {
86+ if (instance == null ) {
87+ synchronized (LoadDetailUI .class ) {
88+ if (instance == null ) {
89+ instance = new LoadDetailUI ();
90+ }
91+ }
92+ }
93+ return instance ;
94+ }
95+
96+ public static void show () {
97+ getInstance ().frame .setVisible (true );
98+ }
99+
100+ public static void hide () {
101+ getInstance ().frame .setVisible (false );
102+ }
103+
104+ private void shutdown (){
105+ hide ();
106+ I18nUpdateMod .shouldShutdown = true ;
107+ }
108+
109+ public static void setStage (LoadStage stage ) {
110+ SwingUtilities .invokeLater (() -> {
111+ LoadDetailUI gui = getInstance ();
112+ gui .statusBar .setString (LoadStage .getDescription (stage ));
113+ gui .statusBar .setValue (stage .getValue ());
114+ gui .logArea .append ("当前阶段: " + LoadStage .getDescription (stage ) + "\n " );
115+ gui .logArea .setCaretPosition (gui .logArea .getDocument ().getLength ());
116+ });
117+ }
118+
119+ public static void appendLog (String log ) {
120+ SwingUtilities .invokeLater (() -> {
121+ LoadDetailUI gui = getInstance ();
122+ gui .logArea .append (log + "\n " );
123+ gui .logArea .setCaretPosition (gui .logArea .getDocument ().getLength ());
124+ });
125+ }
126+ }
0 commit comments