ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.vci.client.ui.process;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
 
 
public class QANProcessBar extends JDialog implements Runnable {
    
 
    /** 
    * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
    */ 
    
    private static final long serialVersionUID = 7120183499441604220L;
    public static JFrame frame = new JFrame();
    private Thread thread = null;
    private String title = null;
    private String content = null;
    private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);
    private JLabel detailLabel = new JLabel();
    private JButton startButton = new JButton("开始");
    private JButton cancelButton = new JButton("取消");
    private ProcessBarInterface pbi = null;
    private Thread taskThread = null;
    private boolean enableCancelButton = false;
 
    public QANProcessBar(Thread thread, JFrame frame, ProcessBarInterface pbi, boolean enableCancelButton) {
        super(frame);
        this.thread = thread;
        this.title = pbi.getProcessBarTitle();
        this.content = pbi.getProcessBarContent();
        this.pbi = pbi;
        this.enableCancelButton = enableCancelButton;
        init();
        startTread();
    }
    
    public QANProcessBar(Thread thread, JFrame frame, ProcessBarInterface pbi,String title, boolean enableCancelButton) {
        super(frame);
        this.thread = thread;
        this.title = title;
        this.content = pbi.getProcessBarContent();
        this.pbi = pbi;
        this.enableCancelButton = enableCancelButton;
        init();
        startTread();
    }
    
    public QANProcessBar(Thread thread, JDialog dialog, ProcessBarInterface pbi, boolean enableCancelButton) {
        super(dialog);
        this.thread = thread;
        this.title = pbi.getProcessBarTitle();
        this.content = pbi.getProcessBarContent();
        this.pbi = pbi;
        this.enableCancelButton = enableCancelButton;
        init();
        startTread();
    }
    
    public void init() {
        this.setTitle(title);
        this.setSize(new Dimension(pbi.getProcessBarDialogWidth(),pbi.getProcessBarDialogHeight()));
        this.setLayout(new BorderLayout());
        this.setResizable(false);
        this.setModal(true);
        this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-400)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-100)/2);
        
        detailLabel.setFont(pbi.getContentFont());
        detailLabel.setForeground(pbi.getContentColor());
        detailLabel.setPreferredSize(new Dimension(550,25));
        detailLabel.setText(content);
        
        progressBar.setForeground(Color.green);
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(550, 25));
        topPanel.setLayout(new BoxLayout(topPanel,BoxLayout.Y_AXIS));
        topPanel.add(progressBar);
        
        startButton.setVisible(pbi.isShowStartButton());
        cancelButton.setEnabled(this.enableCancelButton);
        cancelButton.setVisible(pbi.isShowCancelButton());
 
        cancelButton.addActionListener(new ActionListener(){
 
            public void actionPerformed(ActionEvent e) {
                cancelButtonActionListener();
            }});
        
        this.setLayout(new GridBagLayout());
        this.add(topPanel, new GridBagConstraints(0, 0, 3, 1, 0.0, 1.0
                ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 10, 0, 0), 0, 0));
        
        this.add(detailLabel, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0
                ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0));
        
        this.add(startButton, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
                ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
        
        this.add(new JLabel(""), new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0
                ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0));
        
        this.add(cancelButton, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
                ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0));
 
        this.add(new JLabel(), new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0
                ,GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(10, 10, 0, 0), 0, 0));
        
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }
    
    private void cancelButtonActionListener() {
        pbi.setProcessBarCancel(true);
    }
    
    public void run() {
        while(!pbi.getProcessBarCancel()) {
            if (pbi.isProcessSure()) {
                progressBar.setValue(pbi.getProcessBarValue());
                detailLabel.setText(pbi.getProcessBarContent());
            } else {
                progressBar.setIndeterminate(true);
                detailLabel.setText(pbi.getProcessBarContent());
            }
        }
        this.dispose();
    }
    
    public void startTread() {
        Thread t = new Thread(this);
        t.start();
 
        taskThread = new Thread(){
            public void run() {
                try {
                    thread.start();
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
        };
        taskThread.start();
    }
}