001: package salomeTMF_plug.requirements.ihm;
002:
003: import java.util.Hashtable;
004: import java.util.StringTokenizer;
005:
006: import javax.swing.JTree;
007: import javax.swing.tree.DefaultMutableTreeNode;
008: import javax.swing.tree.DefaultTreeModel;
009: import javax.swing.tree.TreeNode;
010: import javax.swing.tree.TreePath;
011:
012: import salomeTMF_plug.requirements.data.IReqFilter;
013: import salomeTMF_plug.requirements.data.Requirement;
014:
015: public class FilterReqTreeModel extends DefaultTreeModel {
016: protected TreeNode m_root;
017: protected IReqFilter m_filter;
018: protected JTree reqTree;
019:
020: public FilterReqTreeModel(IReqFilter filter, TreeNode root,
021: JTree reqTree) {
022: super (root);
023: m_filter = filter;
024: m_root = root;
025: this .reqTree = reqTree;
026: }
027:
028: public IReqFilter getFilter() {
029: return m_filter;
030: }
031:
032: public void setFiltered(boolean pass) {
033: //Enumeration<TreePath> enumExpandPath = reqTree.getExpandedDescendants();
034: TreePath pTreePath = reqTree.getSelectionPath();
035:
036: /*int nbRow = reqTree.getRowCount();
037: String[] states = new String[nbRow];
038: for (int i = 0; i < nbRow ; i++){
039: states[i] = getExpansionState(reqTree, i);
040: }*/
041: Hashtable<TreePath, Boolean> state = getExpansionState(reqTree);
042: m_filter.setActived(pass);
043: Object[] path = { root };
044: int[] childIndices = new int[root.getChildCount()];
045: Object[] children = new Object[root.getChildCount()];
046: for (int i = 0; i < root.getChildCount(); i++) {
047: childIndices[i] = i;
048: children[i] = root.getChildAt(i);
049: }
050:
051: fireTreeStructureChanged(this , path, childIndices, children);
052: if (pTreePath != null) {
053: DefaultMutableTreeNode pNode = (DefaultMutableTreeNode) pTreePath
054: .getLastPathComponent();
055: Requirement pReq = (Requirement) pNode.getUserObject();
056: reqTree.setSelectionPath(pTreePath);
057: if (!m_filter.isFiltred(pReq)) {
058: boolean trouve = false;
059: while (!trouve && pTreePath != null) {
060: pTreePath = pTreePath.getParentPath();
061: if (pTreePath != null) {
062: pNode = (DefaultMutableTreeNode) pTreePath
063: .getLastPathComponent();
064: pReq = (Requirement) pNode.getUserObject();
065: if (m_filter.isFiltred(pReq)) {
066: trouve = true;
067: }
068: }
069: }
070: reqTree.setSelectionPath(pTreePath);
071: }
072: }
073: /*for (int i = 0; i < nbRow ; i++){
074: restoreExpanstionState(reqTree, i, states[i]);
075: }*/
076: restoreExpanstionState(reqTree, state);
077: }
078:
079: public int getChildCount(Object parent) {
080: int realCount = super .getChildCount(parent), filterCount = 0;
081: ;
082: for (int i = 0; i < realCount; i++) {
083: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) super
084: .getChild(parent, i);
085: if (m_filter.pass(dmtn))
086: filterCount++;
087: }
088: return filterCount;
089: }
090:
091: public Object getChild(Object parent, int index) {
092: int cnt = -1;
093: for (int i = 0; i < super .getChildCount(parent); i++) {
094: Object child = super .getChild(parent, i);
095: if (m_filter.pass(child))
096: cnt++;
097: if (cnt == index)
098: return child;
099: }
100: return null;
101: }
102:
103: /********************* UTIL **************************/
104:
105: // is path1 descendant of path2
106: public boolean isDescendant(TreePath path1, TreePath path2) {
107: int count1 = path1.getPathCount();
108: int count2 = path2.getPathCount();
109: if (count1 <= count2)
110: return false;
111: while (count1 != count2) {
112: path1 = path1.getParentPath();
113: count1--;
114: }
115: return path1.equals(path2);
116: }
117:
118: public Hashtable<TreePath, Boolean> getExpansionState(JTree tree) {
119: Hashtable<TreePath, Boolean> statePath = new Hashtable<TreePath, Boolean>();
120: int nbRow = reqTree.getRowCount();
121: for (int row = 0; row < nbRow; row++) {
122: TreePath rowPath = tree.getPathForRow(row);
123: int rowCount = tree.getRowCount();
124: for (int i = row; i < rowCount; i++) {
125: TreePath path = tree.getPathForRow(i);
126: if (i == row || isDescendant(path, rowPath)) {
127: if (tree.isExpanded(path)) {
128: statePath.put(path, true);
129: }
130: } else
131: break;
132: }
133: }
134: return statePath;
135: }
136:
137: public void restoreExpanstionState(JTree tree,
138: Hashtable<TreePath, Boolean> statePath) {
139: int nbRow = reqTree.getRowCount();
140: for (int row = 0; row < nbRow; row++) {
141: //TreePath rowPath = tree.getPathForRow(row);
142: int rowCount = tree.getRowCount();
143: for (int i = row; i < rowCount; i++) {
144: TreePath path = tree.getPathForRow(i);
145: Boolean expanded = statePath.get(path);
146: if (expanded != null) {
147: tree.expandRow(tree.getRowForPath(path));
148: }
149: }
150: }
151: }
152: }
|