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;
033
034import org.kepler.objectmanager.data.db.DSSchemaIFace;
035import org.kepler.objectmanager.data.db.DSTableFieldIFace;
036
037/**
038 * This class extends DBSelectTableModelBase and is used to enabled the user to
039 * indicate which items will be displayed. It's JTable (view) is displayed in
040 * the "Select" table in the lower panel.
041 */
042public class DBSelectTableModelAdv extends DBSelectTableModelBase {
043        protected String[] COLUMN_TITLES = { "Table", "Field", "Data Type",
044                        "Display" };// , "Criteria"};
045
046        /**
047         * Constructor for Table Model
048         * 
049         * @param aSchema
050         *            the schema object
051         */
052        public DBSelectTableModelAdv(DSSchemaIFace aSchema) {
053                super(aSchema);
054        }
055
056        /**
057         * Look up in the Schema for the field in it's table Then checks to see if
058         * it is an instanceof DBTableField and returns that
059         * 
060         * @param aItemCell
061         *            TableMode cell item
062         * @return List Cell Item (hopefully), or null if it isn't an instance of
063         *         DBTableField
064         */
065        protected DBTableField getFieldFor(DBSelectTableModelItem aItemCell) {
066                DSTableFieldIFace fieldIFace = DBUIUtils.getFieldByName(mSchema,
067                                aItemCell.getTableName(), aItemCell.getName());
068                if (fieldIFace instanceof DBTableField) {
069                        return (DBTableField) fieldIFace;
070                }
071                return null;
072        }
073
074        /**
075         * Sets Display attr in UI List cell, and causes a refresh to occur
076         * 
077         * @param aFieldCell
078         *            field
079         * @param aFlag
080         *            whether it is displayed or not
081         */
082        protected void setDisplayListCell(DBSelectTableModelItem aFieldCell,
083                        boolean aFlag) {
084                DBTableField field = getFieldFor(aFieldCell);
085                if (field != null) {
086                        field.setDisplayed(aFlag);
087                        if (mSchema instanceof DBTableDesktopPane) {
088                                ((DBTableDesktopPane) mSchema).makeDirty();
089                        }
090                }
091        }
092
093        /**
094         * Returns the number of columns
095         * 
096         * @return Number of columns
097         */
098        public int getColumnCount() {
099                return COLUMN_TITLES.length;
100        }
101
102        /**
103         * Returns the Class object for a column
104         * 
105         * @param aCol
106         *            the column in question
107         * @return the Class of the column
108         */
109        public Class getColumnClass(int aCol) {
110                if (aCol == 3)
111                        return Boolean.class;
112                else
113                        return getValueAt(0, aCol).getClass();
114        }
115
116        /**
117         * Indicates if col and row is editable
118         * 
119         * @param aRow
120         *            index of row
121         * @param aCol
122         *            index of column
123         * @return true if editable, false if not
124         */
125        public boolean isCellEditable(int aRow, int aCol) {
126                boolean tableNameOK = isTableNameOK(aRow);
127                switch (aCol) {
128                case 0:
129                        return true;
130                case 1:
131                        return tableNameOK;
132                case 2:
133                        return false;
134                case 3:
135                        return tableNameOK;
136                case 4: {
137                        DBSelectTableModelItem field = getFieldForRow(aRow);
138                        return field != null && tableNameOK
139                                        && !field.getName().equals(DBUIUtils.ALL_FIELDS);
140                }
141                }
142
143                return true;
144        }
145
146        /**
147         * Get the column name
148         * 
149         * @param aCol
150         *            index of column
151         * @return the column name
152         */
153        public String getColumnName(int aCol) {
154                return COLUMN_TITLES[aCol];
155        }
156
157        /**
158         * Gets the value of the row col
159         * 
160         * @param aRow
161         *            index of row
162         * @param aCol
163         *            index of column
164         */
165        public Object getValueAt(int aRow, int aCol) {
166                DBSelectTableModelItem field = getFieldForRow(aRow);
167                if (field != null) {
168                        switch (aCol) {
169                        case 0:
170                                return field.getTableName();
171                        case 1:
172                                return field.getName();
173                        case 2:
174                                return field.getDataType();
175                        case 3:
176                                return new Boolean(field.isDisplayed());
177                        case 4:
178                                return field.getCriteria();
179                        default:
180                                return "N/A";
181                        }
182                }
183                return "";
184        }
185
186        /**
187         * Sets a new value into the Model
188         * 
189         * @param aValue
190         *            new value
191         * @param aRow
192         *            index of row
193         * @param aCol
194         *            index of column
195         */
196        public void setValueAt(Object aValue, int aRow, int aCol) {
197                DBSelectTableModelItem fieldCell = null;
198                if (aRow < mItems.size()) {
199                        fieldCell = (DBSelectTableModelItem) mItems.elementAt(aRow);
200                        switch (aCol) {
201                        case 0: {
202                                String tableName = (String) aValue;
203                                if (!tableName.equals(DBUIUtils.NO_NAME)) {
204                                        // Get the avilable names that are not yet used
205                                        getAvailableFieldNames(tableName);
206                                        String fieldName = (String) mAvailFieldNames.elementAt(0);
207                                        fieldCell.setTableName(tableName);
208                                        fieldCell.setName(fieldName);
209                                        DSTableFieldIFace fieldIFace = DBUIUtils.getFieldByName(
210                                                        mSchema, tableName, fieldName);
211                                        fieldCell.setDataType(fieldIFace.getDataType());
212                                }
213                                setTableName(fieldCell, aRow, aValue);
214                        }
215                                break;
216
217                        case 1:
218                                setFieldName(fieldCell, aValue);
219                                break;
220
221                        case 2:
222                                // shouldn't happen
223                                break;
224
225                        case 3:
226                                setDisplay(fieldCell, aValue);
227                                break;
228
229                        case 4:
230                                setCriteria(fieldCell, aValue);
231                                break;
232
233                        }
234                        fireTableModelChanged();
235                }
236        }
237
238        /**
239         * Fills QueryDef from Model with "select" items. NOTE: it makes copies of
240         * the items.
241         * 
242         * @param aQueryDef
243         *            item to be copied
244         */
245        public void fillQueryDef(DBQueryDef aQueryDef) {
246                for (Enumeration e = mItems.elements(); e.hasMoreElements();) {
247                        DBSelectTableModelItem item = (DBSelectTableModelItem) e
248                                        .nextElement();
249                        if (item.getTableName().length() > 0
250                                        && !item.getTableName().equals(DBUIUtils.NO_NAME)
251                                        && item.getName().length() > 0) {
252                                aQueryDef.addSelectItem(new DBSelectTableModelItem(item));
253                        }
254                }
255        }
256
257        /**
258         * Builds UI from the Query Definition Object The Advance Model just tracks
259         * what is being displayed
260         * 
261         * @param aQueryDef
262         *            the query
263         */
264        public void buildFromQueryDef(DBQueryDef aQueryDef) {
265                if (aQueryDef == null)
266                        return;
267
268                mItems.clear();
269                for (Enumeration e = aQueryDef.getSelects().elements(); e
270                                .hasMoreElements();) {
271                        DBSelectTableModelItem item = (DBSelectTableModelItem) e
272                                        .nextElement();
273                        if (item.getTableName().length() > 0
274                                        && !item.getTableName().equals(DBUIUtils.NO_NAME)
275                                        && item.getName().length() > 0) {
276                                mItems.add(new DBSelectTableModelItem(item));
277                                // XXX not using setDisplayListCell,
278                                // for some reason the last item does get updated correctly
279                                DBTableField field = getFieldFor(item);
280                                item.setDisplayed(true);
281                                if (field != null) {
282                                        field.setDisplayed(true);
283                                }
284                                // XXX setDisplayListCell(item, true);
285                        }
286                }
287
288                // Make sure everything gets correctly update
289                if (mSchema instanceof DBTableDesktopPane) {
290                        ((DBTableDesktopPane) mSchema).makeDirty();
291                }
292
293                add((DBTableField) null, true);
294                fireTableModelChanged();
295        }
296
297}