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.Rectangle;
033import java.awt.datatransfer.DataFlavor;
034import java.awt.dnd.DnDConstants;
035import java.awt.dnd.DragGestureEvent;
036import java.awt.dnd.DragGestureListener;
037import java.awt.dnd.DragSource;
038import java.awt.dnd.DragSourceContext;
039import java.awt.dnd.DragSourceDragEvent;
040import java.awt.dnd.DragSourceDropEvent;
041import java.awt.dnd.DragSourceEvent;
042import java.awt.dnd.DragSourceListener;
043
044import javax.swing.JComponent;
045import javax.swing.JTable;
046import javax.swing.RepaintManager;
047import javax.swing.SwingUtilities;
048
049/**
050 * The JTable of the table schema for "overview", meaning it only displays the
051 * field name and the datatype. This also enables the field items to be draged
052 * from
053 */
054public class DBSelectTableOverviewTable extends JTable implements
055                DragSourceListener, DragGestureListener,
056                DBSelectTableFieldChangedListener {
057        protected int mAceptableActions = DnDConstants.ACTION_COPY_OR_MOVE;
058        protected DragSource mDragSource = DragSource.getDefaultDragSource();
059        protected DataFlavor mDataFlavor = new DataFlavor(
060                        DBSelectTableModelItem.class, "DBSelectTableModelItem");
061
062        /**
063         * Constructor
064         * 
065         * @param aModel
066         *            table overview model
067         */
068        public DBSelectTableOverviewTable(DBSelectTableOverviewModel aModel) {
069                super(aModel);
070                mDragSource.createDefaultDragGestureRecognizer(this, mAceptableActions,
071                                this);
072        }
073
074        /**
075         * Makes sure the entire "panel" will be redrawn by marking the entire
076         * bounds "dirty"
077         */
078        protected void dirtyAll() {
079                Rectangle rect = getBounds();
080                RepaintManager mgr = RepaintManager.currentManager(this);
081                mgr.addDirtyRegion((JComponent) this, rect.x, rect.y, rect.width,
082                                rect.height);
083        }
084
085        /**
086         * Makes sure the entire "panel" will be redrawn
087         * 
088         */
089        public void makeDirty() {
090                SwingUtilities.invokeLater(new Runnable() {
091                        public void run() {
092                                dirtyAll();
093                        }
094                });
095        }
096
097        // --------------------------------------------------------------
098        // ------------------ Drag Source Methods -----------------------
099        // --------------------------------------------------------------
100
101        /**
102         * stubbed
103         */
104        public void dragDropEnd(DragSourceDropEvent e) {
105        }
106
107        /**
108   *
109   */
110        public void dragEnter(DragSourceDragEvent e) {
111                DragSourceContext context = e.getDragSourceContext();
112
113                // intersection of the users selected action, and the source and target
114                // actions
115                int myaction = e.getDropAction();
116                // System.out.println("dragEnter Src- dropAction: "+myaction +
117                // " mAceptableActions "+mAceptableActions +"  "+(((myaction &
118                // mAceptableActions) != 0)));
119                if ((myaction & mAceptableActions) != 0) {
120                        context.setCursor(DragSource.DefaultLinkDrop);
121                } else {
122                        context.setCursor(DragSource.DefaultLinkNoDrop);
123                }
124        }
125
126        /**
127         * sets the cursor
128         */
129        public void dragExit(DragSourceEvent e) {
130                e.getDragSourceContext().setCursor(DragSource.DefaultLinkNoDrop);
131        }
132
133        /**
134         * Same as drag enter
135         */
136        public void dragOver(DragSourceDragEvent e) {
137                dragEnter(e);
138        }
139
140        /**
141         * stubbed
142         */
143        public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
144        }
145
146        // --------------------------------------------------------------
147        // ------- DBSelectTableFieldChangedListener Methods ------------
148        // --------------------------------------------------------------
149
150        /**
151         * When notified, it makes everything as dirty for an visual update
152         */
153        public void notifyFieldChanged() {
154                makeDirty();
155        }
156
157        // --------------------------------------------------------------
158        // -------------- DragGestureListener Methods -------------------
159        // --------------------------------------------------------------
160
161        /**
162         * Starts drag
163         */
164        public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
165                Object obj = null;// getSelectedValue();
166                if (getSelectedRowCount() == 1) {
167                        int rowInx = this.getSelectedRow();
168                        int colInx = this.getSelectedColumn();
169                        DBSelectTableModelItem item = (DBSelectTableModelItem) getModel()
170                                        .getValueAt(rowInx, colInx);
171                        // if (!item.isDisplayed()) // XXX not about this, it might confuse
172                        // the user
173                        obj = item;
174                }
175
176                if (obj == null) {
177                        // Nothing selected, nothing to drag
178                        // System.out.println ("Nothing selected - beep");
179                        getToolkit().beep();
180                } else {
181                        DBSelectTableModelItem item = (DBSelectTableModelItem) obj;
182                        dragGestureEvent
183                                        .startDrag(DragSource.DefaultLinkNoDrop, item, this);
184                }
185        }
186
187}