All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xdev.ui.ganttchart.action.UpdateDateRelationManagementStrategy Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
package xdev.ui.ganttchart.action;

/*-
 * #%L
 * XDEV BI Suite
 * %%
 * Copyright (C) 2011 - 2021 XDEV Software
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */


import java.util.Date;

import xdev.ui.ganttchart.UpdateableGanttEntry;
import xdev.ui.ganttchart.utils.XdevGanttUtils;
import xdev.util.Duration;
import xdev.util.XdevDate;

import com.jidesoft.gantt.GanttChart;
import com.jidesoft.gantt.GanttEntry;
import com.jidesoft.gantt.GanttEntryRelation;
import com.jidesoft.gantt.GanttModel;
import com.jidesoft.range.Range;
import com.jidesoft.range.TimeRange;


/**
 * A concrete implementation of {@link GanttRelationManagementStrategy} which
 * automatically updates all the concerned {@link GanttEntry} start/end data to
 * valid values, if the relation validation failed.
 * 

* Is used as default implementation in Date based * GanttChartTemplates. *

* *

* The {@link Date} typed implementation because of date specific implementation * details. *

* * * @param * the customized {@link GanttEntry} type. * * @author XDEV Software jwill * @since 4.0 */ public class UpdateDateRelationManagementStrategy> extends AbstractRelationManagementStrategy { /** * Initialy validates the existing relations within the {@link GanttChart}. * * @param chart * the Chart to validate. */ public UpdateDateRelationManagementStrategy(GanttModel model) { super(model); } public UpdateDateRelationManagementStrategy() { super(); } /** * {@inheritDoc} */ @Override public boolean manageRelation(GanttEntryRelation relation) { S pre = relation.getPredecessorEntry(); S suc = relation.getSuccessorEntry(); int type = relation.getRelationType(); switch(type) { // if the precessor end is not lower, then the sucessor // start // the sucessor start must be set to precessor end. case GanttEntryRelation.ENTRY_RELATION_FINISH_TO_START: { if(!new FtSRelationManagementController().validateEntryRelation(pre,suc)) { suc.setRange(new TimeRange(pre.getRange().upper(),this.getUpdatedEndDate( suc.getRange(),pre.getRange().upper()))); for(int i = 0; i < suc.getChildrenCount(); i++) { // Have to cast here because of jide typing. The childs // should be always the same type. @SuppressWarnings("unchecked") S childEntry = (S)suc.getChildAt(i); if(!new FtSRelationManagementController().validateEntryRelation( pre,childEntry)) { childEntry.setRange(new TimeRange(pre.getRange().upper(), this.getUpdatedEndDate(childEntry.getRange(),pre.getRange() .upper()))); } } } return true; } // if the precessor start is not lower, then the sucessor // start // the precessor start must be set to precessor start. case GanttEntryRelation.ENTRY_RELATION_START_TO_START: { if(!new StSRelationManagementController().validateEntryRelation(pre,suc)) { suc.setRange(new TimeRange(pre.getRange().lower(),this.getUpdatedEndDate( suc.getRange(),pre.getRange().lower()))); for(int i = 0; i < suc.getChildrenCount(); i++) { // Have to cast here because of jide typing. The childs // should be always the same type. @SuppressWarnings("unchecked") S childEntry = (S)suc.getChildAt(i); if(!new StSRelationManagementController().validateEntryRelation( pre,childEntry)) { childEntry.setRange(new TimeRange(pre.getRange().lower(), this.getUpdatedEndDate(childEntry.getRange(),pre.getRange() .lower()))); } } } return true; } // if the sucessor end is not lower, then the precessor // end // the sucessor end must be set to precessor end. case GanttEntryRelation.ENTRY_RELATION_FINISH_TO_FINISH: { if(!new FtFRelationManagementController().validateEntryRelation(pre,suc)) { suc.setRange(new TimeRange(this.getUpdatedStartDate(suc.getRange(),pre .getRange().upper()),pre.getRange().upper())); if(suc.getChildrenCount() > 0) { S relevantChildEntry = XdevGanttUtils.getMaxDateEntry(XdevGanttUtils .getChildEntries(suc)); if(!new FtFRelationManagementController().validateEntryRelation( pre,relevantChildEntry)) { relevantChildEntry.setRange(new TimeRange(this.getUpdatedStartDate( relevantChildEntry.getRange(),pre.getRange().upper()),pre .getRange().upper())); } } } return true; } // if the precessor start is not lower, then the sucessor // end // the sucessor end must be set to precessor start. case GanttEntryRelation.ENTRY_RELATION_START_TO_FINISH: { if(!new StFRelationManagementController().validateEntryRelation(pre,suc)) { suc.setRange(new TimeRange(this.getUpdatedStartDate(suc.getRange(),pre .getRange().lower()),pre.getRange().lower())); if(suc.getChildrenCount() > 0) { S relevantChildEntry = XdevGanttUtils.getMaxDateEntry(XdevGanttUtils .getChildEntries(suc)); if(!new StFRelationManagementController().validateEntryRelation( pre,relevantChildEntry)) { relevantChildEntry.setRange(new TimeRange(this.getUpdatedStartDate( relevantChildEntry.getRange(),pre.getRange().lower()),pre .getRange().lower())); } } } return true; } // default: // throw new RuntimeException("The relationtype is not registered"); } return false; } /** * Calculates the new end date, relative to the previous range and the given * start date. * * @param entryRange * the previous set range (start until end). * @param newStart * the new start date. * * @return the new end date, relative to the new start date. */ private Date getUpdatedEndDate(Range entryRange, Date newStart) { XdevDate newTime = new XdevDate(newStart); XdevDate xstart = new XdevDate(entryRange.lower()); XdevDate xend = new XdevDate(entryRange.upper()); Duration span = new Duration(xstart,xend); newTime.rollForward(span); return new Date(newTime.getTimeInMillis()); } /** * Calculates the new start date, relative to the previous range and the * given end date. * * @param entryRange * the previous set range (start until end). * @param newEnd * the new end date. * * @return the new start date, relative to the new end date. */ private Date getUpdatedStartDate(Range entryRange, Date newEnd) { XdevDate newTime = new XdevDate(newEnd); XdevDate xstart = new XdevDate(entryRange.lower()); XdevDate xend = new XdevDate(entryRange.upper()); Duration span = new Duration(xstart,xend); newTime.rollBack(span); return new Date(newTime.getTimeInMillis()); } }