implement progress bar for swing application
git-svn-id: https://osmand.googlecode.com/svn/trunk@49 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
parent
2a94d0beb1
commit
acd99e37e6
9 changed files with 376 additions and 58 deletions
|
@ -11,6 +11,8 @@ import java.util.List;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
import javax.xml.stream.XMLStreamException;
|
import javax.xml.stream.XMLStreamException;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.tools.bzip2.CBZip2InputStream;
|
import org.apache.tools.bzip2.CBZip2InputStream;
|
||||||
import org.apache.tools.bzip2.CBZip2OutputStream;
|
import org.apache.tools.bzip2.CBZip2OutputStream;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
@ -68,6 +70,7 @@ import com.osmand.swing.OsmExtractionUI;
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class DataExtraction {
|
public class DataExtraction {
|
||||||
|
private static final Log log = LogFactory.getLog(DataExtraction.class);
|
||||||
|
|
||||||
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XMLStreamException {
|
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XMLStreamException {
|
||||||
new DataExtraction().testReadingOsmFile();
|
new DataExtraction().testReadingOsmFile();
|
||||||
|
@ -95,7 +98,7 @@ public class DataExtraction {
|
||||||
|
|
||||||
Region country;
|
Region country;
|
||||||
if(parseOSM){
|
if(parseOSM){
|
||||||
country = readCountry(f);
|
country = readCountry(f, new ConsoleProgressImplementation());
|
||||||
} else {
|
} else {
|
||||||
country = new Region(null);
|
country = new Region(null);
|
||||||
country.setStorage(new OsmBaseStorage());
|
country.setStorage(new OsmBaseStorage());
|
||||||
|
@ -129,8 +132,9 @@ public class DataExtraction {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Region readCountry(String path) throws IOException, SAXException{
|
public Region readCountry(String path, IProgress progress) throws IOException, SAXException{
|
||||||
InputStream stream = new FileInputStream(path);
|
InputStream stream = new FileInputStream(path);
|
||||||
|
InputStream streamFile = stream;
|
||||||
long st = System.currentTimeMillis();
|
long st = System.currentTimeMillis();
|
||||||
if(path.endsWith(".bz2")){
|
if(path.endsWith(".bz2")){
|
||||||
if (stream.read() != 'B' || stream.read() != 'Z')
|
if (stream.read() != 'B' || stream.read() != 'Z')
|
||||||
|
@ -140,6 +144,9 @@ public class DataExtraction {
|
||||||
stream = new CBZip2InputStream(stream);
|
stream = new CBZip2InputStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(progress != null){
|
||||||
|
progress.startTask("Loading file " + path, -1);
|
||||||
|
}
|
||||||
|
|
||||||
// preloaded data
|
// preloaded data
|
||||||
places = new ArrayList<Node>();
|
places = new ArrayList<Node>();
|
||||||
|
@ -188,8 +195,10 @@ public class DataExtraction {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
storage.parseOSM(stream, new ConsoleProgressImplementation());
|
storage.parseOSM(stream, progress, streamFile);
|
||||||
System.out.println("File parsed : " +(System.currentTimeMillis() - st));
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("File parsed : " + (System.currentTimeMillis() - st));
|
||||||
|
}
|
||||||
|
|
||||||
// 1. found towns !
|
// 1. found towns !
|
||||||
Region country = new Region(null);
|
Region country = new Region(null);
|
||||||
|
|
|
@ -13,7 +13,9 @@ public abstract class DefaultLauncherConstants {
|
||||||
|
|
||||||
public static String pathToOsmFile = pathToTestDataDir + "minsk.osm";
|
public static String pathToOsmFile = pathToTestDataDir + "minsk.osm";
|
||||||
public static String pathToOsmBz2File = pathToTestDataDir + "belarus_2010_04_01.osm.bz2";
|
public static String pathToOsmBz2File = pathToTestDataDir + "belarus_2010_04_01.osm.bz2";
|
||||||
public static String pathToDirWithTiles = pathToTestDataDir +"MinskTiles";
|
|
||||||
|
public static String pathToWorkingDir = pathToTestDataDir +"osmand\\";
|
||||||
|
public static String pathToDirWithTiles = pathToWorkingDir +"tiles";
|
||||||
|
|
||||||
public static String writeTestOsmFile = "C:\\1_tmp.osm"; // could be null - wo writing
|
public static String writeTestOsmFile = "C:\\1_tmp.osm"; // could be null - wo writing
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,15 @@ public class ToDoConstants {
|
||||||
// 5. Fix Java Spring to prepare your data!!! (add progress, import/export data)
|
// 5. Fix Java Spring to prepare your data!!! (add progress, import/export data)
|
||||||
// 6. Enable city/streets/buildings index
|
// 6. Enable city/streets/buildings index
|
||||||
// 7. Search for city/streets/buildings!
|
// 7. Search for city/streets/buildings!
|
||||||
|
// 8. Enable change POI directly on map
|
||||||
// -------------------
|
// -------------------
|
||||||
|
|
||||||
|
|
||||||
|
/// SWING version :
|
||||||
|
// TODO :
|
||||||
|
// 1. save preferences ? where?
|
||||||
|
// 2. specify area to download tiles ()
|
||||||
|
// 3. implement mouse wheel to zoom (add zoom buttons)
|
||||||
|
// 4. download tiles without using dir tiles
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,7 @@ public class Amenity {
|
||||||
prebuiltMap.put("dentist", AmenityType.HEALTHCARE);
|
prebuiltMap.put("dentist", AmenityType.HEALTHCARE);
|
||||||
prebuiltMap.put("doctors", AmenityType.HEALTHCARE);
|
prebuiltMap.put("doctors", AmenityType.HEALTHCARE);
|
||||||
prebuiltMap.put("veterinary", AmenityType.HEALTHCARE);
|
prebuiltMap.put("veterinary", AmenityType.HEALTHCARE);
|
||||||
|
prebuiltMap.put("first_aid", AmenityType.HEALTHCARE);
|
||||||
|
|
||||||
prebuiltMap.put("architect_office", AmenityType.ENTERTAINMENT);
|
prebuiltMap.put("architect_office", AmenityType.ENTERTAINMENT);
|
||||||
prebuiltMap.put("arts_centre", AmenityType.ENTERTAINMENT);
|
prebuiltMap.put("arts_centre", AmenityType.ENTERTAINMENT);
|
||||||
|
|
|
@ -52,22 +52,22 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
// this is used to show feedback to user
|
// this is used to show feedback to user
|
||||||
private IProgress progress;
|
private IProgress progress;
|
||||||
private InputStream inputStream;
|
private InputStream inputStream;
|
||||||
|
private InputStream streamForProgress;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException {
|
||||||
* @param stream
|
|
||||||
* @throws IOException
|
|
||||||
* @throws SAXException - could be
|
|
||||||
*/
|
|
||||||
public synchronized void parseOSM(InputStream stream, IProgress progress) throws IOException, SAXException {
|
|
||||||
this.inputStream = stream;
|
this.inputStream = stream;
|
||||||
this.progress = progress;
|
this.progress = progress;
|
||||||
|
this.streamForProgress = streamForProgress;
|
||||||
|
if(streamForProgress == null){
|
||||||
|
streamForProgress = inputStream;
|
||||||
|
}
|
||||||
SAXParser parser = initSaxParser();
|
SAXParser parser = initSaxParser();
|
||||||
parseStarted = false;
|
parseStarted = false;
|
||||||
entities.clear();
|
entities.clear();
|
||||||
if(progress != null){
|
if(progress != null){
|
||||||
progress.startWork(stream.available());
|
progress.startWork(streamForProgress.available());
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.parse(stream, this);
|
parser.parse(stream, this);
|
||||||
|
@ -77,6 +77,16 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
completeReading();
|
completeReading();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param stream
|
||||||
|
* @throws IOException
|
||||||
|
* @throws SAXException - could be
|
||||||
|
*/
|
||||||
|
public synchronized void parseOSM(InputStream stream, IProgress progress) throws IOException, SAXException {
|
||||||
|
parseOSM(stream, progress, null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private SAXParser saxParser;
|
private SAXParser saxParser;
|
||||||
public SAXParser initSaxParser(){
|
public SAXParser initSaxParser(){
|
||||||
if(saxParser != null){
|
if(saxParser != null){
|
||||||
|
@ -130,10 +140,10 @@ public class OsmBaseStorage extends DefaultHandler {
|
||||||
}
|
}
|
||||||
parseStarted = true;
|
parseStarted = true;
|
||||||
}
|
}
|
||||||
if (currentParsedEntity == null) {
|
if (currentParsedEntity == null && streamForProgress != null) {
|
||||||
if(progress != null && !progress.isIndeterminate()){
|
if(progress != null && !progress.isIndeterminate()){
|
||||||
try {
|
try {
|
||||||
progress.remaining(inputStream.available());
|
progress.remaining(streamForProgress.available());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
progress.startWork(-1);
|
progress.startWork(-1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.osmand.swing;
|
||||||
|
|
||||||
|
public class DataExtractionSettings {
|
||||||
|
|
||||||
|
private static DataExtractionSettings settings = null;
|
||||||
|
public static DataExtractionSettings getSettings(){
|
||||||
|
if(settings == null){
|
||||||
|
settings = new DataExtractionSettings();
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void saveSettings(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -67,17 +68,17 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
private DefaultMutableTreeNode amenitiesTree;
|
private DefaultMutableTreeNode amenitiesTree;
|
||||||
private JTree treePlaces;
|
private JTree treePlaces;
|
||||||
private JList searchList;
|
private JList searchList;
|
||||||
|
private JFrame frame;
|
||||||
|
private JTextField searchTextField;
|
||||||
|
|
||||||
private Region region;
|
private Region region;
|
||||||
|
private File workingDir;
|
||||||
private JFrame frame;
|
|
||||||
|
|
||||||
private JTextField searchTextField;
|
|
||||||
|
|
||||||
public OsmExtractionUI(final Region r){
|
public OsmExtractionUI(final Region r){
|
||||||
this.region = r;
|
this.region = r;
|
||||||
createUI();
|
createUI();
|
||||||
setRegion(r);
|
setRegion(r);
|
||||||
|
workingDir = new File(DefaultLauncherConstants.pathToWorkingDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -86,7 +87,7 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRegion(Region region){
|
public void setRegion(Region region){
|
||||||
if(this.region == region){
|
if (this.region == region) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.region = region;
|
this.region = region;
|
||||||
|
@ -95,17 +96,18 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
amenitiesTree.add(new DataExtractionTreeNode("closest", region));
|
amenitiesTree.add(new DataExtractionTreeNode("closest", region));
|
||||||
root.add(amenitiesTree);
|
root.add(amenitiesTree);
|
||||||
|
|
||||||
for(CityType t : CityType.values()){
|
if (region != null) {
|
||||||
|
for (CityType t : CityType.values()) {
|
||||||
DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t);
|
DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t);
|
||||||
root.add(cityTree);
|
root.add(cityTree);
|
||||||
for(City ct : region.getCitiesByType(t)){
|
for (City ct : region.getCitiesByType(t)) {
|
||||||
DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct);
|
DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct);
|
||||||
cityTree.add(cityNodeTree);
|
cityTree.add(cityNodeTree);
|
||||||
|
|
||||||
for(Street str : ct.getStreets()){
|
for (Street str : ct.getStreets()) {
|
||||||
DefaultMutableTreeNode strTree = new DataExtractionTreeNode(str.getName(), str);
|
DefaultMutableTreeNode strTree = new DataExtractionTreeNode(str.getName(), str);
|
||||||
cityNodeTree.add(strTree);
|
cityNodeTree.add(strTree);
|
||||||
for(Entity e : str.getBuildings()){
|
for (Entity e : str.getBuildings()) {
|
||||||
DefaultMutableTreeNode building = new DataExtractionTreeNode(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER), e);
|
DefaultMutableTreeNode building = new DataExtractionTreeNode(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER), e);
|
||||||
strTree.add(building);
|
strTree.add(building);
|
||||||
|
|
||||||
|
@ -113,10 +115,13 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
DataTileManager<LatLon> amenitiesManager = new DataTileManager<LatLon>();
|
DataTileManager<LatLon> amenitiesManager = new DataTileManager<LatLon>();
|
||||||
for(Amenity a : region.getAmenityManager().getAllObjects()){
|
if (region != null) {
|
||||||
|
for (Amenity a : region.getAmenityManager().getAllObjects()) {
|
||||||
amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon());
|
amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
mapPanel.setPoints(amenitiesManager);
|
mapPanel.setPoints(amenitiesManager);
|
||||||
updateListCities(region, searchTextField.getText(), searchList);
|
updateListCities(region, searchTextField.getText(), searchList);
|
||||||
mapPanel.updateUI();
|
mapPanel.updateUI();
|
||||||
|
@ -269,8 +274,29 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
bar.add(menu);
|
bar.add(menu);
|
||||||
MenuItem loadFile = new MenuItem("Load file...");
|
MenuItem loadFile = new MenuItem("Load file...");
|
||||||
menu.add(loadFile);
|
menu.add(loadFile);
|
||||||
|
MenuItem specifyWorkingDir = new MenuItem("Specify working directory...");
|
||||||
|
menu.add(specifyWorkingDir);
|
||||||
|
|
||||||
bar.add(MapPanel.getMenuToChooseSource(mapPanel));
|
bar.add(MapPanel.getMenuToChooseSource(mapPanel));
|
||||||
|
|
||||||
|
specifyWorkingDir.addActionListener(new ActionListener(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
fc.setDialogTitle("Choose working directory");
|
||||||
|
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
||||||
|
if(workingDir != null){
|
||||||
|
fc.setCurrentDirectory(workingDir);
|
||||||
|
}
|
||||||
|
if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null &&
|
||||||
|
fc.getSelectedFile().isDirectory()){
|
||||||
|
workingDir = fc.getSelectedFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
loadFile.addActionListener(new ActionListener(){
|
loadFile.addActionListener(new ActionListener(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -279,7 +305,7 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
fc.setDialogTitle("Choose osm file");
|
fc.setDialogTitle("Choose osm file");
|
||||||
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||||
fc.setAcceptAllFileFilterUsed(true);
|
fc.setAcceptAllFileFilterUsed(true);
|
||||||
fc.setCurrentDirectory(new File(DefaultLauncherConstants.pathToDirWithTiles));
|
fc.setCurrentDirectory(new File(DefaultLauncherConstants.pathToTestDataDir));
|
||||||
//System.out.println("opening fc for extension " + extension);
|
//System.out.println("opening fc for extension " + extension);
|
||||||
fc.setFileFilter(new FileFilter(){
|
fc.setFileFilter(new FileFilter(){
|
||||||
|
|
||||||
|
@ -296,22 +322,40 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
|
|
||||||
int answer = fc.showOpenDialog(frame) ;
|
int answer = fc.showOpenDialog(frame) ;
|
||||||
if (answer == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null){
|
if (answer == JFileChooser.APPROVE_OPTION && fc.getSelectedFile() != null){
|
||||||
try {
|
loadCountry(fc.getSelectedFile());
|
||||||
Region region = new DataExtraction().readCountry(fc.getSelectedFile().getAbsolutePath());
|
|
||||||
setRegion(region);
|
|
||||||
} catch (IOException e1) {
|
|
||||||
// TODO
|
|
||||||
e1.printStackTrace();
|
|
||||||
} catch (SAXException e1) {
|
|
||||||
// TODO
|
|
||||||
e1.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadCountry(final File f){
|
||||||
|
try {
|
||||||
|
final ProgressDialog dlg = new ProgressDialog(frame);
|
||||||
|
dlg.setRunnable(new Runnable(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Region res;
|
||||||
|
try {
|
||||||
|
res = new DataExtraction().readCountry(f.getAbsolutePath(), dlg);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException(e);
|
||||||
|
} catch (SAXException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
dlg.setResult(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Region region = (Region) dlg.run();
|
||||||
|
setRegion(region);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
// TODO
|
||||||
|
e1.printStackTrace();
|
||||||
|
} catch (InvocationTargetException e1) {
|
||||||
|
// TODO
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -376,17 +420,16 @@ public class OsmExtractionUI implements IMapLocationListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateListCities(Region r, String text, JList jList){
|
public void updateListCities(Region r, String text, JList jList) {
|
||||||
|
|
||||||
Collection<City> city;
|
Collection<City> city;
|
||||||
if(r == null){
|
if (r == null) {
|
||||||
city = Collections.emptyList();
|
city = Collections.emptyList();
|
||||||
} else {
|
} else {
|
||||||
city = r.getSuggestedCities(text, 100);
|
city = r.getSuggestedCities(text, 100);
|
||||||
}
|
}
|
||||||
City[] names = new City[city.size()];
|
City[] names = new City[city.size()];
|
||||||
int i=0;
|
int i = 0;
|
||||||
for(City c : city){
|
for (City c : city) {
|
||||||
names[i++] = c;
|
names[i++] = c;
|
||||||
}
|
}
|
||||||
jList.setListData(names);
|
jList.setListData(names);
|
||||||
|
|
183
DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java
Normal file
183
DataExtractionOSM/src/com/osmand/swing/ProgressDialog.java
Normal file
|
@ -0,0 +1,183 @@
|
||||||
|
package com.osmand.swing;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.JDialog;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JProgressBar;
|
||||||
|
|
||||||
|
import com.osmand.IProgress;
|
||||||
|
|
||||||
|
|
||||||
|
public class ProgressDialog extends JDialog implements IProgress {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3915486672514402269L;
|
||||||
|
private JProgressBar progressBar;
|
||||||
|
private JLabel label;
|
||||||
|
private Runnable run;
|
||||||
|
private InvocationTargetException exception = null;
|
||||||
|
|
||||||
|
private Object result;
|
||||||
|
private boolean finished;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Progress variables
|
||||||
|
private static final float deltaToChange = 0.01f;
|
||||||
|
private String taskName;
|
||||||
|
private int deltaWork;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public ProgressDialog(Component parent, Runnable run){
|
||||||
|
this(parent);
|
||||||
|
this.run = run;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProgressDialog(Component parent){
|
||||||
|
super(JOptionPane.getFrameForComponent(parent), true);
|
||||||
|
initDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object run() throws InvocationTargetException, InterruptedException {
|
||||||
|
finished = false;
|
||||||
|
result = null;
|
||||||
|
new WorkerThread().start();
|
||||||
|
synchronized (this) {
|
||||||
|
if(!finished){
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(exception != null){
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class WorkerThread extends Thread {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(run != null){
|
||||||
|
run.run();
|
||||||
|
}
|
||||||
|
} catch(RuntimeException e){
|
||||||
|
exception = new InvocationTargetException(e);
|
||||||
|
} finally {
|
||||||
|
synchronized (this) {
|
||||||
|
finished = true;
|
||||||
|
setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initDialog() {
|
||||||
|
JPanel pane = new JPanel(new BorderLayout());
|
||||||
|
pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
||||||
|
progressBar = new JProgressBar();
|
||||||
|
pane.add(progressBar, BorderLayout.SOUTH);
|
||||||
|
label = new JLabel();
|
||||||
|
pane.add(label, BorderLayout.CENTER);
|
||||||
|
add(pane);
|
||||||
|
|
||||||
|
label.setText("Please waiting...");
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
setSize(550, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getResult() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResult(Object result) {
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRunnable(Runnable run) {
|
||||||
|
this.run = run;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progress(int deltaWork) {
|
||||||
|
this.deltaWork += deltaWork;
|
||||||
|
if(change(progressBar.getValue() + deltaWork)){
|
||||||
|
progressBar.setValue(progressBar.getValue() + deltaWork);
|
||||||
|
updateMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMessage() {
|
||||||
|
if(!progressBar.isIndeterminate()){
|
||||||
|
label.setText(taskName + String.format("\t%.1f %%", progressBar.getValue() * 100f / ((float) progressBar.getMaximum())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean change(int newProgress) {
|
||||||
|
if (newProgress < progressBar.getValue()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((newProgress - progressBar.getValue()) / ((float) progressBar.getMaximum()) < deltaToChange) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void remaining(int remainingWork) {
|
||||||
|
if(change(progressBar.getMaximum() - remainingWork)){
|
||||||
|
progressBar.setValue(progressBar.getMaximum() - remainingWork);
|
||||||
|
updateMessage();
|
||||||
|
}
|
||||||
|
deltaWork = progressBar.getMaximum() - remainingWork - this.progressBar.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isIndeterminate(){
|
||||||
|
return progressBar.isIndeterminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(String taskName, int work) {
|
||||||
|
if(taskName == null){
|
||||||
|
taskName = "";
|
||||||
|
}
|
||||||
|
label.setText(taskName);
|
||||||
|
this.taskName = taskName;
|
||||||
|
startWork(work);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finishTask() {
|
||||||
|
if (taskName != null) {
|
||||||
|
label.setText(taskName);
|
||||||
|
}
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startWork(int work) {
|
||||||
|
if(work == 0){
|
||||||
|
work = 1;
|
||||||
|
}
|
||||||
|
if(work != -1){
|
||||||
|
progressBar.setMinimum(0);
|
||||||
|
progressBar.setMaximum(work);
|
||||||
|
progressBar.setValue(0);
|
||||||
|
progressBar.setIndeterminate(false);
|
||||||
|
} else {
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
}
|
||||||
|
deltaWork = 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.osmand.swing;
|
||||||
|
|
||||||
|
import com.osmand.IProgress;
|
||||||
|
|
||||||
|
public class SwingProgressImplementation implements IProgress {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finishTask() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isIndeterminate() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void progress(int deltaWork) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remaining(int remainingWork) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(String taskName, int work) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startWork(int work) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue