Author: chatellier Date: 2009-04-14 13:43:46 +0000 (Tue, 14 Apr 2009) New Revision: 2104 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java Log: Fix duplicated entity (need improvement, strange bug) Fix listener on reloaded object. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java 2009-04-14 12:46:21 UTC (rev 2103) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/InputSaveVerifier.java 2009-04-14 13:43:46 UTC (rev 2104) @@ -1,13 +1,26 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ +/* *##% + * Copyright (C) 2005 - 2009 + * Ifremer, Code Lutin, Cedric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ package fr.ifremer.isisfish.ui.input; -import fr.ifremer.isisfish.IsisFishDAOHelper; -import fr.ifremer.isisfish.ui.sensitivity.SensitivityTabUI; -import fr.ifremer.isisfish.ui.widget.ErrorDialogUI; +import static org.codelutin.i18n.I18n._; + import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; @@ -15,61 +28,77 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; + import javax.swing.JButton; import javax.swing.JOptionPane; + import jaxx.runtime.JAXXAction; import jaxx.runtime.JAXXContext; import jaxx.runtime.JAXXInitialContext; import jaxx.runtime.swing.navigation.NavigationTreeModel.NavigationTreeNode; + import org.apache.commons.beanutils.MethodUtils; import org.apache.commons.lang.ClassUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.topia.TopiaContext; import org.codelutin.topia.TopiaException; +import org.codelutin.topia.framework.TopiaContextImpl; import org.codelutin.topia.persistence.TopiaDAO; import org.codelutin.topia.persistence.TopiaEntity; -import static org.codelutin.i18n.I18n._; +import fr.ifremer.isisfish.IsisFishDAOHelper; +import fr.ifremer.isisfish.ui.SaveVerifier; +import fr.ifremer.isisfish.ui.sensitivity.SensitivityTabUI; +import fr.ifremer.isisfish.ui.widget.ErrorDialogUI; + /** + * InputSaveVerifier. * * @author letellier + * @version $Revision: 1312 $ + * + * Last update: $Date: 2008-08-28 10:21:07 +0200 (jeu, 28 aoû 2008) $ + * by : $Author: sletellier $ */ -public class InputSaveVerifier implements JAXXAction, fr.ifremer.isisfish.ui.SaveVerifier { +public class InputSaveVerifier implements JAXXAction, SaveVerifier { + + /** Class logger. */ + private static Log log = LogFactory.getLog(InputAction.class); + protected JButton currentNewButton = null; protected JButton currentDeleteButton = null; protected JButton currentSaveButton = null; protected JButton currentCancelButton = null; + + protected ActionListener saveListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + topiaSave(); + } + }; - protected ActionListener saveListener = new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - topiaSave(); - } - }; protected ActionListener cancelListener = new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - topiaCancel(); - } - }; + @Override + public void actionPerformed(ActionEvent e) { + topiaCancel(); + } + }; + protected ActionListener newListener = new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - topiaCreate(); - } - }; + @Override + public void actionPerformed(ActionEvent e) { + topiaCreate(); + } + }; + protected ActionListener deleteListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + topiaRemove(); + } + }; - @Override - public void actionPerformed(ActionEvent e) { - topiaRemove(); - } - }; - protected String type = null; protected boolean editable = false; @@ -79,16 +108,15 @@ protected String currentOnglet = null; protected TopiaContext isisContext = null; - protected List<TopiaEntity> currentEntities = new ArrayList <TopiaEntity>(); - protected List<InputContentUI> currentPanels = new ArrayList <InputContentUI>(); + protected List<TopiaEntity> currentEntities = new ArrayList<TopiaEntity>(); + protected List<InputContentUI> currentPanels = new ArrayList<InputContentUI>(); - /** to use log facility, just put in your code: log.info(\"...\"); */ - static private Log log = LogFactory.getLog(InputAction.class); protected InputUI rootUI; protected SensitivityTabUI sensUI; - public InputSaveVerifier(){ + public InputSaveVerifier() { } + @Override public JAXXInitialContext init(JAXXContext parentContent, Object... datas) { throw new UnsupportedOperationException("Not supported yet."); @@ -114,11 +142,12 @@ public int askUser(String message) { int response = JOptionPane.showConfirmDialog(rootUI, message, - _("isisfish.input.menu.commit"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); + _("isisfish.input.menu.commit"), + JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); return response; } - protected void topiaChanged(){ + protected void topiaChanged() { changed = true; setPanelsActifs(); setEnabled(currentSaveButton, true); @@ -126,7 +155,8 @@ setEnabled(currentNewButton, false); setEnabled(currentDeleteButton, false); } - protected void noModif(){ + + protected void noModif() { changed = false; setPanelsActifs(); setEnabled(currentSaveButton, false); @@ -134,8 +164,9 @@ setEnabled(currentNewButton, true); setEnabled(currentDeleteButton, editable); } - protected void setEnabled(JButton b, boolean enabled){ - if (b != null){ + + protected void setEnabled(JButton b, boolean enabled) { + if (b != null) { b.setEnabled(enabled); } } @@ -154,22 +185,26 @@ String msg = ""; try { boolean doDelete; - TopiaEntity topiaEntity = (TopiaEntity)currentNode.getJAXXContextValue(rootUI); + TopiaEntity topiaEntity = (TopiaEntity) currentNode + .getJAXXContextValue(rootUI); List<TopiaEntity> allWillBeRemoved = topiaEntity.getComposite(); if (allWillBeRemoved.size() > 0) { - String text = _( - "isisfish.message.delete.object", currentEntities.get(0).toString()); + String text = _("isisfish.message.delete.object", + currentEntities.get(0).toString()); for (TopiaEntity e : allWillBeRemoved) { text += ClassUtils.getShortClassName(e.getClass()) + " - " + e.toString() + "\n"; } int resp = JOptionPane.showConfirmDialog(null, text, - _("isisfish.message.delete.entities"), JOptionPane.YES_NO_OPTION); + _("isisfish.message.delete.entities"), + JOptionPane.YES_NO_OPTION); doDelete = resp == JOptionPane.YES_OPTION; } else { - String text = _("isisfish.message.confirm.delete.object", currentEntities.get(0).toString()); + String text = _("isisfish.message.confirm.delete.object", + currentEntities.get(0).toString()); int resp = JOptionPane.showConfirmDialog(null, text, - _("isisfish.message.delete.entity"), JOptionPane.YES_NO_OPTION); + _("isisfish.message.delete.entity"), + JOptionPane.YES_NO_OPTION); doDelete = resp == JOptionPane.YES_OPTION; } @@ -181,7 +216,9 @@ msg = _("isisfish.message.remove.canceled"); } } catch (Exception eee) { - log.error("Can't remove entity: " + currentEntities.get(0), eee); + if (log.isErrorEnabled()) { + log.error("Can't remove entity: " + currentEntities.get(0), eee); + } ErrorDialogUI.showError(eee); } rootUI.setInfoText(msg); @@ -191,9 +228,8 @@ noModif(); } + protected void topiaCreate() { - protected void topiaCreate(){ - if (log.isTraceEnabled()) { log.trace("create called for " + type); } @@ -205,41 +241,48 @@ Method method = MethodUtils.getAccessibleMethod( IsisFishDAOHelper.class, "get" + type + "DAO", TopiaContext.class); - TopiaDAO<TopiaEntity> dao = (TopiaDAO<TopiaEntity>) method.invoke(null, isisContext); + TopiaDAO<TopiaEntity> dao = (TopiaDAO<TopiaEntity>) method.invoke( + null, isisContext); TopiaEntity entity = dao.create("name", name); entity.update(); isisContext.commitTransaction(); - String path = currentNode.getParent().getContextPath() + "/" + entity.getTopiaId(); - if (!editable){ + String path = currentNode.getParent().getContextPath() + "/" + + entity.getTopiaId(); + if (!editable) { path = currentNode.getContextPath() + "/" + entity.getTopiaId(); } - + rootUI.setTreeModel(); rootUI.setTreeSelection(path); rootUI.setInfoText(_("isisfish.message.creation.finished")); } catch (Exception eee) { - log.error("Can't create entity", eee); + if (log.isErrorEnabled()) { + log.error("Can't create entity", eee); + } showMsgBox(eee); } } - - public void topiaSave(){ + + public void topiaSave() { try { noModif(); - for (TopiaEntity t : currentEntities){ + for (TopiaEntity t : currentEntities) { t.update(); - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("updating : " + t); + } } isisContext.commitTransaction(); rootUI.repaintNode(currentNode.getContextPath()); rootUI.setInfoText(_("isisfish.message.save.finished")); } catch (TopiaException eee) { - log.error("Can't save region", eee); + if (log.isErrorEnabled()) { + log.error("Can't save region", eee); + } showMsgBox(eee); } } @@ -252,17 +295,28 @@ try { noModif(); isisContext.rollbackTransaction(); - List<TopiaEntity> canceledEntity = new ArrayList<TopiaEntity>(); - for (TopiaEntity t : currentEntities){ - t = isisContext.findByTopiaId(t.getTopiaId()); + List<TopiaEntity> canceledEntity = currentEntities; + currentEntities = new ArrayList<TopiaEntity>(); + + // TODO a quoi ca sert de recharger les entités ? + for (TopiaEntity t : canceledEntity) { + TopiaEntity t2 = isisContext.findByTopiaId(t.getTopiaId()); + + // desynchronise la précédente + // fix : org.hibernate.NonUniqueObjectException: a different object with the + // same identifier value was already associated with the session + //((TopiaContextImpl)isisContext).getHibernate().evict(t2); + t.setTopiaId(null); + rootUI.repaintNode(currentNode.getContextPath()); rootUI.setTreeSelection(currentNode.getContextPath()); - canceledEntity.add(t); + addCurrentEntity(t2); } - currentEntities = canceledEntity; rootUI.setInfoText(_("isisfish.message.cancel.finished")); } catch (Exception eee) { - log.error("Can't cancel modification in region", eee); + if (log.isErrorEnabled()) { + log.error("Can't cancel modification in region", eee); + } showMsgBox(eee); } } @@ -270,51 +324,59 @@ protected void showMsgBox(Exception eee) { ErrorDialogUI.showError(eee); } - protected void setPanelsActifs(){ - if (!currentPanels.isEmpty()){ - for (InputContentUI panel : currentPanels){ + + protected void setPanelsActifs() { + if (!currentPanels.isEmpty()) { + for (InputContentUI panel : currentPanels) { panel.setActif(editable); } } } - public void refreshAll(){ - for (InputContentUI panel : currentPanels){ + + public void refreshAll() { + for (InputContentUI panel : currentPanels) { panel.refresh(); panel.setActif(editable); } } + public void addCurrentEntity(TopiaEntity currentEntity) { - if (currentEntity != null){ + if (currentEntity != null) { editable = true; isisContext = currentEntity.getTopiaContext(); - currentEntity.addPropertyChangeListener(new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - if (log.isDebugEnabled()){ - log.debug("PropertyChanged : " + evt.getPropertyName() + " New Value : " + evt.getNewValue()); - } - topiaChanged(); - } - }); + currentEntity + .addPropertyChangeListener(new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (log.isDebugEnabled()) { + log.debug("PropertyChanged : " + + evt.getPropertyName() + + " New Value : " + evt.getNewValue()); + } + topiaChanged(); + } + }); TopiaEntity entity = getEntity(currentEntity.getClass()); - if (entity != null){ + if (entity != null) { currentEntities.remove(entity); } this.currentEntities.add(currentEntity); setPanelsActifs(); } } - public void removeAllEntity(){ + + public void removeAllEntity() { currentEntities.clear(); editable = false; noModif(); setPanelsActifs(); } + public void addCurrentPanel(InputContentUI... panels) { - for (InputContentUI ui : panels){ + for (InputContentUI ui : panels) { editable = !currentEntities.isEmpty(); this.currentPanels.add(ui); - if (rootUI == null){ + if (rootUI == null) { ui.setSens(true); ui.setLayer(true); } @@ -322,7 +384,8 @@ ui.setActif(editable); } } - public void removeAllPanels(){ + + public void removeAllPanels() { currentPanels.clear(); } @@ -330,12 +393,12 @@ return editable; } - public void setSaveButton(JButton saveButton){ + public void setSaveButton(JButton saveButton) { setSaveButton(saveButton, true); } - public void setSaveButton(JButton saveButton, Boolean listener){ - if (listener){ + public void setSaveButton(JButton saveButton, Boolean listener) { + if (listener) { saveButton.removeActionListener(saveListener); saveButton.addActionListener(saveListener); } @@ -344,12 +407,12 @@ this.currentSaveButton = saveButton; } - public void setNewButton(JButton saveButton, String name){ + public void setNewButton(JButton saveButton, String name) { setNewButton(saveButton, name, true); } - public void setNewButton(JButton newButton, String t, Boolean listener){ - if (listener){ + public void setNewButton(JButton newButton, String t, Boolean listener) { + if (listener) { newButton.removeActionListener(newListener); newButton.addActionListener(newListener); } @@ -359,7 +422,7 @@ this.currentNewButton = newButton; } - public void setCancelButton(JButton cancelButton){ + public void setCancelButton(JButton cancelButton) { cancelButton.removeActionListener(cancelListener); cancelButton.addActionListener(cancelListener); cancelButton.setText(_("isisfish.common.cancel")); @@ -367,12 +430,12 @@ this.currentCancelButton = cancelButton; } - public void setDeleteButton(JButton deleteButton){ + public void setDeleteButton(JButton deleteButton) { setDeleteButton(deleteButton, true); } - - public void setDeleteButton(JButton deleteButton, boolean listener){ - if (listener){ + + public void setDeleteButton(JButton deleteButton, boolean listener) { + if (listener) { deleteButton.removeActionListener(deleteListener); deleteButton.addActionListener(deleteListener); } @@ -380,7 +443,7 @@ deleteButton.setText(_("isisfish.common.remove")); this.currentDeleteButton = deleteButton; } - + public TopiaContext getIsisContext() { return isisContext; } @@ -389,9 +452,9 @@ return currentEntities; } - public <E extends TopiaEntity> E getEntity(Class<E> clazz){ - for (TopiaEntity te : currentEntities){ - if (clazz.isInstance(te)){ + public <E extends TopiaEntity> E getEntity(Class<E> clazz) { + for (TopiaEntity te : currentEntities) { + if (clazz.isInstance(te)) { return (E) te; } } @@ -429,11 +492,11 @@ void setRootPanel(InputUI inputUI) { this.rootUI = inputUI; } - + public SensitivityTabUI getSensPanel() { return sensUI; } - + public void setSensPanel(SensitivityTabUI sensUI) { this.sensUI = sensUI; }
participants (1)
-
chatellier@users.labs.libre-entreprise.org