r738 - in trunk/tutti-ui-swing/src/main: java/fr/ifremer/tutti/ui/swing/content/operation/catches java/fr/ifremer/tutti/ui/swing/util java/fr/ifremer/tutti/ui/swing/util/attachment resources/i18n
Author: tchemit Date: 2013-04-04 21:49:47 +0200 (Thu, 04 Apr 2013) New Revision: 738 Url: http://forge.codelutin.com/projects/tutti/repository/revisions/738 Log: fixes #2242: [ERGO] - Impossible de t?\195?\169l?\195?\169charger une pi?\195?\168ce-jointe Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUI.jaxx trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUIHandler.java trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx 2013-04-04 19:09:48 UTC (rev 737) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/operation/catches/EditCatchesUI.jaxx 2013-04-04 19:49:47 UTC (rev 738) @@ -352,10 +352,10 @@ </tab> <tab id='marineLitterTab' title='tutti.label.tab.marineLitter'> <JPanel id='marineLitterTabPanel'> - <JXTitledPanel id='marineLitterTabFishingOperationReminderLabel'> + <JXTitledPanel id='marineLitterTabFishingOperationReminderLabel' + constraints='EditCatchesUIHandler.MAIN_CARD'> <MarineLitterBatchUI id='marineLitterTabContent' - constructorParams='this' - constraints='EditCatchesUIHandler.MAIN_CARD'/> + constructorParams='this'/> </JXTitledPanel> <JXTitledPanel id='marineLitterTabCreateBatchReminderLabel' constraints='EditCatchesUIHandler.CREATE_BATCH_CARD'> Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java 2013-04-04 19:09:48 UTC (rev 737) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/TuttiUIUtil.java 2013-04-04 19:49:47 UTC (rev 738) @@ -37,6 +37,7 @@ import org.jdesktop.swingx.decorator.Highlighter; import org.nuiton.util.FileUtil; +import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; @@ -96,6 +97,34 @@ } /** + * Sauver un fichier via un sélecteur graphique de fichiers. + * + * @param parent le component swing appelant le controle + * @param title le titre du dialogue de sélection + * @param buttonLabel le label du boutton d'acceptation + * @param filters les filtres + descriptions sur le sélecteur de + * fichiers + * @return le fichier choisi ou le fichier incoming si l'opération a été + * annulée + */ + public static File saveFile(String filename, + Component parent, + String title, + String buttonLabel, + String... filters) { + + File file = saveFile(filename, title, buttonLabel, parent, filters); + if (log.isDebugEnabled()) { + log.debug(title + " : " + file); + } + if (file != null) { + Preconditions.checkState(!file.isDirectory()); + FileUtil.setCurrentDirectory(file.getParentFile()); + } + return file; + } + + /** * Choisir un répertoire via un sélecteur graphique de fichiers. * * @param parent le component swing appelant le controle @@ -305,4 +334,87 @@ } } + //TODO Move this to jaxx (but not to nuiton-utils) + + /** + * Retourne le nom du fichier entre dans la boite de dialogue. + * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. + * + * @param title le titre de la boite de dialogue + * @param approvalText le label du boutton d'acceptation + * @param parent le component parent du dialog + * @param patternOrDescriptionFilters les filtres a utiliser, les chaines doivent etre données + * par deux, le pattern du filtre + la description du filtre + * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé + */ + public static File saveFile(String filename, + String title, + String approvalText, + Component parent, + String... patternOrDescriptionFilters) { + + if (patternOrDescriptionFilters.length % 2 != 0) { + throw new IllegalArgumentException( + "Arguments must be (pattern, description) couple"); + } + javax.swing.filechooser.FileFilter[] filters = + new javax.swing.filechooser.FileFilter[ + patternOrDescriptionFilters.length / 2]; + for (int i = 0; i < filters.length; i++) { + String pattern = patternOrDescriptionFilters[i * 2]; + String description = patternOrDescriptionFilters[i * 2 + 1]; + filters[i] = new FileUtil.PatternChooserFilter(pattern, description); + } + File result; + result = saveFile(filename, title, approvalText, parent, filters); + return result; + } + + //TODO Move this to jaxx (but not to nuiton-utils) + + /** + * Retourne le nom du fichier entre dans la boite de dialogue. + * Si le bouton annuler est utilisé, ou qu'il y a une erreur retourne null. + * + * @param title le titre de la boite de dialogue + * @param approvalText le label du boutton d'acceptation + * @param parent le component parent du dialog + * @param filters les filtres a ajouter + * @return le fichier accepté, ou null si rien n'est chois ou l'utilisateur a annulé + */ + public static File saveFile(String filename, + String title, + String approvalText, + Component parent, + javax.swing.filechooser.FileFilter... filters) { + File selectedFile = new File(FileUtil.getCurrentDirectory(), filename); + JFileChooser chooser = new JFileChooser(selectedFile); + + chooser.setDialogType(JFileChooser.SAVE_DIALOG); + chooser.setSelectedFile(selectedFile); + if (filters.length > 0) { + if (filters.length == 1) { + chooser.setFileFilter(filters[0]); + } else { + for (javax.swing.filechooser.FileFilter filter : filters) { + chooser.addChoosableFileFilter(filter); + } + } + } + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setDialogTitle(title); + + File result = null; + + int returnVal = chooser.showDialog(parent, approvalText); + if (returnVal == JFileChooser.APPROVE_OPTION) { + result = chooser.getSelectedFile(); + if (result != null) { + FileUtil.setCurrentDirectory(result); + result = result.getAbsoluteFile(); + } + } + return result; + } + } Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUI.jaxx =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUI.jaxx 2013-04-04 19:09:48 UTC (rev 737) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUI.jaxx 2013-04-04 19:49:47 UTC (rev 738) @@ -21,7 +21,8 @@ <http://www.gnu.org/licenses/gpl-3.0.html>. #L% --> -<JDialog id='attachmentDialog' layout='{new BorderLayout()}'> +<JDialog id='attachmentDialog' layout='{new BorderLayout()}' + implements='fr.ifremer.tutti.ui.swing.util.TuttiUI<TuttiUIContext, AttachmentEditorUIHandler>'> <import> fr.ifremer.tutti.ui.swing.TuttiUIContext @@ -31,6 +32,19 @@ javax.swing.JComponent </import> + <!-- if true, display the form to add attachments + and the button to remove existing attachments --> + <Boolean id='editable' javaBean='true'/> + + <!-- bean property --> + <AttachmentModelAware id='bean' javaBean='null'/> + + <TuttiUIContext id='model' + initializer='getContextValue(TuttiUIContext.class)'/> + + <AttachmentEditorUIHandler id='handler' + initializer='getContextValue(AttachmentEditorUIHandler.class)'/> + <script><![CDATA[ public AttachmentEditorUI(TuttiUIContext context) { @@ -54,16 +68,6 @@ } ]]></script> - <!-- if true, display the form to add attachments - and the button to remove existing attachments --> - <Boolean id='editable' javaBean='true'/> - - <!-- bean property --> - <AttachmentModelAware id='bean' javaBean='null'/> - - <AttachmentEditorUIHandler id='handler' - initializer='getContextValue(AttachmentEditorUIHandler.class)'/> - <JXTitledPanel id='attachmentBody' constraints='BorderLayout.CENTER'> <JScrollPane id='attachmentBodyScrollPane'> <Table id='mainPanel'> Modified: trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUIHandler.java =================================================================== --- trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUIHandler.java 2013-04-04 19:09:48 UTC (rev 737) +++ trunk/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/util/attachment/AttachmentEditorUIHandler.java 2013-04-04 19:49:47 UTC (rev 738) @@ -24,6 +24,7 @@ * #L% */ +import fr.ifremer.tutti.TuttiIOUtil; import fr.ifremer.tutti.persistence.entities.TuttiBeanFactory; import fr.ifremer.tutti.persistence.entities.data.Attachment; import fr.ifremer.tutti.service.PersistenceService; @@ -33,7 +34,7 @@ import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.ComponentMover; import jaxx.runtime.swing.ComponentResizer; -import org.apache.commons.io.FileUtils; +import jaxx.runtime.validator.swing.SwingValidator; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -51,7 +52,6 @@ import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.File; -import java.io.IOException; import java.util.List; import static org.nuiton.i18n.I18n._; @@ -61,7 +61,7 @@ * @author tchemit <chemit@codelutin.com> * @since 0.2 */ -public class AttachmentEditorUIHandler { +public class AttachmentEditorUIHandler extends AbstractTuttiUIHandler<TuttiUIContext, AttachmentEditorUI> { /** Logger. */ private static final Log log = @@ -79,15 +79,18 @@ public AttachmentEditorUIHandler(TuttiUIContext context, AttachmentEditorUI ui) { + super(context, ui); this.context = context; this.ui = ui; this.persistenceService = context.getPersistenceService(); } - protected void beforeInitUI() { + @Override + public void beforeInitUI() { } - protected void afterInitUI() { + @Override + public void afterInitUI() { ui.getFile().setDialogOwner(ui); ui.pack(); @@ -151,6 +154,15 @@ ui.getAttachmentBody().setRightDecoration(jToolBar); } + @Override + public void onCloseUI() { + } + + @Override + public SwingValidator<TuttiUIContext> getValidator() { + return null; + } + protected Action closeAction; protected Action openAction; @@ -253,46 +265,53 @@ } try { File attachmentFile = persistenceService.getAttachmentFile(attachment.getId()); - File file = TuttiUIUtil.chooseFile(ui, - _("tutti.attachmentEditor.saveAttachment.title"), - _("tutti.attachmentEditor.saveAttachment.button")); + File file = TuttiUIUtil.saveFile( + attachment.getName(), + ui, + _("tutti.attachmentEditor.saveAttachment.title"), + _("tutti.attachmentEditor.saveAttachment.button")); if (file != null) { - if (file.exists()) { - String htmlMessage = String.format( - AbstractTuttiUIHandler.CONFIRMATION_FORMAT, - _("tutti.attachmentEditor.saveAttachment.warning.message"), - _("tutti.attachmentEditor.saveAttachment.warning.help")); - - int answer = JOptionPane.showConfirmDialog(ui, - htmlMessage, - _("tutti.attachmentEditor.saveAttachment.warning.title"), - JOptionPane.YES_NO_OPTION, - JOptionPane.WARNING_MESSAGE); - - if (answer == JOptionPane.NO_OPTION) { - return; - } + boolean checkOverwrite = askOverwriteFile(file); + if (checkOverwrite) { + TuttiIOUtil.copyFile(attachmentFile, file, "Error while saving attachment"); + context.showInformationMessage( + _("tutti.attachmentEditor.saveAttachment.success.message", file.getName())); } - FileUtils.copyFile(attachmentFile, file); - TuttiUIUtil.showSuccessMessage(ui, - _("tutti.attachmentEditor.saveAttachment.success.title"), - _("tutti.attachmentEditor.saveAttachment.success.message", file.getName())); +// if (file.exists()) { +// String htmlMessage = String.format( +// AbstractTuttiUIHandler.CONFIRMATION_FORMAT, +// _("tutti.attachmentEditor.saveAttachment.warning.message"), +// _("tutti.attachmentEditor.saveAttachment.warning.help")); +// +// int answer = JOptionPane.showConfirmDialog(ui, +// htmlMessage, +// _("tutti.attachmentEditor.saveAttachment.warning.title"), +// JOptionPane.YES_NO_OPTION, +// JOptionPane.WARNING_MESSAGE); +// +// if (answer == JOptionPane.NO_OPTION) { +// return; +// } +// } +// FileUtils.copyFile(attachmentFile, file); +// context.showInformationMessage( +// _("tutti.attachmentEditor.saveAttachment.success.message", file.getName())); } if (hackDialog) { ui.setAlwaysOnTop(true); } - } catch (IOException e) { - if (log.isErrorEnabled()) { - log.error("Error while saving attachment", e); - } - JOptionPane.showMessageDialog( - ui, - _("tutti.attachmentEditor.saveAttachment.fail.message"), - _("tutti.attachmentEditor.saveAttachment.fail.title"), - JOptionPane.ERROR_MESSAGE - ); +// } catch (IOException e) { +// if (log.isErrorEnabled()) { +// log.error("Error while saving attachment", e); +// } +// JOptionPane.showMessageDialog( +// ui, +// _("tutti.attachmentEditor.saveAttachment.fail.message"), +// _("tutti.attachmentEditor.saveAttachment.fail.title"), +// JOptionPane.ERROR_MESSAGE +// ); } finally { if (hackDialog) { Modified: trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties =================================================================== --- trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2013-04-04 19:09:48 UTC (rev 737) +++ trunk/tutti-ui-swing/src/main/resources/i18n/tutti-ui-swing_fr_FR.properties 2013-04-04 19:49:47 UTC (rev 738) @@ -27,12 +27,12 @@ tutti.attachmentEditor.field.fileComment=Commentaire tutti.attachmentEditor.field.fileName=Nom tutti.attachmentEditor.none.tip=Pas de pièce-jointes -tutti.attachmentEditor.saveAttachment.button=Choisir +tutti.attachmentEditor.saveAttachment.button=Enregistrer tutti.attachmentEditor.saveAttachment.fail.message=Une erreur est survenue lors de la sauvegarde du fichier. tutti.attachmentEditor.saveAttachment.fail.title=Erreur de sauvegarde. tutti.attachmentEditor.saveAttachment.success.message=Le fichier %s a été sauvegardé sur votre machine. tutti.attachmentEditor.saveAttachment.success.title=Sauvegarde réussie. -tutti.attachmentEditor.saveAttachment.title=Choisir un fichier +tutti.attachmentEditor.saveAttachment.title=Sauver la pièce-jointe tutti.attachmentEditor.saveAttachment.warning.help=Que voulez-vous faire ?<ul><li><strong>Non</strong> pour annuler la sauvegarde</li><li><strong>Oui</strong> pour écraser le fichier existant</li></ul> tutti.attachmentEditor.saveAttachment.warning.message=Vous êtes sur le point d'écraser un fichier existant. tutti.attachmentEditor.saveAttachment.warning.title=Fichier existant
participants (1)
-
tchemit@users.forge.codelutin.com