/* * This file is part of ###PROJECT_NAME### * * Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e * Desenvolvemento Tecnolóxico de Galicia * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package org.zkoss.ganttz; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.zkoss.ganttz.adapters.IDisabilityConfiguration; import org.zkoss.ganttz.adapters.PlannerConfiguration; import org.zkoss.ganttz.data.Dependency; import org.zkoss.ganttz.data.GanttDiagramGraph; import org.zkoss.ganttz.data.Position; import org.zkoss.ganttz.data.Task; import org.zkoss.ganttz.extensions.ICommand; import org.zkoss.ganttz.extensions.ICommandOnTask; import org.zkoss.ganttz.extensions.IContext; import org.zkoss.ganttz.timetracker.TimeTracker; import org.zkoss.ganttz.timetracker.TimeTrackerComponent; import org.zkoss.ganttz.timetracker.TimeTrackerComponentWithoutColumns; import org.zkoss.ganttz.util.ComponentsFinder; import org.zkoss.ganttz.util.OnZKDesktopRegistry; import org.zkoss.ganttz.util.script.IScriptsRegister; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.HtmlMacroComponent; import org.zkoss.zul.Separator; public class Planner extends HtmlMacroComponent { private GanttDiagramGraph diagramGraph = new GanttDiagramGraph(); private LeftPane leftPane; private GanttPanel ganttPanel; private List> contextualizedGlobalCommands; private CommandContextualized goingDownInLastArrowCommand; private List> commandsOnTasksContextualized; private CommandOnTaskContextualized editTaskCommand; private FunctionalityExposedForExtensions context; private IDisabilityConfiguration disabilityConfiguration; public Planner() { registerNeededScripts(); } private void registerNeededScripts() { IScriptsRegister register = getScriptsRegister(); register.register(ScriptsRequiredByPlanner.class); } TaskList getTaskList() { if (ganttPanel == null) return null; List children = ganttPanel.getChildren(); return ComponentsFinder.findComponentsOfType(TaskList.class, children).get(0); } public String getContextPath() { return Executions.getCurrent().getContextPath(); } public DependencyList getDependencyList() { if (ganttPanel == null) return null; List children = ganttPanel.getChildren(); List found = ComponentsFinder.findComponentsOfType(DependencyList.class, children); if (found.isEmpty()) return null; return found.get(0); } public void addTasks(Position position, Collection newTasks) { TaskList taskList = getTaskList(); if (taskList != null && leftPane != null) { taskList.addTasks(position, newTasks); leftPane.addTasks(position, newTasks); } } public void addTask(Position position, Task task) { addTasks(position, Arrays.asList(task)); } void addDependencies(Collection dependencies) { DependencyList dependencyList = getDependencyList(); if (dependencyList == null) { return; } for (DependencyComponent d : getTaskList().asDependencyComponents( dependencies)) { dependencyList.addDependencyComponent(d); } } public void zoomIncrease() { if (ganttPanel == null) { return; } ganttPanel.zoomIncrease(); } public void zoomDecrease() { if (ganttPanel == null) { return; } ganttPanel.zoomDecrease(); } public void setConfiguration(PlannerConfiguration configuration) { if (configuration == null) return; this.diagramGraph = new GanttDiagramGraph(); FunctionalityExposedForExtensions context = new FunctionalityExposedForExtensions( this, configuration.getAdapter(), configuration.getNavigator(), diagramGraph); this.contextualizedGlobalCommands = contextualize(context, configuration.getGlobalCommands()); this.commandsOnTasksContextualized = contextualize(context, configuration.getCommandsOnTasks()); goingDownInLastArrowCommand = contextualize(context, configuration .getGoingDownInLastArrowCommand()); editTaskCommand = contextualize(context, configuration .getEditTaskCommand()); this.context = context; this.disabilityConfiguration = configuration; context.add(configuration.getData()); setupComponents(); setAt("insertionPointLeftPanel", leftPane); leftPane.afterCompose(); setAt("insertionPointRightPanel", ganttPanel); ganttPanel.afterCompose(); TimeTrackerComponent timetrackerheader = new TimeTrackerComponentWithoutColumns( ganttPanel.getTimeTracker(), "timetrackerheader"); setAt("insertionPointTimetracker", timetrackerheader); timetrackerheader.afterCompose(); Component chartComponent = configuration.getChartComponent(); if (chartComponent != null) { setAt("insertionPointChart", chartComponent); } Component chartLegend = configuration.getChartLegend(); if (chartLegend != null) { setAt("insertionPointChartLegend", chartLegend); } } private void setAt(String insertionPointId, Component component) { Component insertionPoint = getFellow(insertionPointId); insertionPoint.getChildren().clear(); insertionPoint.appendChild(component); } private List> contextualize( FunctionalityExposedForExtensions context, List> commands) { List> result = new ArrayList>(); for (ICommandOnTask c : commands) { result.add(contextualize(context, c)); } return result; } private CommandOnTaskContextualized contextualize( FunctionalityExposedForExtensions context, ICommandOnTask commandOnTask) { return CommandOnTaskContextualized.create(commandOnTask, context .getMapper(), context); } private CommandContextualized contextualize(IContext context, ICommand command) { if (command == null) return null; return CommandContextualized.create(command, context); } private List> contextualize( IContext context, Collection> commands) { ArrayList> result = new ArrayList>(); for (ICommand command : commands) { result.add(contextualize(context, command)); } return result; } public GanttDiagramGraph getGanttDiagramGraph() { return diagramGraph; } private void setupComponents() { insertGlobalCommands(); this.leftPane = new LeftPane(this.diagramGraph.getTopLevelTasks()); this.ganttPanel = new GanttPanel(this.context, commandsOnTasksContextualized, editTaskCommand, disabilityConfiguration); } @SuppressWarnings("unchecked") private void insertGlobalCommands() { Component toolbar = getToolbar(); Component firstSeparator = getFirstSeparatorFromToolbar(); toolbar.getChildren().removeAll(getBefore(toolbar, firstSeparator)); for (CommandContextualized c : contextualizedGlobalCommands) { toolbar.insertBefore(c.toButton(), firstSeparator); } } @SuppressWarnings("unchecked") private List getBefore(Component parent, Component child) { List children = parent.getChildren(); List result = new ArrayList(); for (Component object : children) { if (object == child) { break; } result.add(object); } return result; } @SuppressWarnings("unchecked") private Component getFirstSeparatorFromToolbar() { Component toolbar = getToolbar(); List children = toolbar.getChildren(); List separators = ComponentsFinder .findComponentsOfType( Separator.class, children); return separators.get(0); } private Component getToolbar() { Component toolbar = getFellow("toolbar"); return toolbar; } void removeTask(Task task) { TaskList taskList = getTaskList(); taskList.remove(task); getDependencyList().taskRemoved(task); leftPane.taskRemoved(task); setHeight(getHeight());// forcing smart update taskList.adjustZoomColumnsHeight(); getDependencyList().redrawDependencies(); } private IScriptsRegister getScriptsRegister() { return OnZKDesktopRegistry.getLocatorFor(IScriptsRegister.class) .retrieve(); } @Override public void afterCompose() { super.afterCompose(); } public TimeTracker getTimeTracker() { return ganttPanel.getTimeTracker(); } }