001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui;
020:
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import javax.swing.event.TableModelListener;
026: import javax.swing.event.EventListenerList;
027: import javax.swing.table.DefaultTableModel;
028: import javax.swing.tree.TreeNode;
029:
030: import org.apache.jorphan.reflect.Functor;
031:
032: public abstract class AbstractTreeTableModel extends DefaultTableModel
033: implements TreeTableModel {
034:
035: protected TreeNode rootNode = null;
036: protected EventListenerList listener = new EventListenerList();
037:
038: protected transient ArrayList objects = new ArrayList();
039:
040: protected transient List headers = new ArrayList();
041:
042: protected transient ArrayList classes = new ArrayList();
043:
044: protected transient ArrayList readFunctors = new ArrayList();
045:
046: protected transient ArrayList writeFunctors = new ArrayList();
047:
048: public AbstractTreeTableModel(TreeNode root) {
049: this .rootNode = root;
050: }
051:
052: public AbstractTreeTableModel(TreeNode root, boolean editable) {
053: this .rootNode = root;
054: }
055:
056: public AbstractTreeTableModel(String[] headers,
057: Functor[] readFunctors, Functor[] writeFunctors,
058: Class[] editorClasses) {
059: this .headers.addAll(Arrays.asList(headers));
060: this .classes.addAll(Arrays.asList(editorClasses));
061: this .readFunctors = new ArrayList(Arrays.asList(readFunctors));
062: this .writeFunctors = new ArrayList(Arrays.asList(writeFunctors));
063: }
064:
065: /**
066: * The root node for the TreeTable
067: * @return the root node
068: */
069: public Object getRootNode() {
070: return this .rootNode;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.apache.jorphan.gui.TreeTableModel#getValueAt(java.lang.Object, int)
075: */
076: public Object getValueAt(Object node, int col) {
077: return null;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.apache.jorphan.gui.TreeTableModel#isCellEditable(java.lang.Object, int)
082: */
083: public boolean isCellEditable(Object node, int col) {
084: return false;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.apache.jorphan.gui.TreeTableModel#setValueAt(java.lang.Object, java.lang.Object, int)
089: */
090: public void setValueAt(Object val, Object node, int column) {
091: }
092:
093: /**
094: * The implementation is exactly the same as ObjectTableModel.getColumnCount.
095: */
096: public int getColumnCount() {
097: return headers.size();
098: }
099:
100: /**
101: * The implementation is exactly the same as ObjectTableModel.getRowCount.
102: */
103: public int getRowCount() {
104: if (objects == null) {
105: return 0;
106: }
107: return objects.size();
108: }
109:
110: /**
111: * By default the abstract class returns true. It is up to subclasses
112: * to override the implementation.
113: */
114: public boolean isCellEditable(int rowIndex, int columnIndex) {
115: return true;
116: }
117:
118: public Class getColumnClass(int arg0) {
119: return (Class) classes.get(arg0);
120: }
121:
122: /**
123: * Subclasses need to implement the logic for the method and
124: * return the value at the specific cell.
125: */
126: public Object getValueAt(int rowIndex, int columnIndex) {
127: return null;
128: }
129:
130: /**
131: *
132: */
133: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
134:
135: }
136:
137: /**
138: *
139: */
140: public String getColumnName(int columnIndex) {
141: return (String) headers.get(columnIndex);
142: }
143:
144: public int getChildCount(Object parent) {
145: return 0;
146: }
147:
148: public Object getChild(Object parent, int index) {
149: return null;
150: }
151:
152: /**
153: * the implementation checks if the Object is a treenode. If it is,
154: * it returns isLeaf(), otherwise it returns false.
155: * @param node
156: * @return whether object is a leaf node or not
157: */
158: public boolean isLeaf(Object node) {
159: if (node instanceof TreeNode) {
160: return ((TreeNode) node).isLeaf();
161: } else {
162: return false;
163: }
164: }
165:
166: /**
167: *
168: */
169: public void addTableModelListener(TableModelListener l) {
170: this .listener.add(TableModelListener.class, l);
171: }
172:
173: /**
174: *
175: */
176: public void removeTableModelListener(TableModelListener l) {
177: this .listener.remove(TableModelListener.class, l);
178: }
179:
180: public void nodeStructureChanged(TreeNode node) {
181:
182: }
183:
184: public void fireTreeNodesChanged(TreeNode source, Object[] path,
185: int[] indexes, Object[] children) {
186:
187: }
188:
189: public void clearData() {
190: int size = getRowCount();
191: objects.clear();
192: super .fireTableRowsDeleted(0, size);
193: }
194: }
|