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.Dimension;
033import java.awt.Point;
034import java.awt.Rectangle;
035import java.awt.datatransfer.DataFlavor;
036import java.awt.datatransfer.Transferable;
037import java.awt.datatransfer.UnsupportedFlavorException;
038import java.io.IOException;
039import java.util.Vector;
040
041import org.kepler.objectmanager.data.db.DSTableFieldDef;
042import org.kepler.objectmanager.data.db.DSTableFieldIFace;
043import org.kepler.objectmanager.data.db.DSTableKeyIFace;
044
045/**
046 * A field in the table frame. This is an item that can be dragged and dropped
047 */
048public class DBTableField implements Transferable, DBDisplayItemIFace {
049        protected DSTableFieldIFace mTableField = null;
050        protected DBTableFrame mTableFrame = null;
051        protected Rectangle mRect = new Rectangle(-1, -1, -1, -1);
052        protected boolean mIsLinked = false;
053        protected boolean mIsDragOver = false;
054        protected boolean mIsDisplayed = false;
055        protected Dimension mPreferredDim = null;
056        protected Vector mMissingValueCode = new Vector();
057
058        protected DataFlavor flavors[] = new DataFlavor[1];
059
060        /**
061         * Default Constructor
062         */
063        public DBTableField() {
064        }
065
066        /**
067         * Constructor
068         * 
069         * @param aField
070         *            the database field
071         * @param aOwner
072         *            the table owning the field
073         */
074        public DBTableField(DSTableFieldIFace aField, DBTableFrame aOwner) {
075                super();
076                mTableField = aField;
077                mTableFrame = aOwner;
078                if (aField != null)
079                        flavors[0] = new DataFlavor(DBTableField.class, "DBTableField");
080                else
081                        flavors[0] = new DataFlavor(DSTableFieldDef.class,
082                                        "DSTableFieldDef");
083        }
084
085        /**
086         * Get the iface obj representing the key
087         * 
088         * @return return obj if it is a key, otherwise false
089         */
090        public DSTableKeyIFace getTableKeyIFace() {
091                return mTableField instanceof DSTableKeyIFace ? (DSTableKeyIFace) mTableField
092                                : null;
093        }
094
095        /**
096         * Returns the owner object
097         * 
098         * @return the owner object
099         */
100        public DBTableFrame getTable() {
101                return mTableFrame;
102        }
103
104        /**
105         * Set whether it is a link or not
106         * 
107         * @param aVal
108         *            whether is it a link
109         */
110        public void setLinked(boolean aVal) {
111                mIsLinked = aVal;
112        }
113
114        /**
115         * Return whether it is a link
116         * 
117         * @return true if linked
118         */
119        public boolean getLinked() {
120                return mIsLinked;
121        }
122
123        /**
124         * Set whether it is displayed or not
125         * 
126         * @param aVal
127         *            true if the display attr is to be set
128         */
129        public void setDisplayed(boolean aVal) {
130                mIsDisplayed = aVal;
131        }
132
133        /**
134         * Return whether it is being displayed
135         * 
136         * @return true if the display attr is set
137         */
138        public boolean isDisplayed() {
139                return mIsDisplayed;
140        }
141
142        /**
143         * Return the name
144         * 
145         * @return name as a string
146         */
147        public String getName() {
148                return mTableField == null ? DBUIUtils.ALL_FIELDS : mTableField
149                                .getName();
150        }
151
152        /**
153         * Return the table name (returns the owner's name)
154         * 
155         * @return name as a string
156         */
157        public String getTableName() {
158                return mTableFrame != null ? mTableFrame.getName() : "";
159        }
160
161        /**
162         * Return the datatype
163         * 
164         * @return data type per DataDescription class
165         */
166        public String getDataType() {
167                return mTableField == null ? "" : mTableField.getDataType();
168        }
169
170        /**
171         * Sets whether it is currently being dragged over
172         * 
173         * @param aVal
174         *            true if dragged over otherwise false
175         */
176        public void setDragOver(boolean aVal) {
177                mIsDragOver = aVal;
178        }
179
180        /**
181         * Returns if it is currently be dragged over
182         * 
183         * @return true if it is currently be dragged over
184         */
185        public boolean isDragOver() {
186                return mIsDragOver;
187        }
188
189        /**
190         * Returns the actual name or DBUIUtils.ALL_FIELDS if no name
191         */
192        public String toString() {
193                return mTableField == null ? DBUIUtils.ALL_FIELDS : mTableField
194                                .getName();// + (mIsDisplayed ? DBUIUtils.ALL_FIELDS : "");
195        }
196
197        /**
198         * Returns the bounds of the item
199         * 
200         * @return the bounds
201         */
202        public Rectangle getBounds() {
203                return new Rectangle(mRect);
204        }
205
206        /**
207         * Sets the rectangle if the item
208         * 
209         * @param aRect
210         */
211        public void setRect(Rectangle aRect) {
212                mRect.setBounds(aRect);
213        }
214
215        /**
216         * Sets the rectangle
217         * 
218         * @param x
219         *            X Do the same as dragOver
220         * @param y
221         *            Y coord
222         * @param w
223         *            Width
224         * @param h
225         *            Height
226         */
227        public void setRect(int x, int y, int w, int h) {
228                mRect.setBounds(x, y, w, h);
229        }
230
231        /**
232         * Translates its position via the point
233         * 
234         * @param aPnt
235         *            the delta point
236         */
237        public void add(Point aPnt) {
238                mRect.translate(aPnt.x, aPnt.y);
239        }
240
241        /**
242         * Sets the preferred dimension
243         * 
244         * @param aDim
245         */
246        public void setPreferredDim(Dimension aDim) {
247                mPreferredDim = aDim;
248        }
249
250        /**
251         * Gets the preferred dimension
252         * 
253         * @return the dimension
254         */
255        public Dimension getPreferredDim() {
256                return mPreferredDim;
257        }
258
259        // ---------------------------------------------------------------
260        // -- Transferable Interface
261        // ---------------------------------------------------------------
262
263        /**
264         * Returns the array of flavors in which it can provide the data.
265         */
266        public synchronized DataFlavor[] getTransferDataFlavors() {
267                return flavors;
268        }
269
270        /**
271         * Returns whether the requested flavor is supported by this object.
272         */
273        public boolean isDataFlavorSupported(DataFlavor flavor) {
274                return flavor.equals(flavors[0]);
275        }
276
277        /**
278         * If the data was requested in the "java.lang.String" flavor, return the
279         * String representing the selection.
280         */
281        public synchronized Object getTransferData(DataFlavor flavor)
282                        throws UnsupportedFlavorException, IOException {
283                if (flavor.equals(flavors[0])) {
284                        return this;
285                } else {
286                        throw new UnsupportedFlavorException(flavor);
287                }
288        }
289
290        /**
291         * Method to return the vector which store the missing value code. If this
292         * attribute doesn't has the missing value code, empty vector will be
293         * returned.
294         * 
295         *       */
296        public Vector getMissingValueCode() {
297                return mMissingValueCode;
298        }
299
300        /**
301         * Method to add missing value code into a vector. This method will be used
302         * to store the missing value code in metadata
303         * 
304         * @param code
305         */
306        public void addMissingValueCode(String code) {
307                if (code != null) {
308                        mMissingValueCode.add(code);
309                }
310        }
311
312        /**
313         * Method to return the vector which store the missing value code. If this
314         * attribute doesn't has the missing value code, empty vector will be
315         * returned.
316         * 
317         *       */
318        public void setMissingValueCode(Vector missingValueVector) {
319                mMissingValueCode = missingValueVector;
320        }
321}