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.awt.datatransfer.DataFlavor;
033import java.awt.datatransfer.Transferable;
034import java.awt.datatransfer.UnsupportedFlavorException;
035import java.io.IOException;
036import java.util.Vector;
037
038/**
039 * This class represents each row in the JTable that enables the user to select
040 * a specific field in table and any additional information that can be changed.
041 * The mDisplayItem data member will hold a valid pointer to an object in the
042 * schema in the Advanced version, but not in the Standard.
043 */
044public class DBSelectTableModelItem implements Transferable, DBDisplayItemIFace {
045        protected DBDisplayItemIFace mDisplayItem = null;
046        protected int mTableId = -1;
047        protected String mTableName = "";
048        protected String mName = "";
049        protected String mDataType = "";
050        protected boolean mIsDisplayed = false; // Uses mDisplayItem's display attr
051                                                                                        // first if possible
052        protected String mCriteria = "";
053        protected String mOper = "";
054
055        protected DataFlavor mFlavors[] = new DataFlavor[1];
056
057        private Vector mMissingValueCode = new Vector();
058
059        /**
060         * Default Constructor
061         */
062        public DBSelectTableModelItem() {
063                mFlavors[0] = new DataFlavor(DBSelectTableModelItem.class,
064                                "DBSelectTableModelItem");
065        }
066
067        /**
068         * Constructor with args
069         * 
070         * @param aTableName
071         *            table name
072         * @param aFieldName
073         *            field name
074         * @param aDataType
075         *            data type
076         * @param aIsDisplayed
077         *            whether it is displayed
078         * @param aCriteria
079         *            criteria
080         * @param aOperator
081         *            operator
082         */
083        public DBSelectTableModelItem(String aTableName, String aFieldName,
084                        String aDataType, boolean aIsDisplayed, String aCriteria,
085                        String aOperator, Vector missingValue) {
086                mTableName = aTableName;
087                mName = aFieldName;
088                mDataType = aDataType;
089                mIsDisplayed = aIsDisplayed;
090                mCriteria = aCriteria;
091                mOper = aOperator;
092                mMissingValueCode = missingValue;
093        }
094
095        /**
096         * Copy Constructor
097         * 
098         * @param aItem
099         *            item to be copied
100         */
101        public DBSelectTableModelItem(DBSelectTableModelItem aItem) {
102                mTableId = aItem.mTableId;
103                mDisplayItem = aItem.mDisplayItem;
104                mTableName = aItem.mTableName;
105                mName = aItem.mName;
106                mDataType = aItem.mDataType;
107                mIsDisplayed = aItem.mIsDisplayed;
108                mCriteria = aItem.mCriteria;
109                mOper = aItem.mOper;
110                mMissingValueCode = aItem.getMissingValueCode();
111        }
112
113        /**
114         * Constructor from DBTableField
115         */
116        public DBSelectTableModelItem(DBDisplayItemIFace aDisplayItem) {
117                if (aDisplayItem != null) {
118                        mDisplayItem = aDisplayItem;
119                        mTableName = aDisplayItem.getTableName();
120                        mName = aDisplayItem.getName();
121                        mDataType = aDisplayItem.getDataType();
122                        mIsDisplayed = aDisplayItem.isDisplayed();
123                }
124        }
125
126        /**
127         * Sets the DBDisplayItemIFace object
128         * 
129         * @param aDisplayItem
130         *            object to set
131         */
132        public void setDisplayItem(DBDisplayItemIFace aDisplayItem) {
133                mDisplayItem = aDisplayItem;
134        }
135
136        /**
137         * Gets the DBDisplayItemIFace object
138         * 
139         * @return DisplayItem object
140         */
141        public DBDisplayItemIFace getDisplayItem() {
142                return mDisplayItem;
143        }
144
145        /**
146         * Gets the dataType
147         * 
148         * @return Returns the dataType.
149         */
150        public String getDataType() {
151                return mDataType;
152        }
153
154        /**
155         * Sets the dataType
156         * 
157         * @param aDataType
158         *            The dataType to set.
159         */
160        public void setDataType(String aDataType) {
161                mDataType = aDataType;
162        }
163
164        /**
165         * Returns whether the item is to be "displayed" in the select statment
166         * 
167         * @return Returns the isDisplayed.
168         */
169        public boolean isDisplayed() {
170                return mDisplayItem != null ? mDisplayItem.isDisplayed() : mIsDisplayed;
171        }
172
173        /**
174         * Sets the displayed attr, if it has a mDisplayItem it will use that
175         * instead for caching the value and making sure the UI is properly updated
176         * 
177         * @param isDisplayed
178         *            The isDisplayed to set.
179         */
180        public void setDisplayed(boolean isDisplayed) {
181                if (mDisplayItem != null) {
182                        mDisplayItem.setDisplayed(isDisplayed);
183                } else {
184                        mIsDisplayed = isDisplayed;
185                }
186        }
187
188        /**
189         * Return the name
190         * 
191         * @return Returns the name.
192         */
193        public String getName() {
194                return mName;
195        }
196
197        /**
198         * @param aName
199         *            The name to set.
200         */
201        public void setName(String aName) {
202                mName = aName;
203        }
204
205        /**
206         * Return the table name
207         * 
208         * @return Returns the tableName.
209         */
210        public String getTableName() {
211                return mTableName;
212        }
213
214        /**
215         * Sets the table name
216         * 
217         * @param aTableName
218         *            The tableName to set.
219         */
220        public void setTableName(String aTableName) {
221                mTableName = aTableName;
222        }
223
224        /**
225         * Return the table Id
226         * 
227         * @return Returns the tableId.
228         */
229        public int getTableId() {
230                return mTableId;
231        }
232
233        /**
234         * Sets the table Id
235         * 
236         * @param aTableId
237         *            The table Id to set.
238         */
239        public void setTableId(int aTableId) {
240                mTableId = aTableId;
241        }
242
243        /**
244         * Return the criteria
245         * 
246         * @return Returns the criteria.
247         */
248        public String getCriteria() {
249                return mCriteria;
250        }
251
252        /**
253         * @param aCriteria
254         *            The criteria to set.
255         */
256        public void setCriteria(String aCriteria) {
257                mCriteria = aCriteria;
258        }
259
260        /**
261         * Returns the operator name
262         * 
263         * @return Returns the operator.
264         */
265        public String getOperator() {
266                return mOper;
267        }
268
269        /**
270         * set the operator by name (string)
271         * 
272         * @param aOper
273         *            the operator
274         */
275        public void setOperator(String aOper) {
276                mOper = aOper;
277        }
278
279        /**
280         * Returns the name as the textual rendering (the name)
281         */
282        public String toString() {
283                return mName;
284        }
285
286        // ---------------------------------------------------------------
287        // -- Transferable Interface
288        // ---------------------------------------------------------------
289
290        /**
291         * Returns the array of mFlavors in which it can provide the data.
292         */
293        public synchronized DataFlavor[] getTransferDataFlavors() {
294                return mFlavors;
295        }
296
297        /**
298         * Returns whether the requested flavor is supported by this object.
299         */
300        public boolean isDataFlavorSupported(DataFlavor flavor) {
301                return flavor.equals(mFlavors[0]);
302        }
303
304        /**
305         * If the data was requested in the "java.lang.String" flavor, return the
306         * String representing the selection.
307         */
308        public synchronized Object getTransferData(DataFlavor flavor)
309                        throws UnsupportedFlavorException, IOException {
310                if (flavor.equals(mFlavors[0])) {
311                        return this;
312                } else {
313                        throw new UnsupportedFlavorException(flavor);
314                }
315        }
316
317        /**
318         * Method to return the vector which store the missing value code. If this
319         * attribute doesn't has the missing value code, empty vector will be
320         * returned.
321         * 
322         *       */
323        public Vector getMissingValueCode() {
324                return mMissingValueCode;
325        }
326
327        /**
328         * Method to add missing value code into a vector. This method will be used
329         * to store the missing value code in metadata
330         * 
331         * @param code
332         */
333        public void addMissingValueCode(String code) {
334                if (code != null) {
335                        mMissingValueCode.add(code);
336                }
337        }
338
339        /**
340         * Method to return the vector which store the missing value code. If this
341         * attribute doesn't has the missing value code, empty vector will be
342         * returned.
343         * 
344         *       */
345        public void setMissingValueCode(Vector missingValueVector) {
346                mMissingValueCode = missingValueVector;
347        }
348}