001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.querybuilder;
031
032import java.util.Enumeration;
033import java.util.Vector;
034
035import javax.swing.JTable;
036import javax.swing.table.AbstractTableModel;
037
038import org.kepler.objectmanager.data.db.DSTableFieldIFace;
039import org.kepler.objectmanager.data.db.DSTableIFace;
040
041/**
042 * This derived class is used for representing a table's schema in a UI "tab"
043 */
044public class DBSelectTableOverviewModel extends AbstractTableModel {
045        protected static final String[] COLUMN_TITLES = { "Field Name", "Data Type" };
046
047        protected DSTableIFace mTable = null;
048        protected Vector mItems = null;
049        protected JTable mTableView = null;
050
051        /**
052         * Constructor for Table Model
053         * 
054         * @param aTable
055         *            the table
056         */
057        public DBSelectTableOverviewModel(DSTableIFace aTable) {
058                mTable = aTable;
059
060                mItems = new Vector();
061                DBSelectTableModelItem item = new DBSelectTableModelItem();
062                item.setName(DBUIUtils.ALL_FIELDS);
063                item.setTableName(aTable.getName());
064                mItems.add(item);
065
066                for (Enumeration et = mTable.getFields().elements(); et
067                                .hasMoreElements();) {
068                        DSTableFieldIFace field = (DSTableFieldIFace) et.nextElement();
069                        item = new DBSelectTableModelItem();
070                        item.setName(field.getName());
071                        item.setDataType(field.getDataType());
072                        item.setTableName(aTable.getName());
073                        mItems.add(item);
074                }
075        }
076
077        /**
078         * The name of the table for this model
079         * 
080         * @return the table name
081         */
082        public String getTableName() {
083                return mTable.getName();
084        }
085
086        /**
087         * Set the table's view
088         * 
089         * @param aTableView
090         *            a JTable
091         */
092        public void setTableView(JTable aTableView) {
093                mTableView = aTableView;
094        }
095
096        /**
097         * The table's view
098         * 
099         * @return the JTable
100         */
101        public JTable getTableView() {
102                return mTableView;
103        }
104
105        /**
106         * Returns DBSelectTableModelItem by name
107         * 
108         * @param aName
109         *            name of item to be returned
110         * @return the item
111         */
112        public DBSelectTableModelItem getItemByName(String aName) {
113                for (Enumeration e = mItems.elements(); e.hasMoreElements();) {
114                        DBSelectTableModelItem item = (DBSelectTableModelItem) e
115                                        .nextElement();
116                        if (item.getName().equals(aName)) {
117                                return item;
118                        }
119                }
120                return null;
121        }
122
123        /**
124         * Returns the number of columns
125         * 
126         * @return Number of columns
127         */
128        public int getColumnCount() {
129                return COLUMN_TITLES.length;
130        }
131
132        /**
133         * Returns the number of rows
134         * 
135         * @return Number of rows
136         */
137        public int getRowCount() {
138                return mItems.size();
139        }
140
141        /**
142         * Returns the Class object for a column
143         * 
144         * @param aColumn
145         *            the column in question
146         * @return the Class of the column
147         */
148        public Class getColumnClass(int aColumn) {
149                return aColumn == 0 ? DBSelectTableModelItem.class : String.class;
150        }
151
152        /**
153         * Gets the current field for a row
154         * 
155         * @return DBSelectTableModelItem
156         */
157        protected DBSelectTableModelItem getFieldForRow(int aRow) {
158                DBSelectTableModelItem field = null;
159                if (aRow < mItems.size()) {
160                        return (DBSelectTableModelItem) mItems.elementAt(aRow);
161                }
162                return null;
163        }
164
165        /**
166         * Always return false
167         */
168        public boolean isCellEditable(int aRrow, int aCol) {
169                return false;
170        }
171
172        /**
173         * Get the column name
174         * 
175         * @param aColumn
176         *            the column in question
177         * @return the column name
178         */
179        public String getColumnName(int aColumn) {
180                return COLUMN_TITLES[aColumn];
181        }
182
183        /**
184         * Gets the value of the row, column
185         * 
186         * @param aRow
187         *            the row in question
188         * @param aColumn
189         *            the column in question
190         * @return the object that that location
191         */
192        public Object getValueAt(int aRow, int aColumn) {
193                DBSelectTableModelItem field = getFieldForRow(aRow);
194
195                // System.out.println("getValueAt ("+row + ","+ column+")  "+field);
196                if (field != null) {
197                        switch (aColumn) {
198                        case 0:
199                                return field;
200                        case 1:
201                                return field.getDataType();
202                        default:
203                                return "N/A";
204                        }
205                }
206                return "";
207        }
208
209        /**
210         * Fills QueryDef from Model
211         * 
212         * @param aQueryDef
213         *            the query
214         */
215        public void fillQueryDef(DBQueryDef aQueryDef) {
216                for (Enumeration e = mItems.elements(); e.hasMoreElements();) {
217                        DBSelectTableModelItem item = (DBSelectTableModelItem) e
218                                        .nextElement();
219                        aQueryDef.addSelectItem(item);
220                }
221        }
222}