trace/tracebuilder/com.nokia.tracebuilder.view/src/com/nokia/tracebuilder/view/CheckListCheckStateListener.java
changeset 10 ed1c9f64298a
equal deleted inserted replaced
9:14dc2103a631 10:ed1c9f64298a
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * State listener for tree viewer check boxes
       
    17 *
       
    18 */
       
    19 package com.nokia.tracebuilder.view;
       
    20 
       
    21 import java.util.Iterator;
       
    22 
       
    23 import org.eclipse.jface.viewers.CheckStateChangedEvent;
       
    24 import org.eclipse.jface.viewers.CheckboxTreeViewer;
       
    25 import org.eclipse.jface.viewers.ICheckStateListener;
       
    26 
       
    27 import com.nokia.tracebuilder.engine.CheckListDialogEntry;
       
    28 
       
    29 /**
       
    30  * State listener for tree viewer check boxes
       
    31  * 
       
    32  */
       
    33 final class CheckListCheckStateListener implements ICheckStateListener {
       
    34 
       
    35 	/**
       
    36 	 * Content provider
       
    37 	 */
       
    38 	private final CheckboxTreeViewer viewer;
       
    39 
       
    40 	/**
       
    41 	 * Constructor
       
    42 	 * 
       
    43 	 * @param viewer
       
    44 	 *            the viewer
       
    45 	 */
       
    46 	CheckListCheckStateListener(CheckboxTreeViewer viewer) {
       
    47 		this.viewer = viewer;
       
    48 	}
       
    49 
       
    50 	/*
       
    51 	 * (non-Javadoc)
       
    52 	 * 
       
    53 	 * @see org.eclipse.jface.viewers.ICheckStateListener#
       
    54 	 *      checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
       
    55 	 */
       
    56 	public void checkStateChanged(CheckStateChangedEvent event) {
       
    57 		CheckListDialogEntry entry = (CheckListDialogEntry) event.getElement();
       
    58 		boolean newState = event.getChecked();
       
    59 		// View update is not needed with the entry that was clicked by user
       
    60 		checkEntryAndRelatives(entry, newState, false);
       
    61 	}
       
    62 
       
    63 	/**
       
    64 	 * Changes the state of an entry, its parent and children
       
    65 	 * 
       
    66 	 * @param entry
       
    67 	 *            the entry
       
    68 	 * @param newState
       
    69 	 *            the new entry state
       
    70 	 * @param updateView
       
    71 	 *            true if view needs to be updated
       
    72 	 * @return true if entries were changed
       
    73 	 */
       
    74 	private boolean checkEntryAndRelatives(CheckListDialogEntry entry,
       
    75 			boolean newState, boolean updateView) {
       
    76 		boolean changed = checkEntry(entry, newState, updateView);
       
    77 		if (changed) {
       
    78 			viewer.setSubtreeChecked(entry, newState);
       
    79 			checkChildren(entry, newState);
       
    80 			checkParent(entry);
       
    81 		}
       
    82 		return changed;
       
    83 	}
       
    84 
       
    85 	/**
       
    86 	 * Checks the children of given entry
       
    87 	 * 
       
    88 	 * @param entry
       
    89 	 *            the entry
       
    90 	 * @param newState
       
    91 	 *            the new state
       
    92 	 */
       
    93 	private void checkChildren(CheckListDialogEntry entry, boolean newState) {
       
    94 		for (CheckListDialogEntry child : entry) {
       
    95 			child.setChecked(newState);
       
    96 			checkChildren(child, newState);
       
    97 		}
       
    98 	}
       
    99 
       
   100 	/**
       
   101 	 * Checks an entry and unchecks the partially checked flag if it exists
       
   102 	 * 
       
   103 	 * @param entry
       
   104 	 *            the entry
       
   105 	 * @param newState
       
   106 	 *            the new entry state
       
   107 	 * @param updateView
       
   108 	 *            true if view needs to be updated
       
   109 	 * @return true if entry was changed
       
   110 	 */
       
   111 	private boolean checkEntry(CheckListDialogEntry entry, boolean newState,
       
   112 			boolean updateView) {
       
   113 		boolean retval;
       
   114 		if (entry.isChecked() != newState) {
       
   115 			entry.setChecked(newState);
       
   116 			if (entry.isPartiallyChecked()) {
       
   117 				entry.setPartiallyChecked(false);
       
   118 				viewer.setGrayChecked(entry, false);
       
   119 			}
       
   120 			if (updateView) {
       
   121 				viewer.setChecked(entry, newState);
       
   122 			}
       
   123 			retval = true;
       
   124 		} else {
       
   125 			retval = false;
       
   126 		}
       
   127 		return retval;
       
   128 	}
       
   129 
       
   130 	/**
       
   131 	 * Recursively checks the parent of the checked element. If all children are
       
   132 	 * checked, the parent is checked. If all children are unchecked, the parent
       
   133 	 * is unchecked. If children are partially checked, the parent is grayed.
       
   134 	 * 
       
   135 	 * @param entry
       
   136 	 *            the entry whose parent needs to be checked
       
   137 	 */
       
   138 	private void checkParent(CheckListDialogEntry entry) {
       
   139 		CheckListDialogEntry parent = entry.getParent();
       
   140 		if (parent != null && parent.hasChildren()) {
       
   141 			boolean checked = false;
       
   142 			boolean unchecked = false;
       
   143 			boolean partiallyChecked = false;
       
   144 			Iterator<CheckListDialogEntry> children = parent.getChildren();
       
   145 			while (children.hasNext() && !partiallyChecked) {
       
   146 				CheckListDialogEntry child = children.next();
       
   147 				boolean childChecked = child.isChecked();
       
   148 				boolean childPartial = child.isPartiallyChecked();
       
   149 				if (childPartial) {
       
   150 					partiallyChecked = true;
       
   151 				} else if (childChecked) {
       
   152 					checked = true;
       
   153 					if (unchecked) {
       
   154 						partiallyChecked = true;
       
   155 					}
       
   156 				} else {
       
   157 					unchecked = true;
       
   158 					if (checked) {
       
   159 						partiallyChecked = true;
       
   160 					}
       
   161 				}
       
   162 			}
       
   163 			if (partiallyChecked) {
       
   164 				if (!parent.isPartiallyChecked()) {
       
   165 					viewer.setGrayChecked(parent, true);
       
   166 					parent.setPartiallyChecked(true);
       
   167 				} else if (!parent.isChecked()) {
       
   168 					viewer.setChecked(parent, true);
       
   169 				}
       
   170 				// Checked flag needs to be set in both cases
       
   171 				parent.setChecked(true);
       
   172 			} else if (checked) {
       
   173 				if (parent.isPartiallyChecked()) {
       
   174 					viewer.setGrayed(parent, false);
       
   175 					parent.setPartiallyChecked(false);
       
   176 				}
       
   177 				if (!parent.isChecked()) {
       
   178 					viewer.setChecked(parent, true);
       
   179 					parent.setChecked(true);
       
   180 				}
       
   181 			} else {
       
   182 				if (parent.isPartiallyChecked()) {
       
   183 					viewer.setGrayChecked(parent, false);
       
   184 					parent.setPartiallyChecked(false);
       
   185 				} else if (parent.isChecked()) {
       
   186 					viewer.setChecked(parent, false);
       
   187 				}
       
   188 				// Checked flag needs to be set in both cases
       
   189 				parent.setChecked(false);
       
   190 			}
       
   191 			checkParent(parent);
       
   192 		}
       
   193 	}
       
   194 }