Source Code Cross Referenced for DataTablePanel.java in  » Database-Client » JSqlTool » org » jsqltool » gui » tableplugins » 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 » JSqlTool » org.jsqltool.gui.tableplugins 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package org.jsqltool.gui.tableplugins;
0002:
0003:        import javax.swing.*;
0004:        import java.awt.*;
0005:        import javax.swing.event.*;
0006:        import javax.swing.table.*;
0007:        import java.awt.event.*;
0008:        import org.jsqltool.conn.DbConnectionUtil;
0009:        import org.jsqltool.model.CustomTableModel;
0010:        import java.sql.*;
0011:        import java.util.*;
0012:        import org.jsqltool.gui.*;
0013:        import org.jsqltool.gui.tablepanel.*;
0014:        import org.jsqltool.*;
0015:        import org.jsqltool.gui.tableplugins.datatable.filter.*;
0016:        import org.jsqltool.gui.panel.*;
0017:        import org.jsqltool.utils.Options;
0018:        import java.io.*;
0019:        import org.jsqltool.utils.ImageLoader;
0020:
0021:        /**
0022:         * <p>Title: JSqlTool Project</p>
0023:         * <p>Description: Panel used to show the table content.
0024:         * </p>
0025:         * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
0026:         *
0027:         * <p> This file is part of JSqlTool project.
0028:         * This library is free software; you can redistribute it and/or
0029:         * modify it under the terms of the (LGPL) Lesser General Public
0030:         * License as published by the Free Software Foundation;
0031:         *
0032:         *                GNU LESSER GENERAL PUBLIC LICENSE
0033:         *                 Version 2.1, February 1999
0034:         *
0035:         * This library is distributed in the hope that it will be useful,
0036:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0037:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0038:         * Library General Public License for more details.
0039:         *
0040:         * You should have received a copy of the GNU Library General Public
0041:         * License along with this library; if not, write to the Free
0042:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0043:         *
0044:         *       The author may be contacted at:
0045:         *           maurocarniel@tin.it</p>
0046:         *
0047:         * @author Mauro Carniel
0048:         * @version 1.0
0049:         */
0050:        public class DataTablePanel extends JPanel implements 
0051:                TableModelListener, TablePlugin {
0052:
0053:            private String tableName = null;
0054:            private Hashtable pk = null;
0055:            JPanel buttonsPanel = new JPanel();
0056:            JButton insertButton = new JButton();
0057:            JButton delButton = new JButton();
0058:            JButton findButton = new JButton();
0059:            JButton cancelEditButton = new JButton();
0060:            JButton refreshButton = new JButton();
0061:            JButton firstButton = new JButton();
0062:            JButton leftButton = new JButton();
0063:            JButton rightButton = new JButton();
0064:            JButton lastButton = new JButton();
0065:            ImageIcon insertImage;
0066:            ImageIcon delImage;
0067:            ImageIcon findImage;
0068:            ImageIcon find2Image;
0069:            ImageIcon refreshImage;
0070:            ImageIcon cancelEditImage;
0071:            ImageIcon commitImage;
0072:            ImageIcon firstImage;
0073:            ImageIcon leftImage;
0074:            ImageIcon rightImage;
0075:            ImageIcon lastImage;
0076:            FlowLayout flowLayout1 = new FlowLayout();
0077:            DataPanel dataPanel = null;
0078:            BorderLayout borderLayout1 = new BorderLayout();
0079:            BorderLayout borderLayout2 = new BorderLayout();
0080:            private DbConnectionUtil dbConnUtil = null;
0081:            private JFrame parent = null;
0082:            private static final String ASC_SYMBOL = "ASC";
0083:            private static final String DESC_SYMBOL = "DESC";
0084:            private Object[] selectedRow = null;
0085:
0086:            /** current changed row */
0087:            private int currentRow = -1;
0088:
0089:            /** current selected cell value */
0090:            private JTextField filterTF = new JTextField(15);
0091:
0092:            /** current selected column name */
0093:            private String columnName = null;
0094:
0095:            public DataTablePanel() {
0096:                try {
0097:                    jbInit();
0098:                } catch (Exception e) {
0099:                    e.printStackTrace();
0100:                }
0101:            }
0102:
0103:            /**
0104:             * Remove a column filter clause, if there exist.
0105:             * @param colName column name
0106:             * @param refresh flag used to force data table reloading
0107:             */
0108:            private void removeColumnFilter(String colName, boolean refresh) {
0109:                FilterModel fm = (FilterModel) dbConnUtil.getDbConnection()
0110:                        .getFilters().get(tableName);
0111:                if (fm == null) {
0112:                    fm = new FilterModel();
0113:                    dbConnUtil.getDbConnection().getFilters()
0114:                            .put(tableName, fm);
0115:                }
0116:                String sql = fm.getWhereClause();
0117:                int i = -1;
0118:                int j = -1;
0119:                int j1 = -1;
0120:                while ((i = sql.indexOf(columnName)) != -1) {
0121:                    j = sql.toLowerCase().indexOf(" and ", i);
0122:                    j1 = sql.toLowerCase().indexOf(" or ", i);
0123:                    if (j1 != -1 && j1 < j)
0124:                        j = j1;
0125:                    if (i == 0) {
0126:                        if (j == -1)
0127:                            sql = "";
0128:                        else
0129:                            sql = sql.substring(j);
0130:                    } else {
0131:                        if (j == -1)
0132:                            sql = sql.substring(0, i);
0133:                        else
0134:                            sql = sql.substring(0, i) + sql.substring(j);
0135:                    }
0136:                }
0137:                if (sql.toLowerCase().trim().endsWith("and"))
0138:                    sql = sql
0139:                            .substring(0, sql.toLowerCase().lastIndexOf("and"));
0140:                if (sql.toLowerCase().trim().endsWith("or"))
0141:                    sql = sql.substring(0, sql.toLowerCase().lastIndexOf("or"));
0142:
0143:                i = sql.indexOf("WHERE");
0144:                if (i == -1)
0145:                    i = 0;
0146:                else
0147:                    i = i + 5;
0148:                if (sql.substring(i).toLowerCase().trim().startsWith("and"))
0149:                    sql = sql.substring(sql.toLowerCase().indexOf("and") + 3);
0150:                if (sql.substring(i).toLowerCase().trim().startsWith("or"))
0151:                    sql = sql.substring(sql.toLowerCase().indexOf("of") + 2);
0152:
0153:                if (sql.trim().equals("WHERE"))
0154:                    sql = "";
0155:
0156:                fm.setWhereClause(sql);
0157:                dbConnUtil.saveProfile(true);
0158:                if (refresh)
0159:                    refreshButton_actionPerformed(null);
0160:            }
0161:
0162:            private void init() {
0163:                // pop-up menu creation...
0164:                final JPopupMenu tableMenu = new JPopupMenu();
0165:
0166:                filterTF.addKeyListener(new KeyAdapter() {
0167:                    public void keyTyped(KeyEvent e) {
0168:                        if (e.getKeyChar() == '\n') {
0169:                            tableMenu.setVisible(false);
0170:
0171:                            if (columnName != null) {
0172:                                // remove column filtering...
0173:                                removeColumnFilter(columnName, false);
0174:
0175:                                // reset table filtering...
0176:                                FilterModel fm = (FilterModel) dbConnUtil
0177:                                        .getDbConnection().getFilters().get(
0178:                                                tableName);
0179:                                if (fm == null) {
0180:                                    fm = new FilterModel();
0181:                                    dbConnUtil.getDbConnection().getFilters()
0182:                                            .put(tableName, fm);
0183:                                }
0184:                                String sql = fm.getWhereClause().trim();
0185:
0186:                                String value = filterTF.getText();
0187:                                try {
0188:                                    Number n = new Double(value);
0189:                                } catch (Exception ex) {
0190:                                    value = "'" + value + "'";
0191:                                }
0192:
0193:                                if (sql.length() == 0) {
0194:                                    sql = columnName + " = " + value;
0195:                                } else {
0196:                                    sql += " and " + columnName + " = " + value;
0197:                                }
0198:                                fm.setWhereClause(sql);
0199:                                dbConnUtil.saveProfile(true);
0200:
0201:                                // reload data
0202:                                refreshButton_actionPerformed(null);
0203:                            }
0204:                        }
0205:                    }
0206:                });
0207:                JPanel filterPanel = new JPanel();
0208:                filterPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 5));
0209:                JLabel filterLabel = new JLabel(Options.getInstance()
0210:                        .getResource("filter by "));
0211:                filterPanel.add(filterLabel, null);
0212:                filterPanel.add(filterTF, null);
0213:                JMenuItem removeFilterMenu = new JMenuItem(Options
0214:                        .getInstance().getResource("remove filter"));
0215:                removeFilterMenu.addActionListener(new ActionListener() {
0216:                    public void actionPerformed(ActionEvent e) {
0217:                        // remove column filtering and reload data...
0218:                        if (columnName != null)
0219:                            removeColumnFilter(columnName, true);
0220:                    }
0221:                });
0222:                tableMenu.add(filterPanel);
0223:                tableMenu.add(removeFilterMenu);
0224:                tableMenu.add(new JSeparator());
0225:                filterLabel.setFont(removeFilterMenu.getFont());
0226:
0227:                JMenuItem copyMenu = new JMenuItem(Options.getInstance()
0228:                        .getResource("copy row"));
0229:                JMenuItem countMenu = new JMenuItem(Options.getInstance()
0230:                        .getResource("record count"));
0231:                JMenuItem exportMenu = new JMenuItem(Options.getInstance()
0232:                        .getResource("data export..."));
0233:                final JMenuItem impoMenu = new JMenuItem(Options.getInstance()
0234:                        .getResource("import file into..."));
0235:                final JMenuItem expoMenu = new JMenuItem(Options.getInstance()
0236:                        .getResource("export to file..."));
0237:                impoMenu.addActionListener(new ActionListener() {
0238:                    public void actionPerformed(ActionEvent e) {
0239:                        try {
0240:                            int index = dataPanel.getTable().getSelectedRow();
0241:                            int col = dataPanel.getTable().getSelectedColumn();
0242:                            if (index != -1 && col != -1) {
0243:                                ((CustomTableModel) dataPanel.getTableModel())
0244:                                        .setEditMode(CustomTableModel.EDIT_REC);
0245:                                JFileChooser chooser = new JFileChooser(".");
0246:                                int returnVal = chooser.showDialog(null,
0247:                                        Options.getInstance().getResource(
0248:                                                "import file"));
0249:                                if (returnVal == JFileChooser.APPROVE_OPTION) {
0250:                                    File f = chooser.getSelectedFile();
0251:                                    BufferedInputStream in = new BufferedInputStream(
0252:                                            new FileInputStream(f));
0253:                                    byte[] bytes = new byte[(int) f.length()];
0254:                                    in.read(bytes);
0255:                                    in.close();
0256:
0257:                                    // definisco la parte where...
0258:                                    String wherePK = "";
0259:                                    ArrayList pkValues = new ArrayList();
0260:                                    Enumeration en = pk.keys();
0261:                                    String colName = null;
0262:                                    while (en.hasMoreElements()) {
0263:                                        colName = en.nextElement().toString();
0264:                                        wherePK += colName + "=? and ";
0265:                                        pkValues.add(dataPanel.getTableModel()
0266:                                                .getValueAt(
0267:                                                        index,
0268:                                                        ((Integer) pk
0269:                                                                .get(colName))
0270:                                                                .intValue()));
0271:                                    }
0272:                                    if (wherePK.length() > 0)
0273:                                        wherePK = wherePK.substring(0, wherePK
0274:                                                .length() - 4);
0275:                                    else if (!Options.getInstance()
0276:                                            .isUpdateWhenNoPK()
0277:                                            || selectedRow == null) {
0278:                                        JOptionPane
0279:                                                .showMessageDialog(
0280:                                                        null,
0281:                                                        Options
0282:                                                                .getInstance()
0283:                                                                .getResource(
0284:                                                                        "can't update data: pk is not defined."),
0285:                                                        Options
0286:                                                                .getInstance()
0287:                                                                .getResource(
0288:                                                                        "error"),
0289:                                                        JOptionPane.ERROR_MESSAGE);
0290:                                        return;
0291:                                    } else {
0292:                                        for (int i = 0; i < dataPanel
0293:                                                .getTableModel()
0294:                                                .getColumnCount(); i++) {
0295:                                            wherePK += dataPanel
0296:                                                    .getTableModel()
0297:                                                    .getColumnName(i)
0298:                                                    + "=? AND ";
0299:                                            pkValues.add(selectedRow[i]);
0300:                                        }
0301:                                        wherePK = wherePK.substring(0, wherePK
0302:                                                .length() - 4);
0303:                                    }
0304:                                    colName = dataPanel.getTableModel()
0305:                                            .getColumnName(col);
0306:                                    if (pk.containsKey(colName)) {
0307:                                        JOptionPane
0308:                                                .showMessageDialog(
0309:                                                        null,
0310:                                                        Options
0311:                                                                .getInstance()
0312:                                                                .getResource(
0313:                                                                        "can't update pk!"),
0314:                                                        Options
0315:                                                                .getInstance()
0316:                                                                .getResource(
0317:                                                                        "error"),
0318:                                                        JOptionPane.ERROR_MESSAGE);
0319:                                        return;
0320:                                    }
0321:                                    PreparedStatement pstmt = dbConnUtil
0322:                                            .getConn().prepareStatement(
0323:                                                    "UPDATE " + tableName
0324:                                                            + " SET " + colName
0325:                                                            + "=? WHERE "
0326:                                                            + wherePK);
0327:                                    dbConnUtil.writeBlob(bytes, pstmt);
0328:                                    for (int i = 0; i < pkValues.size(); i++)
0329:                                        pstmt.setObject(i + 2, pkValues.get(i));
0330:                                    pstmt.execute();
0331:                                    pstmt.close();
0332:                                }
0333:                            }
0334:                        } catch (Exception ex) {
0335:                            ex.printStackTrace();
0336:                            JOptionPane.showMessageDialog(null, Options
0337:                                    .getInstance().getResource(
0338:                                            "error while setting blob content")
0339:                                    + ":\n" + ex.getMessage(), Options
0340:                                    .getInstance().getResource(
0341:                                            "import file into blob field"),
0342:                                    JOptionPane.ERROR_MESSAGE);
0343:                        }
0344:                    }
0345:                });
0346:
0347:                expoMenu.addActionListener(new ActionListener() {
0348:                    public void actionPerformed(ActionEvent e) {
0349:                        try {
0350:                            int index = dataPanel.getTable().getSelectedRow();
0351:                            int col = dataPanel.getTable().getSelectedColumn();
0352:                            if (index != -1 && col != -1) {
0353:                                JFileChooser chooser = new JFileChooser(".");
0354:                                int returnVal = chooser.showDialog(null,
0355:                                        Options.getInstance().getResource(
0356:                                                "export to file"));
0357:                                if (returnVal == JFileChooser.APPROVE_OPTION) {
0358:                                    File f = chooser.getSelectedFile();
0359:
0360:                                    // where...
0361:                                    String wherePK = "";
0362:                                    ArrayList pkValues = new ArrayList();
0363:                                    Enumeration en = pk.keys();
0364:                                    String colName = null;
0365:                                    while (en.hasMoreElements()) {
0366:                                        colName = en.nextElement().toString();
0367:                                        wherePK += colName + "=? and ";
0368:                                        pkValues.add(dataPanel.getTableModel()
0369:                                                .getValueAt(
0370:                                                        index,
0371:                                                        ((Integer) pk
0372:                                                                .get(colName))
0373:                                                                .intValue()));
0374:                                    }
0375:                                    if (wherePK.length() > 0)
0376:                                        wherePK = wherePK.substring(0, wherePK
0377:                                                .length() - 4);
0378:                                    else if (!Options.getInstance()
0379:                                            .isUpdateWhenNoPK()
0380:                                            || selectedRow == null) {
0381:                                        JOptionPane
0382:                                                .showMessageDialog(
0383:                                                        null,
0384:                                                        Options
0385:                                                                .getInstance()
0386:                                                                .getResource(
0387:                                                                        "can't fetch data: pk is not defined."),
0388:                                                        Options
0389:                                                                .getInstance()
0390:                                                                .getResource(
0391:                                                                        "error"),
0392:                                                        JOptionPane.ERROR_MESSAGE);
0393:                                        return;
0394:                                    } else {
0395:                                        for (int i = 0; i < dataPanel
0396:                                                .getTableModel()
0397:                                                .getColumnCount(); i++) {
0398:                                            wherePK += dataPanel
0399:                                                    .getTableModel()
0400:                                                    .getColumnName(i)
0401:                                                    + "=? AND ";
0402:                                            pkValues.add(selectedRow[i]);
0403:                                        }
0404:                                        wherePK = wherePK.substring(0, wherePK
0405:                                                .length() - 4);
0406:                                    }
0407:                                    colName = dataPanel.getTableModel()
0408:                                            .getColumnName(col);
0409:                                    if (pk.containsKey(colName)) {
0410:                                        JOptionPane
0411:                                                .showMessageDialog(
0412:                                                        null,
0413:                                                        Options
0414:                                                                .getInstance()
0415:                                                                .getResource(
0416:                                                                        "can't fetch pk!"),
0417:                                                        Options
0418:                                                                .getInstance()
0419:                                                                .getResource(
0420:                                                                        "error"),
0421:                                                        JOptionPane.ERROR_MESSAGE);
0422:                                        return;
0423:                                    }
0424:                                    PreparedStatement pstmt = dbConnUtil
0425:                                            .getConn()
0426:                                            .prepareStatement(
0427:                                                    "select "
0428:                                                            + ((CustomTableModel) dataPanel
0429:                                                                    .getTableModel())
0430:                                                                    .getColumnName(col)
0431:                                                            + " from "
0432:                                                            + tableName
0433:                                                            + " where "
0434:                                                            + wherePK);
0435:                                    for (int i = 0; i < pkValues.size(); i++)
0436:                                        pstmt.setObject(i + 1, pkValues.get(i));
0437:                                    ResultSet rset = pstmt.executeQuery();
0438:                                    if (rset.next()) {
0439:                                        Blob blob = rset.getBlob(1);
0440:                                        int l = (int) blob.length();
0441:                                        byte[] bytes = blob.getBytes(1, l);
0442:                                        BufferedOutputStream out = new BufferedOutputStream(
0443:                                                new FileOutputStream(f));
0444:                                        out.write(bytes);
0445:                                        out.flush();
0446:                                        out.close();
0447:                                    }
0448:                                    rset.close();
0449:                                    pstmt.close();
0450:                                }
0451:                            }
0452:                        } catch (Exception ex) {
0453:                            ex.printStackTrace();
0454:                            JOptionPane.showMessageDialog(null, Options
0455:                                    .getInstance().getResource(
0456:                                            "error while getting blob content")
0457:                                    + ":\n" + ex.getMessage(), Options
0458:                                    .getInstance().getResource(
0459:                                            "import file into blob field"),
0460:                                    JOptionPane.ERROR_MESSAGE);
0461:                        }
0462:                    }
0463:                });
0464:
0465:                copyMenu.addActionListener(new ActionListener() {
0466:                    public void actionPerformed(ActionEvent e) {
0467:                        try {
0468:                            int index = dataPanel.getTable().getSelectedRow();
0469:                            if (index != -1) {
0470:                                ((CustomTableModel) dataPanel.getTableModel())
0471:                                        .setEditMode(CustomTableModel.INSERT_REC);
0472:                                for (int i = 0; i < ((CustomTableModel) dataPanel
0473:                                        .getTableModel()).getColumnCount(); i++) {
0474:                                    if (!pk
0475:                                            .containsKey(((CustomTableModel) dataPanel
0476:                                                    .getTableModel())
0477:                                                    .getColumnName(i)))
0478:                                        ((CustomTableModel) dataPanel
0479:                                                .getTableModel()).setValueAt(
0480:                                                ((CustomTableModel) dataPanel
0481:                                                        .getTableModel())
0482:                                                        .getValueAt(index, i),
0483:                                                ((CustomTableModel) dataPanel
0484:                                                        .getTableModel())
0485:                                                        .getRowCount() - 1, i);
0486:                                }
0487:                            }
0488:                        } catch (Exception ex) {
0489:                            ex.printStackTrace();
0490:                            JOptionPane.showMessageDialog(parent, Options
0491:                                    .getInstance().getResource(
0492:                                            "error while copying record")
0493:                                    + ":\n" + ex.getMessage(), Options
0494:                                    .getInstance().getResource("record count"),
0495:                                    JOptionPane.ERROR_MESSAGE);
0496:                        }
0497:                    }
0498:                });
0499:                countMenu.addActionListener(new ActionListener() {
0500:                    public void actionPerformed(ActionEvent e) {
0501:                        try {
0502:                            FilterModel fm = (FilterModel) dbConnUtil
0503:                                    .getDbConnection().getFilters().get(
0504:                                            tableName);
0505:                            if (fm == null) {
0506:                                fm = new FilterModel();
0507:                                dbConnUtil.getDbConnection().getFilters().put(
0508:                                        tableName, fm);
0509:                            }
0510:
0511:                            String filter = fm.getWhereClause()
0512:                                    + fm.getOrderClause();
0513:                            Statement stmt = dbConnUtil.getConn()
0514:                                    .createStatement();
0515:                            ResultSet rset = stmt
0516:                                    .executeQuery("SELECT count(*) FROM "
0517:                                            + tableName + filter);
0518:                            int count = 0;
0519:                            if (rset.next()) {
0520:                                count = rset.getInt(1);
0521:                            }
0522:                            rset.close();
0523:                            stmt.close();
0524:                            JOptionPane.showMessageDialog(parent, count
0525:                                    + " "
0526:                                    + Options.getInstance().getResource(
0527:                                            "records found."), Options
0528:                                    .getInstance().getResource("record count"),
0529:                                    JOptionPane.INFORMATION_MESSAGE);
0530:                        } catch (SQLException ex) {
0531:                            ex.printStackTrace();
0532:                            JOptionPane.showMessageDialog(parent, Options
0533:                                    .getInstance().getResource(
0534:                                            "error while record counting")
0535:                                    + ":\n" + ex.getMessage(), Options
0536:                                    .getInstance().getResource("record count"),
0537:                                    JOptionPane.ERROR_MESSAGE);
0538:                        }
0539:                    }
0540:                });
0541:                exportMenu.addActionListener(new ActionListener() {
0542:                    public void actionPerformed(ActionEvent e) {
0543:                        TableExportDialog f = new TableExportDialog(parent,
0544:                                dbConnUtil, tableName);
0545:                    }
0546:                });
0547:                tableMenu.add(impoMenu);
0548:                tableMenu.add(expoMenu);
0549:                tableMenu.add(new JSeparator());
0550:                tableMenu.add(copyMenu);
0551:                tableMenu.add(countMenu);
0552:                tableMenu.add(exportMenu);
0553:
0554:                dataPanel.getTable().addMouseListener(new MouseAdapter() {
0555:                    public void mouseClicked(MouseEvent e) {
0556:                        if (SwingUtilities.isRightMouseButton(e)) { // right mouse click
0557:                            impoMenu
0558:                                    .setEnabled(((CustomTableModel) dataPanel
0559:                                            .getTableModel()).getEditMode() == CustomTableModel.EDIT_REC
0560:                                            && dataPanel.getTable()
0561:                                                    .getSelectedColumn() != -1
0562:                                            && ((CustomTableModel) dataPanel
0563:                                                    .getTableModel())
0564:                                                    .isBlob(dataPanel
0565:                                                            .getTable()
0566:                                                            .getSelectedColumn()));
0567:                            expoMenu
0568:                                    .setEnabled(((CustomTableModel) dataPanel
0569:                                            .getTableModel()).getEditMode() == CustomTableModel.EDIT_REC
0570:                                            && dataPanel.getTable()
0571:                                                    .getSelectedColumn() != -1
0572:                                            && ((CustomTableModel) dataPanel
0573:                                                    .getTableModel())
0574:                                                    .isBlob(dataPanel
0575:                                                            .getTable()
0576:                                                            .getSelectedColumn()));
0577:                            // show pop-up menu...
0578:
0579:                            try {
0580:                                if (dataPanel.getTable().rowAtPoint(
0581:                                        e.getPoint()) != -1
0582:                                        && dataPanel.getTable().columnAtPoint(
0583:                                                e.getPoint()) != -1) {
0584:                                    Object value = dataPanel
0585:                                            .getTable()
0586:                                            .getValueAt(
0587:                                                    dataPanel
0588:                                                            .getTable()
0589:                                                            .rowAtPoint(
0590:                                                                    e
0591:                                                                            .getPoint()),
0592:                                                    dataPanel
0593:                                                            .getTable()
0594:                                                            .columnAtPoint(
0595:                                                                    e
0596:                                                                            .getPoint()));
0597:                                    columnName = dataPanel
0598:                                            .getTableModel()
0599:                                            .getColumnName(
0600:                                                    dataPanel
0601:                                                            .getTable()
0602:                                                            .columnAtPoint(
0603:                                                                    e
0604:                                                                            .getPoint()));
0605:                                    filterTF.setText(value == null ? "" : value
0606:                                            .toString());
0607:                                } else {
0608:                                    columnName = null;
0609:                                    filterTF.setText("");
0610:                                }
0611:                            } catch (Exception ex) {
0612:                            }
0613:                            tableMenu
0614:                                    .show(e.getComponent(), e.getX(), e.getY());
0615:                            filterTF.requestFocus();
0616:                        }
0617:                    }
0618:                });
0619:
0620:                //    dataPanel.getTable().setColumnSelectionAllowed(false);
0621:                //    dataPanel.getTable().setRowSelectionAllowed(false);
0622:                dataPanel.getTable().setListSelectionListener(
0623:                        new ListSelectionListener() {
0624:                            public void valueChanged(ListSelectionEvent e) {
0625:                                if (currentRow != -1
0626:                                        && dataPanel.getTable()
0627:                                                .getSelectedRow() != currentRow) {
0628:                                    if (((CustomTableModel) dataPanel
0629:                                            .getTableModel()).getEditMode() == CustomTableModel.INSERT_REC)
0630:                                        insertRecord(currentRow);
0631:                                    else if (((CustomTableModel) dataPanel
0632:                                            .getTableModel()).getEditMode() == CustomTableModel.EDIT_REC)
0633:                                        updateRecord(currentRow);
0634:
0635:                                }
0636:                                if (dataPanel.getTable().getSelectedRow() == -1) {
0637:                                    selectedRow = null;
0638:                                    return;
0639:                                }
0640:                                selectedRow = new Object[dataPanel
0641:                                        .getTableModel().getColumnCount()];
0642:                                for (int i = 0; i < dataPanel.getTableModel()
0643:                                        .getColumnCount(); i++)
0644:                                    selectedRow[i] = dataPanel.getTableModel()
0645:                                            .getValueAt(
0646:                                                    dataPanel.getTable()
0647:                                                            .getSelectedRow(),
0648:                                                    i);
0649:                            }
0650:                        });
0651:
0652:            }
0653:
0654:            public void setQuery(String tableName) {
0655:                if (tableName == null)
0656:                    return;
0657:                selectedRow = null;
0658:                this .tableName = tableName;
0659:                FilterModel fm = (FilterModel) dbConnUtil.getDbConnection()
0660:                        .getFilters().get(tableName);
0661:                if (fm == null) {
0662:                    fm = new FilterModel();
0663:                    dbConnUtil.getDbConnection().getFilters()
0664:                            .put(tableName, fm);
0665:                }
0666:
0667:                String filter = fm.getWhereClause() + fm.getOrderClause();
0668:                dataPanel.setQuery("SELECT * FROM " + tableName + " " + filter,
0669:                        new Vector());
0670:                pk = dbConnUtil.getPK(tableName);
0671:                if (filter.length() > 0)
0672:                    findButton.setIcon(find2Image);
0673:                else
0674:                    findButton.setIcon(findImage);
0675:
0676:                // set editability...
0677:                ((CustomTableModel) dataPanel.getTableModel())
0678:                        .setEditMode(CustomTableModel.EDIT_REC);
0679:                boolean[] editableCols = new boolean[dataPanel.getTableModel()
0680:                        .getColumnCount()];
0681:                for (int i = 0; i < dataPanel.getTableModel().getColumnCount(); i++)
0682:                    editableCols[i] = !pk.contains(dataPanel.getTableModel()
0683:                            .getColumnName(i))
0684:                            && (Options.getInstance().isUpdateWhenNoPK() || pk
0685:                                    .size() > 0);
0686:                ((CustomTableModel) dataPanel.getTableModel())
0687:                        .setEditableCols(editableCols);
0688:            }
0689:
0690:            /**
0691:             * Insert a record in the table.
0692:             * @param rowNum row index which contains the new row that will be inserted
0693:             */
0694:            private void insertRecord(int rowNum) {
0695:                try {
0696:                    // verifico se i campi PK sono stati tutti valorizzati, altrimenti esco...
0697:                    String names = "";
0698:                    String values = "";
0699:                    Object value = null;
0700:                    ArrayList bindValues = new ArrayList();
0701:                    for (int i = 0; i < dataPanel.getTableModel()
0702:                            .getColumnCount(); i++)
0703:                        if (dataPanel.getTableModel().getValueAt(rowNum, i) != null) {
0704:                            if (!dbConnUtil.getDbConnection().isQuotes())
0705:                                names += dataPanel.getTableModel()
0706:                                        .getColumnName(i)
0707:                                        + ", ";
0708:                            else
0709:                                names += "\""
0710:                                        + dataPanel.getTableModel()
0711:                                                .getColumnName(i) + "\", ";
0712:                            values += "?, ";
0713:                            value = dataPanel.getTableModel().getValueAt(
0714:                                    rowNum, i);
0715:                            if (value != null
0716:                                    && value.getClass().getName().equals(
0717:                                            "java.util.Date"))
0718:                                value = new java.sql.Timestamp(
0719:                                        ((java.util.Date) value).getTime());
0720:                            bindValues.add(value);
0721:                        }
0722:                    names = names.substring(0, names.length() - 2);
0723:                    values = values.substring(0, values.length() - 2);
0724:
0725:                    PreparedStatement pstmt = dbConnUtil
0726:                            .getConn()
0727:                            .prepareStatement(
0728:                                    !dbConnUtil.getDbConnection().isQuotes() ? ("INSERT INTO "
0729:                                            + tableName
0730:                                            + "("
0731:                                            + names
0732:                                            + ") VALUES(" + values + ")")
0733:                                            : ("INSERT INTO \"" + tableName
0734:                                                    + "\"(" + names
0735:                                                    + ") VALUES(" + values + ")"));
0736:                    for (int i = 0; i < bindValues.size(); i++)
0737:                        pstmt.setObject(i + 1, bindValues.get(i));
0738:                    pstmt.execute();
0739:                    pstmt.close();
0740:                    ((CustomTableModel) dataPanel.getTableModel())
0741:                            .setEditMode(CustomTableModel.EDIT_REC);
0742:                    currentRow = -1;
0743:                } catch (SQLException ex) {
0744:                    ex.printStackTrace();
0745:                    JOptionPane.showMessageDialog(this , Options.getInstance()
0746:                            .getResource("error on inserting data")
0747:                            + ":\n" + ex.getMessage(), Options.getInstance()
0748:                            .getResource("error"), JOptionPane.ERROR_MESSAGE);
0749:                    dataPanel.getTable()
0750:                            .setRowSelectionInterval(rowNum, rowNum);
0751:                }
0752:
0753:            }
0754:
0755:            /**
0756:             * Update a record in the table.
0757:             * @param rowNum row index which contains the row that will be updated
0758:             */
0759:            private void updateRecord(int rowNum) {
0760:                try {
0761:                    // prepare SET...
0762:                    String colNames = "";
0763:                    ArrayList values = new ArrayList();
0764:                    Object value = null;
0765:                    for (int i = 0; i < dataPanel.getTableModel()
0766:                            .getColumnCount(); i++) {
0767:                        value = dataPanel.getTableModel().getValueAt(rowNum, i);
0768:                        if (value != null) {
0769:                            if (!dbConnUtil.getDbConnection().isQuotes())
0770:                                colNames += dataPanel.getTableModel()
0771:                                        .getColumnName(i)
0772:                                        + "=?, ";
0773:                            else
0774:                                colNames += "\""
0775:                                        + dataPanel.getTableModel()
0776:                                                .getColumnName(i) + "\"=?, ";
0777:
0778:                            if (value != null
0779:                                    && value.getClass().getName().equals(
0780:                                            "java.util.Date"))
0781:                                value = new java.sql.Timestamp(
0782:                                        ((java.util.Date) value).getTime());
0783:                            values.add(value);
0784:                        } else {
0785:                            if (!dbConnUtil.getDbConnection().isQuotes())
0786:                                colNames += dataPanel.getTableModel()
0787:                                        .getColumnName(i)
0788:                                        + "=null, ";
0789:                            else
0790:                                colNames += "\""
0791:                                        + dataPanel.getTableModel()
0792:                                                .getColumnName(i) + "\"=null, ";
0793:                        }
0794:                    }
0795:                    colNames = colNames.substring(0, colNames.length() - 2);
0796:
0797:                    // prepare WHERE...
0798:                    String wherePK = "";
0799:                    Enumeration en = pk.keys();
0800:                    String colName = null;
0801:                    while (en.hasMoreElements()) {
0802:                        colName = en.nextElement().toString();
0803:                        if (!dbConnUtil.getDbConnection().isQuotes())
0804:                            wherePK += colName + "=? and ";
0805:                        else
0806:                            wherePK += "\"" + colName + "\"=? and ";
0807:                        value = dataPanel.getTableModel().getValueAt(rowNum,
0808:                                ((Integer) pk.get(colName)).intValue());
0809:                        if (value != null
0810:                                && value.getClass().getName().equals(
0811:                                        "java.util.Date"))
0812:                            value = new java.sql.Timestamp(
0813:                                    ((java.util.Date) value).getTime());
0814:                        values.add(value);
0815:                    }
0816:                    if (wherePK.length() > 0)
0817:                        wherePK = wherePK.substring(0, wherePK.length() - 4);
0818:                    else if (!Options.getInstance().isUpdateWhenNoPK()/* || selectedRow==null*/) {
0819:                        dataPanel.getTableModel()
0820:                                .removeTableModelListener(this );
0821:                        for (int i = 0; i < dataPanel.getTableModel()
0822:                                .getColumnCount(); i++) {
0823:                            dataPanel.getTableModel().setValueAt(
0824:                                    selectedRow[i], rowNum, i);
0825:                        }
0826:                        dataPanel.getTableModel().addTableModelListener(this );
0827:                        JOptionPane
0828:                                .showMessageDialog(
0829:                                        this ,
0830:                                        Options
0831:                                                .getInstance()
0832:                                                .getResource(
0833:                                                        "can't update data: pk is not defined."),
0834:                                        Options.getInstance().getResource(
0835:                                                "error"),
0836:                                        JOptionPane.ERROR_MESSAGE);
0837:                        return;
0838:                    } else {
0839:                        for (int i = 0; i < dataPanel.getTableModel()
0840:                                .getColumnCount(); i++) {
0841:                            wherePK += dataPanel.getTableModel().getColumnName(
0842:                                    i)
0843:                                    + "=? AND ";
0844:                            values.add(selectedRow[i]);
0845:                        }
0846:                        wherePK = wherePK.substring(0, wherePK.length() - 4);
0847:                    }
0848:
0849:                    PreparedStatement pstmt = dbConnUtil
0850:                            .getConn()
0851:                            .prepareStatement(
0852:                                    !dbConnUtil.getDbConnection().isQuotes() ? ("UPDATE "
0853:                                            + tableName
0854:                                            + " SET "
0855:                                            + colNames
0856:                                            + " WHERE " + wherePK)
0857:                                            : ("UPDATE \"" + tableName
0858:                                                    + "\" SET " + colNames
0859:                                                    + " WHERE " + wherePK)
0860:
0861:                            );
0862:
0863:                    for (int i = 0; i < values.size(); i++)
0864:                        pstmt.setObject(i + 1, values.get(i));
0865:                    pstmt.execute();
0866:                    pstmt.close();
0867:                    currentRow = -1;
0868:                } catch (SQLException ex) {
0869:                    ex.printStackTrace();
0870:                    JOptionPane.showMessageDialog(this , Options.getInstance()
0871:                            .getResource("error on updating data")
0872:                            + ":\n" + ex.getMessage(), Options.getInstance()
0873:                            .getResource("error"), JOptionPane.ERROR_MESSAGE);
0874:                    dataPanel.getTable()
0875:                            .setRowSelectionInterval(rowNum, rowNum);
0876:                }
0877:            }
0878:
0879:            /**
0880:             *  This fine grain notification tells listeners the exact range of cells,
0881:             * rows, or columns that changed.
0882:             */
0883:            public void tableChanged(TableModelEvent e) {
0884:                if (dbConnUtil.getDbConnection().isReadOnly())
0885:                    return;
0886:                if (e.getType() == e.UPDATE
0887:                        && ((CustomTableModel) dataPanel.getTableModel())
0888:                                .getEditMode() == CustomTableModel.EDIT_REC) {
0889:                    if (currentRow == -1)
0890:                        currentRow = e.getFirstRow();
0891:                    else if (currentRow != e.getFirstRow()) {
0892:                        updateRecord(currentRow);
0893:                        currentRow = e.getFirstRow();
0894:                    }
0895:
0896:                } else if (e.getType() == e.UPDATE
0897:                        && ((CustomTableModel) dataPanel.getTableModel())
0898:                                .getEditMode() == CustomTableModel.INSERT_REC) {
0899:                    if (currentRow == -1)
0900:                        currentRow = e.getFirstRow();
0901:                    else if (currentRow != e.getFirstRow()) {
0902:                        insertRecord(currentRow);
0903:                        currentRow = e.getFirstRow();
0904:                    }
0905:                }
0906:            }
0907:
0908:            public String getTableName() {
0909:                return tableName;
0910:            }
0911:
0912:            /**
0913:             * This method is called from the table detail to set entity name.
0914:             * @param tableName table name (edventualy including catalog name) that table plugin have to show
0915:             */
0916:            public final void setTableName(String tableName) {
0917:                this .tableName = tableName;
0918:            }
0919:
0920:            public void setEditMode(int editMode) {
0921:                if (dbConnUtil.getDbConnection().isReadOnly())
0922:                    return;
0923:                ((CustomTableModel) dataPanel.getTableModel())
0924:                        .setEditMode(editMode);
0925:            }
0926:
0927:            public int getEditMode() {
0928:                return ((CustomTableModel) dataPanel.getTableModel())
0929:                        .getEditMode();
0930:            }
0931:
0932:            void insertButton_actionPerformed(ActionEvent e) {
0933:                if (this .getEditMode() != CustomTableModel.INSERT_REC) {
0934:                    this .setEditMode(CustomTableModel.INSERT_REC);
0935:                    dataPanel.getTable().setRowSelectionInterval(
0936:                            dataPanel.getTable().getRowCount() - 1,
0937:                            dataPanel.getTable().getRowCount() - 1);
0938:                    try {
0939:                        dataPanel.getTable().scrollRectToVisible(
0940:                                dataPanel.getTable().getCellRect(
0941:                                        dataPanel.getTable().getRowCount() - 1,
0942:                                        0, true));
0943:                    } catch (Exception ex) {
0944:                    }
0945:                    dataPanel.getTable().editCellAt(
0946:                            dataPanel.getTable().getRowCount(), 0);
0947:                }
0948:            }
0949:
0950:            void delButton_actionPerformed(ActionEvent e) {
0951:                if (dbConnUtil.getDbConnection().isReadOnly())
0952:                    return;
0953:                if (((CustomTableModel) dataPanel.getTableModel())
0954:                        .getEditMode() == CustomTableModel.INSERT_REC)
0955:                    return;
0956:                if (dataPanel.getTable().getSelectedRow() == -1)
0957:                    return;
0958:                try {
0959:                    String wherePK = "";
0960:                    ArrayList pkValues = new ArrayList();
0961:                    Enumeration en = pk.keys();
0962:                    String colName = null;
0963:                    while (en.hasMoreElements()) {
0964:                        colName = en.nextElement().toString();
0965:                        if (!dbConnUtil.getDbConnection().isQuotes())
0966:                            wherePK += colName + "=? and ";
0967:                        else
0968:                            wherePK += "\"" + colName + "\"=? and ";
0969:                        pkValues.add(dataPanel.getTableModel().getValueAt(
0970:                                dataPanel.getTable().getSelectedRow(),
0971:                                ((Integer) pk.get(colName)).intValue()));
0972:                    }
0973:                    if (wherePK.length() > 0)
0974:                        wherePK = wherePK.substring(0, wherePK.length() - 4);
0975:                    else {
0976:                        JOptionPane
0977:                                .showMessageDialog(
0978:                                        this ,
0979:                                        Options
0980:                                                .getInstance()
0981:                                                .getResource(
0982:                                                        "can't delete data: pk is not defined."),
0983:                                        Options.getInstance().getResource(
0984:                                                "error"),
0985:                                        JOptionPane.ERROR_MESSAGE);
0986:                        return;
0987:                    }
0988:                    PreparedStatement pstmt = dbConnUtil
0989:                            .getConn()
0990:                            .prepareStatement(
0991:                                    !dbConnUtil.getDbConnection().isQuotes() ? ("DELETE from "
0992:                                            + tableName + " WHERE " + wherePK)
0993:                                            : ("DELETE from \"" + tableName
0994:                                                    + "\" WHERE " + wherePK));
0995:                    for (int i = 0; i < pkValues.size(); i++)
0996:                        pstmt.setObject(i + 1, pkValues.get(i));
0997:                    pstmt.execute();
0998:                    pstmt.close();
0999:                    ((CustomTableModel) dataPanel.getTableModel())
1000:                            .removeRow(dataPanel.getTable().getSelectedRow());
1001:                } catch (SQLException ex) {
1002:                    ex.printStackTrace();
1003:                    JOptionPane.showMessageDialog(this , Options.getInstance()
1004:                            .getResource("error on deleting data")
1005:                            + ":\n" + ex.getMessage(), Options.getInstance()
1006:                            .getResource("error"), JOptionPane.ERROR_MESSAGE);
1007:                }
1008:
1009:            }
1010:
1011:            void refreshButton_actionPerformed(ActionEvent e) {
1012:                if (this .getTableName() != null)
1013:                    this .setQuery(this .getTableName());
1014:            }
1015:
1016:            class DataTablePanel_insertButton_actionAdapter implements 
1017:                    java.awt.event.ActionListener {
1018:                DataTablePanel adaptee;
1019:
1020:                DataTablePanel_insertButton_actionAdapter(DataTablePanel adaptee) {
1021:                    this .adaptee = adaptee;
1022:                }
1023:
1024:                public void actionPerformed(ActionEvent e) {
1025:                    adaptee.insertButton_actionPerformed(e);
1026:                }
1027:            }
1028:
1029:            class DataTablePanel_delButton_actionAdapter implements 
1030:                    java.awt.event.ActionListener {
1031:                DataTablePanel adaptee;
1032:
1033:                DataTablePanel_delButton_actionAdapter(DataTablePanel adaptee) {
1034:                    this .adaptee = adaptee;
1035:                }
1036:
1037:                public void actionPerformed(ActionEvent e) {
1038:                    adaptee.delButton_actionPerformed(e);
1039:                }
1040:            }
1041:
1042:            class DataTablePanel_findButton_actionAdapter implements 
1043:                    java.awt.event.ActionListener {
1044:                DataTablePanel adaptee;
1045:
1046:                DataTablePanel_findButton_actionAdapter(DataTablePanel adaptee) {
1047:                    this .adaptee = adaptee;
1048:                }
1049:
1050:                public void actionPerformed(ActionEvent e) {
1051:                    adaptee.findButton_actionPerformed(e);
1052:                }
1053:            }
1054:
1055:            class DataTablePanel_refreshButton_actionAdapter implements 
1056:                    java.awt.event.ActionListener {
1057:                DataTablePanel adaptee;
1058:
1059:                DataTablePanel_refreshButton_actionAdapter(
1060:                        DataTablePanel adaptee) {
1061:                    this .adaptee = adaptee;
1062:                }
1063:
1064:                public void actionPerformed(ActionEvent e) {
1065:                    adaptee.refreshButton_actionPerformed(e);
1066:                }
1067:
1068:            }
1069:
1070:            private void jbInit() throws Exception {
1071:                this .setLayout(borderLayout2);
1072:                cancelEditButton
1073:                        .addActionListener(new DataTablePanel_cancelEditButton_actionAdapter(
1074:                                this ));
1075:                this .add(buttonsPanel, BorderLayout.NORTH);
1076:                insertButton.setBorder(null);
1077:                insertButton.setPreferredSize(new Dimension(24, 24));
1078:                insertButton
1079:                        .addActionListener(new DataTablePanel_insertButton_actionAdapter(
1080:                                this ));
1081:                delButton.setBorder(null);
1082:                delButton.setPreferredSize(new Dimension(24, 24));
1083:                delButton
1084:                        .addActionListener(new DataTablePanel_delButton_actionAdapter(
1085:                                this ));
1086:                findButton.setBorder(null);
1087:                findButton.setPreferredSize(new Dimension(24, 24));
1088:                findButton
1089:                        .addActionListener(new DataTablePanel_findButton_actionAdapter(
1090:                                this ));
1091:
1092:                cancelEditButton.setBorder(null);
1093:                cancelEditButton.setPreferredSize(new Dimension(24, 24));
1094:                refreshButton.setBorder(null);
1095:                refreshButton.setPreferredSize(new Dimension(24, 24));
1096:                refreshButton
1097:                        .addActionListener(new DataTablePanel_refreshButton_actionAdapter(
1098:                                this ));
1099:                firstButton.setBorder(null);
1100:                firstButton.setPreferredSize(new Dimension(24, 24));
1101:                firstButton
1102:                        .addActionListener(new DataTablePanel_firstButton_actionAdapter(
1103:                                this ));
1104:                leftButton.setBorder(null);
1105:                leftButton.setPreferredSize(new Dimension(24, 24));
1106:                leftButton
1107:                        .addActionListener(new DataTablePanel_leftButton_actionAdapter(
1108:                                this ));
1109:                rightButton.setBorder(null);
1110:                rightButton.setPreferredSize(new Dimension(24, 24));
1111:                rightButton
1112:                        .addActionListener(new DataTablePanel_rightButton_actionAdapter(
1113:                                this ));
1114:                lastButton.setBorder(null);
1115:                lastButton.setPreferredSize(new Dimension(24, 24));
1116:                lastButton
1117:                        .addActionListener(new DataTablePanel_lastButton_actionAdapter(
1118:                                this ));
1119:
1120:                buttonsPanel.setLayout(flowLayout1);
1121:                insertButton
1122:                        .addActionListener(new DataTablePanel_insertButton_actionAdapter(
1123:                                this ));
1124:                delButton
1125:                        .addActionListener(new DataTablePanel_delButton_actionAdapter(
1126:                                this ));
1127:                flowLayout1.setAlignment(FlowLayout.LEFT);
1128:                flowLayout1.setHgap(0);
1129:                refreshButton
1130:                        .addActionListener(new DataTablePanel_refreshButton_actionAdapter(
1131:                                this ));
1132:                insertImage = ImageLoader.getInstance().getIcon("insert.gif");
1133:                delImage = ImageLoader.getInstance().getIcon("delete.gif");
1134:                findImage = ImageLoader.getInstance().getIcon("find.gif");
1135:                find2Image = ImageLoader.getInstance().getIcon("find2.gif");
1136:                cancelEditImage = ImageLoader.getInstance().getIcon(
1137:                        "canceledit.gif");
1138:                // -MC 02/05/2006    refreshImage = ImageLoader.getInstance().getIcon("refreshdata.gif");
1139:                refreshImage = ImageLoader.getInstance()
1140:                        .getIcon("refresh2.gif");
1141:                commitImage = ImageLoader.getInstance().getIcon("commit.gif");
1142:                firstImage = ImageLoader.getInstance().getIcon("first.gif");
1143:                leftImage = ImageLoader.getInstance().getIcon("left.gif");
1144:                rightImage = ImageLoader.getInstance().getIcon("right.gif");
1145:                lastImage = ImageLoader.getInstance().getIcon("last.gif");
1146:
1147:                insertButton.setIcon(insertImage);
1148:                insertButton.setToolTipText(Options.getInstance().getResource(
1149:                        "insert record"));
1150:                insertButton.setMaximumSize(new Dimension(24, 24));
1151:                delButton.setIcon(delImage);
1152:                delButton.setToolTipText(Options.getInstance().getResource(
1153:                        "delete record"));
1154:                delButton.setMaximumSize(new Dimension(24, 24));
1155:                findButton.setMaximumSize(new Dimension(24, 24));
1156:
1157:                cancelEditButton.setIcon(cancelEditImage);
1158:                cancelEditButton.setToolTipText(Options.getInstance()
1159:                        .getResource("cancel edit"));
1160:                cancelEditButton.setMaximumSize(new Dimension(24, 24));
1161:                refreshButton.setIcon(refreshImage);
1162:                refreshButton.setToolTipText(Options.getInstance().getResource(
1163:                        "refresh data"));
1164:                refreshButton.setMaximumSize(new Dimension(24, 24));
1165:                firstButton.setMaximumSize(new Dimension(24, 24));
1166:                leftButton.setMaximumSize(new Dimension(24, 24));
1167:                rightButton.setMaximumSize(new Dimension(24, 24));
1168:                lastButton.setMaximumSize(new Dimension(24, 24));
1169:                findButton.setIcon(findImage);
1170:                findButton.setToolTipText(Options.getInstance().getResource(
1171:                        "filter data"));
1172:                firstButton.setIcon(firstImage);
1173:                firstButton.setToolTipText(Options.getInstance().getResource(
1174:                        "first records"));
1175:                leftButton.setIcon(leftImage);
1176:                leftButton.setToolTipText(Options.getInstance().getResource(
1177:                        "previous record"));
1178:                rightButton.setIcon(rightImage);
1179:                rightButton.setToolTipText(Options.getInstance().getResource(
1180:                        "next record"));
1181:                lastButton.setIcon(lastImage);
1182:                lastButton.setToolTipText(Options.getInstance().getResource(
1183:                        "last records"));
1184:                buttonsPanel.add(findButton, null);
1185:                buttonsPanel.add(insertButton, null);
1186:                buttonsPanel.add(delButton, null);
1187:                buttonsPanel.add(cancelEditButton, null);
1188:                buttonsPanel.add(refreshButton, null);
1189:                buttonsPanel.add(firstButton, null);
1190:                buttonsPanel.add(leftButton, null);
1191:                buttonsPanel.add(rightButton, null);
1192:                buttonsPanel.add(lastButton, null);
1193:            }
1194:
1195:            public void resetPanel() {
1196:                dataPanel.resetPanel();
1197:            }
1198:
1199:            void findButton_actionPerformed(ActionEvent e) {
1200:                FilterDialog f = new FilterDialog(parent, tableName, dbConnUtil);
1201:                f.setVisible(true);
1202:                refreshButton_actionPerformed(null);
1203:            }
1204:
1205:            void firstButton_actionPerformed(ActionEvent e) {
1206:                dataPanel.setQuery();
1207:                if (dataPanel.getTableModel().getRowCount() > 0) {
1208:                    dataPanel.getTable().setRowSelectionInterval(0, 0);
1209:                    dataPanel.getTable().scrollRectToVisible(
1210:                            dataPanel.getTable().getCellRect(
1211:                                    dataPanel.getTable().getSelectedRow(), 0,
1212:                                    true));
1213:                }
1214:                dataPanel.getTable().requestFocus();
1215:            }
1216:
1217:            void leftButton_actionPerformed(ActionEvent e) {
1218:                if (dataPanel.getTable().getSelectedRow() != -1
1219:                        && dataPanel.getTable().getSelectedRow() > 0)
1220:                    dataPanel.getTable().setRowSelectionInterval(
1221:                            dataPanel.getTable().getSelectedRow() - 1,
1222:                            dataPanel.getTable().getSelectedRow() - 1);
1223:                dataPanel.getTable().requestFocus();
1224:            }
1225:
1226:            void rightButton_actionPerformed(ActionEvent e) {
1227:                if (dataPanel.getTable().getSelectedRow() != -1
1228:                        && dataPanel.getTable().getSelectedRow() < dataPanel
1229:                                .getTableModel().getRowCount() - 1)
1230:                    dataPanel.getTable().setRowSelectionInterval(
1231:                            dataPanel.getTable().getSelectedRow() + 1,
1232:                            dataPanel.getTable().getSelectedRow() + 1);
1233:                dataPanel.getTable().requestFocus();
1234:            }
1235:
1236:            void lastButton_actionPerformed(ActionEvent e) {
1237:                dataPanel.setLastQuery();
1238:
1239:                if (dataPanel.getTableModel().getRowCount() > 0) {
1240:                    dataPanel.getTable().setRowSelectionInterval(
1241:                            dataPanel.getTableModel().getRowCount() - 1,
1242:                            dataPanel.getTableModel().getRowCount() - 1);
1243:                    dataPanel.getTable().scrollRectToVisible(
1244:                            dataPanel.getTable().getCellRect(
1245:                                    dataPanel.getTable().getSelectedRow(), 0,
1246:                                    true));
1247:                }
1248:                dataPanel.getTable().requestFocus();
1249:            }
1250:
1251:            void cancelEditButton_actionPerformed(ActionEvent e) {
1252:                try {
1253:                    //      dbConnUtil.getConn().rollback();
1254:                    //      refreshButton_actionPerformed(null);
1255:                    if (((CustomTableModel) dataPanel.getTableModel())
1256:                            .getEditMode() == CustomTableModel.INSERT_REC) {
1257:                        ((CustomTableModel) dataPanel.getTableModel())
1258:                                .removeRow(((CustomTableModel) dataPanel
1259:                                        .getTableModel()).getRowCount() - 1);
1260:                        dataPanel.getTable().requestFocus();
1261:                        dataPanel.getTable().setRowSelectionInterval(
1262:                                dataPanel.getTable().getRowCount() - 1,
1263:                                dataPanel.getTable().getRowCount() - 1);
1264:                        ((CustomTableModel) dataPanel.getTableModel())
1265:                                .setEditMode(CustomTableModel.EDIT_REC);
1266:                    } else if (((CustomTableModel) dataPanel.getTableModel())
1267:                            .getEditMode() == CustomTableModel.EDIT_REC
1268:                            && currentRow != -1 && selectedRow != null) {
1269:                        for (int i = 0; i < dataPanel.getTableModel()
1270:                                .getColumnCount(); i++)
1271:                            dataPanel.getTableModel().setValueAt(
1272:                                    selectedRow[i], currentRow, i);
1273:                        dataPanel.getTable().requestFocus();
1274:                        dataPanel.getTable().setRowSelectionInterval(
1275:                                currentRow, currentRow);
1276:                    }
1277:                    currentRow = -1;
1278:                } catch (Exception ex) {
1279:                    ex.printStackTrace();
1280:                }
1281:            }
1282:
1283:            /**
1284:             * @return indica la posizione del pannello all'interno del JTabbedPane associato al dettaglio tabella
1285:             */
1286:            public int getTabbedPosition() {
1287:                return 1;
1288:            }
1289:
1290:            /**
1291:             * @return indica il nome del folder che ospita il pannello del plugin, all'interno del JTabbedPane associato al dettaglio tabella
1292:             */
1293:            public String getTabbedName() {
1294:                return Options.getInstance().getResource("data");
1295:            }
1296:
1297:            /**
1298:             * Metodo chiamato in automatico dal dettaglio tabella per iniziazzare il plugin
1299:             */
1300:            public void initPanel(MainFrame parent, DbConnectionUtil dbConnUtil) {
1301:                this .parent = parent;
1302:                this .dbConnUtil = dbConnUtil;
1303:                this .dataPanel = new DataPanel(dbConnUtil, this );
1304:                this .add(dataPanel, BorderLayout.CENTER);
1305:                init();
1306:            }
1307:
1308:            /**
1309:             * Metodo chiamato dal dettaglio tabella per aggiornare il contenuto del pannello del plugin.
1310:             * @param tableName nome tabella (eventualmente comprensivo di nome catalogo) che il pannello del plugin deve aggiornare
1311:             */
1312:            public void updateContent() {
1313:                this .setQuery(tableName);
1314:            }
1315:
1316:            /**
1317:             * @return informazioni relative all'autore del plugin; "" oppure null non riportera' tali informazioni nella finestra di about
1318:             */
1319:            public String getAuthor() {
1320:                return "";
1321:            }
1322:
1323:            /**
1324:             * @return indica la versione del plugin
1325:             */
1326:            public String getVersion() {
1327:                return "1.0";
1328:            }
1329:
1330:            /**
1331:             * @return nome del plugin, riportato nella finestra di about
1332:             */
1333:            public String getName() {
1334:                return Options.getInstance().getResource("table data plugin");
1335:            }
1336:
1337:            public boolean equals(Object o) {
1338:                return (o instanceof  TablePlugin && ((TablePlugin) o).getName()
1339:                        .equals(getName()));
1340:            }
1341:
1342:        }
1343:
1344:        class DataTablePanel_findButton_actionAdapter implements 
1345:                java.awt.event.ActionListener {
1346:            DataTablePanel adaptee;
1347:
1348:            DataTablePanel_findButton_actionAdapter(DataTablePanel adaptee) {
1349:                this .adaptee = adaptee;
1350:            }
1351:
1352:            public void actionPerformed(ActionEvent e) {
1353:                adaptee.findButton_actionPerformed(e);
1354:            }
1355:        }
1356:
1357:        class DataTablePanel_firstButton_actionAdapter implements 
1358:                java.awt.event.ActionListener {
1359:            DataTablePanel adaptee;
1360:
1361:            DataTablePanel_firstButton_actionAdapter(DataTablePanel adaptee) {
1362:                this .adaptee = adaptee;
1363:            }
1364:
1365:            public void actionPerformed(ActionEvent e) {
1366:                adaptee.firstButton_actionPerformed(e);
1367:            }
1368:        }
1369:
1370:        class DataTablePanel_leftButton_actionAdapter implements 
1371:                java.awt.event.ActionListener {
1372:            DataTablePanel adaptee;
1373:
1374:            DataTablePanel_leftButton_actionAdapter(DataTablePanel adaptee) {
1375:                this .adaptee = adaptee;
1376:            }
1377:
1378:            public void actionPerformed(ActionEvent e) {
1379:                adaptee.leftButton_actionPerformed(e);
1380:            }
1381:        }
1382:
1383:        class DataTablePanel_rightButton_actionAdapter implements 
1384:                java.awt.event.ActionListener {
1385:            DataTablePanel adaptee;
1386:
1387:            DataTablePanel_rightButton_actionAdapter(DataTablePanel adaptee) {
1388:                this .adaptee = adaptee;
1389:            }
1390:
1391:            public void actionPerformed(ActionEvent e) {
1392:                adaptee.rightButton_actionPerformed(e);
1393:            }
1394:        }
1395:
1396:        class DataTablePanel_lastButton_actionAdapter implements 
1397:                java.awt.event.ActionListener {
1398:            DataTablePanel adaptee;
1399:
1400:            DataTablePanel_lastButton_actionAdapter(DataTablePanel adaptee) {
1401:                this .adaptee = adaptee;
1402:            }
1403:
1404:            public void actionPerformed(ActionEvent e) {
1405:                adaptee.lastButton_actionPerformed(e);
1406:            }
1407:        }
1408:
1409:        class DataTablePanel_insertButton_actionAdapter implements 
1410:                java.awt.event.ActionListener {
1411:            DataTablePanel adaptee;
1412:
1413:            DataTablePanel_insertButton_actionAdapter(DataTablePanel adaptee) {
1414:                this .adaptee = adaptee;
1415:            }
1416:
1417:            public void actionPerformed(ActionEvent e) {
1418:                adaptee.insertButton_actionPerformed(e);
1419:            }
1420:        }
1421:
1422:        class DataTablePanel_delButton_actionAdapter implements 
1423:                java.awt.event.ActionListener {
1424:            DataTablePanel adaptee;
1425:
1426:            DataTablePanel_delButton_actionAdapter(DataTablePanel adaptee) {
1427:                this .adaptee = adaptee;
1428:            }
1429:
1430:            public void actionPerformed(ActionEvent e) {
1431:                adaptee.delButton_actionPerformed(e);
1432:            }
1433:        }
1434:
1435:        class DataTablePanel_refreshButton_actionAdapter implements 
1436:                java.awt.event.ActionListener {
1437:            DataTablePanel adaptee;
1438:
1439:            DataTablePanel_refreshButton_actionAdapter(DataTablePanel adaptee) {
1440:                this .adaptee = adaptee;
1441:            }
1442:
1443:            public void actionPerformed(ActionEvent e) {
1444:                adaptee.refreshButton_actionPerformed(e);
1445:            }
1446:        }
1447:
1448:        class DataTablePanel_cancelEditButton_actionAdapter implements 
1449:                java.awt.event.ActionListener {
1450:            DataTablePanel adaptee;
1451:
1452:            DataTablePanel_cancelEditButton_actionAdapter(DataTablePanel adaptee) {
1453:                this .adaptee = adaptee;
1454:            }
1455:
1456:            public void actionPerformed(ActionEvent e) {
1457:                adaptee.cancelEditButton_actionPerformed(e);
1458:            }
1459:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.