Subclassing IlvFilterGanttModel

Article ID: 2261
Last updated: 29 May, 2018
Article ID: 2261
Last updated: 29 May, 2018
Revision: 3
Views: 607
Posted: 23 Jun, 2010
by Dean J.
Updated: 29 May, 2018
by Gargani A.

Question

How to subclass IlvFilterGanttModel?

Answer

Starting with JViews Gantt 6.0, the Gantt Filter sample provided in the distribution illustrates how to create an IlvFilterGanttModel subclass that filters activities based upon a regular expression that is applied to the activity names. In addition, here is a basic IlvFilterGanttModel subclass that allows the user to display a subtree of activities.

import ilog.views.gantt.*;
import ilog.views.gantt.event.*;
import ilog.views.gantt.model.filter.IlvFilterGanttModel;
import java.util.*;

public class SubTreeGanttModel extends IlvFilterGanttModel {
    // Instance Variables private
    IlvActivity rootActivity;

    // Instance Construction and Initialization
    public SubTreeGanttModel() {
    }

    public SubTreeGanttModel(IlvGanttModel filteredModel) {
        super(filteredModel);
    }

    public void setFilteredModel(IlvGanttModel model) {
        setRootActivity(null);
        super.setFilteredModel(model);
        setRootActivity(model.getRootActivity());
    }

    public boolean contains(IlvHierarchyNode activityOrResource) {
        if (activityOrResource instanceof IlvActivity)
            return containsActivity((IlvActivity) activityOrResource);
        else
            return containsResource((IlvResource) activityOrResource);
    }

    // =========================================
    // Accessing Activities
    // =========================================
    private void processActivityRemoved(IlvActivity parent, IlvActivity child,
            int childIndex) {
        List children = new ArrayList(1);
        children.add(child);
        if (parent == null)
            childIndex = 0;
        int[] childIndices = new int[] { childIndex };
        fireActivitiesRemoved(parent, children, childIndices);
    }

    private void processActivityInserted(IlvActivity parent, IlvActivity child) {
        List children = new ArrayList(1);
        children.add(child);
        fireActivitiesInserted(parent, children);
    }

    private boolean containsActivity(IlvActivity activity) {
        // Check that the activity is a member of the encapsulated data model.
        IlvGanttModel filteredModel = getFilteredModel();
        if (filteredModel == null || !filteredModel.contains(activity))
            return false;
        IlvActivity root = getRootActivity();
        if (root == null)
            return false;
        return activity == root // Use filtered model as slight optimization
                || IlvGanttModelUtil
                        .isDescendant(filteredModel, root, activity);
    }

    /**
     * Returns the root activity of the data model, or null if the data model
     * contains no activities.
     */
    public IlvActivity getRootActivity() {
        return rootActivity;
    }

    public void setRootActivity(IlvActivity root) {
        // If non-null, the new root activity must be a member of the
        // encapsulated data model.
        if (root != null) {
            IlvGanttModel filteredModel = getFilteredModel();
            if (filteredModel == null || !filteredModel.contains(root))
                throw new IllegalArgumentException("Root " + root
                        + " is not member of filtered model");
        }
        // Fire an event for the old root activity.
        if (rootActivity != null) {
            processActivityRemoved(null, rootActivity, 0);
        }
        // Set the new root activity and fire an event.
        rootActivity = root;
        if (rootActivity != null) {
            processActivityInserted(null, root);
        }
    }

    public IlvActivity getParentActivity(IlvActivity activity) {
        if (activity == getRootActivity())
            return null;
        else
            return super.getParentActivity(activity);
    }

    public int getParentActivityIndex(IlvActivity activity) {
        if (activity == getRootActivity())
            return -1;
        else
            return super.getParentActivityIndex(activity);
    }

    // =========================================
    // Handling Activity Events
    // =========================================
    protected ActivityListener createActivityEventHandler() {
        return new ActivityHandler();
    }

    protected ActivityHierarchyListener createActivityHierarchyEventHandler() {
        return new ActivityHierarchyHandler();
    }

    protected class ActivityHandler extends
            IlvFilterGanttModel.ActivityEventHandler {

        public void activityChanged(ActivityEvent event) {
            IlvActivity activity = event.getActivity();
            if (containsActivity(activity))
                fireActivityEvent(event);
        }
    }

    protected class ActivityHierarchyHandler extends
            IlvFilterGanttModel.ActivityHierarchyEventHandler {

        public void activitiesInserted(ActivitiesInsertedEvent event) {
            IlvActivity activity = event.getActivity();
            if (containsActivity(activity))
                fireActivitiesInserted(activity, event.getChildren());
        }

        public void activitiesRemoved(ActivitiesRemovedEvent event) {
            IlvActivity activity = event.getActivity();
            if (containsActivity(activity)) {
                fireActivitiesRemoved(activity, event.getChildren(),
                        event.getChildIndices());
            }
        }

        public void activityMoved(ActivityMovedEvent event) {
            IlvActivity activity = event.getActivity();
            IlvActivity oldParent = event.getOldParent();
            IlvActivity newParent = event.getNewParent();
            if (containsActivity(oldParent) && containsActivity(newParent)) {
                fireActivityMoved(activity, oldParent, event.getOldIndex(),
                        newParent, event.getNewIndex());
                return;
            } else if (containsActivity(newParent))
                processActivityInserted(newParent, activity);
            if (containsActivity(oldParent))
                processActivityRemoved(oldParent, activity, event.getOldIndex());
        }
    }

    // =========================================
    // Accessing Resources
    // =========================================
    private boolean containsResource(IlvResource resource) {
        // Check that the resource is a member of the encapsulated data model.
        IlvGanttModel filteredModel = getFilteredModel();
        if (filteredModel == null || !filteredModel.contains(resource))
            return false;
        IlvResource root = getRootResource();
        if (root == null)
            return false;
        return resource == root // Use filtered model as slight optimization
                || IlvGanttModelUtil
                        .isDescendant(filteredModel, root, resource);
    }

}

If you want to test this subclass, you can download and execute the sample below. It displays two frames, one with a complete Gantt model and another with a Gantt model which is filtered to "Activity 2" and its subactivities: see the attached file Sample.zip

Note: In order to run this sample in JViews 8.7 and later, you must call the ilog.views.util.IlvProductUtil.DeploymentLicenceRequired method with the appropriate argument. 

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2261
Last updated: 29 May, 2018
Revision: 3
Views: 607
Posted: 23 Jun, 2010 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Attached files

Also listed in


Others in this category