This is an automated email from the git hooks/post-receive script. New commit to branch feature/7929_editeur_de_zone in repository tutti. See http://git.codelutin.com/tutti.git commit ea86043f569ce4557f2cfff793e4e9ac1cb0230e Author: Kevin Morin <morin@codelutin.com> Date: Thu Jan 28 19:21:56 2016 +0100 création des modeles de strates et sous strates (refs #7929) --- .../content/protocol/zones/ZonesTreeModel.java | 98 ---------- .../protocol/zones/models/StrataUIModel.java | 192 ++++++++++++++++++++ .../protocol/zones/models/SubStrataUIModel.java | 108 +++++++++++ .../protocol/zones/{ => models}/ZoneUIModel.java | 44 ++++- .../content/protocol/zones/nodes/StrataNode.java | 20 -- .../protocol/zones/nodes/SubStrataNode.java | 20 -- .../content/protocol/zones/tree/StrataNode.java | 20 ++ .../zones/{ => tree}/StratasTreeModel.java | 75 +++++++- .../content/protocol/zones/tree/SubStrataNode.java | 20 ++ .../{ => tree}/ZoneEditorTreeCellRenderer.java | 16 +- .../protocol/zones/{nodes => tree}/ZoneNode.java | 4 +- .../protocol/zones/tree/ZonesTreeModel.java | 202 +++++++++++++++++++++ 12 files changed, 656 insertions(+), 163 deletions(-) diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZonesTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZonesTreeModel.java deleted file mode 100644 index b2d3e19..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZonesTreeModel.java +++ /dev/null @@ -1,98 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones; - -import com.google.common.base.Preconditions; -import fr.ifremer.tutti.persistence.entities.protocol.Strata; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.ZoneNode; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.DefaultTreeModel; -import java.util.Collection; -import java.util.Enumeration; -import java.util.HashSet; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class ZonesTreeModel extends DefaultTreeModel { - - /** Logger. */ - private static final Log log = LogFactory.getLog(ZonesTreeModel.class); - - public ZonesTreeModel() { - super(new DefaultMutableTreeNode()); - } - - public void removeZones(Collection<ZoneUIModel> zonesToRemove) { - - DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); - - Enumeration rootChildren = root.children(); - Collection<DefaultMutableTreeNode> nodesToRemove = new HashSet<>(); - - while (rootChildren.hasMoreElements()) { - - ZoneNode zoneNode = (ZoneNode) rootChildren.nextElement(); - - if (zonesToRemove.contains(zoneNode.getZone())) { - nodesToRemove.add(zoneNode); - } - } - - nodesToRemove.forEach(zoneNode ->removeNodeFromParent(zoneNode)); - } - - public void addZones(Collection<ZoneUIModel> zonesToAdd) { - - DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); - - zonesToAdd.forEach(zone -> { - - ZoneNode zoneNode = new ZoneNode(zone); - insertNodeInto(zoneNode, root, root.getChildCount()); - - }); - } - - public void setStratas(ZoneUIModel zone, Collection<Strata> stratas) { - - DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); - - Enumeration rootChildren = root.children(); - - ZoneNode zoneNode = null; - - while (zoneNode == null && rootChildren.hasMoreElements()) { - ZoneNode nextNode = (ZoneNode) rootChildren.nextElement(); - if (zone.equals(nextNode.getZone())) { - zoneNode = nextNode; - } - } - - Preconditions.checkNotNull(zoneNode); - - for (Strata strata : stratas) { - - StrataNode strataNode = new StrataNode(strata); - - if (log.isInfoEnabled()) { - log.info("add strata " + strata.getLocation().getLabel()); - } - - insertNodeInto(strataNode, zoneNode, zoneNode.getChildCount()); - -// newAvailableStratas.get(strata).forEach(substrata -> { -// -// if (substrata != null) { -// SubStrataNode subStrataNode = new SubStrataNode(substrata); -// insertNodeInto(subStrataNode, strataNode, strataNode.getChildCount()); -// } -// -// }); - - } - } -} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/StrataUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/StrataUIModel.java new file mode 100644 index 0000000..e7a09f8 --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/StrataUIModel.java @@ -0,0 +1,192 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; + +import fr.ifremer.tutti.persistence.entities.protocol.Strata; +import fr.ifremer.tutti.persistence.entities.protocol.Stratas; +import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; +import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation; +import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel; +import org.nuiton.util.beans.Binder; +import org.nuiton.util.beans.BinderFactory; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Objects; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class StrataUIModel extends AbstractTuttiBeanUIModel<Strata, StrataUIModel> implements Strata { + + public static final String PROPERTY_ZONE = "zone"; + + protected final Strata delegate = Stratas.newStrata(); + + protected ZoneUIModel zone; + + protected static Binder<StrataUIModel, Strata> toBeanBinder = BinderFactory.newBinder(StrataUIModel.class, Strata.class); + + protected static Binder<Strata, StrataUIModel> fromBeanBinder = BinderFactory.newBinder(Strata.class, StrataUIModel.class); + + public StrataUIModel() { + super(fromBeanBinder, toBeanBinder); + } + + public StrataUIModel(TuttiLocation location) { + this(); + setLocation(location); + } + + @Override + public void addAllSubstrata(Collection<SubStrata> substrata) { + Object oldValue = new HashSet<>(getSubstrata()); + delegate.addAllSubstrata(substrata); + firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); + } + + @Override + public void addSubstrata(SubStrata substrata) { + Object oldValue = new HashSet<>(getSubstrata()); + delegate.addSubstrata(substrata); + firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); + } + + @Override + public boolean containsAllSubstrata(Collection<SubStrata> substrata) { + return delegate.containsAllSubstrata(substrata); + } + + @Override + public boolean containsSubstrata(SubStrata substrata) { + return delegate.containsSubstrata(substrata); + } + + @Override + public TuttiLocation getLocation() { + return delegate.getLocation(); + } + + @Override + public Collection<SubStrata> getSubstrata() { + return delegate.getSubstrata(); + } + + @Override + public SubStrata getSubstrata(int index) { + return delegate.getSubstrata(index); + } + + @Override + public boolean isSubstrataEmpty() { + return delegate.isSubstrataEmpty(); + } + + @Override + public boolean removeAllSubstrata(Collection<SubStrata> substrata) { + Object oldValue = new HashSet<>(getSubstrata()); + boolean result = delegate.removeAllSubstrata(substrata); + firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); + return result; + } + + @Override + public boolean removeSubstrata(SubStrata substrata) { + Object oldValue = new HashSet<>(getSubstrata()); + boolean result = delegate.removeSubstrata(substrata); + firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); + return result; + } + + @Override + public void setLocation(TuttiLocation location) { + Object oldValue = getLocation(); + delegate.setLocation(location); + firePropertyChanged(PROPERTY_LOCATION, oldValue, getLocation()); + } + + @Override + public void setSubstrata(Collection<SubStrata> substrata) { + Object oldValue = new HashSet<>(getSubstrata()); + delegate.setSubstrata(substrata); + firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); + } + + @Override + public int sizeSubstrata() { + return delegate.sizeSubstrata(); + } + + @Override + public String getId() { + return delegate.getId(); + } + + @Override + public Integer getIdAsInt() { + return delegate.getIdAsInt(); + } + + @Override + public void setId(Integer id) { + Object oldValue = getId(); + delegate.setId(id); + firePropertyChanged(PROPERTY_ID, oldValue, getId()); + } + + @Override + public void setId(String id) { + Object oldValue = getId(); + delegate.setId(id); + firePropertyChanged(PROPERTY_ID, oldValue, getId()); + } + + public String getLabel() { + return getLocation() == null ? null : getLocation().getLabel(); + } + + public ZoneUIModel getZone() { + return zone; + } + + public void setZone(ZoneUIModel zone) { + Object oldValue = getZone(); + this.zone = zone; + firePropertyChange(PROPERTY_ZONE, oldValue, zone); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (!StrataUIModel.class.equals(obj.getClass())) { + return false; + } + if (getId() != null) { + return getId().equals(((StrataUIModel) obj).getId()); + } + if (getLocation() != null) { + return getLocation().equals(((StrataUIModel) obj).getLocation()); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(getId(), getLocation()); + } + + @Override + public String toString() { + return getLabel(); + } + + @Override + protected Strata newEntity() { + return Stratas.newStrata(); + } + +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/SubStrataUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/SubStrataUIModel.java new file mode 100644 index 0000000..a57aa4c --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/SubStrataUIModel.java @@ -0,0 +1,108 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; + +import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; +import fr.ifremer.tutti.persistence.entities.protocol.SubStratas; +import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation; +import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel; +import org.nuiton.util.beans.Binder; +import org.nuiton.util.beans.BinderFactory; + +import java.util.Objects; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class SubStrataUIModel extends AbstractTuttiBeanUIModel<SubStrata, SubStrataUIModel> implements SubStrata { + + protected final SubStrata delegate = SubStratas.newSubStrata(); + + protected static Binder<SubStrataUIModel, SubStrata> toBeanBinder = BinderFactory.newBinder(SubStrataUIModel.class, SubStrata.class); + + protected static Binder<SubStrata, SubStrataUIModel> fromBeanBinder = BinderFactory.newBinder(SubStrata.class, SubStrataUIModel.class); + + public SubStrataUIModel() { + super(fromBeanBinder, toBeanBinder); + } + + public SubStrataUIModel(TuttiLocation location) { + this(); + setLocation(location); + } + + @Override + public TuttiLocation getLocation() { + return delegate.getLocation(); + } + + @Override + public void setLocation(TuttiLocation location) { + Object oldValue = getLocation(); + delegate.setLocation(location); + firePropertyChanged(PROPERTY_LOCATION, oldValue, getLocation()); + } + + @Override + public String getId() { + return delegate.getId(); + } + + @Override + public void setId(Integer id) { + Object oldValue = getId(); + delegate.setId(id); + firePropertyChanged(PROPERTY_ID, oldValue, getId()); + } + + @Override + public void setId(String id) { + Object oldValue = getId(); + delegate.setId(id); + firePropertyChanged(PROPERTY_ID, oldValue, getId()); + } + + @Override + public Integer getIdAsInt() { + return delegate.getIdAsInt(); + } + + public String getLabel() { + return getLocation() == null ? null : getLocation().getLabel(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (!StrataUIModel.class.equals(obj.getClass())) { + return false; + } + if (getId() != null) { + return getId().equals(((StrataUIModel) obj).getId()); + } + if (getLocation() != null) { + return getLocation().equals(((StrataUIModel) obj).getLocation()); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(getId(), getLocation()); + } + + @Override + public String toString() { + return getLabel(); + } + + @Override + protected SubStrata newEntity() { + return SubStratas.newSubStrata(); + } + +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/ZoneUIModel.java similarity index 69% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneUIModel.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/ZoneUIModel.java index e01af7e..6ff1188 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneUIModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/ZoneUIModel.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones; +package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; import fr.ifremer.tutti.persistence.entities.protocol.Strata; import fr.ifremer.tutti.persistence.entities.protocol.Zone; @@ -30,14 +30,16 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp public void addAllStrata(Collection<Strata> strata) { Object oldValue = new HashSet<>(getStrata()); delegate.addAllStrata(strata); - firePropertyChanged(PROPERTY_STRATA, oldValue, strata); + updateStrataZone(strata, this); + firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); } @Override public void addStrata(Strata strata) { Object oldValue = new HashSet<>(getStrata()); delegate.addStrata(strata); - firePropertyChanged(PROPERTY_STRATA, oldValue, strata); + updateStrataZone(strata, this); + firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); } @Override @@ -65,6 +67,14 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp return delegate.getStrata(index); } + /** + * @param strata + * @return the strata from the stratas of the zone which equals the strata in parameters + */ + public Strata getStrata(Strata strata) { + return getStrata().stream().filter(s -> s != null && s.equals(strata)).findFirst().orElse(null); + } + @Override public boolean isStrataEmpty() { return delegate.isStrataEmpty(); @@ -74,7 +84,8 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp public boolean removeAllStrata(Collection<Strata> strata) { Object oldValue = new HashSet<>(getStrata()); boolean result = delegate.removeAllStrata(strata); - firePropertyChanged(PROPERTY_STRATA, oldValue, strata); + updateStrataZone(strata, null); + firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); return result; } @@ -82,7 +93,8 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp public boolean removeStrata(Strata strata) { Object oldValue = new HashSet<>(getStrata()); boolean result = delegate.removeStrata(strata); - firePropertyChanged(PROPERTY_STRATA, oldValue, strata); + updateStrataZone(strata, null); + firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); return result; } @@ -96,8 +108,10 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp @Override public void setStrata(Collection<Strata> strata) { Object oldValue = new HashSet<>(getStrata()); + updateStrataZone(getStrata(), null); delegate.setStrata(strata); - firePropertyChanged(PROPERTY_STRATA, oldValue, strata); + updateStrataZone(strata, this); + firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); } @Override @@ -130,7 +144,25 @@ public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> imp } @Override + public String toString() { + return getLabel(); + } + + @Override protected Zone newEntity() { return Zones.newZone(); } + + protected void updateStrataZone(Collection<Strata> stratas, ZoneUIModel zone) { + stratas.stream() + .filter(strata -> strata instanceof StrataUIModel) + .forEach(strata -> ((StrataUIModel) strata).setZone(zone)); + } + + protected void updateStrataZone(Strata strata, ZoneUIModel zone) { + if (strata instanceof StrataUIModel) { + ((StrataUIModel) strata).setZone(zone); + } + } + } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/StrataNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/StrataNode.java deleted file mode 100644 index 87255bc..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/StrataNode.java +++ /dev/null @@ -1,20 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes; - -import fr.ifremer.tutti.persistence.entities.protocol.Strata; - -import javax.swing.tree.DefaultMutableTreeNode; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class StrataNode extends DefaultMutableTreeNode { - - public StrataNode(Strata strata) { - super(strata, true); - } - - public Strata getStrata() { - return (Strata) userObject; - } -} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/SubStrataNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/SubStrataNode.java deleted file mode 100644 index 0cc705d..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/SubStrataNode.java +++ /dev/null @@ -1,20 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes; - -import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; - -import javax.swing.tree.DefaultMutableTreeNode; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class SubStrataNode extends DefaultMutableTreeNode { - - public SubStrataNode(SubStrata substrata) { - super(substrata, false); - } - - public SubStrata getSubstrata() { - return (SubStrata) userObject; - } -} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StrataNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StrataNode.java new file mode 100644 index 0000000..c4a4f03 --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StrataNode.java @@ -0,0 +1,20 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; + +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.StrataUIModel; + +import javax.swing.tree.DefaultMutableTreeNode; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class StrataNode extends DefaultMutableTreeNode { + + public StrataNode(StrataUIModel strata) { + super(strata, true); + } + + public StrataUIModel getStrata() { + return (StrataUIModel) userObject; + } +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/StratasTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StratasTreeModel.java similarity index 57% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/StratasTreeModel.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StratasTreeModel.java index 4a17b0f..b75dd47 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/StratasTreeModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StratasTreeModel.java @@ -1,9 +1,9 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones; +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; -import fr.ifremer.tutti.persistence.entities.protocol.Strata; +import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.SubStrataNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.StrataUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.SubStrataUIModel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -26,7 +26,7 @@ public class StratasTreeModel extends DefaultTreeModel { super(new DefaultMutableTreeNode()); } - public void removeStratas(Collection<Strata> stratasToRemove) { + public void removeStratas(Collection<StrataUIModel> stratasToRemove) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); @@ -66,7 +66,7 @@ public class StratasTreeModel extends DefaultTreeModel { // // } - public void addStratas(Collection<Strata> stratasToAdd) { + public void addStratas(Collection<StrataUIModel> stratasToAdd) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); @@ -78,7 +78,7 @@ public class StratasTreeModel extends DefaultTreeModel { strata.getSubstrata().forEach(substrata -> { if (substrata != null) { - SubStrataNode subStrataNode = new SubStrataNode(substrata); + SubStrataNode subStrataNode = new SubStrataNode((SubStrataUIModel) substrata); insertNodeInto(subStrataNode, strataNode, strataNode.getChildCount()); } @@ -88,14 +88,14 @@ public class StratasTreeModel extends DefaultTreeModel { } - public void addSubStratas(Collection<SubStrata> subStratas, Strata strata) { + public void addSubStratas(Collection<SubStrata> subStratas, StrataUIModel strata) { StrataNode strataNode = findStrataNode(strata); subStratas.forEach(substrata -> { if (substrata != null) { - SubStrataNode subStrataNode = new SubStrataNode(substrata); + SubStrataNode subStrataNode = new SubStrataNode((SubStrataUIModel) substrata); insertNodeInto(subStrataNode, strataNode, strataNode.getChildCount()); } @@ -103,7 +103,46 @@ public class StratasTreeModel extends DefaultTreeModel { } - protected StrataNode findStrataNode(Strata strata) { + public void updateSubStratas(StrataUIModel strata, + Collection<SubStrataUIModel> subStratasToAdd, + Collection<SubStrataUIModel> subStratasToRemove) { + + StrataNode strataNode = findStrataNode(strata); + updateSubStratas(strataNode, subStratasToAdd, subStratasToRemove); + + } + + public void updateSubStratas(StrataNode strataNode, + Collection<SubStrataUIModel> subStratasToAdd, + Collection<SubStrataUIModel> subStratasToRemove) { + + Preconditions.checkNotNull(strataNode); + + for (SubStrataUIModel subStrata : subStratasToRemove) { + + SubStrataNode subStrataNode = findSubStrataNode(subStrata, strataNode); + + if (log.isInfoEnabled()) { + log.info("remove subStrata " + subStrata.getLabel()); + } + + removeNodeFromParent(subStrataNode); + } + + for (SubStrataUIModel subStrata : subStratasToAdd) { + + SubStrataNode subStrataNode = new SubStrataNode(subStrata); + + if (log.isInfoEnabled()) { + log.info("add strata " + subStrata.getLabel()); + } + + insertNodeInto(subStrataNode, strataNode, strataNode.getChildCount()); + + } + } + + protected StrataNode findStrataNode(StrataUIModel strata) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); @@ -120,4 +159,20 @@ public class StratasTreeModel extends DefaultTreeModel { return strataNode; } + + protected SubStrataNode findSubStrataNode(SubStrataUIModel subStrata, StrataNode strataNode) { + + Enumeration strataChildren = strataNode.children(); + + SubStrataNode subStrataNode = null; + + while (subStrataNode == null && strataChildren.hasMoreElements()) { + SubStrataNode nextNode = (SubStrataNode) strataChildren.nextElement(); + if (subStrata.equals(nextNode.getSubstrata())) { + subStrataNode = nextNode; + } + } + + return subStrataNode; + } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/SubStrataNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/SubStrataNode.java new file mode 100644 index 0000000..0ce5a2f --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/SubStrataNode.java @@ -0,0 +1,20 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; + +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.SubStrataUIModel; + +import javax.swing.tree.DefaultMutableTreeNode; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class SubStrataNode extends DefaultMutableTreeNode { + + public SubStrataNode(SubStrataUIModel substrata) { + super(substrata, false); + } + + public SubStrataUIModel getSubstrata() { + return (SubStrataUIModel) userObject; + } +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java similarity index 70% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java index a319e9e..97aad64 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java @@ -1,11 +1,10 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones; +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; -import fr.ifremer.tutti.persistence.entities.protocol.Strata; import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; import fr.ifremer.tutti.persistence.entities.protocol.Zone; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.SubStrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.StrataUIModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import javax.swing.JTree; import javax.swing.tree.DefaultTreeCellRenderer; @@ -17,6 +16,9 @@ import java.awt.Component; */ public class ZoneEditorTreeCellRenderer extends DefaultTreeCellRenderer { + /** Logger. */ + private static final Log log = LogFactory.getLog(ZoneEditorTreeCellRenderer.class); + @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { @@ -25,8 +27,8 @@ public class ZoneEditorTreeCellRenderer extends DefaultTreeCellRenderer { String text = ""; if (value instanceof StrataNode) { - Strata strata = ((StrataNode) value).getStrata(); - text = strata.getLocation().getLabel(); + StrataUIModel strata = ((StrataNode) value).getStrata(); + text = strata.getLabel(); } else if (value instanceof SubStrataNode) { SubStrata substrata = ((SubStrataNode) value).getSubstrata(); diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/ZoneNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java similarity index 69% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/ZoneNode.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java index 8406978..924fa07 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/nodes/ZoneNode.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java @@ -1,6 +1,6 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.nodes; +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.ZoneUIModel; import javax.swing.tree.DefaultMutableTreeNode; diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZonesTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZonesTreeModel.java new file mode 100644 index 0000000..3ee80fe --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZonesTreeModel.java @@ -0,0 +1,202 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; + +import com.google.common.base.Preconditions; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.StrataUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.SubStrataUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.ZoneUIModel; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class ZonesTreeModel extends DefaultTreeModel { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ZonesTreeModel.class); + + public ZonesTreeModel() { + super(new DefaultMutableTreeNode()); + } + + public void removeZones(Collection<ZoneUIModel> zonesToRemove) { + + DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); + + Enumeration rootChildren = root.children(); + Collection<DefaultMutableTreeNode> nodesToRemove = new HashSet<>(); + + while (rootChildren.hasMoreElements()) { + + ZoneNode zoneNode = (ZoneNode) rootChildren.nextElement(); + + if (zonesToRemove.contains(zoneNode.getZone())) { + nodesToRemove.add(zoneNode); + } + } + + nodesToRemove.forEach(zoneNode ->removeNodeFromParent(zoneNode)); + } + + public void addZones(Collection<ZoneUIModel> zonesToAdd) { + + DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); + + zonesToAdd.forEach(zone -> { + + ZoneNode zoneNode = new ZoneNode(zone); + insertNodeInto(zoneNode, root, root.getChildCount()); + + }); + } + + public void updateStratas(ZoneUIModel zone, + Collection<StrataUIModel> stratasToAdd, + Collection<StrataUIModel> stratasToRemove) { + + ZoneNode zoneNode = findZoneNode(zone); + updateStratas(zoneNode, stratasToAdd, stratasToRemove); + } + + public void updateStratas(ZoneNode zoneNode, + Collection<StrataUIModel> stratasToAdd, + Collection<StrataUIModel> stratasToRemove) { + + Preconditions.checkNotNull(zoneNode); + + for (StrataUIModel strata : stratasToRemove) { + + StrataNode strataNode = findStrataNode(strata, zoneNode); + + if (log.isInfoEnabled()) { + log.info("remove strata " + strata.getLabel()); + } + + removeNodeFromParent(strataNode); + } + + for (StrataUIModel strata : stratasToAdd) { + + StrataNode strataNode = new StrataNode(strata); + + if (log.isInfoEnabled()) { + log.info("add strata " + strata.getLabel()); + } + + insertNodeInto(strataNode, zoneNode, zoneNode.getChildCount()); + + Set<SubStrataUIModel> subStratasToAdd = strata.getSubstrata() + .stream() + .map(subStrata -> (SubStrataUIModel) subStrata) + .collect(Collectors.toSet()); + + updateSubStratas(strataNode, subStratasToAdd, Collections.emptySet()); + } + } + + public void updateSubStratas(StrataUIModel strata, + Collection<SubStrataUIModel> subStratasToAdd, + Collection<SubStrataUIModel> subStratasToRemove) { + + StrataNode strataNode = findStrataNode(strata, strata.getZone()); + updateSubStratas(strataNode, subStratasToAdd, subStratasToRemove); + + } + + public void updateSubStratas(StrataNode strataNode, + Collection<SubStrataUIModel> subStratasToAdd, + Collection<SubStrataUIModel> subStratasToRemove) { + + Preconditions.checkNotNull(strataNode); + + for (SubStrataUIModel subStrata : subStratasToRemove) { + + SubStrataNode subStrataNode = findSubStrataNode(subStrata, strataNode); + + if (log.isInfoEnabled()) { + log.info("remove subStrata " + subStrata.getLabel()); + } + + removeNodeFromParent(subStrataNode); + } + + for (SubStrataUIModel subStrata : subStratasToAdd) { + + SubStrataNode subStrataNode = new SubStrataNode(subStrata); + + if (log.isInfoEnabled()) { + log.info("add strata " + subStrata.getLabel()); + } + + insertNodeInto(subStrataNode, strataNode, strataNode.getChildCount()); + + } + } + + protected ZoneNode findZoneNode(ZoneUIModel zone) { + + DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); + + Enumeration rootChildren = root.children(); + + ZoneNode zoneNode = null; + + while (zoneNode == null && rootChildren.hasMoreElements()) { + ZoneNode nextNode = (ZoneNode) rootChildren.nextElement(); + if (zone.equals(nextNode.getZone())) { + zoneNode = nextNode; + } + } + + return zoneNode; + } + + protected StrataNode findStrataNode(StrataUIModel strata, ZoneUIModel zone) { + + ZoneNode zoneNode = findZoneNode(zone); + + return findStrataNode(strata, zoneNode); + } + + protected StrataNode findStrataNode(StrataUIModel strata, ZoneNode zoneNode) { + + Enumeration zoneChildren = zoneNode.children(); + + StrataNode strataNode = null; + + while (strataNode == null && zoneChildren.hasMoreElements()) { + StrataNode nextNode = (StrataNode) zoneChildren.nextElement(); + if (strata.equals(nextNode.getStrata())) { + strataNode = nextNode; + } + } + + return strataNode; + } + + protected SubStrataNode findSubStrataNode(SubStrataUIModel subStrata, StrataNode strataNode) { + + Enumeration strataChildren = strataNode.children(); + + SubStrataNode subStrataNode = null; + + while (subStrataNode == null && strataChildren.hasMoreElements()) { + SubStrataNode nextNode = (SubStrataNode) strataChildren.nextElement(); + if (subStrata.equals(nextNode.getSubstrata())) { + subStrataNode = nextNode; + } + } + + return subStrataNode; + } +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.