Source Code Cross Referenced for Table.java in  » Database-Client » DBBrowser » org » dbbrowser » ui » widget » 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 » DBBrowser » org.dbbrowser.ui.widget 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.dbbrowser.ui.widget;
002:
003:        import infrastructure.internationalization.InternationalizationManager;
004:        import infrastructure.propertymanager.PropertyManager;
005:        import java.awt.Component;
006:        import java.awt.FontMetrics;
007:        import java.awt.Toolkit;
008:        import java.awt.datatransfer.StringSelection;
009:        import java.awt.event.ActionEvent;
010:        import java.awt.event.ActionListener;
011:        import java.awt.event.MouseAdapter;
012:        import java.awt.event.MouseEvent;
013:        import java.awt.event.MouseListener;
014:        import javax.swing.*;
015:        import javax.swing.table.AbstractTableModel;
016:        import javax.swing.table.TableColumn;
017:        import org.dbbrowser.ui.*;
018:        import org.dbbrowser.ui.helper.DBTableDataTableModel;
019:        import org.dbbrowser.ui.helper.exporthelper.wizard.DataExporterUsingWizard;
020:        import org.dbbrowser.util.TableSorter;
021:        import org.dbbrowser.help.HelpManager;
022:
023:        /**
024:         * A table which can be sorted by clicking on the row header
025:         */
026:        public class Table extends JPanel implements  ActionListener {
027:            private static final long serialVersionUID = UIControllerForQueries.version;
028:
029:            private static final String POP_UP_MENU_COPY_MENU_ITEM_LABEL = InternationalizationManager
030:                    .getInstance()
031:                    .getMessage(
032:                            "dbbrowser-ui",
033:                            "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-copy-menu-item-label",
034:                            null);;
035:            private static final String POP_UP_MENU_EXPORT_MENU_ITEM_LABEL = InternationalizationManager
036:                    .getInstance()
037:                    .getMessage(
038:                            "dbbrowser-ui",
039:                            "dbbrowser-ui-dbbrowser-window-tools-menu-copy-label",
040:                            null);;
041:            private static final String POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL = InternationalizationManager
042:                    .getInstance()
043:                    .getMessage(
044:                            "dbbrowser-ui",
045:                            "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-whats-this-menu-item-label",
046:                            null);;
047:            private static final String POP_UP_MENU_HELP_MENU_ITEM_LABEL = InternationalizationManager
048:                    .getInstance()
049:                    .getMessage(
050:                            "dbbrowser-ui",
051:                            "dbbrowser-ui-dbbrowser-browser-tab-tabledata-popup-menu-help-menu-item-label",
052:                            null);;
053:
054:            private static final String COPY_POPUP_ITEM_ICON_FILENAME = PropertyManager
055:                    .getInstance().getProperty(
056:                            "dbbrowser-popup-menu-copy-icon-filename");
057:            private static final String EXPORT_POPUP_ITEM_ICON_FILENAME = PropertyManager
058:                    .getInstance()
059:                    .getProperty(
060:                            "dbbrowser-ui-dbbrowser-window-toolbar-export-icon");
061:            private static final String WHATS_THIS_POPUP_ITEM_ICON_FILENAME = PropertyManager
062:                    .getInstance().getProperty(
063:                            "dbbrowser-popup-menu-whats-this-icon-filename");
064:            private static final String HELP_POPUP_ITEM_ICON_FILENAME = PropertyManager
065:                    .getInstance().getProperty(
066:                            "dbbrowser-ui-dbbrowser-window-toolbar-help-icon");
067:
068:            private UIControllerForQueries uiControllerForQueries = null;
069:            private UIControllerForUpdates uiControllerForUpdates = null;
070:            private AbstractTableModel abstractTableModel = null;
071:
072:            //Widgets in the panel
073:            private JScrollPane paneForTable = null;
074:            private TableSorter sorter = null;
075:            private JTable tableForTableData = null;
076:
077:            //Popup menu
078:            private JPopupMenu popupMenuForResultsTable = null;
079:
080:            /**
081:             * Constructer
082:             * @param uiControllerForQueries
083:             * @param uiControllerForUpdates
084:             */
085:            public Table(UIControllerForQueries uiControllerForQueries,
086:                    UIControllerForUpdates uiControllerForUpdates) {
087:                this .uiControllerForQueries = uiControllerForQueries;
088:                this .uiControllerForUpdates = uiControllerForUpdates;
089:                this .setLayout(new BoxLayout(this , BoxLayout.PAGE_AXIS));
090:            }
091:
092:            /**
093:             * Returns the selected row
094:             * @return
095:             */
096:            public Integer getSelectedRow() {
097:                int selectedRow = this .tableForTableData.getSelectedRow();
098:                Integer returnValue = null;
099:                if (selectedRow >= 0) {
100:                    int modelIndex = sorter.modelIndex(selectedRow);
101:                    returnValue = new Integer(modelIndex);
102:                }
103:                return returnValue;
104:            }
105:
106:            /**
107:             * Update the table
108:             */
109:            public void update() {
110:                this .abstractTableModel.fireTableDataChanged();
111:                this .tableForTableData.updateUI();
112:            }
113:
114:            /**
115:             * Set up table for display
116:             * @param abstractTableModel
117:             */
118:            public void initializeTable(AbstractTableModel abstractTableModel) {
119:                this .abstractTableModel = abstractTableModel;
120:
121:                //If the panel already had a table(scrollpane), remove it
122:                if (this .tableForTableData != null) {
123:                    this .remove(this .paneForTable);
124:                    this .paneForTable = null;
125:                    this .tableForTableData = null;
126:                }
127:
128:                //Wrap the table model around a sorter
129:                sorter = new TableSorter(this .abstractTableModel);
130:
131:                //Build a new JTable
132:                this .tableForTableData = new JTable(sorter);
133:                sorter.setTableHeader(this .tableForTableData.getTableHeader());
134:
135:                //Add listener for Double clicks
136:                this .tableForTableData
137:                        .addMouseListener(new TableMouseListener());
138:
139:                //Allow cell selection
140:                this .tableForTableData.setCellSelectionEnabled(true);
141:
142:                //Other values
143:                this .tableForTableData
144:                        .setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //if we dont set this, scrollbar is never shown
145:
146:                //Set the min width of each column so it as wide as it needs to be
147:                //Set the table header renderer for the columns which contain the primary key
148:                FontMetrics fontMetrics = this .getGraphics().getFontMetrics();
149:
150:                for (int i = 0; i < this .abstractTableModel.getColumnCount(); i++) {
151:                    TableColumn tc = this .tableForTableData.getColumnModel()
152:                            .getColumn(i);
153:
154:                    //Set the width of first column
155:                    if (i == 0) {
156:                        tc.setPreferredWidth(80);
157:                    } else {
158:                        //Set the width of the subsequent columns to be little more than width of the column header
159:                        String columnName = this .abstractTableModel
160:                                .getColumnName(i);
161:                        int columnWidth = fontMetrics.stringWidth(columnName) + 60;
162:                        tc.setPreferredWidth(columnWidth);
163:                    }
164:                }
165:
166:                //Set the renderer for the first column
167:                //TableColumn firstColumn = this.tableForTableData.getColumnModel().getColumn(0);
168:                //firstColumn.setCellRenderer(new RowHeaderCellRenderer());
169:
170:                //Wrap the table in a scrollpane and add the scrollpane to the panel
171:                this .paneForTable = new JScrollPane(this .tableForTableData);
172:                this .add(this .paneForTable);
173:
174:                //Add the popup menu
175:                popupMenuForResultsTable = new JPopupMenu();
176:                JMenuItem copyMenuItem = new JMenuItem(
177:                        POP_UP_MENU_COPY_MENU_ITEM_LABEL, new ImageIcon(
178:                                COPY_POPUP_ITEM_ICON_FILENAME));
179:                copyMenuItem.addActionListener(this );
180:                popupMenuForResultsTable.add(copyMenuItem);
181:                JMenuItem exportMenuItem = new JMenuItem(
182:                        POP_UP_MENU_EXPORT_MENU_ITEM_LABEL, new ImageIcon(
183:                                EXPORT_POPUP_ITEM_ICON_FILENAME));
184:                exportMenuItem.addActionListener(this );
185:                popupMenuForResultsTable.add(exportMenuItem);
186:                popupMenuForResultsTable.addSeparator();
187:                JMenuItem whatsThisMenuItem = new JMenuItem(
188:                        POP_UP_MENU_WHATS_THIS_MENU_ITEM_LABEL, new ImageIcon(
189:                                WHATS_THIS_POPUP_ITEM_ICON_FILENAME));
190:                HelpManager.getInstance().registerCSH(whatsThisMenuItem, this ,
191:                        "data_browser");
192:                popupMenuForResultsTable.add(whatsThisMenuItem);
193:                JMenuItem helpMenuItem = new JMenuItem(
194:                        POP_UP_MENU_HELP_MENU_ITEM_LABEL, new ImageIcon(
195:                                HELP_POPUP_ITEM_ICON_FILENAME));
196:                helpMenuItem.addActionListener(HelpManager.getInstance()
197:                        .getActionListenerForHelpEvents());
198:                popupMenuForResultsTable.add(helpMenuItem);
199:
200:                //Add listener to components that can bring up popup menus.
201:                MouseListener popupListenerForResultsTable = new BasicPopupListener(
202:                        this .popupMenuForResultsTable);
203:                this .tableForTableData
204:                        .addMouseListener(popupListenerForResultsTable);
205:
206:                //Update the table
207:                this .abstractTableModel.fireTableDataChanged();
208:                this .tableForTableData.doLayout();
209:                this .updateUI();
210:            }
211:
212:            public void actionPerformed(ActionEvent e) {
213:                //If the user wants to copy something
214:                if (POP_UP_MENU_COPY_MENU_ITEM_LABEL.equals(e
215:                        .getActionCommand())) {
216:                    int[] selectedRows = this .tableForTableData
217:                            .getSelectedRows();
218:                    int[] selectedColumns = this .tableForTableData
219:                            .getSelectedColumns();
220:
221:                    StringBuffer buffer = new StringBuffer();
222:                    for (int i = 0; i < selectedRows.length; i++) {
223:                        for (int j = 0; j < selectedColumns.length; j++) {
224:                            Object selectedCell = this .tableForTableData
225:                                    .getValueAt(selectedRows[i],
226:                                            selectedColumns[j]);
227:                            String selectedValue = selectedCell.toString();
228:
229:                            if (j == selectedColumns.length - 1) {
230:                                buffer.append(selectedValue);
231:                            } else {
232:                                buffer.append(selectedValue + "\t");
233:                            }
234:                        }
235:
236:                        if (i != selectedRows.length - 1) {
237:                            buffer.append("\n");
238:                        }
239:                    }
240:
241:                    //Copy the contents to the system clipboard
242:                    StringSelection ss = new StringSelection(buffer.toString());
243:                    Toolkit.getDefaultToolkit().getSystemClipboard()
244:                            .setContents(ss, ss);
245:                }
246:
247:                //If the user wants to copy something
248:                if (POP_UP_MENU_EXPORT_MENU_ITEM_LABEL.equals(e
249:                        .getActionCommand())) {
250:                    DataExporterUsingWizard dataExporterUsingWizard = new DataExporterUsingWizard();
251:                    dataExporterUsingWizard.export(this .abstractTableModel);
252:                }
253:            }
254:
255:            /**
256:             * Listener for double clicks
257:             * @author amangat
258:             */
259:            private class TableMouseListener extends MouseAdapter {
260:                public void mouseClicked(MouseEvent evt) {
261:                    if (evt.getClickCount() == 2) {
262:                        Component sourceComponent = evt.getComponent();
263:
264:                        //if the source is a jtabel, get the selected row
265:                        if (sourceComponent instanceof  JTable) {
266:                            JTable aTable = (JTable) sourceComponent;
267:                            int selectedRow = aTable.getSelectedRow();
268:
269:                            //if the source is a row in the table
270:                            if (selectedRow != -1) {
271:                                //if there are any rows to display
272:                                if (abstractTableModel.getRowCount() != 0) {
273:                                    if (abstractTableModel instanceof  DBTableDataTableModel) {
274:                                        DBTableDataTableModel dbTableDataTableModel = (DBTableDataTableModel) abstractTableModel;
275:
276:                                        //Get the index of the row selected from the model
277:                                        int modelIndex = sorter
278:                                                .modelIndex(selectedRow);
279:
280:                                        //Build the view record window
281:                                        ViewRecordWindow viewRecordWindow = new ViewRecordWindow(
282:                                                uiControllerForUpdates,
283:                                                dbTableDataTableModel
284:                                                        .getDBTable(),
285:                                                new Integer(modelIndex));
286:                                        viewRecordWindow.show();
287:                                    }
288:                                }
289:                            }
290:                        }
291:                    }
292:                }
293:            }
294:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.