MATSIM
AsyncFileInputProgressDialog.java
Go to the documentation of this file.
1 
2 /* *********************************************************************** *
3  * project: org.matsim.*
4  * AsyncFileInputProgressDialog.java
5  * *
6  * *********************************************************************** *
7  * *
8  * copyright : (C) 2019 by the members listed in the COPYING, *
9  * LICENSE and WARRANTY file. *
10  * email : info at matsim dot org *
11  * *
12  * *********************************************************************** *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * See also COPYING, LICENSE and WARRANTY file *
19  * *
20  * *********************************************************************** */
21 
22 package org.matsim.run.gui;
23 
24 import java.awt.Dimension;
25 import java.awt.Toolkit;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.nio.channels.FileChannel;
29 
30 import javax.swing.GroupLayout;
31 import javax.swing.GroupLayout.Alignment;
32 import javax.swing.JDialog;
33 import javax.swing.JProgressBar;
34 import javax.swing.SwingUtilities;
35 
36 import org.apache.logging.log4j.LogManager;
37 import org.apache.logging.log4j.Logger;
38 
45 /*package*/ class AsyncFileInputProgressDialog extends JDialog {
46 
47  private final static Logger log = LogManager.getLogger(AsyncFileInputProgressDialog.class);
48 
49  private static final long serialVersionUID = 1L;
50 
51  final JProgressBar progressbar;
52 
53  public AsyncFileInputProgressDialog() {
54  this("Operation in Progress…");
55  }
56 
57  public AsyncFileInputProgressDialog(final String title) {
58  setTitle(title);
59  progressbar = new JProgressBar(0, 1000);
60  GroupLayout groupLayout = new GroupLayout(getContentPane());
61  groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
62  .addGroup(groupLayout.createSequentialGroup()
63  .addContainerGap()
64  .addComponent(progressbar, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
65  .addContainerGap()));
66  groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
67  .addGroup(groupLayout.createSequentialGroup()
68  .addContainerGap()
69  .addComponent(progressbar)
70  .addContainerGap()));
71  getContentPane().setLayout(groupLayout);
72 
73  this.setModal(false);
74  this.setResizable(false);
75  this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
76  this.pack();
77 
78  // center on screen
79  Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
80  int w = this.getSize().width;
81  int h = this.getSize().height;
82  int x = (dim.width - w) / 2;
83  int y = (dim.height - h) / 2;
84  this.setLocation(x, y);
85 
86  this.setVisible(true);
87  }
88 
89  // safe to call from outside the event dispatch thread
90  public void observeProgress(FileInputStream fis) {
91  Thread t = new Thread(() -> {
92  FileChannel ch = fis.getChannel();
93  while (ch.isOpen()) {
94  try {
95  long size = ch.size();
96  long pos = ch.position();
97  final int progress = (int)((((double)pos) / ((double)size)) * 1000.0);
98  SwingUtilities.invokeLater(() -> progressbar.setValue(progress));
99  Thread.sleep(250);
100  } catch (InterruptedException | IOException e) {
101  log.error(e.getMessage(), e);
102  }
103  }
104  }, "ProgressObserver");
105  t.setDaemon(true);
106  t.start();
107  }
108 }