001: /*
002: * Created on 07/09/2007
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2007 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.gfpshare.beans.table;
024:
025: import javax.swing.tree.DefaultMutableTreeNode;
026:
027: import br.com.gfpshare.db.PersistentObject;
028:
029: /**
030: * Esta classe extende o DBTreeTableModel default para permitir que ele exiba não somente
031: * propriedades referentes a colunas da tabela a qual o Bo esta amarrado, mas também
032: * propriedades que podem ser resultado de calculos ou até mesmo resultado de consultas em
033: * outras tabelas do banco de dados.
034: *
035: * Para proriedades resultantes de consultas a DB deve-se atentar para o controle de cache
036: * pois este foge a responsabilidade deste componente.
037: *
038: * @author Igor Regis da Silva Simoes
039: * @since 07/09/2007
040: */
041: public class ExtendedDBTreeTableModel extends DBTreeTableModel {
042: private final String[] extraProperties;
043:
044: public ExtendedDBTreeTableModel(PersistentObject dataType,
045: String fieldParentName, String[] extraProperties) {
046: super (dataType, fieldParentName);
047: this .extraProperties = extraProperties;
048: }
049:
050: @Override
051: public int getColumnCount() {
052: return super .getColumnCount() + extraProperties.length;
053: }
054:
055: @Override
056: public int findColumn(String columnName) {
057: int column = super .findColumn(columnName);
058: if (column == -1)
059: for (int i = 0; i < extraProperties.length; i++)
060: if (extraProperties[i].equals(columnName))
061: return i + super .getColumnCount() + 1;
062: return column;
063: }
064:
065: @Override
066: public String getColumnName(int columnIndex) {
067: String nome = super .getColumnName(columnIndex);
068: if (nome == null)
069: return extraProperties[columnIndex - super .getColumnCount()];
070: return nome;
071: }
072:
073: @Override
074: public Object getValueAt(int rowIndex, int columnIndex) {
075: if (columnIndex < super .getColumnCount())
076: return super .getValueAt(rowIndex, columnIndex);
077: try {
078: PersistentObject p = getPersistentObject(rowIndex);
079: Object ret = p.getClass().getMethod(
080: "get"
081: + extraProperties[columnIndex
082: - super .getColumnCount()], null)
083: .invoke(p, null);
084: System.out.println(ret);
085: return ret;
086: } catch (Exception e) {
087: // TODO Auto-generated catch block
088: e.printStackTrace();
089: }
090: return null;
091: }
092:
093: @Override
094: public Object getValueAt(Object node, int column) {
095: Object retorno = super .getValueAt(node, column);
096: if (retorno == null) {
097: if (((DefaultMutableTreeNode) node).getUserObject() instanceof PersistentObject) {
098: try {
099: return ((PersistentObject) ((DefaultMutableTreeNode) node)
100: .getUserObject())
101: .getClass()
102: .getMethod("get" + getColumnName(column),
103: null)
104: .invoke(
105: (PersistentObject) ((DefaultMutableTreeNode) node)
106: .getUserObject(), null);
107: } catch (Exception e) {
108: // TODO Auto-generated catch block
109: e.printStackTrace();
110: }
111: }
112: }
113: return retorno;
114: }
115: }
|