Author: tchemit Date: 2012-10-10 17:43:42 +0200 (Wed, 10 Oct 2012) New Revision: 621 Url: http://forge.codelutin.com/repositories/revision/sammoa/621 Log: fixes #1585: Am?\195?\169lioration du changement d'?\195?\169cran Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/MainUIHandler.java Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/MainUIHandler.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/MainUIHandler.java 2012-10-03 14:05:01 UTC (rev 620) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/MainUIHandler.java 2012-10-10 15:43:42 UTC (rev 621) @@ -59,7 +59,6 @@ import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.plaf.BorderUIResource; -import java.awt.BorderLayout; import java.awt.Color; import java.awt.Desktop; import java.io.File; @@ -131,93 +130,71 @@ @Override public void onCloseUI() { - if (currentBody != null) { - SammoaUI<?> body = (SammoaUI<?>) currentBody; - body.getHandler().onCloseUI(); - currentBody = null; - } + closeCurrentBody(); - context.getSwingSession().save(); - context.close(); } public void setScreen(SammoaScreen screen) { - if (screen.equals(ui.getScreen())) { - // do nothing + try { - } else { - + // busy ui SammoaUtil.updateBusyState(ui, true); - if (currentBody != null) { - SammoaUI<?> body = (SammoaUI<?>) currentBody; - body.getHandler().onCloseUI(); + // remove any screen + ui.setScreen(null); - context.getSwingSession().save(); -// context.getSwingSession().remove(currentBody); + closeCurrentBody(); - ui.getBody().remove(currentBody); - } - - ui.setScreen(screen); - switch (screen) { - default: case HOME: - currentBody = new HomeUI(context); - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.home")); - ui.getBody().add(currentBody); + changeScreen(new HomeUI(context), + _("sammoa.title.home")); break; - case VALIDATION: case FLIGHT: FlightUI flightUI = new FlightUI(context); Flight flight = flightUI.getModel().getFlight(); Campaign campaign = flight.getCampaign(); - currentBody = flightUI; - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.flight", - flight.getSystemId(), - campaign.getCode(), - campaign.getRegion().getCode(), - flight.getFlightNumber())); - ui.getBody().add(currentBody, BorderLayout.CENTER); + changeScreen(flightUI, + _("sammoa.title.flight", + flight.getSystemId(), + campaign.getCode(), + campaign.getRegion().getCode(), + flight.getFlightNumber())); break; - case CAMPAIGN: - currentBody = new CampaignUI(context); - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.campaign")); - ui.getBody().add(currentBody, BorderLayout.CENTER); + changeScreen(new CampaignUI(context), + _("sammoa.title.campaign")); break; - case EXPORT_MAP: - currentBody = new ExportMapUI(context); - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.exportshape")); - ui.getBody().add(currentBody); + changeScreen(new ExportMapUI(context), + _("sammoa.title.exportshape")); break; - case EXPORT_APPLICATION: - currentBody = new ExportApplicationUI(context); - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.exportApplication")); - ui.getBody().add(currentBody); + changeScreen(new ExportApplicationUI(context), + _("sammoa.title.exportApplication")); break; - case IMPORT_APPLICATION: - currentBody = new ImportApplicationUI(context); - context.getSwingSession().add(currentBody); - ui.getBody().setTitle(_("sammoa.title.importApplication")); - ui.getBody().add(currentBody); + changeScreen(new ImportApplicationUI(context), + _("sammoa.title.importApplication")); break; } + // set new screen only if everything is ok + ui.setScreen(screen); + } catch (Exception e) { + + ErrorDialogUI.showError(e); + + // go back to home + setScreen(SammoaScreen.HOME); + } finally { + + // free ui SammoaUtil.updateBusyState(ui, false); } } @@ -226,23 +203,6 @@ closeSammoa(true); } - protected void closeSammoa(boolean exit) { - - if (checkCurrentFlight(_("sammoa.confirmDialog.flightInProgress.message.exit"))) { - - context.getSwingSession().save(); - - ui.setVisible(false); - ui.dispose(); - - onCloseUI(); - - if (exit) { - System.exit(0); - } - } - } - public void reloadSammoa() { // Close the application @@ -461,13 +421,51 @@ frame.setVisible(true); } + protected void closeCurrentBody() { + if (currentBody != null) { + SammoaUI<?> body = (SammoaUI<?>) currentBody; + body.getHandler().onCloseUI(); + + context.getSwingSession().save(); + + ui.getBody().remove(currentBody); + + currentBody = null; + } + } + + protected void changeScreen(JComponent newBody, String title) { + currentBody = newBody; + context.getSwingSession().add(currentBody); + ui.getBody().setTitle(title); + ui.getBody().add(currentBody); + } + + protected void closeSammoa(boolean exit) { + + if (checkCurrentFlight( + _("sammoa.confirmDialog.flightInProgress.message.exit"))) { + + context.getSwingSession().save(); + + ui.setVisible(false); + ui.dispose(); + + onCloseUI(); + + if (exit) { + System.exit(0); + } + } + } + /** * Reload the given device identified by its {@code deviceManagerClass} for * {@code ui}. * - * @param ui FlightUI contains the controller that manage devices + * @param ui FlightUI contains the controller that manage devices * @param deviceManagerClass Class of the device to reload - * @param <T> Type of {@link DeviceManager} + * @param <T> Type of {@link DeviceManager} * @see FlightController#openDeviceManager(Class) */ protected <T extends DeviceManager> void reloadDevice(FlightUI ui, @@ -482,14 +480,16 @@ } public void showHome() { - if (checkCurrentFlight(_("sammoa.confirmDialog.flightInProgress.message.showHome"))) { + if (checkCurrentFlight( + _("sammoa.confirmDialog.flightInProgress.message.showHome"))) { setScreen(SammoaScreen.HOME); } } public void showAbout() { - ApplicationConfig applicationConfig = context.getConfig().getApplicationConfig(); + ApplicationConfig applicationConfig = + context.getConfig().getApplicationConfig(); String iconPath = applicationConfig.getOption("application.icon.path"); String name = "sammoa-ui-swing"; @@ -510,7 +510,10 @@ years = inceptionYear + ""; } - about.setBottomText(_("sammoa.about.bottomText", config.getOrganizationName(), years, config.getVersion())); + about.setBottomText(_("sammoa.about.bottomText", + config.getOrganizationName(), + years, + config.getVersion())); about.setIconPath(iconPath); about.setLicenseFile(licensePath); about.setThirdpartyFile(thirdPartyPath); @@ -532,7 +535,8 @@ if (logger.isDebugEnabled()) { logger.debug("goto " + siteURL); } - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + if (Desktop.isDesktopSupported() && + Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { try { Desktop.getDesktop().browse(siteURL.toURI()); } catch (Exception e) { @@ -543,7 +547,8 @@ } public void showLogs() { - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) { + if (Desktop.isDesktopSupported() && + Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) { File logFile = context.getConfig().getLogFile(); try { Desktop.getDesktop().open(logFile);