added progress bar during quick-loads

This commit is contained in:
fros4943 2007-03-22 15:02:55 +00:00
parent 1e1e49f3ff
commit 3a3459a939

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: GUI.java,v 1.23 2007/03/22 13:59:33 fros4943 Exp $ * $Id: GUI.java,v 1.24 2007/03/22 15:02:55 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -1695,7 +1695,7 @@ public class GUI {
* @param quick Quick-load simulation * @param quick Quick-load simulation
* @param configFile Configuration file to load, if null a dialog will appear * @param configFile Configuration file to load, if null a dialog will appear
*/ */
public void doLoadConfig(boolean askForConfirmation, boolean quick, File configFile) { public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) {
if (CoreComm.hasLibraryBeenLoaded()) { if (CoreComm.hasLibraryBeenLoaded()) {
JOptionPane JOptionPane
@ -1721,25 +1721,13 @@ public class GUI {
doRemoveSimulation(false); doRemoveSimulation(false);
// Check already selected file, or select file using filechooser
if (configFile != null) { if (configFile != null) {
if (configFile.exists() && configFile.canRead()) { if (!configFile.exists() || !configFile.canRead()) {
Simulation newSim = null;
try {
newSim = loadSimulationConfig(configFile, quick);
addToFileHistory(configFile);
} catch (UnsatisfiedLinkError e) {
logger.warn("Could not reopen libraries: " + e.getMessage());
newSim = null;
}
if (newSim != null) {
myGUI.setSimulation(newSim);
}
} else
logger.fatal("No read access to file"); logger.fatal("No read access to file");
return; return;
} }
} else {
JFileChooser fc = new JFileChooser(); JFileChooser fc = new JFileChooser();
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES); fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
@ -1753,33 +1741,82 @@ public class GUI {
int returnVal = fc.showOpenDialog(frame); int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
File loadFile = fc.getSelectedFile(); configFile = fc.getSelectedFile();
// Try adding extension if not founds // Try adding extension if not founds
if (!loadFile.exists()) { if (!configFile.exists()) {
loadFile = new File(loadFile.getParent(), loadFile.getName() configFile = new File(configFile.getParent(), configFile.getName()
+ SAVED_SIMULATIONS_FILES); + SAVED_SIMULATIONS_FILES);
} }
if (loadFile.exists() && loadFile.canRead()) { if (!configFile.exists() || !configFile.canRead()) {
logger.fatal("No read access to file");
return;
}
} else {
logger.info("Load command cancelled by user...");
return;
}
}
// Load simulation in separate thread, while showing progress monitor
final JDialog progressDialog = new JDialog(frame, "Loading", true);
final File fileToLoad = configFile;
final Thread loadThread = new Thread(new Runnable() {
public void run() {
Simulation newSim = null; Simulation newSim = null;
try { try {
newSim = loadSimulationConfig(loadFile, quick); newSim = loadSimulationConfig(fileToLoad, quick);
addToFileHistory(loadFile); addToFileHistory(fileToLoad);
if (progressDialog != null)
progressDialog.dispose();
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
logger.warn("Could not reopen libraries: " + e.getMessage()); logger.warn("Could not reopen libraries: " + e.getMessage());
if (progressDialog != null)
progressDialog.dispose();
newSim = null; newSim = null;
} }
if (newSim != null) { if (newSim != null) {
myGUI.setSimulation(newSim); myGUI.setSimulation(newSim);
} }
} else
logger.fatal("No read access to file");
} else {
logger.info("Load command cancelled by user...");
} }
});
loadThread.start();
JPanel progressPanel = new JPanel(new BorderLayout());
JProgressBar progressBar;
JButton button;
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setIndeterminate(true);
button = new JButton("Cancel");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (loadThread != null && loadThread.isAlive()) {
loadThread.interrupt();
doRemoveSimulation(false);
}
progressDialog.dispose();
}
});
progressPanel.add(BorderLayout.CENTER, progressBar);
progressPanel.add(BorderLayout.SOUTH, button);
progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
progressPanel.setVisible(true);
progressDialog.getContentPane().add(progressPanel);
progressDialog.pack();
progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setLocationRelativeTo(frame);
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
if (quick)
progressDialog.setVisible(true);
} }
/** /**