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.awt.dnd.DropTargetDropEvent;
036import java.awt.dnd.DropTargetEvent;
037import java.io.IOException;
038import java.util.Enumeration;
039import java.util.Vector;
040
041import javax.swing.DefaultCellEditor;
042
043/**
044 * This is the is derived from the abstract base class DBSelectTableUIBase, that
045 * displays all the items to be displayed as part of the query. The advanced
046 * class only allows the display attribute to be set
047 */
048public class DBSelectTableUIAdv extends DBSelectTableUIBase {
049        /**
050         * Default Constructor
051         */
052        public DBSelectTableUIAdv() {
053                mDataFlavor[0] = new DataFlavor(DBTableField.class, "DBTableField");
054                // mDataFlavor[1] = new DataFlavor(DSTableFieldDef.class,
055                // "DSTableFieldDef"); // XXX Is this still needed???
056
057        }
058
059        /**
060         * Notification that an model item was added
061         * 
062         * @param aItem
063         */
064        protected void itemWasAdded(DBSelectTableModelItem aItem) {
065                // For Advanced Model
066                if (mModel.getSchema() instanceof DBTableDesktopPane) {
067                        aItem.setDisplayed(true);
068
069                        super.itemWasAdded(aItem); // notify listener
070                }
071
072        }
073
074        /**
075         * Creates and Installs cell editors, must be called AFTER setting the model
076         * 
077         */
078        public void installEditors() {
079                super.installEditors();
080                getColumnModel().getColumn(3).setCellEditor(
081                                new DefaultCellEditor(mIsDisplayedCheckbox));
082        }
083
084        /**
085         * Return the DBTableField from the transferable
086         * 
087         * @param e
088         *            the event
089         * @return the field item for this transferable
090         */
091        private DBTableField getTransferableAsDBField(DropTargetDropEvent e) {
092                DBTableField dbTableField = null;
093                try {
094                        Transferable tr = e.getTransferable();
095                        if (tr != null) {
096                                if (tr.isDataFlavorSupported(mDataFlavor[0])) {
097                                        dbTableField = (DBTableField) tr
098                                                        .getTransferData(mDataFlavor[0]);
099                                }
100                        }
101                } catch (IOException io) {
102                        io.printStackTrace();
103                } catch (UnsupportedFlavorException ufe) {
104                        ufe.printStackTrace();
105                }
106                return dbTableField;
107        }
108
109        /**
110         * Does the drop
111         * 
112         * @param e
113         *            the event
114         */
115        protected void doDrop(DropTargetDropEvent e) {
116
117                DBTableField dbTableField = getTransferableAsDBField(e);
118                if (dbTableField != null) {
119                        e.acceptDrop(mAceptableActions);
120                        e.getDropTargetContext().dropComplete(true);
121
122                        String tableName = dbTableField.getTable().getName();
123                        String fieldName = dbTableField.getName();
124
125                        boolean found = false;
126                        Vector fieldNames = mModel.getAvailableFieldNames(tableName);
127                        for (Enumeration et = fieldNames.elements(); et.hasMoreElements();) {
128                                String tblFieldName = (String) et.nextElement();
129                                if (tblFieldName.equals(fieldName)) {
130                                        found = true;
131                                        break;
132                                }
133                        }
134
135                        if (found) {
136                                itemWasAdded(mModel.add(dbTableField, false));
137                        }
138
139                        dragExit((DropTargetEvent) null);
140                        return;
141                }
142
143                e.rejectDrop();
144                dragExit((DropTargetEvent) null);
145        }
146}