| 1 | |
package de.glossmaker.gui.gloss.main.tree; |
| 2 | |
|
| 3 | |
import javax.swing.event.TreeModelListener; |
| 4 | |
import javax.swing.tree.TreeModel; |
| 5 | |
import javax.swing.tree.TreePath; |
| 6 | |
|
| 7 | |
import de.glossmaker.gloss.datastructure.GlossItem; |
| 8 | |
import de.glossmaker.gloss.datastructure.GlossItems; |
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
public class GlossItemTreeModel implements TreeModel { |
| 17 | |
|
| 18 | 0 | private GlossItems mGlossItems = null; |
| 19 | 0 | private String mRoot = null; |
| 20 | |
|
| 21 | 0 | public GlossItemTreeModel(GlossItems items, String root) { |
| 22 | 0 | mGlossItems = items; |
| 23 | 0 | mRoot = root; |
| 24 | 0 | } |
| 25 | |
|
| 26 | |
public void setFileName(String fileName) { |
| 27 | 0 | mRoot = fileName; |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
@Override |
| 31 | |
public Object getChild(Object parent, int index) { |
| 32 | 0 | if (parent.equals(mRoot)) { |
| 33 | 0 | return mGlossItems.getHeading(index); |
| 34 | 0 | } else if (parent instanceof String) { |
| 35 | 0 | return mGlossItems.get((String)parent, index); |
| 36 | |
} |
| 37 | 0 | return null; |
| 38 | |
} |
| 39 | |
|
| 40 | |
@Override |
| 41 | |
public int getChildCount(Object parent) { |
| 42 | 0 | if (parent.equals(mRoot)) { |
| 43 | 0 | return mGlossItems.getHeadingElementCount(); |
| 44 | 0 | } else if (parent instanceof String) { |
| 45 | 0 | return mGlossItems.getHeadingCount((String)parent); |
| 46 | |
} |
| 47 | 0 | return 0; |
| 48 | |
} |
| 49 | |
|
| 50 | |
@Override |
| 51 | |
public int getIndexOfChild(Object parent, Object child) { |
| 52 | 0 | int max = getChildCount( parent ); |
| 53 | 0 | int result = -1; |
| 54 | 0 | for(int i = 0; i < max; i++) { |
| 55 | 0 | if(getChild(parent, i).equals(child)) { |
| 56 | 0 | result = i; |
| 57 | 0 | break; |
| 58 | |
} |
| 59 | |
} |
| 60 | 0 | return result; |
| 61 | |
} |
| 62 | |
|
| 63 | |
@Override |
| 64 | |
public Object getRoot() { |
| 65 | 0 | return mRoot; |
| 66 | |
} |
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public boolean isLeaf(Object obj) { |
| 70 | 0 | if (obj instanceof String) { |
| 71 | 0 | return false; |
| 72 | |
} else { |
| 73 | 0 | return true; |
| 74 | |
} |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
@Override |
| 79 | 0 | public void addTreeModelListener(TreeModelListener arg0) {} |
| 80 | |
|
| 81 | |
|
| 82 | |
@Override |
| 83 | 0 | public void removeTreeModelListener(TreeModelListener arg0) {} |
| 84 | |
|
| 85 | |
|
| 86 | |
@Override |
| 87 | 0 | public void valueForPathChanged(TreePath arg0, Object arg1) {} |
| 88 | |
|
| 89 | |
public Object[] getPathToRoot(GlossItem item) { |
| 90 | 0 | Object[] result = new Object[3]; |
| 91 | 0 | result[2] = item; |
| 92 | 0 | result[1] = item.getHeading(); |
| 93 | 0 | result[0] = mRoot; |
| 94 | 0 | return result; |
| 95 | |
} |
| 96 | |
|
| 97 | |
} |