Source Code Cross Referenced for EnhancedTable.java in  » Database-Client » iSQL-Viewer » org » isqlviewer » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » iSQL Viewer » org.isqlviewer.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The contents of this file are subject to the Mozilla Public License
003:         * Version 1.1 (the "License"); you may not use this file except in
004:         * compliance with the License. You may obtain a copy of the License at
005:         * http://www.mozilla.org/MPL/
006:         *
007:         * Software distributed under the License is distributed on an "AS IS"
008:         * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009:         * License for the specific language governing rights and limitations
010:         * under the License.
011:         * 
012:         * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013:         *
014:         * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015:         * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016:         *
017:         * Contributor(s): 
018:         *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019:         *  
020:         * If you didn't download this code from the following link, you should check
021:         * if you aren't using an obsolete version: http://www.isqlviewer.com
022:         */
023:        package org.isqlviewer.swing;
024:
025:        import java.awt.Color;
026:        import java.awt.Component;
027:        import java.awt.Dimension;
028:        import java.awt.Font;
029:        import java.awt.FontMetrics;
030:        import java.awt.Graphics2D;
031:        import java.awt.GraphicsConfiguration;
032:        import java.awt.Point;
033:        import java.awt.Toolkit;
034:        import java.awt.datatransfer.Clipboard;
035:        import java.awt.datatransfer.StringSelection;
036:        import java.awt.datatransfer.Transferable;
037:        import java.awt.dnd.DnDConstants;
038:        import java.awt.dnd.DragGestureEvent;
039:        import java.awt.dnd.DragGestureListener;
040:        import java.awt.dnd.DragGestureRecognizer;
041:        import java.awt.dnd.DragSource;
042:        import java.awt.dnd.DragSourceDragEvent;
043:        import java.awt.dnd.DragSourceDropEvent;
044:        import java.awt.dnd.DragSourceEvent;
045:        import java.awt.dnd.DragSourceListener;
046:        import java.awt.event.ActionEvent;
047:        import java.awt.event.ActionListener;
048:        import java.awt.event.InputEvent;
049:        import java.awt.event.KeyEvent;
050:        import java.awt.event.MouseEvent;
051:        import java.awt.image.BufferedImage;
052:        import java.util.Date;
053:        import java.util.Hashtable;
054:
055:        import javax.swing.AbstractAction;
056:        import javax.swing.ActionMap;
057:        import javax.swing.InputMap;
058:        import javax.swing.JComponent;
059:        import javax.swing.JPopupMenu;
060:        import javax.swing.JTable;
061:        import javax.swing.KeyStroke;
062:        import javax.swing.SwingUtilities;
063:        import javax.swing.UIDefaults;
064:        import javax.swing.event.MouseInputAdapter;
065:        import javax.swing.event.TableColumnModelEvent;
066:        import javax.swing.event.TableModelEvent;
067:        import javax.swing.table.JTableHeader;
068:        import javax.swing.table.TableCellRenderer;
069:        import javax.swing.table.TableColumn;
070:        import javax.swing.table.TableColumnModel;
071:        import javax.swing.table.TableModel;
072:
073:        import org.isqlviewer.swing.table.EnhancedTableModel;
074:        import org.isqlviewer.swing.table.Sortable;
075:        import org.isqlviewer.ui.laf.EnhancedTableCellRenderer;
076:        import org.isqlviewer.ui.laf.SortableHeaderRenderer;
077:
078:        /**
079:         * TODO Add EnhancedTable Object overview JavaDoc information.
080:         * <p>
081:         * 
082:         * @author Mark A. Kobold
083:         * @version 1.0
084:         */
085:        public class EnhancedTable extends JTable {
086:
087:            private static final long serialVersionUID = -9002910430801390897L;
088:            public static final int INPUT_EVENT_DOUBLE_CLICK = 0x01FF;
089:            public static final int INPUT_EVENT_COMMAND_CLICK = 0x02FF;
090:            public static final int INPUT_EVENT_HEADER_CLICK = 0x03FF;
091:            public static final String CLIENT_PRINTING = "isPrinting";
092:            public static final String CLIENT_JPOPUP_MENU = "EnhancedTable.PopupMenu";
093:            private static DragSource dndDragSource = DragSource
094:                    .getDefaultDragSource();
095:            protected SortableHeaderRenderer header = new SortableHeaderRenderer(
096:                    this );
097:            protected MouseInputAdapter mouseInputHandler = null;
098:            protected int defaultSortedColumn = -1;
099:            protected boolean selectionToggle = true;
100:            protected DnDSupport dndHandler = new DnDSupport(this );
101:            protected DragGestureRecognizer dndRecognizer = null;
102:
103:            public EnhancedTable(TableModel model) {
104:
105:                this ();
106:                setModel(model);
107:            }
108:
109:            public EnhancedTable() {
110:
111:                super (0, 0);
112:                super .setDragEnabled(false);
113:
114:                mouseInputHandler = new TableMouseAdapter(this );
115:                setRowSelectionAllowed(true);
116:                addMouseListener(mouseInputHandler);
117:                addMouseMotionListener(mouseInputHandler);
118:                getTableHeader().setDefaultRenderer(header);
119:                getTableHeader().addMouseListener(mouseInputHandler);
120:
121:                setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
122:                InputMap im = getInputMap();
123:                ActionMap am = getActionMap();
124:                KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_C,
125:                        org.isqlviewer.swing.SwingUtilities.MENU_SHORTCUT_MASK);
126:                im.put(ks, "CELL.COPY");
127:                am.put("CELL.COPY", new CellCopyAction(this ));
128:                setGridColor(EnhancedTableCellRenderer.gridColor);
129:            }
130:
131:            @Override
132:            protected void createDefaultRenderers() {
133:
134:                defaultRenderersByColumnClass = new UIDefaults();
135:
136:                // Numbers
137:                setLazyRenderer(Number.class,
138:                        "org.isqlviewer.ui.laf.EnhancedNumberCellRenderer");
139:                // Doubles and Floats
140:                setLazyRenderer(Float.class,
141:                        "org.isqlviewer.ui.laf.EnhancedDecimalCellRenderer");
142:                setLazyRenderer(Double.class,
143:                        "org.isqlviewer.ui.laf.EnhancedDecimalCellRendererr");
144:                // Dates
145:                setLazyRenderer(Date.class,
146:                        "org.isqlviewer.ui.laf.EnhancedDateCellRenderer");
147:                // Booleans
148:                setLazyRenderer(Boolean.class,
149:                        "org.isqlviewer.ui.laf.EnhancedBooleanCellRenderer");
150:                // Everything else
151:                setLazyRenderer(Object.class,
152:                        "org.isqlviewer.ui.laf.EnhancedTableCellRenderer");
153:            }
154:
155:            public void refreshSorted() {
156:
157:                Sortable mdl = null;
158:                if (getModel() instanceof  Sortable) {
159:                    mdl = (Sortable) getModel();
160:                }
161:                if (mdl == null)
162:                    return;
163:                int column = header.getSortedColumn();
164:                if (column == -1)
165:                    return;
166:                boolean direct = header.isAscending();
167:                if (mdl.canSort(column, direct)) {
168:                    mdl.sort(column, direct);
169:                }
170:            }
171:
172:            public EnhancedTableModel getSelectedSubModel() {
173:
174:                int[] rows = null;
175:                if (selectionToggle) {
176:                    if (getRowSelectionAllowed()) {
177:                        rows = getSelectedRows();
178:                    } else {
179:                        rows = createIterativeArray(getRowCount());
180:                    }
181:                } else {
182:                    rows = getSelectedRows();
183:                }
184:
185:                int[] columns = null;
186:                if (selectionToggle) {
187:                    if (getColumnSelectionAllowed()) {
188:                        columns = getSelectedColumns();
189:                    } else {
190:                        columns = createIterativeArray(getColumnCount());
191:                    }
192:                } else {
193:                    columns = getSelectedRows();
194:                }
195:
196:                return ((EnhancedTableModel) getModel())
197:                        .subModel(rows, columns);
198:            }
199:
200:            public void setSelectionToggleEnabled(boolean f) {
201:
202:                selectionToggle = f;
203:            }
204:
205:            public void addActionListener(ActionListener al) {
206:
207:                listenerList.add(ActionListener.class, al);
208:            }
209:
210:            public void removeActionListener(ActionListener al) {
211:
212:                listenerList.remove(ActionListener.class, al);
213:            }
214:
215:            public void allowColumnReordering(boolean f) {
216:
217:                getTableHeader().setReorderingAllowed(f);
218:            }
219:
220:            public void allowColumnResizing(boolean f) {
221:
222:                getTableHeader().setResizingAllowed(f);
223:            }
224:
225:            protected void fireActionEvent(ActionEvent e) {
226:
227:                ActionListener[] lst = listenerList
228:                        .getListeners(ActionListener.class);
229:                for (int i = 0; i < lst.length; i++) {
230:                    try {
231:                        lst[i].actionPerformed(e);
232:                    } catch (Throwable t) {
233:                    }
234:                }
235:            }
236:
237:            protected void paintRow(Graphics2D g2, int[] columns, int row,
238:                    boolean selected) {
239:
240:                int isw = getIntercellSpacing().width
241:                        + (4 * SortableHeaderRenderer.SORT_ICON_WIDTH);
242:                int x = 0;
243:                for (int i = 0; i < columns.length; i++) {
244:                    TableCellRenderer tcr = getCellRenderer(row, columns[i]);
245:                    Object obj = getValueAt(row, columns[i]);
246:                    Component view = tcr.getTableCellRendererComponent(this ,
247:                            obj, selected, false, row, columns[i]);
248:                    TableColumn column = columnModel.getColumn(columns[i]);
249:                    int headerWidth = column.getWidth() + isw;
250:                    Dimension dim = new Dimension(headerWidth, view
251:                            .getPreferredSize().height);
252:                    view.setSize(dim);
253:                    view.setLocation(0, 0);
254:                    view.paint(g2);
255:                    x += dim.width;
256:                    g2.translate(dim.width, 0);
257:                    if (x >= g2.getClipBounds().width) {
258:                        break;
259:                    }
260:                }
261:            }
262:
263:            @Override
264:            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
265:                    int condition, boolean pressed) {
266:
267:                int lrow = getSelectedRow();
268:                boolean f = super .processKeyBinding(ks, e, condition, pressed);
269:                if (getModel() instanceof  EnhancedTableModel) {
270:                    EnhancedTableModel mdl = (EnhancedTableModel) getModel();
271:                    if (mdl == null) {
272:                        return f;
273:                    }
274:                    switch (e.getKeyCode()) {
275:                    case KeyEvent.VK_DOWN:
276:                    case KeyEvent.VK_PAGE_DOWN:
277:                        if (lrow == getRowCount() - 1) {
278:                            if (!ks.isOnKeyRelease() && mdl.pageDown()) {
279:                                setRowSelectionInterval(0, 0);
280:                                scrollRectToVisible(getCellRect(0, 0, true));
281:                            }
282:                        }
283:                        break;
284:                    case KeyEvent.VK_UP:
285:                    case KeyEvent.VK_PAGE_UP:
286:                        if (lrow == 0) {
287:                            if (!ks.isOnKeyRelease() && mdl.pageUp()) {
288:                                setRowSelectionInterval(getRowCount() - 1,
289:                                        getRowCount() - 1);
290:                                scrollRectToVisible(getCellRect(0, 0, true));
291:                            }
292:                        }
293:                        break;
294:                    }
295:                }
296:                return f;
297:            }
298:
299:            @Override
300:            public void setFont(Font fnt) {
301:
302:                super .setFont(fnt);
303:                FontMetrics fm = getFontMetrics(fnt);
304:                setRowHeight(fm.getHeight());
305:            }
306:
307:            @Override
308:            public void setDragEnabled(boolean f) {
309:
310:                if (f) {
311:                    if (dndRecognizer == null) {
312:                        dndRecognizer = dndDragSource
313:                                .createDefaultDragGestureRecognizer(this ,
314:                                        DnDConstants.ACTION_COPY_OR_MOVE,
315:                                        dndHandler);
316:                    }
317:                } else {
318:                    if (dndRecognizer != null) {
319:                        dndRecognizer.resetRecognizer();
320:                        dndRecognizer.removeDragGestureListener(dndHandler);
321:                        dndRecognizer.setComponent(null);
322:                        dndRecognizer = null;
323:                    }
324:                }
325:            }
326:
327:            @Override
328:            public boolean getDragEnabled() {
329:
330:                return (dndRecognizer == null);
331:            }
332:
333:            @Override
334:            public synchronized void setModel(TableModel mdl) {
335:
336:                super .setModel(mdl);
337:                synchronized (this ) {
338:                    if (mdl instanceof  Sortable) {
339:                        refreshSorted();
340:                    }
341:                }
342:            }
343:
344:            @Override
345:            public void tableChanged(TableModelEvent event) {
346:
347:                super .tableChanged(event);
348:
349:                if (event.getFirstRow() == TableModelEvent.HEADER_ROW) {
350:                    setColumnSizes(0, getColumnCount());
351:                    try {
352:                        boolean allowed = getTableHeader()
353:                                .getReorderingAllowed();
354:                        allowColumnReordering(allowed
355:                                && (getColumnCount() >= 2));
356:                    } catch (Throwable t) {
357:                    }
358:                }
359:            }
360:
361:            @Override
362:            public void columnAdded(TableColumnModelEvent event) {
363:
364:                if (getModel() != null) {
365:                    setColumnSizes(event.getToIndex(), event.getToIndex());
366:                }
367:                super .columnAdded(event);
368:            }
369:
370:            /** Sizes the columns with the max size of the header and the columns data. */
371:            private void setColumnSizes(int from, int to) {
372:
373:                TableColumnModel cmodel = getColumnModel();
374:                int columnCount = cmodel.getColumnCount();
375:                int fromIndex = from;
376:                int toIndex = to;
377:                if (fromIndex < 0) {
378:                    fromIndex = 0;
379:                }
380:                if (toIndex >= columnCount) {
381:                    toIndex = columnCount - 1;
382:                }
383:                if (fromIndex > toIndex)
384:                    return;
385:                TableModel model = getModel();
386:                int rowCount = getRowCount();
387:                TableCellRenderer headerRenderer = getTableHeader()
388:                        .getDefaultRenderer();
389:                int isw = getIntercellSpacing().width
390:                        + (4 * SortableHeaderRenderer.SORT_ICON_WIDTH);
391:                for (int viewCol = fromIndex; viewCol <= toIndex; viewCol++) {
392:                    TableColumn column = cmodel.getColumn(viewCol);
393:                    int headerWidth = getHeaderWidth(headerRenderer, column)
394:                            + isw;
395:                    int cellWidth = (rowCount == 0) ? 0 : getDataWidth(model,
396:                            column, viewCol, rowCount);
397:                    column.setPreferredWidth(Math.max(headerWidth, cellWidth));
398:                    if (getColumnClass(viewCol) == Boolean.class
399:                            && getRowCount() >= 1) {
400:                        TableCellRenderer rend = getCellRenderer(0, viewCol);
401:                        Object o = getValueAt(0, viewCol);
402:                        Component comp = rend.getTableCellRendererComponent(
403:                                this , o, false, false, 0, viewCol);
404:                        setRowHeight(comp.getPreferredSize().height);
405:                    }
406:                }
407:            }
408:
409:            private void setLazyValue(Hashtable h, Class c, String s) {
410:
411:                h.put(c, new UIDefaults.ProxyLazyValue(s));
412:            }
413:
414:            private void setLazyRenderer(Class c, String s) {
415:
416:                setLazyValue(defaultRenderersByColumnClass, c, s);
417:            }
418:
419:            private int getHeaderWidth(TableCellRenderer headerRenderer,
420:                    TableColumn column) {
421:
422:                Component comp = headerRenderer.getTableCellRendererComponent(
423:                        this , column.getHeaderValue(), false, false, 0, 0);
424:                return comp.getPreferredSize().width;
425:            }
426:
427:            private int getDataWidth(TableModel model, TableColumn column,
428:                    int col, int rowCount) {
429:
430:                Component comp;
431:                int modelCol = convertColumnIndexToModel(col);
432:                TableCellRenderer renderer = column.getCellRenderer();
433:                if (renderer == null) {
434:                    renderer = getDefaultRenderer(model
435:                            .getColumnClass(modelCol));
436:                    column.setCellRenderer(renderer);
437:                }
438:                if (renderer == null) {
439:                    return 0;
440:                }
441:
442:                Boolean b = (Boolean) getClientProperty(EnhancedTable.CLIENT_PRINTING);
443:                b = (b == null ? Boolean.FALSE : b);
444:                int maxWidth = 0;
445:                for (int row = 0; row < rowCount; ++row) {
446:                    Object v = model.getValueAt(row, modelCol);
447:                    comp = renderer.getTableCellRendererComponent(this , v,
448:                            false, false, row, col);
449:                    if (b.booleanValue()) {
450:                        int current = getRowHeight(row);
451:                        int preferred = comp.getPreferredSize().height;
452:                        preferred = Math.max(preferred, current);
453:                        setRowHeight(row, preferred);
454:                    }
455:                    int this CellWidth = SortableHeaderRenderer.SORT_ICON_WIDTH;
456:                    this CellWidth += (comp.getPreferredSize().width + (2 * getIntercellSpacing().width));
457:                    maxWidth = Math.max(maxWidth, this CellWidth);
458:                }
459:                return maxWidth;
460:            }
461:
462:            private static int[] createIterativeArray(int size) {
463:
464:                int[] result = new int[size];
465:                for (int i = 0; i < size; i++) {
466:                    result[i] = i;
467:                }
468:                return result;
469:            }
470:
471:            /**
472:             * Creates a tab delimited text for selected cells of the given JTable.
473:             * <p>
474:             * 
475:             * @param table to copy selection from.
476:             */
477:            private static void copySelectedCellsToClipBoard(JTable table) {
478:
479:                if (table != null) {
480:                    try {
481:                        table.getCellEditor().cancelCellEditing();
482:                    } catch (Throwable t) {
483:
484:                    }
485:                    StringBuffer buff = new StringBuffer("");
486:                    StringBuffer row = new StringBuffer("");
487:                    for (int r = 0; r < table.getRowCount(); r++) {
488:                        for (int c = 0; c < table.getColumnCount(); c++) {
489:                            if (table.isCellSelected(r, c))
490:                                row.append(table.getValueAt(r, c) + "\t");
491:                        }
492:
493:                        if (row.toString().trim().length() >= 1) {
494:                            buff.append(row);
495:                            buff.append(System.getProperty("line.seperator",
496:                                    "\n"));
497:                        }
498:                        row.setLength(0);
499:                    }
500:
501:                    Clipboard cb = Toolkit.getDefaultToolkit()
502:                            .getSystemClipboard();
503:                    StringSelection ss = new StringSelection(buff.toString()
504:                            .trim());
505:                    cb.setContents(ss, ss);
506:                }
507:            }
508:
509:            private static class CellCopyAction extends AbstractAction {
510:
511:                private static final long serialVersionUID = 8845117056535654319L;
512:                private EnhancedTable reference = null;
513:
514:                public CellCopyAction(EnhancedTable table) {
515:
516:                    reference = table;
517:                }
518:
519:                public void actionPerformed(ActionEvent e) {
520:
521:                    if (reference != null) {
522:                        if (!reference.getSelectionModel().isSelectionEmpty()) {
523:                            copySelectedCellsToClipBoard(reference);
524:                        } else {
525:                            reference.getToolkit().beep();
526:                        }
527:                    } else {
528:                        reference.getToolkit().beep();
529:                    }
530:                }
531:            }
532:
533:            private static class TableMouseAdapter extends MouseInputAdapter {
534:
535:                private EnhancedTable table = null;
536:
537:                public TableMouseAdapter(EnhancedTable reference) {
538:
539:                    table = reference;
540:                }
541:
542:                @Override
543:                public void mousePressed(MouseEvent e) {
544:
545:                    Object src = e.getSource();
546:                    if (src instanceof  JComponent) {
547:                        JComponent c = (JComponent) src;
548:                        if (handlePopup(e, c)) {
549:                            return;
550:                        }
551:                    }
552:                    int mods = e.getModifiers();
553:                    int row = table.rowAtPoint(e.getPoint());
554:                    int col = table.columnAtPoint(e.getPoint());
555:                    boolean isShft = (mods & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK;
556:                    boolean isMeta = (mods & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK;
557:                    boolean isAlt = (mods & InputEvent.ALT_MASK) == InputEvent.ALT_MASK;
558:                    boolean isLeft = SwingUtilities.isLeftMouseButton(e);
559:                    boolean isEdit = table.getModel().isCellEditable(row, col);
560:                    int cc = e.getClickCount();
561:                    if (src == table) {
562:                        if (isLeft && cc == 2 && (!isShft && !isMeta)) {
563:                            if (!isEdit && table.selectionToggle) {
564:                                table.setRowSelectionAllowed(!(table
565:                                        .getRowSelectionAllowed()));
566:                                table.repaint();
567:                                ActionEvent evt = new ActionEvent(table,
568:                                        EnhancedTable.INPUT_EVENT_DOUBLE_CLICK,
569:                                        "");
570:                                table.fireActionEvent(evt);
571:                            }
572:                        } else if (cc == 1 && isAlt) {
573:                            if (row >= 0 && col >= 0) {
574:                                ActionEvent evt = new ActionEvent(
575:                                        src,
576:                                        EnhancedTable.INPUT_EVENT_COMMAND_CLICK,
577:                                        Integer.toString(col), row);
578:                                table.fireActionEvent(evt);
579:                            }
580:                        }
581:                        return;
582:                    } else if (table.getModel() instanceof  Sortable) {
583:                        int pcc = 2;
584:                        JTableHeader th = (JTableHeader) src;
585:                        if (th.getResizingColumn() != null) {
586:                            if (cc == 2) {
587:                                table.setColumnSizes(0, table.getColumnCount());
588:                            }
589:                            // click from resizing columns
590:                            return;
591:                        }
592:                        Sortable mdl = (Sortable) table.getModel();
593:                        int idx = th.columnAtPoint(e.getPoint());
594:                        if (cc == pcc && isLeft && idx >= 0 && mdl != null) {
595:                            int sci = table.header.getRenderedSortedColumn();
596:                            boolean asc = table.header.isAscending();
597:                            if (!mdl.canSort(idx, asc)) {
598:                                th.getToolkit().beep();
599:                                return;
600:                            }
601:                            if (sci != idx) {
602:                                table.header.setSortedColumn(idx, table, false);
603:                            } else {
604:                                table.header.setSortedColumn(sci, table, !asc);
605:                            }
606:                            asc = table.header.isAscending();
607:                            idx = table.header.getSortedColumn();
608:                            synchronized (mdl) {
609:                                mdl.sort(idx, asc);
610:                                th.repaint();
611:                            }
612:                        }
613:                        return;
614:                    }
615:                }
616:
617:                @Override
618:                public void mouseReleased(MouseEvent e) {
619:
620:                    Object src = e.getSource();
621:                    if (src instanceof  JComponent) {
622:                        JComponent c = (JComponent) src;
623:                        handlePopup(e, c);
624:                    }
625:                }
626:
627:                private boolean handlePopup(MouseEvent e, JComponent jc) {
628:
629:                    JPopupMenu popup = null;
630:                    if (e.isPopupTrigger()) {
631:                        try {
632:                            popup = (JPopupMenu) jc
633:                                    .getClientProperty(CLIENT_JPOPUP_MENU);
634:                        } catch (Throwable t) {
635:                            return false;
636:                        }
637:                        if (popup != null) {
638:                            int row = table.rowAtPoint(e.getPoint());
639:                            int[] selected = table.getSelectedRows();
640:                            boolean isSelected = false;
641:                            for (int i = 0; i < selected.length; i++) {
642:                                if (selected[i] == row) {
643:                                    isSelected = true;
644:                                    break;
645:                                }
646:                            }
647:                            if (!isSelected) {
648:                                table.setRowSelectionInterval(row, row);
649:                            }
650:                            popup.show(jc, e.getX(), e.getY());
651:                            e.consume();
652:                            return true;
653:                        }
654:
655:                        return false;
656:                    }
657:                    return false;
658:                }
659:            }
660:
661:            private static class DnDSupport implements  DragGestureListener,
662:                    DragSourceListener {
663:
664:                private static final Point pt = new Point(2, 2);
665:                private static final int MAX_ROWS = 7;
666:                private EnhancedTable table = null;
667:
668:                public void dragDropEnd(DragSourceDropEvent dsde) {
669:
670:                }
671:
672:                public void dragEnter(DragSourceDragEvent dsde) {
673:
674:                }
675:
676:                public void dragExit(DragSourceEvent dse) {
677:
678:                }
679:
680:                public void dragOver(DragSourceDragEvent dsde) {
681:
682:                }
683:
684:                public void dropActionChanged(DragSourceDragEvent dsde) {
685:
686:                }
687:
688:                public DnDSupport(EnhancedTable reference) {
689:
690:                    table = reference;
691:                }
692:
693:                public void dragGestureRecognized(DragGestureEvent event) {
694:
695:                    DragGestureRecognizer dndRecognizer = event
696:                            .getSourceAsDragGestureRecognizer();
697:                    try {
698:                        EnhancedTableModel selection = table
699:                                .getSelectedSubModel();
700:                        if (selection.isEmpty()) {
701:                            return;
702:                        }
703:                        int[] rows = null;
704:                        if (table.selectionToggle) {
705:                            if (table.getRowSelectionAllowed()) {
706:                                rows = table.getSelectedRows();
707:                            } else {
708:                                rows = createIterativeArray(table.getRowCount());
709:                            }
710:                        } else {
711:                            rows = table.getSelectedRows();
712:                        }
713:
714:                        int[] columns = null;
715:                        if (table.selectionToggle) {
716:                            if (table.getColumnSelectionAllowed()) {
717:                                columns = table.getSelectedColumns();
718:                            } else {
719:                                columns = createIterativeArray(table
720:                                        .getColumnCount());
721:                            }
722:                        } else {
723:                            columns = table.getSelectedRows();
724:                        }
725:
726:                        Transferable data = selection;
727:                        GraphicsConfiguration gConfig = table
728:                                .getGraphicsConfiguration();
729:                        Dimension dim = table.getVisibleRect().getSize();
730:                        int height = 0;
731:                        int min = Math.min(MAX_ROWS, rows.length);
732:                        for (int i = 0; i < min; i++) {
733:                            height += table.getRowHeight(rows[i]);
734:                        }
735:                        int width = 0;
736:                        for (int i = 0; i < columns.length; i++) {
737:                            width += table.getColumnModel().getColumn(
738:                                    columns[i]).getPreferredWidth();
739:                        }
740:                        dim.setSize(Math.min(dim.width, width), height);
741:                        BufferedImage bi = gConfig.createCompatibleImage(
742:                                dim.width, height);
743:                        int x = 0;
744:                        int y = 0;
745:                        Graphics2D g2 = bi.createGraphics();
746:                        g2.setColor(Color.WHITE);
747:                        g2.fillRect(0, 0, dim.width, dim.height);
748:                        for (int i = 0; i < rows.length; i++) {
749:                            int rowHeight = table.getRowHeight(rows[i]);
750:                            Graphics2D subg = (Graphics2D) g2.create(x, y,
751:                                    dim.width, rowHeight);
752:                            table.paintRow(subg, columns, rows[i], true);
753:                            y += rowHeight;
754:                        }
755:                        g2.setColor(new Color(225, 225, 225, 128));
756:                        g2.fillRect(0, 0, dim.width, dim.height);
757:                        g2.setColor(Color.BLACK);
758:                        g2.drawRect(0, 0, dim.width - 1, height - 1);
759:                        dndDragSource
760:                                .startDrag(event, null, bi, pt, data, this );
761:                    } catch (Throwable t) {
762:                        dndRecognizer.resetRecognizer();
763:                        table.getToolkit().beep();
764:                    }
765:                }
766:            }
767:
768:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.