dangsn
2024-12-26 4e9ff2ce6a830bb2340d7c8612c72eea0c5a553e
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.vci.client.logon.client;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.Timer;
 
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.plaf.basic.BasicStatusBarUI;
 
import com.vci.client.common.ConfigUtils;
import com.vci.client.framework.util.RightControlUtil;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.components.VCIJButton;
import com.vci.client.ui.swing.components.VCIJDialog;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.table.AbstractVCIJTableDataProvider;
import com.vci.client.ui.swing.components.table.VCIJTableNode;
import com.vci.client.ui.swing.components.table.VCIJTablePanel;
import com.vci.mw.ClientContextVariable;
 
public class StatusBar extends JXStatusBar implements PropertyChangeListener{
 
    /**
     * 
     */
    private static final long serialVersionUID = -8839998684796112327L;
 
    private static StatusBar _instance = null;
    public static StatusBar getInstance(){
        if(_instance == null){
            synchronized(StatusBar.class){
                if(_instance == null){
                    _instance = new StatusBar();
                }
            }
        }
        return _instance;
    }
    
    private VCIJLabel lblStatus = new VCIJLabel();
    private VCIJLabel lightIcon = new VCIJLabel();
    private JProgressBar progressBar = new JProgressBar(0, 100);
    private VCIJLabel lblDate = new VCIJLabel();
    private StatusBar(){
        super();
        init();
    }
    
    private void init(){
        boolean autoAddSeparator = true;
        boolean isWindowPlatform = System.getProperty("os.name").toLowerCase().contains("windows");
        if(isWindowPlatform){
            double osVersion = Double.valueOf(System.getProperty("os.version"));
            if(osVersion >= 6){
                autoAddSeparator = false;
            }
        }
        putClientProperty(BasicStatusBarUI.AUTO_ADD_SEPARATOR, autoAddSeparator);
        lightIcon.setIcon(VCISwingUtil.createImageIcon("lightbulb.png"));
        Insets insets = new Insets(0, 5, 0, 5);
        Constraint c1 = new Constraint(insets);
        c1.setFixedWidth(32);
        add(lightIcon, c1);
        
        lblStatus.setForeground(Color.BLUE);
        Constraint c2 = new Constraint(insets);
        c2.setFixedWidth(400);
        add(lblStatus, c2);
        lblStatus.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        lblStatus.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON1){
                    showHistoryMessage();
                }
            }
        });
        
        Constraint c3 = new Constraint(JXStatusBar.Constraint.ResizeBehavior.FILL, insets);
        progressBar.setStringPainted(true);
        progressBar.setValue(100);
        add(progressBar, c3);
        
        Constraint c4 = new Constraint(insets);
        c4.setFixedWidth(200);
        String displayTime = ConfigUtils.getConfigValue("statusbar.display.time");
        if (displayTime != null && displayTime.equalsIgnoreCase("true")) {
            lblDate.setText(getNowDateString());
            timerNowDate.start();
            add(lblDate, c4);
        }
        
        Constraint c5 = new Constraint(insets);
        c5.setFixedWidth(200);
        add(new VCIJLabel(""), c5);
    }
    
    private Timer timerNowDate = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            lblDate.setText(getNowDateString());
        }
    });
    
    
    private String getNowDateString(){
        String res = "";
        try{
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            res = sdf.format(cal.getTime()) + " " + getDayOfWeekString(dayOfWeek);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return res;
    }
    
    private String getDayOfWeekString(int dayOfWeek){
        String res = "";
        if(dayOfWeek == Calendar.MONDAY) res = "星期一";
        else if(dayOfWeek == Calendar.TUESDAY) res = "星期二";
        else if(dayOfWeek == Calendar.WEDNESDAY) res = "星期三";
        else if(dayOfWeek == Calendar.THURSDAY) res = "星期四";
        else if(dayOfWeek == Calendar.FRIDAY) res = "星期五";
        else if(dayOfWeek == Calendar.SATURDAY) res = "星期六";
        else if(dayOfWeek == Calendar.SUNDAY) res = "星期日";
        return res;
    }
    
    private Timer timer = new Timer(5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            lblStatus.setText("点击查看更多历史消息!");
        }
    });
    /**
     * 设置状态栏消息内容
     * @param message 消息内容 
     * @param toHistory 是否需要将消息内容追加到HistoryTable列表中,true:追加 false:追加
     */
    public void setStatusMessage(String message, boolean toHistory){
        if(message == null) return;
        histMsgList.clear();
        if(toHistory && !"".equals(message.trim())){
            histMsgList.add(new HistoryMessage(getNowDateString(), message));
        }
        lblStatus.setText(message);
        timer.setRepeats(false);
        timer.start();
    }
    /**
     * 设置状态栏消息内容,默认将内容添加HistoryTable列表中,以便延迟查看
     * @param message 消息内容
     */
    public void setStatusMessage(String message){
        setStatusMessage(message, true);
    }
 
    public void clearStatusMessage(){
        histMsgList.clear();
        lblStatus.setText("");
        tablePanel.refreshTableData();
    }
    
    private void showHistoryMessage(){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                showHistoryMessageDialog();
            }
        });
    }
    
    VCIJDialog dialogHistMsg = null;
    private void showHistoryMessageDialog(){
        Frame frame = ClientContextVariable.getFrame();
        if(dialogHistMsg == null){
            dialogHistMsg = new VCIJDialog(frame);
            dialogHistMsg.setModal(true);;
            dialogHistMsg.setTitle("消息列表");
            dialogHistMsg.setSize(800, 600);
            if(tablePanel == null){
                tablePanel = getHistoryMessageDataPanel();
            }
            VCIJPanel pal = new VCIJPanel(new BorderLayout());
            pal.add(tablePanel, BorderLayout.CENTER);
            pal.add(getDialogSouthButtonPanel(), BorderLayout.SOUTH);
            dialogHistMsg.getContentPane().add(pal);
        }
        tablePanel.refreshTableData();
        dialogHistMsg.setLocationRelativeTo(frame);
        dialogHistMsg.setVisible(true);
    }
    
    private VCIJPanel getDialogSouthButtonPanel(){
        VCIJPanel pal = new VCIJPanel();
        VCIJButton btnClose = VCISwingUtil.createVCIJButton("close", "关闭", "关闭", "close.png", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(dialogHistMsg != null){
                    dialogHistMsg.setVisible(false);
                }
            }
        });
        VCIJButton btnClear = VCISwingUtil.createVCIJButton("clearn", "清空", "清空", "clear.gif", new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearStatusMessage();
            }
        });
        pal.add(btnClear);
        pal.add(btnClose);
        return pal;
    }
    
    private VCIJTablePanel<HistoryMessage> tablePanel = null;
    private VCIJTablePanel<HistoryMessage> getHistoryMessageDataPanel(){
        HistoryMessageDataProvider dataProvider = new HistoryMessageDataProvider();
        tablePanel = new VCIJTablePanel<StatusBar.HistoryMessage>(dataProvider);
        tablePanel.setShowExport(false);
        tablePanel.setShowPaging(false);
        tablePanel.setShowProgressBar(false);
        LinkedHashMap<Integer, Integer> columnWidthMap = new LinkedHashMap<Integer, Integer>();
        int index = dataProvider.getDataColumnStartIndex();
        columnWidthMap.put(index++, 200);
        columnWidthMap.put(index++, 450);
        tablePanel.setColumnDefaultWidthMax(1000);
        tablePanel.setColumnWidthMaps(columnWidthMap);
        tablePanel.buildTablePanel();
        tablePanel.refreshTableData();
        return tablePanel;
    }
    
    private class HistoryMessageDataProvider extends AbstractVCIJTableDataProvider<HistoryMessage>{
        @Override
        public String[] getSpecialColumns() {
            return "时间,消息内容".split(",");
        }
 
        @Override
        public int getTotal() {
            return histMsgList.size();
        }
 
        @Override
        public VCIJTableNode<HistoryMessage> getNewRowNode(
                HistoryMessage dataObj) {
            VCIJTableNode<HistoryMessage> tableNode = new VCIJTableNode<StatusBar.HistoryMessage>(dataObj);
            int index = 0;
            String[] names = getSpecialColumns();
            tableNode.setPropertyValue(names[index++], dataObj.getAddTime());
            tableNode.setPropertyValue(names[index++], dataObj.getMessageContent());
            return tableNode;
        }
 
        @Override
        public HistoryMessage[] getDatas(int pageIndex, int pageSize) {
            return histMsgList.toArray(new HistoryMessage[]{});
        }
    }
    
    private LinkedList<HistoryMessage> histMsgList = new LinkedList<StatusBar.HistoryMessage>();
    private class HistoryMessage{
        private String addTime = "";
        private String messageContent = "";
        public HistoryMessage(String addTime, String messageContent) {
            super();
            this.addTime = addTime;
            this.messageContent = messageContent;
        }
        public String getAddTime() {
            return addTime;
        }
        public void setAddTime(String addTime) {
            this.addTime = addTime;
        }
        public String getMessageContent() {
            return messageContent;
        }
        public void setMessageContent(String messageContent) {
            this.messageContent = messageContent;
        }
    }
    
    public void launchFinished(String message){
        setStatusMessage(message);
        setProgressBarVisiable(false, 3);
    }
    public void setProgressBarVisiable(final boolean visiable){
        setProgressBarVisiable(visiable, 3);
    }
    public void setProgressBarVisiable(final boolean visiable, final int lazySeconds){
        new ScheduledThreadPoolExecutor(10).schedule(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                progressBar.setVisible(visiable);
                return null;
            }
        }, lazySeconds, TimeUnit.SECONDS);
    }
    
    
    @SuppressWarnings("rawtypes")
    public void launchModuleAndShowProgressBar(SwingWorker worker,
            boolean indeterminate){
        launchModuleAndShowProgressBar(worker, indeterminate, true);
    }
    @SuppressWarnings("rawtypes")
    public void launchModuleAndShowProgressBar(SwingWorker worker,
            boolean indeterminate, boolean addProgressPropertyChange){
        setProgressBarVisiable(true, 0);
        setStatusMessage("", false);
        // 进度条确定&非确定模式
        this.progressBar.setIndeterminate(indeterminate);
        if(addProgressPropertyChange){
            worker.addPropertyChangeListener(this);
        }
        worker.execute();
    }
    
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            Object newValue = evt.getNewValue();
            if(newValue instanceof Integer){
                int progress = (Integer)newValue;
                this.progressBar.setValue(progress);
            }
        }
    }
    public void setProgressBarValue(int value){
        this.progressBar.setValue(value);
    }
}