Parent and Child Tasks
Contents
[
Hide
Show
]Tasks can be organized in a hierarchy. When a task has one or more tasks beneath it, they are referred to as parents. The tasks underneath are called parents.
Working with Parent Tasks and Children
The Task class exposes classes that helps you determine:
- Parent: determines that a task is a parent task. Accepts and returns a Task object.
- Children: determines that a task is a child task. Accepts and returns an array list of Task objects.
Parent and Child Tasks in Microsoft Project
To declare a task as a parent or a child task in Microsoft Project:
- In the Task Entry form, select a task and click it.
- Select Outdent to turn a task into a parent, or,
- Select Indent to turn a task into a child.
Parent tasks and children in Microsoft Project
Getting Parent and Child Tasks
The following examples show viewing parent and child tasks in a project using Aspose.Tasks.
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(ParentAndChildTasks.class);
4
5Project prj = new Project(dataDir + "ParentAndChildTask.mpp");
6
7// Create a ChildTasksCollector instance
8ChildTasksCollector collector = new ChildTasksCollector();
9
10// Use TaskUtils to get all children tasks in RootTask
11TaskUtils.apply(prj.getRootTask(), collector, 0);
12
13List tasks = collector.getTasks();
14int iSize = tasks.size();
15// Parse through all the collected tasks
16for (int i = 0; i < iSize; i++) {
17 Task tsk = (Task) tasks.get(i);
18 System.out.println("Task Name = " + tsk.get(Tsk.NAME));
19}
Setting Child Tasks
1// For complete examples and data files, please go to https://github.com/aspose-tasks/Aspose.Tasks-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir(ParentAndChildTasks.class);
4
5Project proj = new Project(dataDir + "Blank2010.mpp");
6proj.set(Prj.NEW_TASKS_ARE_MANUAL, new NullableBool(false));
7double oneDay = 8d * 60d * 60d * 10000000d;
8java.util.Calendar cal = java.util.Calendar.getInstance();
9cal.set(2014, 9, 13, 8, 0, 0);
10Date startDate = cal.getTime();
11proj.set(Prj.START_DATE, startDate);
12
13Task task1 = proj.getRootTask().getChildren().add("Task 1");
14cal.set(2014, 9, 13, 8, 0, 0);
15task1.set(Tsk.START, cal.getTime());
16task1.set(Tsk.DURATION, proj.getDuration(29, TimeUnitType.Day));
17Task task2 = proj.getRootTask().getChildren().add("Task 2");
18
19// add child tasks to task 2
20Task task3 = task2.getChildren().add("Task 3");
21Task task4 = task2.getChildren().add("Task 4");
22
23cal.set(2014, 9, 15, 8, 0, 0);
24task3.set(Tsk.START, cal.getTime());
25task3.set(Tsk.DURATION, proj.getDuration(3, TimeUnitType.Day));
26task3.set(Tsk.CONSTRAINT_TYPE, ConstraintType.StartNoEarlierThan);
27task3.set(Tsk.CONSTRAINT_DATE, task3.get(Tsk.START));
28
29cal.set(2014, 9, 17, 8, 0, 0);
30task4.set(Tsk.START, cal.getTime());
31task4.set(Tsk.DURATION, proj.getDuration(3, TimeUnitType.Day));
32task4.set(Tsk.CONSTRAINT_TYPE, ConstraintType.StartNoEarlierThan);
33task4.set(Tsk.CONSTRAINT_DATE, task3.get(Tsk.START));
34
35task3.set(Tsk.PERCENT_COMPLETE, 50);
36task4.set(Tsk.PERCENT_COMPLETE, 70);
37
38proj.save(dataDir + "ProjectJava.mpp", SaveFileFormat.MPP);