Source Code Cross Referenced for ExcelAdapter.java in  » Database-Client » SQLMinus » isql » 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 » SQLMinus » isql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package isql;
002:
003:        import java.awt.*;
004:        import java.awt.event.*;
005:        import javax.swing.*;
006:        import java.awt.datatransfer.*;
007:        import java.util.*;
008:
009:        /**
010:         * ExcelAdapter enables Copy-Paste Clipboard functionality on JTables.
011:         * The clipboard data format used by the adapter is compatible with
012:         * the clipboard format used by Excel. This provides for clipboard
013:         * interoperability between enabled JTables and Excel.
014:         * RK: Taken from javaworld.
015:         */
016:        public class ExcelAdapter implements  ActionListener {
017:            private String rowstring, value;
018:            private Clipboard system;
019:            private StringSelection stsel;
020:            private JTable jTable1;
021:
022:            /**
023:             * The Excel Adapter is constructed with a
024:             * JTable on which it enables Copy-Paste and acts
025:             * as a Clipboard listener.
026:             */
027:            public ExcelAdapter(JTable myJTable) {
028:                jTable1 = myJTable;
029:                KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C,
030:                        ActionEvent.CTRL_MASK, false);
031:                // Identifying the copy KeyStroke user can modify this
032:                // to copy on some other Key combination.
033:                KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,
034:                        ActionEvent.CTRL_MASK, false);
035:                // Identifying the Paste KeyStroke user can modify this
036:                //to copy on some other Key combination.
037:                jTable1.registerKeyboardAction(this , "Copy", copy,
038:                        JComponent.WHEN_FOCUSED);
039:                jTable1.registerKeyboardAction(this , "Paste", paste,
040:                        JComponent.WHEN_FOCUSED);
041:                system = Toolkit.getDefaultToolkit().getSystemClipboard();
042:            }
043:
044:            /**
045:             * Public Accessor methods for the Table on which this adapter acts.
046:             */
047:            public JTable getJTable() {
048:                return jTable1;
049:            }
050:
051:            public void setJTable(JTable jTable1) {
052:                this .jTable1 = jTable1;
053:            }
054:
055:            /**
056:             * This method is activated on the Keystrokes we are listening to
057:             * in this implementation. Here it listens for Copy and Paste ActionCommands.
058:             * Selections comprising non-adjacent cells result in invalid selection and
059:             * then copy action cannot be performed.
060:             * Paste is done by aligning the upper left corner of the selection with the
061:             * 1st element in the current selection of the JTable.
062:             */
063:            public void actionPerformed(ActionEvent e) {
064:                if (e.getActionCommand().compareTo("Copy") == 0) {
065:                    StringBuffer sbf = new StringBuffer();
066:                    // Check to ensure we have selected only a contiguous block of
067:                    // cells
068:                    int numcols = jTable1.getSelectedColumnCount();
069:                    int numrows = jTable1.getSelectedRowCount();
070:                    int[] rowsselected = jTable1.getSelectedRows();
071:                    int[] colsselected = jTable1.getSelectedColumns();
072:                    if (!((numrows - 1 == rowsselected[rowsselected.length - 1]
073:                            - rowsselected[0] && numrows == rowsselected.length) && (numcols - 1 == colsselected[colsselected.length - 1]
074:                            - colsselected[0] && numcols == colsselected.length))) {
075:                        JOptionPane.showMessageDialog(null,
076:                                "Invalid Copy Selection",
077:                                "Invalid Copy Selection",
078:                                JOptionPane.ERROR_MESSAGE);
079:                        return;
080:                    }
081:                    for (int i = 0; i < numrows; i++) {
082:                        for (int j = 0; j < numcols; j++) {
083:                            sbf.append(jTable1.getValueAt(rowsselected[i],
084:                                    colsselected[j]));
085:                            if (j < numcols - 1)
086:                                sbf.append("\t");
087:                        }
088:                        sbf.append("\n");
089:                    }
090:                    stsel = new StringSelection(sbf.toString());
091:                    system = Toolkit.getDefaultToolkit().getSystemClipboard();
092:                    system.setContents(stsel, stsel);
093:                }
094:                if (e.getActionCommand().compareTo("Paste") == 0) {
095:                    System.out.println("Trying to Paste");
096:                    int startRow = (jTable1.getSelectedRows())[0];
097:                    int startCol = (jTable1.getSelectedColumns())[0];
098:                    try {
099:                        String trstring = (String) (system.getContents(this )
100:                                .getTransferData(DataFlavor.stringFlavor));
101:                        System.out.println("String is:" + trstring);
102:                        StringTokenizer st1 = new StringTokenizer(trstring,
103:                                "\n");
104:                        for (int i = 0; st1.hasMoreTokens(); i++) {
105:                            rowstring = st1.nextToken();
106:                            StringTokenizer st2 = new StringTokenizer(
107:                                    rowstring, "\t");
108:                            for (int j = 0; st2.hasMoreTokens(); j++) {
109:                                value = (String) st2.nextToken();
110:                                if (startRow + i < jTable1.getRowCount()
111:                                        && startCol + j < jTable1
112:                                                .getColumnCount())
113:                                    jTable1.setValueAt(value, startRow + i,
114:                                            startCol + j);
115:                                System.out.println("Putting " + value
116:                                        + "at row=" + startRow + i + "column="
117:                                        + startCol + j);
118:                            }
119:                        }
120:                    } catch (Exception ex) {
121:                        ex.printStackTrace();
122:                    }
123:                }
124:            }
125:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.