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 * Used to render the table of items (rows) that make up the entire query.
045 */
046public class DBSelectTableUIStd extends DBSelectTableUIBase {
047        public static final String[] OPERS = { DBUIUtils.NO_NAME, " = ", " != ",
048                        " > ", " < " };
049        public static final String[] OPERS_TXT = { DBUIUtils.NO_NAME, "EQUALS",
050                        "NOT EQUALS", "GREATER THAN", "LESS THAN" };
051        public static final int NONAME_INX = 0;
052        public static final int EQUALS_INX = 1;
053        public static final int NOTEQUALS_INX = 2;
054        public static final int GT_INX = 3;
055        public static final int LT_INX = 4;
056
057        /**
058         * Default Constructor
059         * 
060         */
061        public DBSelectTableUIStd() {
062                mDataFlavor[0] = new DataFlavor(DBSelectTableModelItem.class,
063                                "DBSelectTableModelItem");
064        }
065
066        /**
067         * Notification that an model item was added
068         * 
069         * @param aItem
070         */
071        protected void itemWasAdded(DBSelectTableModelItem aItem) {
072                aItem.setDisplayed(true);
073                super.itemWasAdded(aItem); // notify listener
074        }
075
076        /**
077         * Creates and Installs cell editors, must be called AFTER setting the model
078         */
079        public void installEditors() {
080                super.installEditors();
081
082                for (int i = 0; i < OPERS_TXT.length; i++) {
083                        mBoolOpers.addItem(OPERS_TXT[i]);
084                }
085                getColumn("Operator").setCellEditor(new DefaultCellEditor(mBoolOpers));
086                getColumn("Operator").setCellRenderer(new JComboBoxCellRenderer());
087                // add right click menu
088                PopupListener rightMenuListener = new PopupListener(this);
089                this.addMouseListener(rightMenuListener);
090        }
091
092        /**
093         * Return the DBTableField from the transferable
094         * 
095         * @param e
096         *            the event
097         * @return the field item for this transferable
098         */
099        private DBSelectTableModelItem getTransferableAsItem(DropTargetDropEvent e) {
100                DBSelectTableModelItem item = null;
101                try {
102                        Transferable tr = e.getTransferable();
103                        if (tr != null) {
104                                if (tr.isDataFlavorSupported(mDataFlavor[0])) {
105                                        item = (DBSelectTableModelItem) tr
106                                                        .getTransferData(mDataFlavor[0]);
107                                }
108                        }
109                } catch (IOException io) {
110                        io.printStackTrace();
111                } catch (UnsupportedFlavorException ufe) {
112                        ufe.printStackTrace();
113                }
114                return item;
115        }
116
117        /**
118         * Does the drop,
119         */
120        protected void doDrop(DropTargetDropEvent e) {
121
122                DBSelectTableModelItem item = getTransferableAsItem(e);
123                if (item != null) {
124                        e.acceptDrop(mAceptableActions);
125                        e.getDropTargetContext().dropComplete(true);
126
127                        String tableName = item.getTableName();
128                        String fieldName = item.getName();
129
130                        boolean found = false;
131                        Vector fieldNames = mModel.getAvailableFieldNames(tableName);
132                        for (Enumeration et = fieldNames.elements(); et.hasMoreElements();) {
133                                String tblFieldName = (String) et.nextElement();
134                                if (tblFieldName.equals(fieldName)) {
135                                        found = true;
136                                        break;
137                                }
138                        }
139
140                        if (found) {
141                                itemWasAdded(mModel.add(item, false));
142                        }
143
144                        dragExit((DropTargetEvent) null);
145                        return;
146                }
147
148                e.rejectDrop();
149                dragExit((DropTargetEvent) null);
150        }
151
152        /**
153         * Returns the symbol for the text descript
154         * 
155         * @param aBoolStr
156         *            the string name of boolean operator
157         * @return symbol string
158         */
159        public static String getBoolOperSymbol(String aBoolStr) {
160                for (int i = 0; i < OPERS_TXT.length; i++) {
161                        if (aBoolStr.equals(OPERS_TXT[i])) {
162                                return OPERS[i];
163                        }
164                }
165                return aBoolStr;
166        }
167
168}