branch feature/reecriture_editeur_de_zones created (now affd875)
This is an automated email from the git hooks/post-receive script. New change to branch feature/reecriture_editeur_de_zones in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git at affd875 Remise à plat de l'éditeur de zone qui ne fonctionne pas bien du tout This branch includes the following new commits: new affd875 Remise à plat de l'éditeur de zone qui ne fonctionne pas bien du tout The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit affd875586f41fd7a79fcf7377b07bdc023f5b48 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Mar 21 09:14:59 2016 +0100 Remise à plat de l'éditeur de zone qui ne fonctionne pas bien du tout -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/reecriture_editeur_de_zones in repository tutti. See https://gitlab.nuiton.org/codelutin/tutti.git commit affd875586f41fd7a79fcf7377b07bdc023f5b48 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Mar 21 09:14:59 2016 +0100 Remise à plat de l'éditeur de zone qui ne fonctionne pas bien du tout --- .../persistence/entities/TuttiEntityBean.java | 5 +- .../persistence/entities/protocol/SubStratas.java | 12 + .../entities/referential/TuttiLocationBean.java | 16 ++ tutti-service/src/test/data2 | 1 + .../content/protocol/EditProtocolUIHandler.java | 76 +++--- .../content/protocol/EditProtocolUIModel.java | 255 ++++++------------ .../protocol/zones/AvailableStratasTreeModel.java | 114 ++++++++ .../zones/{tree/ZoneNode.java => RootNode.java} | 16 +- .../protocol/zones/{tree => }/StrataNode.java | 13 +- .../protocol/zones/{tree => }/SubStrataNode.java | 18 +- ...eEditorNode.java => ZoneEditorNodeSupport.java} | 27 +- .../{tree => }/ZoneEditorTreeCellRenderer.java | 2 +- .../protocol/zones/ZoneEditorTreeModelSupport.java | 91 +++++++ .../swing/content/protocol/zones/ZoneEditorUI.jaxx | 11 +- .../swing/content/protocol/zones/ZoneEditorUI.jcss | 8 + .../protocol/zones/ZoneEditorUIHandler.java | 252 ++---------------- ...tractZoneEditorTreeModel.java => ZoneNode.java} | 21 +- .../content/protocol/zones/ZonesTreeModel.java | 295 +++++++++++++++++++++ .../protocol/zones/actions/AddStratasAction.java | 164 +++++++++--- .../protocol/zones/actions/CreateZoneAction.java | 16 +- .../protocol/zones/actions/DeleteZoneAction.java | 21 +- .../zones/actions/RemoveStratasAction.java | 128 ++++++--- .../protocol/zones/actions/RenameZoneAction.java | 5 +- .../protocol/zones/models/StrataUIModel.java | 249 ----------------- .../protocol/zones/models/SubStrataUIModel.java | 146 ---------- .../content/protocol/zones/models/ZoneUIModel.java | 228 ---------------- .../protocol/zones/tree/StratasTreeModel.java | 153 ----------- .../protocol/zones/tree/ZonesTreeModel.java | 229 ---------------- 28 files changed, 998 insertions(+), 1574 deletions(-) diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntityBean.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntityBean.java index 09df268..3c8acf3 100644 --- a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntityBean.java +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/TuttiEntityBean.java @@ -22,6 +22,7 @@ package fr.ifremer.tutti.persistence.entities; * #L% */ +import com.google.common.base.MoreObjects; import org.apache.commons.lang3.ObjectUtils; import org.nuiton.util.CollectionUtil; @@ -95,7 +96,9 @@ public abstract class TuttiEntityBean implements Serializable, TuttiEntity { @Override public String toString() { - return super.toString() + ": " + id; + return MoreObjects.toStringHelper(this) + .add(PROPERTY_ID, getId()) + .toString(); } protected <B> B getChild(Collection<B> child, int index) { diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/protocol/SubStratas.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/protocol/SubStratas.java new file mode 100644 index 0000000..c36d056 --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/protocol/SubStratas.java @@ -0,0 +1,12 @@ +package fr.ifremer.tutti.persistence.entities.protocol; + +import fr.ifremer.tutti.persistence.entities.referential.TuttiLocation; + +public class SubStratas extends AbstractSubStratas { + + public static SubStrata newSubStrata(TuttiLocation tuttiLocation) { + SubStrata subStrata = newSubStrata(); + subStrata.setLocation(tuttiLocation); + return subStrata; + } +} diff --git a/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TuttiLocationBean.java b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TuttiLocationBean.java new file mode 100644 index 0000000..8aa8bbf --- /dev/null +++ b/tutti-persistence/src/main/java/fr/ifremer/tutti/persistence/entities/referential/TuttiLocationBean.java @@ -0,0 +1,16 @@ +package fr.ifremer.tutti.persistence.entities.referential; + +import com.google.common.base.MoreObjects; + +public class TuttiLocationBean extends AbstractTuttiLocationBean { + + private static final long serialVersionUID = 7089001410149429815L; + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add(PROPERTY_ID, id) + .add(PROPERTY_NAME, name) + .toString(); + } +} diff --git a/tutti-service/src/test/data2 b/tutti-service/src/test/data2 new file mode 120000 index 0000000..0826fc7 --- /dev/null +++ b/tutti-service/src/test/data2 @@ -0,0 +1 @@ +/home/tchemit/Clients/tutti/db4Tests/services/ \ No newline at end of file diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java index aa1e242..793eb0b 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIHandler.java @@ -40,7 +40,9 @@ import fr.ifremer.tutti.persistence.entities.protocol.CaracteristicType; import fr.ifremer.tutti.persistence.entities.protocol.OperationFieldMappingRow; import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol; 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.protocol.SubStratas; import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol; import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocols; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; @@ -60,8 +62,6 @@ import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.Calcif import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorUI; import fr.ifremer.tutti.ui.swing.content.protocol.rtp.RtpCellEditor; import fr.ifremer.tutti.ui.swing.content.protocol.rtp.RtpCellRenderer; -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.util.AbstractTuttiUIHandler; import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil; import jaxx.runtime.SwingUtil; @@ -201,18 +201,17 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI // load cache data - List<Species> allSpecies = Lists.newArrayList(getDataContext().getSpecies()); + List<Species> allSpecies = new ArrayList<>(getDataContext().getSpecies()); model.setAllSpecies(allSpecies); - Multimap<String, Species> allSpeciesByTaxonId = - Speciess.splitByReferenceTaxonId(allSpecies); + Multimap<String, Species> allSpeciesByTaxonId = Speciess.splitByReferenceTaxonId(allSpecies); model.setAllSpeciesByTaxonId(allSpeciesByTaxonId); List<Species> referentSpecies = getDataContext().getReferentSpecies(); Map<String, Species> allReferentSpeciesByTaxonId = Speciess.splitReferenceSpeciesByReferenceTaxonId(referentSpecies); model.setAllReferentSpeciesByTaxonId(allReferentSpeciesByTaxonId); - List<Caracteristic> caracteristics = Lists.newArrayList(getDataContext().getCaracteristics()); + List<Caracteristic> caracteristics = new ArrayList<>(getDataContext().getCaracteristics()); model.setCaracteristics(caracteristics); Map<String, Caracteristic> allCaracteristic = TuttiEntities.splitById(caracteristics); @@ -342,8 +341,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI if (log.isDebugEnabled()) { log.debug("Will edit protocol with " + - speciesRows.size() + " protocolSpecies and " + - benthosRows.size() + " benthos declared."); + speciesRows.size() + " protocolSpecies and " + + benthosRows.size() + " benthos declared."); } } @@ -409,9 +408,9 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI addIndividualObservationColumnToModel(columnModel, table); addColumnToModel(columnModel, - RtpCellEditor.newEditor(ui), - new RtpCellRenderer(), - EditProtocolSpeciesTableModel.USE_RTP); + RtpCellEditor.newEditor(ui), + new RtpCellRenderer(), + EditProtocolSpeciesTableModel.USE_RTP); initTable(table, columnModel, @@ -472,9 +471,9 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI addIndividualObservationColumnToModel(columnModel, table); addColumnToModel(columnModel, - RtpCellEditor.newEditor(ui), - new RtpCellRenderer(), - EditProtocolSpeciesTableModel.USE_RTP); + RtpCellEditor.newEditor(ui), + new RtpCellRenderer(), + EditProtocolSpeciesTableModel.USE_RTP); initTable(table, columnModel, @@ -731,37 +730,42 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI Multimap<TuttiLocation, TuttiLocation> programStratasAndSubstratas = HashMultimap.create(persistenceService.getAllFishingOperationStratasAndSubstratas(programZone.getId())); - Set<TuttiLocation> usedLocations = new LinkedHashSet<>(); + Set<TuttiLocation> usedStrataLocations = new LinkedHashSet<>(); + Set<TuttiLocation> usedSubStrataLocations = new LinkedHashSet<>(); getModel().getZone().forEach(zone -> { Collection<Strata> zoneStratas = zone.getStrata(); - Set<TuttiLocation> locations = zoneStratas.stream().map(Strata::getLocation).collect(Collectors.toSet()); - usedLocations.addAll(locations); + Set<TuttiLocation> strataLocations = zoneStratas.stream() + .map(Strata::getLocation) + .collect(Collectors.toSet()); + usedStrataLocations.addAll(strataLocations); - zoneStratas.stream().map(Strata::getSubstrata).forEach(subStratas -> { - Set<TuttiLocation> locations2 = subStratas.stream().map(SubStrata::getLocation).collect(Collectors.toSet()); - usedLocations.addAll(locations2); - }); + Set<TuttiLocation> subStrataLocations = zoneStratas.stream() + .map(Strata::getSubstrata) + .flatMap(Collection::stream) + .map(SubStrata::getLocation) + .collect(Collectors.toSet()); + usedSubStrataLocations.addAll(subStrataLocations); }); // on transforme les données du service en modele d'ui - Collection<StrataUIModel> availableStratas = new HashSet<>(); + Collection<Strata> availableStratas = new HashSet<>(); programStratasAndSubstratas.keySet().stream().forEach(strataLocation -> { Collection<TuttiLocation> subStrataLocations = new HashSet<>(programStratasAndSubstratas.get(strataLocation)); subStrataLocations.remove(null); List<SubStrata> subStratas = subStrataLocations.stream() - .filter(tuttiLocation -> !usedLocations.contains(tuttiLocation)) - .map(SubStrataUIModel::new) - .collect(Collectors.toList()); - + .filter(tuttiLocation -> !usedSubStrataLocations.contains(tuttiLocation)) + .map(SubStratas::newSubStrata) + .collect(Collectors.toList()); - if (!(subStratas.isEmpty() && usedLocations.contains(strataLocation))) { + if (!(subStratas.isEmpty() && usedStrataLocations.contains(strataLocation))) { // the strata is already consumed - StrataUIModel strata = new StrataUIModel(strataLocation); + Strata strata = Stratas.newStrata(); + strata.setLocation(strataLocation); strata.setSubstrata(subStratas); availableStratas.add(strata); } @@ -823,8 +827,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI String lengthStepPmfmId = speciesProtocol.getLengthStepPmfmId(); Caracteristic lengthStepPmfm = allCaracteristic.get(lengthStepPmfmId); if (lengthStepPmfmId != null && - !lengthClassesPmfmId.contains(lengthStepPmfm) && - !model.containsLengthClassesPmfmId(lengthStepPmfmId)) { + !lengthClassesPmfmId.contains(lengthStepPmfm) && + !model.containsLengthClassesPmfmId(lengthStepPmfmId)) { if (log.isInfoEnabled()) { log.info("Found a new lengthStep pmfm: " + lengthStepPmfmId); } @@ -888,9 +892,9 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI List<CalcifiedPiecesSamplingEditorRowModel> speciesCpsRows = getModel().getCpsRows() - .stream() - .filter(row -> row.getProtocolSpecies().equals(result)) - .collect(Collectors.toList()); + .stream() + .filter(row -> row.getProtocolSpecies().equals(result)) + .collect(Collectors.toList()); if (!speciesCpsRows.isEmpty()) { String htmlMessage = String.format( @@ -1274,8 +1278,8 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI int type = e.getType(); if (type == TableModelEvent.DELETE || - type == TableModelEvent.INSERT || - e.getLastRow() == Integer.MAX_VALUE) { + type == TableModelEvent.INSERT || + e.getLastRow() == Integer.MAX_VALUE) { // get species column TableColumnExt tableColumn = @@ -1385,7 +1389,7 @@ public class EditProtocolUIHandler extends AbstractTuttiUIHandler<EditProtocolUI EditProtocolUIModel model = getModel(); String importColumn = row.getImportColumn(); return row.getType() != null && - (importColumn == null || model.numberOfRows(importColumn) < 2); + (importColumn == null || model.numberOfRows(importColumn) < 2); } protected void populateImportColumnTableEditors() { diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java index f0b6dd6..86a7974 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/EditProtocolUIModel.java @@ -38,21 +38,19 @@ import fr.ifremer.tutti.persistence.entities.protocol.Zone; import fr.ifremer.tutti.persistence.entities.referential.Caracteristic; import fr.ifremer.tutti.persistence.entities.referential.Species; import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorRowModel; -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 fr.ifremer.tutti.ui.swing.content.protocol.zones.AvailableStratasTreeModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.StrataNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZonesTreeModel; import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.mutable.MutableInt; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.nuiton.util.beans.Binder; import org.nuiton.util.beans.BinderFactory; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -64,9 +62,6 @@ import java.util.stream.Collectors; */ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, EditProtocolUIModel> implements TuttiProtocol { - /** Logger. */ - private static final Log log = LogFactory.getLog(EditProtocolUIModel.class); - private static final long serialVersionUID = 1L; public static final String PROPERTY_CARACTERISTIC_MAPPING_ROWS = "caracteristicMappingRows"; @@ -176,7 +171,9 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, protected List<CalcifiedPiecesSamplingEditorRowModel> cpsRows; - protected final Collection<StrataUIModel> availableStratas = new HashSet<>(); + protected final ZonesTreeModel zonesTreeModel = new ZonesTreeModel(); + + protected final AvailableStratasTreeModel availableStratasTreeModel = new AvailableStratasTreeModel(); protected static Binder<EditProtocolUIModel, TuttiProtocol> toBeanBinder = BinderFactory.newBinder(EditProtocolUIModel.class, @@ -194,26 +191,28 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, return TuttiProtocols.newTuttiProtocol(); } - @Override - public void fromEntity(TuttiProtocol entity) { - fromBeanBinder.copyExcluding(entity, this, PROPERTY_ZONE); - - List<Zone> zoneModels = entity.getZone() - .stream() - .map(zone -> { - ZoneUIModel zoneUIModel = new ZoneUIModel(); - zoneUIModel.fromEntity(zone); - return zoneUIModel; - }) - .collect(Collectors.toList()); - - setZone(zoneModels); - } +// @Override +// public void fromEntity(TuttiProtocol entity) { +// fromBeanBinder.copyExcluding(entity, this, PROPERTY_ZONE); +// +// entity.getZone().forEach(this::addZone); +// +// List<Zone> zoneModels = entity.getZone() +// .stream() +// .map(zone -> { +// ZoneUIModel zoneUIModel = new ZoneUIModel(); +// zoneUIModel.fromEntity(zone); +// return zoneUIModel; +// }) +// .collect(Collectors.toList()); +// +// setZone(zoneModels); +// } @Override public TuttiProtocol toEntity() { - TuttiProtocol result = newEntity(); - toBeanBinder.copyExcluding(this, result, PROPERTY_ZONE); + TuttiProtocol result = super.toEntity(); +// toBeanBinder.copyExcluding(this, result, PROPERTY_ZONE); Collection<EditProtocolCaracteristicsRowModel> protocolCaracteristicMappingRows = getCaracteristicMappingRows(); List<CaracteristicMappingRow> caracteristicMappingRows = new ArrayList<>(); @@ -228,7 +227,7 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, List<OperationFieldMappingRow> operationFieldMappingRows = new ArrayList<>(); for (EditProtocolOperationFieldsRowModel row : protocolOperationFieldMappingRows) { if (StringUtils.isNotBlank(row.getField()) && StringUtils.isNotBlank(row.getImportColumn()) - && row.isValid()) { + && row.isValid()) { operationFieldMappingRows.add(row.toEntity()); } } @@ -248,8 +247,8 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, Collection<CalcifiedPiecesSamplingEditorRowModel> cpsRows = cpsRowsBySpecies.get(row); protocol.setCalcifiedPiecesSamplingDefinition(cpsRows.stream() - .map(CalcifiedPiecesSamplingEditorRowModel::toEntity) - .collect(Collectors.toList())); + .map(CalcifiedPiecesSamplingEditorRowModel::toEntity) + .collect(Collectors.toList())); } } result.setSpecies(speciesProtocols); @@ -263,23 +262,23 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, Collection<CalcifiedPiecesSamplingEditorRowModel> cpsRows = cpsRowsBySpecies.get(row); protocol.setCalcifiedPiecesSamplingDefinition(cpsRows.stream() - .map(CalcifiedPiecesSamplingEditorRowModel::toEntity) - .collect(Collectors.toList())); + .map(CalcifiedPiecesSamplingEditorRowModel::toEntity) + .collect(Collectors.toList())); } } result.setBenthos(benthosProtocols); - Collection<Zone> zoneModels = getZone().stream() - .map(zone -> { - ZoneUIModel zoneUIModel = (ZoneUIModel) zone; - return zoneUIModel.toEntity(); - }) - .collect(Collectors.toList()); - - result.setZone(zoneModels); return result; } + public ZonesTreeModel getZonesTreeModel() { + return zonesTreeModel; + } + + public AvailableStratasTreeModel getAvailableStratasTreeModel() { + return availableStratasTreeModel; + } + public void setLengthClassesPmfm(List<Caracteristic> lengthClassesPmfm) { List<String> ids = Lists.newArrayList(TuttiEntities.collecIds(lengthClassesPmfm)); setLengthClassesPmfmId(ids); @@ -980,71 +979,77 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, firePropertyChanged(TuttiProtocol.PROPERTY_OPERATION_FIELD_MAPPING, null, getOperationFieldMapping()); } + public Collection<Strata> getAvailableStratas() { + return availableStratasTreeModel.getStratas(); + } + + public void setAvailableStratas(Collection<Strata> availableStratas) { + availableStratasTreeModel.setCreatingModel(true); + try { + + availableStratas.forEach(availableStratasTreeModel::addStrata); + } finally { + availableStratasTreeModel.setCreatingModel(false); + } + firePropertyChange(PROPERTY_AVAILABLE_STRATAS, Collections.emptyList(), getAvailableStratas()); + } + @Override - public Collection<Zone> getZone() { - return editObject.getZone(); + public List<Zone> getZone() { + return zonesTreeModel.getZones(); } @Override public void setZone(Collection<Zone> zones) { - Object oldZones = new ArrayList<>(getZone()); - editObject.setZone(zones); - firePropertyChanged(TuttiProtocol.PROPERTY_ZONE, oldZones, getZone()); + zonesTreeModel.setCreatingModel(true); + try { + zones.forEach(zonesTreeModel::addZone); + } finally { + zonesTreeModel.setCreatingModel(false); + } } @Override public Zone getZone(int index) { - return editObject.getZone(index); + return getZone().get(index); } @Override public boolean isZoneEmpty() { - return editObject.isZoneEmpty(); + return getZone().isEmpty(); } @Override public int sizeZone() { - return editObject.sizeZone(); + return getZone().size(); } @Override public void addZone(Zone zone) { - Object oldZones = new ArrayList<>(getZone()); - editObject.addZone(zone); - firePropertyChanged(TuttiProtocol.PROPERTY_ZONE, oldZones, getZone()); } @Override public void addAllZone(Collection<Zone> zones) { - Object oldZones = new ArrayList<>(getZone()); - editObject.addAllZone(zones); - firePropertyChanged(TuttiProtocol.PROPERTY_ZONE, oldZones, getZone()); } @Override public boolean removeZone(Zone zone) { - Object oldZones = new ArrayList<>(getZone()); - boolean result = editObject.removeZone(zone); - firePropertyChanged(TuttiProtocol.PROPERTY_ZONE, oldZones, getZone()); - return result; + return false; } @Override public boolean removeAllZone(Collection<Zone> zones) { - Object oldZones = new ArrayList<>(getZone()); - boolean result = editObject.removeAllZone(zones); - firePropertyChanged(TuttiProtocol.PROPERTY_ZONE, oldZones, getZone()); - return result; + return false; } @Override public boolean containsZone(Zone zone) { - return editObject.containsZone(zone); + return getZone().contains(zone); } @Override public boolean containsAllZone(Collection<Zone> zones) { - return editObject.containsAllZone(zones); + return false; } @Override @@ -1148,122 +1153,36 @@ public class EditProtocolUIModel extends AbstractTuttiBeanUIModel<TuttiProtocol, return mutableInt.intValue(); } - public Collection<StrataUIModel> getAvailableStratas() { - return availableStratas; - } - - public void setAvailableStratas(Collection<StrataUIModel> availableStratas) { - this.availableStratas.clear(); - addAllAvailableStratas(availableStratas); - } - - public void addAllAvailableStratas(Collection<StrataUIModel> availableStratas) { - Object oldValue = copyAvailableStratas(); - this.availableStratas.addAll(availableStratas); - firePropertyChange(PROPERTY_AVAILABLE_STRATAS, oldValue, getAvailableStratas()); - } - - public void addAvailableStrata(StrataUIModel availableStrata) { - Object oldValue = copyAvailableStratas(); - this.availableStratas.add(availableStrata); - firePropertyChange(PROPERTY_AVAILABLE_STRATAS, oldValue, getAvailableStratas()); - } - - public void removeAllAvailableStratas(Collection<StrataUIModel> stratas) { - - Object oldValue = copyAvailableStratas(); - - availableStratas.removeAll(stratas); - - firePropertyChange(PROPERTY_AVAILABLE_STRATAS, oldValue, getAvailableStratas()); - - } - - public void removeAvailableStrata(StrataUIModel strata) { - Object oldValue = copyAvailableStratas(); + public void moveStrataNodeToAvailableStratasTreeModel(StrataNode strataNode) { - availableStratas.remove(strata); + // Suppression de l'arbre des zones + zonesTreeModel.removeNodeFromParent(strataNode); - firePropertyChange(PROPERTY_AVAILABLE_STRATAS, oldValue, getAvailableStratas()); + Strata strata = strataNode.getUserObject(); - } + Optional<StrataNode> optionalStrataNode = availableStratasTreeModel.getStrataNode(strata); - public void moveStratasToZone(Collection<StrataUIModel> stratas, ZoneUIModel zone) { - if (log.isInfoEnabled()) { - log.info("stratas " + stratas); - } + if (optionalStrataNode.isPresent()) { - zone.addAllStrata(new ArrayList<>(stratas)); - removeAllAvailableStratas(stratas); - } + // la strate existe déjà dans l'univers des disponibles, on va juste pousser toutes les sous-strates + StrataNode newStrataNode = optionalStrataNode.get(); + Strata newStrata = newStrataNode.getUserObject(); + strata.getSubstrata().forEach(subStrata -> { - public void removeStratasFromZone(Collection<StrataUIModel> stratas) { - if (log.isInfoEnabled()) { - log.info("stratas " + stratas); - } + // ajout dans le modèle + newStrata.addSubstrata(subStrata); - stratas.forEach(strata -> strata.getZone().removeStrata(strata)); - addAllAvailableStratas(stratas); - } - - public void moveSubStratasToZone(Collection<SubStrataUIModel> subStratas, ZoneUIModel zone) { - if (log.isInfoEnabled()) { - log.info("substrats " + subStratas); - } - - subStratas.forEach(subStrata -> { - StrataUIModel strata = subStrata.getStrata(); - strata.removeSubstrata(subStrata); - - Strata zoneStrata = zone.getStrata(strata); - - if (zoneStrata == null) { - zoneStrata = new StrataUIModel(strata.getLocation()); - zone.addStrata(zoneStrata); - } + // ajout du nœud dans l'arbre des disponibles + availableStratasTreeModel.addSubsStrata(newStrataNode, subStrata); - zoneStrata.addSubstrata(subStrata); + }); - if (strata.isSubstrataEmpty()) { - removeAvailableStrata(strata); - } - }); + } else { - } + // on ajouter à l'univers des disponibles toute la strate + availableStratasTreeModel.addStrata(strata); - public void removeSubStratasFromZone(Collection<SubStrataUIModel> subStratas) { - if (log.isInfoEnabled()) { - log.info("subStratas " + subStratas); } - - subStratas.forEach(subStrata -> { - StrataUIModel strata = subStrata.getStrata(); - strata.removeSubstrata(subStrata); - - Optional<StrataUIModel> availableStrata = getStrata(strata); - if (!availableStrata.isPresent()) { - availableStrata = Optional.of(new StrataUIModel(strata.getLocation())); - addAvailableStrata(availableStrata.get()); - } - availableStrata.get().addSubstrata(subStrata); - - if (strata.isSubstrataEmpty()) { - strata.getZone().removeStrata(strata); - } - - }); - } - - /** - * @param strata - * @return the strata from the available stratas which equals the strata in parameters - */ - public Optional<StrataUIModel> getStrata(StrataUIModel strata) { - return availableStratas.stream().filter(s -> s != null && s.equals(strata)).findFirst(); - } - - protected List<StrataUIModel> copyAvailableStratas() { - return new ArrayList<>(availableStratas); } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/AvailableStratasTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/AvailableStratasTreeModel.java new file mode 100644 index 0000000..134e787 --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/AvailableStratasTreeModel.java @@ -0,0 +1,114 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones; + +/* + * #%L + * Tutti :: UI + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2016 Ifremer + * %% + * 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 3 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, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +import fr.ifremer.tutti.persistence.entities.protocol.Strata; +import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; + +import static org.nuiton.i18n.I18n.t; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class AvailableStratasTreeModel extends ZoneEditorTreeModelSupport { + + /** Logger. */ + private static final Log log = LogFactory.getLog(AvailableStratasTreeModel.class); + + /** + * Un cache des strates du modèle. + */ + private List<Strata> stratasCache; + + public AvailableStratasTreeModel() { + super(t("tutti.zoneEditor.availableStratas.root.label")); + } + + public StrataNode addStrata(Strata strata) { + + if (log.isInfoEnabled()) { + log.info("Add strata: " + strata); + } + StrataNode strataNode = new StrataNode(strata); + addNode(getRoot(), strataNode); + + strata.getSubstrata().forEach(subStrata -> addSubsStrata(strataNode, subStrata)); + + stratasCache = null; + return strataNode; + + } + + public boolean removeStrata(Strata strata) { + Enumeration children = getRoot().children(); + while (children.hasMoreElements()) { + StrataNode strataNode = (StrataNode) children.nextElement(); + if (strata.equals(strataNode.getUserObject())) { + if (log.isInfoEnabled()) { + log.info("Remove strata: " + strata); + } + strataNode.removeFromParent(); + stratasCache = null; + return true; + } + } + return false; + } + + public SubStrataNode addSubsStrata(StrataNode strataNode, SubStrata subStrata) { + if (log.isInfoEnabled()) { + log.info("Add subStrata: " + subStrata + " to strata node: " + strataNode); + } + SubStrataNode subStrataNode = new SubStrataNode(subStrata); + addNode(strataNode, subStrataNode); + return subStrataNode; + } + + public List<Strata> getStratas() { + if (stratasCache == null) { + stratasCache = new LinkedList<>(); + Enumeration children = getRoot().children(); + while (children.hasMoreElements()) { + StrataNode strataNode = (StrataNode) children.nextElement(); + stratasCache.add(strataNode.getUserObject()); + } + } + return stratasCache; + } + + public Optional<StrataNode> getStrataNode(Strata strata) { + int index = getStratas().indexOf(strata); + return Optional.ofNullable(index == -1 ? null : (StrataNode) getRoot().getChildAt(index)); + } + +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/RootNode.java similarity index 71% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/RootNode.java index 5672b53..7850593 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneNode.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/RootNode.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L @@ -24,20 +24,18 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; * #L% */ -import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.ZoneUIModel; - /** * @author Kevin Morin (Code Lutin) * @since 4.5 */ -public class ZoneNode extends ZoneEditorNode { +public class RootNode extends ZoneEditorNodeSupport { - public ZoneNode(ZoneUIModel zone) { - super(zone, true); + public RootNode(String userObject) { + super(userObject, true); } - public ZoneUIModel getZone() { - return (ZoneUIModel) userObject; + @Override + public String getUserObject() { + return (String) super.getUserObject(); } - } 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/StrataNode.java similarity index 73% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StrataNode.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/StrataNode.java index dc2a48b..66d5202 100644 --- 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/StrataNode.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L @@ -24,20 +24,21 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; * #L% */ -import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.StrataUIModel; +import fr.ifremer.tutti.persistence.entities.protocol.Strata; /** * @author Kevin Morin (Code Lutin) * @since 4.5 */ -public class StrataNode extends ZoneEditorNode { +public class StrataNode extends ZoneEditorNodeSupport { - public StrataNode(StrataUIModel strata) { + public StrataNode(Strata strata) { super(strata, true); } - public StrataUIModel getStrata() { - return (StrataUIModel) userObject; + @Override + public Strata getUserObject() { + return (Strata) super.getUserObject(); } } 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/SubStrataNode.java similarity index 66% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/SubStrataNode.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/SubStrataNode.java index 30176b8..f113061 100644 --- 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/SubStrataNode.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L @@ -24,19 +24,25 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; * #L% */ -import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.SubStrataUIModel; +import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; /** * @author Kevin Morin (Code Lutin) * @since 4.5 */ -public class SubStrataNode extends ZoneEditorNode { +public class SubStrataNode extends ZoneEditorNodeSupport { - public SubStrataNode(SubStrataUIModel substrata) { + public SubStrataNode(SubStrata substrata) { super(substrata, false); } - public SubStrataUIModel getSubstrata() { - return (SubStrataUIModel) userObject; + @Override + public SubStrata getUserObject() { + return (SubStrata) super.getUserObject(); + } + + @Override + public StrataNode getParent() { + return (StrataNode) super.getParent(); } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorNode.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorNodeSupport.java similarity index 70% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorNode.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorNodeSupport.java index 44f0c3b..d9ab563 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorNode.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorNodeSupport.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L @@ -33,38 +33,33 @@ import java.util.List; * @author Kevin Morin (Code Lutin) * @since 4.5 */ -public class ZoneEditorNode extends DefaultMutableTreeNode implements Comparable { +public abstract class ZoneEditorNodeSupport extends DefaultMutableTreeNode implements Comparable<ZoneEditorNodeSupport> { - public ZoneEditorNode(String label) { - super(label); - } - - public ZoneEditorNode(Object userObject, boolean allowchildren) { + public ZoneEditorNodeSupport(Object userObject, boolean allowchildren) { super(userObject, allowchildren); } - public void sortChildren() { - if (children != null) { - Collections.sort(children); - } - } - - public int getFutureNodePosition(ZoneEditorNode node) { + public int getFutureNodePosition(ZoneEditorNodeSupport node) { if (children == null) { return 0; } - List<ZoneEditorNode> orderedChildren = new ArrayList<>(children); + List<ZoneEditorNodeSupport> orderedChildren = new ArrayList<>(children); orderedChildren.add(node); Collections.sort(orderedChildren); return orderedChildren.indexOf(node); } @Override - public int compareTo(Object o) { + public int compareTo(ZoneEditorNodeSupport o) { if (o == null) { return 1; } return toString().compareTo(o.toString()); } + + @Override + public ZoneEditorNodeSupport getParent() { + return (ZoneEditorNodeSupport) super.getParent(); + } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java similarity index 95% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java index 1edf7e9..9e2a57f 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZoneEditorTreeCellRenderer.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeCellRenderer.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeModelSupport.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeModelSupport.java new file mode 100644 index 0000000..9015a9d --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorTreeModelSupport.java @@ -0,0 +1,91 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones; + +/* + * #%L + * Tutti :: UI + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2016 Ifremer + * %% + * 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 3 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, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + + +import javax.swing.tree.DefaultTreeModel; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public abstract class ZoneEditorTreeModelSupport extends DefaultTreeModel { + + /** + * Un drapeau pour bloquer les listeners lors de la création du modèle. + */ + private boolean creatingModel; + + public ZoneEditorTreeModelSupport(String rootLabel) { + super(new RootNode(rootLabel)); + } + + public void setCreatingModel(boolean creatingModel) { + this.creatingModel = creatingModel; + } + + public void addNode(ZoneEditorNodeSupport parent, ZoneEditorNodeSupport newChild) { + int index = parent.getFutureNodePosition(newChild); + insertNodeInto(newChild, parent, index); + } + + @Override + public RootNode getRoot() { + return (RootNode) super.getRoot(); + } + + @Override + protected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children) { + if (creatingModel) { + return; + } + super.fireTreeNodesChanged(source, path, childIndices, children); + } + + @Override + protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children) { + if (creatingModel) { + return; + } + super.fireTreeNodesInserted(source, path, childIndices, children); + } + + @Override + protected void fireTreeNodesRemoved(Object source, Object[] path, int[] childIndices, Object[] children) { + if (creatingModel) { + return; + } + super.fireTreeNodesRemoved(source, path, childIndices, children); + } + + @Override + protected void fireTreeStructureChanged(Object source, Object[] path, int[] childIndices, Object[] children) { + if (creatingModel) { + return; + } + super.fireTreeStructureChanged(source, path, childIndices, children); + } + +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jaxx b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jaxx index 70d82eb..4597420 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jaxx +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jaxx @@ -27,7 +27,7 @@ implements='fr.ifremer.tutti.ui.swing.util.TuttiUI<EditProtocolUIModel, ZoneEditorUIHandler>'> <import> - fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneEditorTreeCellRenderer + fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorTreeCellRenderer fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel fr.ifremer.tutti.ui.swing.util.TuttiUI fr.ifremer.tutti.ui.swing.util.TuttiUIUtil @@ -78,7 +78,8 @@ <cell weightx='0.5' weighty='1' fill='both'> <JScrollPane onFocusGained='availableStratasTree.requestFocus()'> <!-- List of the available stratas and substratas --> - <JTree id='availableStratasTree' onValueChanged="availableStratasTree.expandPath(event.getNewLeadSelectionPath())" + <JTree id='availableStratasTree' + onValueChanged="availableStratasTree.expandPath(event.getNewLeadSelectionPath())" onMouseClicked="handler.onMouseClickedOnAvailableStratas(event)" onKeyPressed="handler.onKeyPressedOnAvailableStratas(event)"/> <!--onFocusGained='handler.selectFirstRowIfNoSelection(event)'--> @@ -89,8 +90,8 @@ <cell anchor='north'> <JPanel layout='{new GridLayout(0,1)}'> - <JButton id='addButton' /> - <JButton id='removeButton' /> + <JButton id='addButton'/> + <JButton id='removeButton'/> </JPanel> </cell> @@ -98,7 +99,7 @@ <JScrollPane onFocusGained='zonesTree.requestFocus()'> <!-- List of the zones --> <JTree id='zonesTree' onMouseClicked="handler.onMouseClickedPressedOnZones(event, zonePopupMenu)" - onKeyPressed="handler.onKeyPressedOnZones(event)"/> + onKeyPressed="handler.onKeyPressedOnZones(event)"/> <!--onFocusGained='handler.selectFirstRowIfNoSelection(event)'--> <!--onMouseClicked='handler.onUniverseListClicked(event)'--> <!--onKeyPressed='handler.onKeyPressedOnUniverseList(event)'--> diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jcss b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jcss index cce689c..310ae8e 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jcss +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUI.jcss @@ -34,6 +34,14 @@ JToolBar { _help: {"tutti.zoneEditor.action.rename.help"}; } +#availableStratasTree { + model: {getModel().getAvailableStratasTreeModel()}; +} + +#zonesTree { + model: {getModel().getZonesTreeModel()}; +} + #expandZonesTree { actionIcon: expand; _simpleAction: {fr.ifremer.tutti.ui.swing.content.protocol.zones.actions.ExpandZonesTreeAction.class}; diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUIHandler.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUIHandler.java index b37fe1e..0ce2253 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUIHandler.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneEditorUIHandler.java @@ -25,14 +25,6 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones; */ import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel; -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 fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.StratasTreeModel; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.SubStrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZonesTreeModel; import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler; import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.JAXXWidgetUtil; @@ -47,17 +39,10 @@ import javax.swing.SwingUtilities; import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelListener; import javax.swing.event.TreeSelectionListener; -import javax.swing.tree.TreeModel; import javax.swing.tree.TreePath; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Optional; -import java.util.stream.Collectors; /** * @author Kevin Morin (Code Lutin) @@ -68,143 +53,17 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo /** Logger. */ private static final Log log = LogFactory.getLog(ZoneEditorUIHandler.class); - protected final PropertyChangeListener labelChangeListener; - - protected final PropertyChangeListener stratasChangeListener; - - protected final PropertyChangeListener zoneSubStratasChangeListener; - - protected final PropertyChangeListener availableSubStratasChangeListener; - - public ZoneEditorUIHandler() { - - availableSubStratasChangeListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - - StrataUIModel strata = (StrataUIModel) evt.getSource(); - - Collection<SubStrataUIModel> newSubStratas = (Collection<SubStrataUIModel>) evt.getNewValue(); - Collection<SubStrataUIModel> oldSubStratas = (Collection<SubStrataUIModel>) evt.getOldValue(); - - Collection<SubStrataUIModel> subStratasToAdd = new ArrayList<>(newSubStratas); - subStratasToAdd.removeAll(oldSubStratas); - - Collection<SubStrataUIModel> subStratasToRemove = new ArrayList<>(oldSubStratas); - subStratasToRemove.removeAll(newSubStratas); - - StratasTreeModel stratasTreeModel = (StratasTreeModel) ZoneEditorUIHandler.this.getUI().getAvailableStratasTree().getModel(); - stratasTreeModel.updateSubStratas(strata, subStratasToAdd, subStratasToRemove); - - } - }; - zoneSubStratasChangeListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - - StrataUIModel strata = (StrataUIModel) evt.getSource(); - - Collection<SubStrataUIModel> newSubStratas = (Collection<SubStrataUIModel>) evt.getNewValue(); - Collection<SubStrataUIModel> oldSubStratas = (Collection<SubStrataUIModel>) evt.getOldValue(); - - Collection<SubStrataUIModel> subStratasToAdd = new ArrayList<>(newSubStratas); - subStratasToAdd.removeAll(oldSubStratas); - - Collection<SubStrataUIModel> subStratasToRemove = new ArrayList<>(oldSubStratas); - subStratasToRemove.removeAll(newSubStratas); - - ZonesTreeModel zonesTreeModel = (ZonesTreeModel) ZoneEditorUIHandler.this.getUI().getZonesTree().getModel(); - zonesTreeModel.updateSubStratas(strata, subStratasToAdd, subStratasToRemove); - - } - }; - labelChangeListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - - ZoneUIModel zone = (ZoneUIModel) evt.getSource(); - ZonesTreeModel zonesTreeModel = (ZonesTreeModel) ZoneEditorUIHandler.this.getUI().getZonesTree().getModel(); - zonesTreeModel.zoneLabelChanged(zone); - } - }; - stratasChangeListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - - ZoneUIModel zone = (ZoneUIModel) evt.getSource(); - - Collection<StrataUIModel> newStratas = (Collection<StrataUIModel>) evt.getNewValue(); - Collection<StrataUIModel> oldStratas = (Collection<StrataUIModel>) evt.getOldValue(); - - Collection<StrataUIModel> stratasToAdd = new ArrayList<>(newStratas); - stratasToAdd.removeAll(oldStratas); - - Collection<StrataUIModel> stratasToRemove = new ArrayList<>(oldStratas); - stratasToRemove.removeAll(newStratas); - - stratasToRemove.forEach(strata -> strata.removePropertyChangeListener(StrataUIModel.PROPERTY_SUBSTRATA, - zoneSubStratasChangeListener)); - stratasToAdd.forEach(strata -> strata.addPropertyChangeListener(StrataUIModel.PROPERTY_SUBSTRATA, - zoneSubStratasChangeListener)); - - ZonesTreeModel zonesTreeModel = (ZonesTreeModel) ZoneEditorUIHandler.this.getUI().getZonesTree().getModel(); - zonesTreeModel.updateStratas(zone, stratasToAdd, stratasToRemove); - } - }; - } - @Override public void afterInit(ZoneEditorUI zoneEditorUI) { initUI(zoneEditorUI); - // init models - - getModel().addPropertyChangeListener(EditProtocolUIModel.PROPERTY_ZONE, - evt -> { - - Collection<ZoneUIModel> newZones = (Collection<ZoneUIModel>) evt.getNewValue(); - Collection<ZoneUIModel> oldZones = (Collection<ZoneUIModel>) evt.getOldValue(); - - Collection<ZoneUIModel> zonesToAdd = new ArrayList<>(newZones); - zonesToAdd.removeAll(oldZones); - - Collection<ZoneUIModel> zonesToRemove = new ArrayList<>(oldZones); - zonesToRemove.removeAll(newZones); - - updateZonesTreeModel(zonesToAdd, zonesToRemove); - - // si pas de zones avant, c'est soit que le modele etait vide, soit qu'on a initialisé l'arbre - if (oldZones.isEmpty()) { - SwingUtil.expandTree(zoneEditorUI.getZonesTree()); - } - - }); - - getModel().addPropertyChangeListener(EditProtocolUIModel.PROPERTY_AVAILABLE_STRATAS, evt -> { - - Collection<StrataUIModel> oldAvailableStratas = (Collection<StrataUIModel>) evt.getOldValue(); - Collection<StrataUIModel> newAvailableStratas = (Collection<StrataUIModel> ) evt.getNewValue(); - - Collection<StrataUIModel> stratasToAdd = new ArrayList<>(newAvailableStratas); - stratasToAdd.removeAll(oldAvailableStratas); - - Collection<StrataUIModel> stratasToRemove = new ArrayList<>(oldAvailableStratas); - stratasToRemove.removeAll(newAvailableStratas); - - updateAvailableStratasTreeModel(stratasToAdd, stratasToRemove); - }); - // init trees - TreeModel availableStratasTreeModel = new StratasTreeModel(); - final JTree availableStratasTree = ui.getAvailableStratasTree(); - availableStratasTree.setModel(availableStratasTreeModel); + JTree availableStratasTree = ui.getAvailableStratasTree(); SwingUtil.addExpandOnClickListener(availableStratasTree); - TreeModel zonesTreeModel = new ZonesTreeModel(); - final JTree zonesTree = ui.getZonesTree(); - zonesTree.setModel(zonesTreeModel); + JTree zonesTree = ui.getZonesTree(); SwingUtil.addExpandOnClickListener(zonesTree); zonesTree.addTreeSelectionListener(event -> { @@ -223,7 +82,7 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo for (TreePath selectedPath : selectedPaths) { Object lastPathComponent = selectedPath.getLastPathComponent(); if (!(lastPathComponent instanceof StrataNode - || lastPathComponent instanceof SubStrataNode)) { + || lastPathComponent instanceof SubStrataNode)) { allStrataNode = false; break; } @@ -238,19 +97,21 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo TreeSelectionListener enableAddStrataListener = evt -> { boolean addButtonEnabled = zonesTree.getSelectionCount() == 1 - && zonesTree.getSelectionPath().getLastPathComponent() instanceof ZoneNode - && availableStratasTree.getSelectionCount() >= 1; + && zonesTree.getSelectionPath().getLastPathComponent() instanceof ZoneNode + && availableStratasTree.getSelectionCount() >= 1; getUI().getAddButton().setEnabled(addButtonEnabled); - + }; zonesTree.addTreeSelectionListener(enableAddStrataListener); availableStratasTree.addTreeSelectionListener(enableAddStrataListener); // expand nodes when children are added - zonesTreeModel.addTreeModelListener(new TreeModelListener() { + getModel().getZonesTreeModel().addTreeModelListener(new TreeModelListener() { @Override - public void treeNodesChanged(TreeModelEvent e) { getModel().setModify(true); } + public void treeNodesChanged(TreeModelEvent e) { + getModel().setModify(true); + } @Override public void treeNodesInserted(TreeModelEvent e) { @@ -258,16 +119,19 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo } @Override - public void treeNodesRemoved(TreeModelEvent e) { } + public void treeNodesRemoved(TreeModelEvent e) { + } @Override - public void treeStructureChanged(TreeModelEvent e) { } + public void treeStructureChanged(TreeModelEvent e) { + } }); - availableStratasTreeModel.addTreeModelListener(new TreeModelListener() { + getModel().getAvailableStratasTreeModel().addTreeModelListener(new TreeModelListener() { @Override - public void treeNodesChanged(TreeModelEvent e) { } + public void treeNodesChanged(TreeModelEvent e) { + } @Override public void treeNodesInserted(TreeModelEvent e) { @@ -275,10 +139,12 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo } @Override - public void treeNodesRemoved(TreeModelEvent e) { } + public void treeNodesRemoved(TreeModelEvent e) { + } @Override - public void treeStructureChanged(TreeModelEvent e) { } + public void treeStructureChanged(TreeModelEvent e) { + } }); // get data @@ -299,20 +165,20 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo @Override public void onCloseUI() { - getModel().getZone().forEach(zone -> ((ZoneUIModel) zone).removePropertyChangeListener(ZoneUIModel.PROPERTY_STRATA, - stratasChangeListener)); +// getModel().getZone().forEach(zone -> ((ZoneUIModel) zone).removePropertyChangeListener(ZoneUIModel.PROPERTY_STRATA, +// stratasChangeListener)); } public void onKeyPressedOnZones(KeyEvent e) { - if (e.getKeyCode()== KeyEvent.VK_ENTER && getUI().getRemoveButton().isEnabled()) { + if (e.getKeyCode() == KeyEvent.VK_ENTER && getUI().getRemoveButton().isEnabled()) { getContext().getActionEngine().runAction(getUI().getRemoveButton()); } } public void onKeyPressedOnAvailableStratas(KeyEvent e) { - if (e.getKeyCode()== KeyEvent.VK_ENTER && getUI().getAddButton().isEnabled()) { + if (e.getKeyCode() == KeyEvent.VK_ENTER && getUI().getAddButton().isEnabled()) { getContext().getActionEngine().runAction(getUI().getAddButton()); } } @@ -351,72 +217,4 @@ public class ZoneEditorUIHandler extends AbstractTuttiUIHandler<EditProtocolUIMo } } - protected void updateZonesTreeModel(Collection<ZoneUIModel> zonesToAdd, Collection<ZoneUIModel> zonesToRemove) { - JTree zonesTree = getUI().getZonesTree(); - ZonesTreeModel zonesTreeModel = (ZonesTreeModel) zonesTree.getModel(); - - getModel().removeStratasFromZone(zonesToRemove.stream() - .map(ZoneUIModel::getStrata) - .flatMap(Collection::stream) - .map(strata -> (StrataUIModel) strata) - .collect(Collectors.toSet())); - - zonesToAdd.stream() - .map(ZoneUIModel::getStrata) - .flatMap(Collection::stream) - .map(strata -> (StrataUIModel) strata) - .forEach(strata -> { - - Optional<StrataUIModel> availableStrata = getModel().getStrata(strata); - // if the strata is available, then remove it from the available strata (idem for the substrata - if (availableStrata.isPresent()) { - - if (strata.getSubstrata().isEmpty()) { - getModel().removeAvailableStrata(availableStrata.get()); - - } else { - availableStrata.get().removeAllSubstrata(strata.getSubstrata() - .stream() - .map(subStrata -> (SubStrataUIModel) subStrata) - .collect(Collectors.toSet())); - - if (availableStrata.get().isSubstrataEmpty()) { - getModel().removeAvailableStrata(availableStrata.get()); - } - } - } - }); - - zonesTreeModel.removeZones(zonesToRemove); - - zonesTreeModel.addZones(zonesToAdd); - - zonesToRemove.forEach(zone -> { - zone.removePropertyChangeListener(ZoneUIModel.PROPERTY_STRATA, stratasChangeListener); - zone.removePropertyChangeListener(ZoneUIModel.PROPERTY_LABEL, labelChangeListener); - }); - zonesToAdd.forEach(zone -> { - zone.addPropertyChangeListener(ZoneUIModel.PROPERTY_STRATA, stratasChangeListener); - zone.addPropertyChangeListener(ZoneUIModel.PROPERTY_LABEL, labelChangeListener); - }); - - } - - protected void updateAvailableStratasTreeModel(Collection<StrataUIModel> stratasToAdd, - Collection<StrataUIModel> stratasToRemove) { - - JTree availableStratasTree = getUI().getAvailableStratasTree(); - StratasTreeModel availableStratasTreeModel = (StratasTreeModel) availableStratasTree.getModel(); - - availableStratasTreeModel.removeStratas(stratasToRemove); - - availableStratasTreeModel.addStratas(stratasToAdd); - - stratasToRemove.forEach(strata -> strata.removePropertyChangeListener(StrataUIModel.PROPERTY_SUBSTRATA, - availableSubStratasChangeListener)); - stratasToAdd.forEach(strata -> strata.addPropertyChangeListener(StrataUIModel.PROPERTY_SUBSTRATA, - availableSubStratasChangeListener)); - - } - } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/AbstractZoneEditorTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneNode.java similarity index 64% rename from tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/AbstractZoneEditorTreeModel.java rename to tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneNode.java index 8135eb0..3a2f9d0 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/AbstractZoneEditorTreeModel.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZoneNode.java @@ -1,4 +1,4 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; +package fr.ifremer.tutti.ui.swing.content.protocol.zones; /* * #%L @@ -24,20 +24,25 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; * #L% */ -import javax.swing.tree.DefaultTreeModel; +import fr.ifremer.tutti.persistence.entities.protocol.Zone; /** * @author Kevin Morin (Code Lutin) * @since 4.5 */ -public abstract class AbstractZoneEditorTreeModel extends DefaultTreeModel { +public class ZoneNode extends ZoneEditorNodeSupport { - public AbstractZoneEditorTreeModel(String rootLabel) { - super(new ZoneEditorNode(rootLabel)); + public ZoneNode(Zone zone) { + super(zone, true); } - public void addNode(ZoneEditorNode newChild, ZoneEditorNode parent) { - int index = parent.getFutureNodePosition(newChild); - insertNodeInto(newChild, parent, index); + @Override + public Zone getUserObject() { + return (Zone) super.getUserObject(); + } + + @Override + public RootNode getParent() { + return (RootNode) super.getParent(); } } 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 new file mode 100644 index 0000000..ca40526 --- /dev/null +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/ZonesTreeModel.java @@ -0,0 +1,295 @@ +package fr.ifremer.tutti.ui.swing.content.protocol.zones; + +/* + * #%L + * Tutti :: UI + * $Id:$ + * $HeadURL:$ + * %% + * Copyright (C) 2012 - 2016 Ifremer + * %% + * 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 3 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, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.List; + +import static org.nuiton.i18n.I18n.t; + +/** + * @author Kevin Morin (Code Lutin) + * @since 4.5 + */ +public class ZonesTreeModel extends ZoneEditorTreeModelSupport { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ZonesTreeModel.class); + + /** + * Un cache des zones du modèle. + */ + private List<Zone> zonesCache; + + public ZonesTreeModel() { + super(t("tutti.zoneEditor.zones.root.label")); + } + + public List<Zone> getZones() { + if (zonesCache == null) { + zonesCache = new LinkedList<>(); + Enumeration children = getRoot().children(); + while (children.hasMoreElements()) { + ZoneNode zoneNode = (ZoneNode) children.nextElement(); + zonesCache.add(zoneNode.getUserObject()); + } + } + return zonesCache; + } + + public ZoneNode addZone(Zone zone) { + + if (log.isInfoEnabled()) { + log.info("Add zone: " + zone); + } + ZoneNode zoneNode = new ZoneNode(zone); + addNode(getRoot(), zoneNode); + + zone.getStrata().forEach(strata -> { + + StrataNode strataNode = addStrata(zoneNode, strata); + strata.getSubstrata().forEach(subStrata -> addSubsStrata(strataNode, subStrata)); + + }); + + zonesCache = null; + return zoneNode; + + } + + public boolean removeZone(Zone zone) { + Enumeration children = getRoot().children(); + while (children.hasMoreElements()) { + ZoneNode zoneNode = (ZoneNode) children.nextElement(); + if (zone.equals(zoneNode.getUserObject())) { + if (log.isInfoEnabled()) { + log.info("Remove zone: " + zone); + } + zoneNode.removeFromParent(); + zonesCache = null; + return true; + } + } + return false; + } + + public StrataNode addStrata(ZoneNode zoneNode, Strata strata) { + if (log.isInfoEnabled()) { + log.info("Add strata: " + strata + " to zone node: " + zoneNode); + } + StrataNode strataNode = new StrataNode(strata); + addNode(zoneNode, strataNode); + return strataNode; + } + + public SubStrataNode addSubsStrata(StrataNode strataNode, SubStrata subStrata) { + if (log.isInfoEnabled()) { + log.info("Add subStrata: " + subStrata + " to strata node: " + strataNode); + } + SubStrataNode subStrataNode = new SubStrataNode(subStrata); + addNode(strataNode, subStrataNode); + return subStrataNode; + } + + // public void removeZones(Collection<ZoneUIModel> zonesToRemove) { +// +// ZoneEditorNode root = (ZoneEditorNode) getRoot(); +// +// Enumeration rootChildren = root.children(); +// Collection<DefaultMutableTreeNode> nodesToRemove = new HashSet<>(); +// +// while (rootChildren.hasMoreElements()) { +// +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode zoneNode = (fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode) rootChildren.nextElement(); +// +// if (zonesToRemove.contains(zoneNode.getUserObject())) { +// nodesToRemove.add(zoneNode); +// } +// } +// +// nodesToRemove.forEach(this::removeNodeFromParent); +// } + +// public void addZones(Collection<ZoneUIModel> zonesToAdd) { +// +// ZoneEditorNode root = (ZoneEditorNode) getRoot(); +// +// zonesToAdd.forEach(zone -> { +// +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode zoneNode = new fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode(zone); +// addNode(zoneNode, root); +// +// Set<StrataUIModel> stratasToAdd = zone.getStrata() +// .stream() +// .map(strata -> (StrataUIModel) strata) +// .collect(Collectors.toSet()); +// +// updateStratas(zoneNode, stratasToAdd, Collections.emptySet()); +// +// }); +// +// } +// +// public void updateStratas(ZoneUIModel zone, +// Collection<StrataUIModel> stratasToAdd, +// Collection<StrataUIModel> stratasToRemove) { +// +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode zoneNode = findZoneNode(zone); +// updateStratas(zoneNode, stratasToAdd, stratasToRemove); +// } +// +// public void updateStratas(fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.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); +// +// addNode(strataNode, zoneNode); +// +// 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); +// +// removeNodeFromParent(subStrataNode); +// } +// +// for (SubStrataUIModel subStrata : subStratasToAdd) { +// +// SubStrataNode subStrataNode = new SubStrataNode(subStrata); +// +// addNode(subStrataNode, strataNode); +// +// } +// +// } +// +// public fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode findZoneNode(ZoneUIModel zone) { +// +// DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); +// +// Enumeration rootChildren = root.children(); +// +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode zoneNode = null; +// +// while (zoneNode == null && rootChildren.hasMoreElements()) { +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode nextNode = (fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode) rootChildren.nextElement(); +// if (zone.equals(nextNode.getUserObject())) { +// zoneNode = nextNode; +// } +// } +// +// return zoneNode; +// } +// +// public StrataNode findStrataNode(StrataUIModel strata, ZoneUIModel zone) { +// +// fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode zoneNode = findZoneNode(zone); +// +// return findStrataNode(strata, zoneNode); +// } +// +// public 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.getUserObject())) { +// strataNode = nextNode; +// } +// } +// +// return strataNode; +// } +// +// public 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.getUserObject())) { +// subStrataNode = nextNode; +// } +// } +// +// return subStrataNode; +// } + +// public void zoneLabelChanged(Zone zone) { +// nodeChanged(findZoneNode(zone)); +// } +} diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/AddStratasAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/AddStratasAction.java index e4c8e1b..9431e2c 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/AddStratasAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/AddStratasAction.java @@ -24,22 +24,28 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions; * #L% */ -import fr.ifremer.tutti.persistence.entities.protocol.SubStrata; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import fr.ifremer.tutti.persistence.entities.protocol.Strata; +import fr.ifremer.tutti.persistence.entities.protocol.Zone; +import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.AvailableStratasTreeModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.StrataNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.SubStrataNode; import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI; -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 fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.SubStrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZonesTreeModel; import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import javax.swing.JTree; import javax.swing.tree.TreePath; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; /** * @author Kevin Morin (Code Lutin) @@ -47,6 +53,9 @@ import java.util.stream.Collectors; */ public class AddStratasAction extends SimpleActionSupport<ZoneEditorUI> { + /** Logger. */ + private static final Log log = LogFactory.getLog(AddStratasAction.class); + public AddStratasAction(ZoneEditorUI zoneEditorUI) { super(zoneEditorUI); setEnabled(false); @@ -55,49 +64,134 @@ public class AddStratasAction extends SimpleActionSupport<ZoneEditorUI> { @Override protected void onActionPerformed(ZoneEditorUI zoneEditorUI) { + Optional<ZoneNode> optionalSelectedZoneNode = getSelectedZone(zoneEditorUI); + if (!optionalSelectedZoneNode.isPresent()) { + if (log.isInfoEnabled()) { + log.info("No zone selected"); + } + return; + } + JTree availableStratasTree = zoneEditorUI.getAvailableStratasTree(); - TreePath[] selectedStratas = availableStratasTree.getSelectionPaths(); + TreePath[] selectedAvailableStrataPaths = availableStratasTree.getSelectionPaths(); - JTree zonesTree = zoneEditorUI.getZonesTree(); - TreePath selectedZonePath = zonesTree.getSelectionPath(); + if (selectedAvailableStrataPaths == null) { - if (selectedZonePath != null) { - ZoneNode zoneNode = (ZoneNode) selectedZonePath.getPathComponent(1); - ZoneUIModel selectedZone = zoneNode.getZone(); + if (log.isInfoEnabled()) { + log.info("No available stratas, nor subStratas selected"); + } + return; + } - Set<StrataUIModel> stratasToAdd = new HashSet<>(); - Set<SubStrataUIModel> subStratasToAdd = new HashSet<>(); + Set<StrataNode> strataNodesToAdd = getStrataNodesToAdd(selectedAvailableStrataPaths); + Multimap<StrataNode, SubStrataNode> subStrataNodesToAddByStrataNode = getSubStrataNodesToAdd(selectedAvailableStrataPaths, strataNodesToAdd); + Set<SubStrataNode> subStrataNodesToAdd = new LinkedHashSet<>(); - for (TreePath treePath : selectedStratas) { + subStrataNodesToAddByStrataNode.asMap().entrySet().forEach(entry -> { + StrataNode strataNode = entry.getKey(); + Collection<SubStrataNode> subStrataNodes = entry.getValue(); + if (strataNode.getChildCount() == subStrataNodes.size()) { - Object node = treePath.getLastPathComponent(); + // toute la strate est sélectionnée + strataNodesToAdd.add(strataNode); + if (log.isInfoEnabled()) { + log.info("Add full strata " + strataNode + " to add"); + } + } else { + subStrataNodesToAdd.addAll(subStrataNodes); - if (node instanceof StrataNode) { + } + }); - StrataUIModel strata = ((StrataNode) node).getStrata(); - stratasToAdd.add(strata); + ZoneNode selectedZoneNode = optionalSelectedZoneNode.get(); + Zone selectedZone = selectedZoneNode.getUserObject(); - } else if (node instanceof SubStrataNode) { - SubStrataUIModel subStrata = ((SubStrataNode) node).getSubstrata(); - subStratasToAdd.add(subStrata); + EditProtocolUIModel model = zoneEditorUI.getModel(); + AvailableStratasTreeModel availableStratasTreeModel = model.getAvailableStratasTreeModel(); + ZonesTreeModel zonesTreeModel = model.getZonesTreeModel(); - } else { - // the root node is selected, add all - stratasToAdd.addAll(zoneEditorUI.getModel().getAvailableStratas()); - } + strataNodesToAdd.forEach(strataNode -> { + + // suppression de l'arbre des disponibles + availableStratasTreeModel.removeNodeFromParent(strataNode); + + // ajout au modèle + selectedZone.addStrata(strataNode.getUserObject()); + + // ajout du nœud dans l'arbre + zonesTreeModel.addNode(selectedZoneNode, strataNode); + + }); + + subStrataNodesToAdd.forEach(subStrataNode -> { + + // suppression de l'arbre des disponibles + availableStratasTreeModel.removeNodeFromParent(subStrataNode); + + StrataNode strataNode = subStrataNode.getParent(); + + Strata strata = strataNode.getUserObject(); + + if (!selectedZone.containsStrata(strata)) { + + // ajout au modèle + selectedZone.addStrata(strata); + + // ajout du nœud dans l'arbre + strataNode = zonesTreeModel.addStrata(selectedZoneNode, strata); } - Collection<SubStrata> alreadyAddedSubStratas = stratasToAdd.stream() - .map(StrataUIModel::getSubstrata) - .flatMap(Collection::stream) - .collect(Collectors.toSet()); + // ajout au modèle + strata.addSubstrata(subStrataNode.getUserObject()); + + // ajout du nœud dans l'arbre + zonesTreeModel.addNode(strataNode, subStrataNode); + + }); - subStratasToAdd.removeAll(alreadyAddedSubStratas); + } + + protected Optional<ZoneNode> getSelectedZone(ZoneEditorUI zoneEditorUI) { + + JTree zonesTree = zoneEditorUI.getZonesTree(); + TreePath selectedZonePath = zonesTree.getSelectionPath(); + return Optional.ofNullable(selectedZonePath == null ? null : (ZoneNode) selectedZonePath.getPathComponent(1)); + } + + protected Set<StrataNode> getStrataNodesToAdd(TreePath[] selectedStratas) { + Set<StrataNode> result = new HashSet<>(); + for (TreePath selectedStrata : selectedStratas) { + + Object node = selectedStrata.getLastPathComponent(); + if (node instanceof StrataNode) { + StrataNode strataNode = (StrataNode) node; + if (log.isInfoEnabled()) { + log.info("found strata " + strataNode + " to add"); + } + result.add(strataNode); + } - zoneEditorUI.getModel().moveStratasToZone(stratasToAdd, selectedZone); - zoneEditorUI.getModel().moveSubStratasToZone(subStratasToAdd, selectedZone); } + return result; + } + protected Multimap<StrataNode, SubStrataNode> getSubStrataNodesToAdd(TreePath[] selectedStratas, Set<StrataNode> stratasNodeToAdd) { + Multimap<StrataNode, SubStrataNode> result = ArrayListMultimap.create(); + for (TreePath selectedStrata : selectedStratas) { + + Object node = selectedStrata.getLastPathComponent(); + if (node instanceof SubStrataNode) { + SubStrataNode subStrataNode = (SubStrataNode) node; + StrataNode strataNode = subStrataNode.getParent(); + if (!stratasNodeToAdd.contains(strataNode)) { + if (log.isInfoEnabled()) { + log.info("found subStrata " + subStrataNode + " to add"); + } + result.put(strataNode, subStrataNode); + } + } + } + return result; } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/CreateZoneAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/CreateZoneAction.java index 12fd8ad..14e55bd 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/CreateZoneAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/CreateZoneAction.java @@ -24,10 +24,11 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions; * #L% */ +import fr.ifremer.tutti.persistence.entities.protocol.Zone; +import fr.ifremer.tutti.persistence.entities.protocol.Zones; +import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel; import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.models.ZoneUIModel; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZonesTreeModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneNode; import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport; import org.apache.commons.lang3.StringUtils; @@ -56,13 +57,16 @@ public class CreateZoneAction extends SimpleActionSupport<ZoneEditorUI> { JOptionPane.QUESTION_MESSAGE); if (StringUtils.isNotEmpty(zoneLabel)) { - ZoneUIModel zone = new ZoneUIModel(); + + Zone zone = Zones.newZone(); zone.setLabel(zoneLabel); - zoneEditorUI.getModel().addZone(zone); + + EditProtocolUIModel model = zoneEditorUI.getModel(); + + ZoneNode zoneNode = model.getZonesTreeModel().addZone(zone); //select newly created node JTree zonesTree = zoneEditorUI.getZonesTree(); - ZoneNode zoneNode = ((ZonesTreeModel) zonesTree.getModel()).findZoneNode(zone); TreePath path = new TreePath(zoneNode.getPath()); zonesTree.setSelectionPath(path); } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/DeleteZoneAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/DeleteZoneAction.java index 30db550..691f0aa 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/DeleteZoneAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/DeleteZoneAction.java @@ -25,13 +25,16 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions; */ import fr.ifremer.tutti.persistence.entities.protocol.Zone; +import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.StrataNode; import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneNode; import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport; import javax.swing.JOptionPane; import javax.swing.JTree; import javax.swing.tree.TreePath; +import java.util.Enumeration; import static org.nuiton.i18n.I18n.t; @@ -53,7 +56,9 @@ public class DeleteZoneAction extends SimpleActionSupport<ZoneEditorUI> { if (zonesTree.getSelectionCount() == 1) { TreePath selectedPath = zonesTree.getSelectionPath(); - Zone zone = ((ZoneNode) selectedPath.getPathComponent(1)).getZone(); + ZoneNode zoneNode = (ZoneNode) selectedPath.getPathComponent(1); + + Zone zone = zoneNode.getUserObject(); int confirmDeletion = JOptionPane.showConfirmDialog(zoneEditorUI, t("tutti.zoneEditor.deleteZone.message", zone), @@ -62,7 +67,17 @@ public class DeleteZoneAction extends SimpleActionSupport<ZoneEditorUI> { JOptionPane.QUESTION_MESSAGE); if (confirmDeletion == JOptionPane.YES_OPTION) { - zoneEditorUI.getModel().removeZone(zone); + + EditProtocolUIModel model = zoneEditorUI.getModel(); + + Enumeration children = zoneNode.children(); + while (children.hasMoreElements()) { + + StrataNode strateNode = (StrataNode) children.nextElement(); + model.moveStrataNodeToAvailableStratasTreeModel(strateNode); + + } + } } } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RemoveStratasAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RemoveStratasAction.java index 12d0e6e..f3b9c78 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RemoveStratasAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RemoveStratasAction.java @@ -24,23 +24,24 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions; * #L% */ +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.ui.swing.content.protocol.EditProtocolUIModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.AvailableStratasTreeModel; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.StrataNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.SubStrataNode; import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI; -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.tree.StrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.SubStrataNode; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZonesTreeModel; import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.swing.JTree; import javax.swing.tree.TreePath; -import java.util.Collection; import java.util.HashSet; +import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; /** * @author Kevin Morin (Code Lutin) @@ -62,54 +63,101 @@ public class RemoveStratasAction extends SimpleActionSupport<ZoneEditorUI> { JTree zonesTree = zoneEditorUI.getZonesTree(); TreePath[] selectedStratas = zonesTree.getSelectionPaths(); - Set<StrataUIModel> stratasToRemove = new HashSet<>(); - Set<SubStrataUIModel> subStratasToRemove = new HashSet<>(); + if (selectedStratas == null) { - if (selectedStratas!=null) { - for (TreePath treePath : selectedStratas) { + if (log.isInfoEnabled()) { + log.info("No selected stratas, nor subStratas selected"); + } + return; + } - Object node = treePath.getLastPathComponent(); + // récupération des strates à supprimer + Set<StrataNode> stratasNodeToRemove = getStrataNodesToRemove(selectedStratas); - if (node instanceof ZoneNode) { + // récupération des sous-strates à supprimer + Set<SubStrataNode> subStratasNodeToRemove = getSubStrataNodesToRemove(selectedStratas, stratasNodeToRemove); -// ZoneUIModel zone = ((ZoneNode) node).getZone(); -// if (log.isInfoEnabled()) { -// log.info("remove zone stratas " + zone.getLabel()); -// } -// stratasToRemove.addAll(zone.getStrata()); + EditProtocolUIModel model = zoneEditorUI.getModel(); + AvailableStratasTreeModel availableStratasTreeModel = model.getAvailableStratasTreeModel(); + ZonesTreeModel zonesTreeModel = model.getZonesTreeModel(); - } else if (node instanceof StrataNode) { + stratasNodeToRemove.forEach(model::moveStrataNodeToAvailableStratasTreeModel); - StrataUIModel strata = ((StrataNode) node).getStrata(); - if (log.isInfoEnabled()) { - log.info("remove strata " + strata.getLabel()); - } - stratasToRemove.add(strata); + subStratasNodeToRemove.forEach(subStrataNode -> { - } else if (node instanceof SubStrataNode) { - SubStrataUIModel subStrata = ((SubStrataNode) node).getSubstrata(); - if (log.isInfoEnabled()) { - log.info("remove subtrata " + subStrata.getLabel() + " from " + subStrata.getStrata()); - } - subStratasToRemove.add(subStrata); - } + // Suppression de l'arbre des zones + zonesTreeModel.removeNodeFromParent(subStrataNode); + + SubStrata subStrata = subStrataNode.getUserObject(); + + StrataNode strataNode = subStrataNode.getParent(); + + Strata strata = strataNode.getUserObject(); + + Optional<StrataNode> optionalStrataNode = availableStratasTreeModel.getStrataNode(strata); + + if (optionalStrataNode.isPresent()) { + + // la strate existe déjà dans l'univers des disponibles, on va juste pousser la sous-strate + StrataNode newStrataNode = optionalStrataNode.get(); + + // ajout dans le modèle + newStrataNode.getUserObject().addSubstrata(subStrata); + + // ajout du nœud dans l'arbre des disponibles + availableStratasTreeModel.addSubsStrata(newStrataNode, subStrata); + } else { + + // la strate n'existe pas, on peut ajouter la strate puis la sous-strate + Strata newStrata = Stratas.newStrata(); + newStrata.setLocation(strata.getLocation()); + newStrata.addSubstrata(subStrata); + + // ajout du nœud dans l'arbre des disponibles + availableStratasTreeModel.addStrata(newStrata); } + }); - Collection<SubStrata> alreadyRemovedSubStratas = stratasToRemove.stream() - .map(StrataUIModel::getSubstrata) - .flatMap(Collection::stream) - .collect(Collectors.toSet()); + // select zone path + zonesTree.setSelectionPath(selectedStratas[0].getParentPath()); - subStratasToRemove.removeAll(alreadyRemovedSubStratas); + } - zoneEditorUI.getModel().removeStratasFromZone(stratasToRemove); - zoneEditorUI.getModel().removeSubStratasFromZone(subStratasToRemove); + protected Set<StrataNode> getStrataNodesToRemove(TreePath[] selectedStratas) { + Set<StrataNode> result = new HashSet<>(); + for (TreePath selectedStrata : selectedStratas) { - // select zone path - zonesTree.setSelectionPath(selectedStratas[0].getParentPath()); + Object node = selectedStrata.getLastPathComponent(); + if (node instanceof StrataNode) { + StrataNode strataNode = (StrataNode) node; + if (log.isInfoEnabled()) { + log.info("found strata " + strataNode + " to remove"); + } + result.add((StrataNode) node); + } } + return result; + } + + protected Set<SubStrataNode> getSubStrataNodesToRemove(TreePath[] selectedStratas, Set<StrataNode> stratasNodeToRemove) { + Set<SubStrataNode> result = new HashSet<>(); + for (TreePath selectedStrata : selectedStratas) { + Object node = selectedStrata.getLastPathComponent(); + if (node instanceof SubStrataNode) { + SubStrataNode subStrataNode = (SubStrataNode) node; + if (!stratasNodeToRemove.contains(subStrataNode.getParent())) { + if (log.isInfoEnabled()) { + log.info("found subStrata " + subStrataNode + " to remove"); + } + result.add(subStrataNode); + } + } + + } + return result; } + } diff --git a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RenameZoneAction.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RenameZoneAction.java index 49587e4..126d8da 100644 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RenameZoneAction.java +++ b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/actions/RenameZoneAction.java @@ -26,7 +26,7 @@ package fr.ifremer.tutti.ui.swing.content.protocol.zones.actions; import fr.ifremer.tutti.persistence.entities.protocol.Zone; import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneEditorUI; -import fr.ifremer.tutti.ui.swing.content.protocol.zones.tree.ZoneNode; +import fr.ifremer.tutti.ui.swing.content.protocol.zones.ZoneNode; import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport; import org.apache.commons.lang3.StringUtils; @@ -55,7 +55,7 @@ public class RenameZoneAction extends SimpleActionSupport<ZoneEditorUI> { TreePath selectedPath = zonesTree.getSelectionPath(); ZoneNode zoneNode = (ZoneNode) selectedPath.getPathComponent(1); - Zone zone = zoneNode.getZone(); + Zone zone = zoneNode.getUserObject(); String zoneLabel = (String) JOptionPane.showInputDialog(zoneEditorUI, t("tutti.zoneEditor.renameZone.message", zone.getLabel()), @@ -67,6 +67,7 @@ public class RenameZoneAction extends SimpleActionSupport<ZoneEditorUI> { if (StringUtils.isNotEmpty(zoneLabel)) { zone.setLabel(zoneLabel); + zoneEditorUI.getModel().getZonesTreeModel().nodeChanged(zoneNode); } } } 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 deleted file mode 100644 index 15ae412..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/StrataUIModel.java +++ /dev/null @@ -1,249 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; - -/* - * #%L - * Tutti :: UI - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2016 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -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 fromEntity(Strata entity) { - fromBeanBinder.copyExcluding(entity, this, PROPERTY_SUBSTRATA); - entity.getSubstrata().forEach(subStrata -> { - SubStrataUIModel copy = new SubStrataUIModel(); - copy.fromEntity(subStrata); - addSubstrata(copy); - }); - } - - @Override - public Strata toEntity() { - Strata result = newEntity(); - toBeanBinder.copyExcluding(this, result, PROPERTY_SUBSTRATA); - getSubstrata().forEach(subStrata -> result.addSubstrata(((SubStrataUIModel) subStrata).toEntity())); - return result; - } - - @Override - public void addAllSubstrata(Collection<SubStrata> substrata) { - Object oldValue = new HashSet<>(getSubstrata()); - delegate.addAllSubstrata(substrata); - updateSubStrataStrata(substrata, this); - firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); - } - - @Override - public void addSubstrata(SubStrata substrata) { - Object oldValue = new HashSet<>(getSubstrata()); - delegate.addSubstrata(substrata); - updateSubStrataStrata(substrata, this); - 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); - updateSubStrataStrata(substrata, null); - firePropertyChanged(PROPERTY_SUBSTRATA, oldValue, getSubstrata()); - return result; - } - - @Override - public boolean removeSubstrata(SubStrata substrata) { - Object oldValue = new HashSet<>(getSubstrata()); - boolean result = delegate.removeSubstrata(substrata); - updateSubStrataStrata(substrata, null); - 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()); - updateSubStrataStrata(getSubstrata(), null); - delegate.setSubstrata(substrata); - updateSubStrataStrata(getSubstrata(), this); - 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(); - } - - protected void updateSubStrataStrata(Collection<SubStrata> subStratas, StrataUIModel strata) { - subStratas.forEach(subStrata -> updateSubStrataStrata(subStrata, strata)); - } - - protected void updateSubStrataStrata(SubStrata subStrata, StrataUIModel strata) { - if (subStrata instanceof SubStrataUIModel) { - ((SubStrataUIModel) subStrata).setStrata(strata); - } - } -} 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 deleted file mode 100644 index d7b58da..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/SubStrataUIModel.java +++ /dev/null @@ -1,146 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; - -/* - * #%L - * Tutti :: UI - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2016 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -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 { - - public static final String PROPERTY_STRATA = "strata"; - - protected final SubStrata delegate = SubStratas.newSubStrata(); - - protected StrataUIModel strata; - - 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(); - } - - public StrataUIModel getStrata() { - return strata; - } - - public void setStrata(StrataUIModel strata) { - Object oldValue = getStrata(); - this.strata = strata; - firePropertyChange(PROPERTY_STRATA, oldValue, strata); - } - - @Override - public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (obj == this) { - return true; - } - if (!SubStrataUIModel.class.equals(obj.getClass())) { - return false; - } - if (getId() != null) { - return getId().equals(((SubStrataUIModel) obj).getId()); - } - if (getLocation() != null) { - return getLocation().equals(((SubStrataUIModel) 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/models/ZoneUIModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/ZoneUIModel.java deleted file mode 100644 index f4e48d7..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/models/ZoneUIModel.java +++ /dev/null @@ -1,228 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.models; - -/* - * #%L - * Tutti :: UI - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2016 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import fr.ifremer.tutti.persistence.entities.protocol.Strata; -import fr.ifremer.tutti.persistence.entities.protocol.Zone; -import fr.ifremer.tutti.persistence.entities.protocol.Zones; -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; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class ZoneUIModel extends AbstractTuttiBeanUIModel<Zone, ZoneUIModel> implements Zone { - - protected final Zone delegate = Zones.newZone(); - - protected static Binder<ZoneUIModel, Zone> toBeanBinder = BinderFactory.newBinder(ZoneUIModel.class, Zone.class); - - protected static Binder<Zone, ZoneUIModel> fromBeanBinder = BinderFactory.newBinder(Zone.class, ZoneUIModel.class); - - public ZoneUIModel() { - super(fromBeanBinder, toBeanBinder); - } - - @Override - public void fromEntity(Zone entity) { - fromBeanBinder.copyExcluding(entity, this, PROPERTY_STRATA); - entity.getStrata().forEach(strata -> { - StrataUIModel copy = new StrataUIModel(); - copy.fromEntity(strata); - addStrata(copy); - }); - } - - @Override - public Zone toEntity() { - Zone result = newEntity(); - toBeanBinder.copyExcluding(this, result, PROPERTY_STRATA); - getStrata().forEach(strata -> result.addStrata(((StrataUIModel) strata).toEntity())); - return result; - } - - @Override - public void addAllStrata(Collection<Strata> stratas) { - Object oldValue = new HashSet<>(getStrata()); - - stratas.forEach(strata -> { - Strata existingStrata = getStrata(strata); - if (existingStrata != null) { - existingStrata.addAllSubstrata(strata.getSubstrata()); - - } else { - delegate.addStrata(strata); - } - }); - - updateStrataZone(stratas, this); - - firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); - } - - @Override - public void addStrata(Strata strata) { - Object oldValue = new HashSet<>(getStrata()); - - Strata existingStrata = getStrata(strata); - if (existingStrata != null) { - existingStrata.addAllSubstrata(strata.getSubstrata()); - - } else { - delegate.addStrata(strata); - } - - updateStrataZone(strata, this); - - firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); - } - - @Override - public boolean containsAllStrata(Collection<Strata> strata) { - return delegate.containsAllStrata(strata); - } - - @Override - public boolean containsStrata(Strata strata) { - return delegate.containsStrata(strata); - } - - @Override - public String getLabel() { - return delegate.getLabel(); - } - - @Override - public Collection<Strata> getStrata() { - return delegate.getStrata(); - } - - @Override - public Strata getStrata(int index) { - 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(); - } - - @Override - public boolean removeAllStrata(Collection<Strata> strata) { - Object oldValue = new HashSet<>(getStrata()); - boolean result = delegate.removeAllStrata(strata); - updateStrataZone(strata, null); - firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); - return result; - } - - @Override - public boolean removeStrata(Strata strata) { - Object oldValue = new HashSet<>(getStrata()); - boolean result = delegate.removeStrata(strata); - updateStrataZone(strata, null); - firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); - return result; - } - - @Override - public void setLabel(String label) { - Object oldValue = getLabel(); - delegate.setLabel(label); - firePropertyChanged(PROPERTY_LABEL, oldValue, label); - } - - @Override - public void setStrata(Collection<Strata> strata) { - Object oldValue = new HashSet<>(getStrata()); - updateStrataZone(getStrata(), null); - delegate.setStrata(strata); - updateStrataZone(strata, this); - firePropertyChanged(PROPERTY_STRATA, oldValue, getStrata()); - } - - @Override - public int sizeStrata() { - return delegate.sizeStrata(); - } - - @Override - public void setId(String id) { - Object oldValue = getId(); - delegate.setId(id); - firePropertyChanged(PROPERTY_ID, oldValue, id); - } - - @Override - public void setId(Integer id) { - Object oldValue = getId(); - delegate.setId(id); - firePropertyChanged(PROPERTY_ID, oldValue, id); - } - - @Override - public Integer getIdAsInt() { - return delegate.getIdAsInt(); - } - - @Override - public String getId() { - return delegate.getId(); - } - - @Override - public String toString() { - return getLabel(); - } - - @Override - protected Zone newEntity() { - return Zones.newZone(); - } - - protected void updateStrataZone(Collection<Strata> stratas, ZoneUIModel zone) { - stratas.forEach(strata -> updateStrataZone(strata, 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/tree/StratasTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StratasTreeModel.java deleted file mode 100644 index 9129e3f..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/StratasTreeModel.java +++ /dev/null @@ -1,153 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; - -/* - * #%L - * Tutti :: UI - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2016 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -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 javax.swing.tree.DefaultMutableTreeNode; -import java.util.Collection; -import java.util.Enumeration; -import java.util.HashSet; - -import static org.nuiton.i18n.I18n.t; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class StratasTreeModel extends AbstractZoneEditorTreeModel { - - public StratasTreeModel() { - super(t("tutti.zoneEditor.availableStratas.root.label")); - } - - public void removeStratas(Collection<StrataUIModel> stratasToRemove) { - - ZoneEditorNode root = (ZoneEditorNode) getRoot(); - - Enumeration rootChildren = root.children(); - Collection<DefaultMutableTreeNode> nodesToRemove = new HashSet<>(); - - while (rootChildren.hasMoreElements()) { - - StrataNode strataNode = (StrataNode) rootChildren.nextElement(); - - if (stratasToRemove.contains(strataNode.getStrata())) { - nodesToRemove.add(strataNode); - } - } - - nodesToRemove.forEach(this::removeNodeFromParent); - } - - public void addStratas(Collection<StrataUIModel> stratasToAdd) { - - ZoneEditorNode root = (ZoneEditorNode) getRoot(); - - stratasToAdd.forEach(strata -> { - - StrataNode strataNode = new StrataNode(strata); - addNode(strataNode, root); - - strata.getSubstrata().forEach(substrata -> { - - if (substrata != null) { - SubStrataNode subStrataNode = new SubStrataNode((SubStrataUIModel) substrata); - addNode(subStrataNode, strataNode); - } - - }); - - }); - - } - - 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); - - removeNodeFromParent(subStrataNode); - } - - for (SubStrataUIModel subStrata : subStratasToAdd) { - - SubStrataNode subStrataNode = new SubStrataNode(subStrata); - - addNode(subStrataNode, strataNode); - - } - } - - public StrataNode findStrataNode(StrataUIModel strata) { - - DefaultMutableTreeNode root = (DefaultMutableTreeNode) getRoot(); - - Enumeration rootChildren = root.children(); - - StrataNode strataNode = null; - - while (strataNode == null && rootChildren.hasMoreElements()) { - StrataNode nextNode = (StrataNode) rootChildren.nextElement(); - if (strata.equals(nextNode.getStrata())) { - strataNode = nextNode; - } - } - - return strataNode; - } - - public 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/ZonesTreeModel.java b/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZonesTreeModel.java deleted file mode 100644 index 9be292a..0000000 --- a/tutti-ui-swing/src/main/java/fr/ifremer/tutti/ui/swing/content/protocol/zones/tree/ZonesTreeModel.java +++ /dev/null @@ -1,229 +0,0 @@ -package fr.ifremer.tutti.ui.swing.content.protocol.zones.tree; - -/* - * #%L - * Tutti :: UI - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 - 2016 Ifremer - * %% - * 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 3 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, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -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 java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -import static org.nuiton.i18n.I18n.t; - -/** - * @author Kevin Morin (Code Lutin) - * @since 4.5 - */ -public class ZonesTreeModel extends AbstractZoneEditorTreeModel { - - /** Logger. */ - private static final Log log = LogFactory.getLog(ZonesTreeModel.class); - - public ZonesTreeModel() { - super(t("tutti.zoneEditor.zones.root.label")); - } - - public void removeZones(Collection<ZoneUIModel> zonesToRemove) { - - ZoneEditorNode root = (ZoneEditorNode) 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(this::removeNodeFromParent); - } - - public void addZones(Collection<ZoneUIModel> zonesToAdd) { - - ZoneEditorNode root = (ZoneEditorNode) getRoot(); - - zonesToAdd.forEach(zone -> { - - ZoneNode zoneNode = new ZoneNode(zone); - addNode(zoneNode, root); - - Set<StrataUIModel> stratasToAdd = zone.getStrata() - .stream() - .map(strata -> (StrataUIModel) strata) - .collect(Collectors.toSet()); - - updateStratas(zoneNode, stratasToAdd, Collections.emptySet()); - - }); - - } - - 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); - - addNode(strataNode, zoneNode); - - 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); - - removeNodeFromParent(subStrataNode); - } - - for (SubStrataUIModel subStrata : subStratasToAdd) { - - SubStrataNode subStrataNode = new SubStrataNode(subStrata); - - addNode(subStrataNode, strataNode); - - } - - } - - public 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; - } - - public StrataNode findStrataNode(StrataUIModel strata, ZoneUIModel zone) { - - ZoneNode zoneNode = findZoneNode(zone); - - return findStrataNode(strata, zoneNode); - } - - public 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; - } - - public 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; - } - - public void zoneLabelChanged(ZoneUIModel zone) { - nodeChanged(findZoneNode(zone)); - } -} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.
participants (1)
-
codelutin.com scm