@Override public void alignCollapseAll(LocaleId trgLoc) { ContainerIterator ci = new ContainerIterator(trgLoc); // keeping track of collapsed containers to check which to set to // ALIGNED LinkedList<TextContainer> collapsed = new LinkedList<TextContainer>(); if (ci.hasSource()) { ci.getSource().joinAll(); ci.getSource().setHasBeenSegmentedFlag(false); collapsed.add(ci.getSource()); } if (ci.hasTarget()) { ci.getTarget().joinAll(); ci.getTarget().setHasBeenSegmentedFlag(false); collapsed.add(ci.getTarget()); } // mark target/source pairs aligned if both have been collapsed TextContainer src, trg; for (LocaleId loc : parent.getTargetLocales()) { src = getSource(loc); if (collapsed.contains(src)) { trg = parent.getTarget(loc); if (collapsed.contains(trg)) { trg.getSegments().setAlignmentStatus(AlignmentStatus.ALIGNED); // TODO: check that this is the desired behavior trg.getFirstSegment().id = src.getFirstSegment().getId(); } } } }
@Override public void setSegment(int index, Segment seg, LocaleId trgLoc) { ISegments segs = getSource(trgLoc).getSegments(); String oldId = segs.get(index).id; String newId = seg.id; boolean idChanged = !newId.equals(oldId); int theIndex = index; Segment tempSeg; ContainerIterator ci = new ContainerIterator(trgLoc); if (ci.hasSource()) ci.getSource().getSegments().set(theIndex, seg.clone()); if (ci.hasTarget()) { segs = ci.getTarget().getSegments(); segs.set(segs.getIndex(oldId), seg.clone()); } // update the ids if (idChanged) { ci = new ContainerIterator(trgLoc); if (ci.hasSource()) { tempSeg = ci.getSource().getSegments().get(oldId); if (tempSeg != null) tempSeg.id = newId; } if (ci.hasTarget()) { tempSeg = ci.getTarget().getSegments().get(oldId); if (tempSeg != null) tempSeg.id = newId; } } }
@Override public Segment splitSource(LocaleId trgLoc, Segment srcSeg, int splitPos) { TextContainer theSource = getSource(trgLoc); ISegments srcSegs = theSource.getSegments(); int segIndex = srcSegs.getIndex(srcSeg.id); if (segIndex == -1) return null; // segment id not found in the container int partIndex = srcSegs.getPartIndex(segIndex); // split the source theSource.split(partIndex, splitPos, splitPos, false); Segment newSeg = srcSegs.get(segIndex + 1); ISegments currentSegs; Segment currentSeg; ContainerIterator ci = new ContainerIterator(trgLoc); // inserting new segments in all the places they go if (ci.hasTarget()) { currentSegs = ci.getTarget().getSegments(); currentSeg = currentSegs.get(srcSeg.id); if (currentSeg != null) currentSegs.insert(currentSegs.getIndex(srcSeg.id) + 1, new Segment(srcSeg.id, new TextFragment(""))); } return newSeg; }
@Override public Segment splitTarget(LocaleId trgLoc, Segment trgSeg, int splitPos) { TextContainer theTarget = parent.createTarget(trgLoc, false, IResource.COPY_SEGMENTATION); ISegments trgSegs = theTarget.getSegments(); int segIndex = trgSegs.getIndex(trgSeg.id); if (segIndex == -1) return null; // segment id not found in the container int partIndex = trgSegs.getPartIndex(segIndex); // split the source theTarget.split(partIndex, splitPos, splitPos, false); Segment newSeg = trgSegs.get(segIndex + 1); ISegments currentSegs; Segment currentSeg; ContainerIterator ci = new ContainerIterator(trgLoc); // inserting new segments in all the places they go if (ci.hasSource()) { currentSegs = ci.getSource().getSegments(); currentSeg = currentSegs.get(trgSeg.id); if (currentSeg != null) currentSegs.insert(currentSegs.getIndex(trgSeg.id) + 1, new Segment(trgSeg.id, new TextFragment(""))); } return newSeg; }
private void insertOrAppend(boolean append, int index, Segment srcSeg, Segment trgSeg, LocaleId trgLoc) { Segment sourceSeg = (srcSeg != null ? srcSeg : new Segment(null, new TextFragment(""))); Segment targetSeg = (trgSeg != null ? trgSeg : new Segment(null, new TextFragment(""))); String originalId = null; String insertedId = null; ContainerIterator ci = new ContainerIterator(trgLoc); if (append) { targetSeg.id = sourceSeg.id; // make sure ids match if (ci.hasSource()) ci.getSource().getSegments().append(sourceSeg); if (ci.hasTarget()) ci.getTarget().getSegments().append(targetSeg); } else { // insert // get id at insertion location originalId = getSource(trgLoc).getSegments().get(index).id; if (ci.hasSource()) insertedId = doInsert(ci.getSource(), index, null, null, sourceSeg); if (ci.hasTarget()) insertedId = doInsert(ci.getTarget(), index, originalId, insertedId, targetSeg); } }
@Override public boolean remove(Segment seg, LocaleId trgLoc) { int count = 0; ContainerIterator ci = new ContainerIterator(trgLoc); if (ci.hasSource()) count += removeSegment(ci.getSource(), seg.id); if (ci.hasTarget()) count += removeSegment(ci.getTarget(), seg.id); return (count > 0); }
@Override public void joinWithNext(Segment seg, LocaleId trgLoc) { ContainerIterator ci = new ContainerIterator(trgLoc); if (ci.hasSource()) doJoinWithNext(ci.getSource(), seg.id); if (ci.hasTarget()) doJoinWithNext(ci.getTarget(), seg.id); }
@Override public void joinAll(LocaleId trgLoc) { ContainerIterator ci = new ContainerIterator(trgLoc); if (ci.hasSource()) ci.getSource().joinAll(); if (ci.hasTarget()) ci.getTarget().joinAll(); }