Source Code Cross Referenced for STable.java in  » J2EE » Sofia » com » salmonllc » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

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


0001:        //** Copyright Statement ***************************************************
0002:        //The Salmon Open Framework for Internet Applications (SOFIA)
0003:        // Copyright (C) 1999 - 2002, Salmon LLC
0004:        //
0005:        // This program is free software; you can redistribute it and/or
0006:        // modify it under the terms of the GNU General Public License version 2
0007:        // as published by the Free Software Foundation;
0008:        //
0009:        // This program is distributed in the hope that it will be useful,
0010:        // but WITHOUT ANY WARRANTY; without even the implied warranty of
0011:        // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012:        // GNU General Public License for more details.
0013:        //
0014:        // You should have received a copy of the GNU General Public License
0015:        // along with this program; if not, write to the Free Software
0016:        // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
0017:        //
0018:        // For more information please visit http://www.salmonllc.com
0019:        //** End Copyright Statement ***************************************************
0020:        package com.salmonllc.swing;
0021:
0022:        import com.salmonllc.sql.DataStoreBuffer;
0023:        import com.salmonllc.sql.DataStoreException;
0024:        import com.salmonllc.sql.DataStoreEvaluator;
0025:        import com.salmonllc.sql.DataStoreExpression;
0026:        import com.salmonllc.swing.events.ValueChangedEvent;
0027:        import com.salmonllc.swing.events.ValueChangedListener;
0028:        import com.salmonllc.swing.table.*;
0029:
0030:        import javax.swing.*;
0031:        import javax.swing.table.TableColumnModel;
0032:        import javax.swing.table.TableColumn;
0033:
0034:        import java.awt.Color;
0035:        import java.awt.event.MouseAdapter;
0036:        import java.awt.event.MouseEvent;
0037:        import java.util.EventObject;
0038:        import java.util.StringTokenizer;
0039:        import java.util.Vector;
0040:
0041:        /**
0042:         * SOFIA implementation of a JTable. This JTable allows for binding to columns and expressions in a DataStore, and allows the use of SComponents for editing and display.
0043:         */
0044:
0045:        public class STable extends JTable implements  SComponent {
0046:
0047:            private DataStoreBuffer _ds;
0048:            private SComponentHelper _helper;
0049:            private boolean _clickSort = true;
0050:            private boolean _selectCurrentRow = true;
0051:            private Vector _invisibleColumns = new Vector();
0052:            private DataStoreEvaluator _backgroundColorExpression;
0053:            private DataStoreEvaluator _foregroundColorExpression;
0054:            private DataStoreEvaluator _selectedForegroundColorExpression;
0055:            private DataStoreEvaluator _selectedBackgroundColorExpression;
0056:
0057:            public STable() {
0058:                super ();
0059:                setAutoCreateColumnsFromModel(false);
0060:                _helper = new SComponentHelper(this );
0061:                setAutoSelectCurrentRow(true);
0062:                getTableHeader().addMouseListener(new ListMouseListener());
0063:                setSurrendersFocusOnKeystroke(true);
0064:            }
0065:
0066:            /**
0067:             * Sets the DataStore that this table will use. Call this method before adding any columns to the table
0068:             */
0069:            public void setDataStore(DataStoreBuffer ds) {
0070:                _ds = ds;
0071:                DataStoreTableModel mod = new DataStoreTableModel(ds);
0072:                mod.setSTable(this );
0073:                setModel(mod);
0074:            }
0075:
0076:            /**
0077:             * Adds a column from the datastore to the visual grid
0078:             * @param name The identifier for the column
0079:             * @param columnExpression The expression to evaluate to display on the grid
0080:             * @param caption The caption to display
0081:             * @return The Table Column object that controls this column
0082:             * @throws DataStoreException if the expression is invalid
0083:             */
0084:            public STableColumn addDisplayColumn(String name,
0085:                    String columnExpression, String caption, SCheckBox cbx)
0086:                    throws DataStoreException {
0087:                TableColumnModel mod = getColumnModel();
0088:                STableColumn col = new STableColumn(name, this );
0089:                col.setHeaderValue(caption);
0090:                col.setModelIndex(_ds.getColumnIndex(columnExpression));
0091:                CheckBoxCellRenderer rend = new CheckBoxCellRenderer(this , cbx);
0092:                col.setCellRenderer(rend);
0093:                mod.addColumn(col);
0094:                return col;
0095:            }
0096:
0097:            /**
0098:             * Adds a column from the datastore to the visual grid
0099:             * @param columnExpression The expression to evaluate to display on the grid
0100:             * @param caption The caption to display
0101:             * @return The Table Column object that controls this column
0102:             * @throws DataStoreException if the expression is invalid
0103:             */
0104:            public STableColumn addDisplayColumn(String columnExpression,
0105:                    String caption, SCheckBox cbx) throws DataStoreException {
0106:                return addDisplayColumn(genName(), columnExpression, caption,
0107:                        cbx);
0108:            }
0109:
0110:            /**
0111:             * Adds a column from the datastore to the visual grid. It uses an SComboBox to render the column.
0112:             * @param name The identifier for the column
0113:             * @param columnExpression The expression to evaluate to display on the grid
0114:             * @param caption The caption to display
0115:             * @return The Table Column object that controls this column
0116:             * @throws DataStoreException if the expression is invalid
0117:             */
0118:            public STableColumn addDisplayColumn(String name,
0119:                    String columnExpression, String caption, SComboBox cbx)
0120:                    throws DataStoreException {
0121:                TableColumnModel mod = getColumnModel();
0122:                STableColumn col = new STableColumn(name, this );
0123:                col.setHeaderValue(caption);
0124:                col.setModelIndex(_ds.getColumnIndex(columnExpression));
0125:                ComboBoxCellRenderer rend = new ComboBoxCellRenderer(this , cbx);
0126:                col.setCellRenderer(rend);
0127:                mod.addColumn(col);
0128:                return col;
0129:            }
0130:
0131:            /**
0132:             * Adds a column from the datastore to the visual grid. It uses an SComboBox to render the column.
0133:             * @param columnExpression The expression to evaluate to display on the grid
0134:             * @param caption The caption to display
0135:             * @return The Table Column object that controls this column
0136:             * @throws DataStoreException if the expression is invalid
0137:             */
0138:            public STableColumn addDisplayColumn(String columnExpression,
0139:                    String caption, SComboBox cbx) throws DataStoreException {
0140:                return addDisplayColumn(genName(), columnExpression, caption,
0141:                        cbx);
0142:            }
0143:
0144:            /**
0145:             * Adds a column from the datastore to the visual grid.
0146:             * @param columnExpression The expression to evaluate to display on the grid
0147:             * @param caption The caption to display
0148:             * @return The Table Column object that controls this column
0149:             * @throws DataStoreException if the expression is invalid
0150:             */
0151:            public STableColumn addDisplayColumn(String columnExpression,
0152:                    String caption) throws DataStoreException {
0153:                return addDisplayColumn(genName(), new DataStoreEvaluator(_ds,
0154:                        columnExpression), caption);
0155:            }
0156:
0157:            /**
0158:             * Adds a column from the datastore to the visual grid.
0159:             * @param name The identifier for the column
0160:             * @param columnExpression The expression to evaluate to display on the grid
0161:             * @param caption The caption to display
0162:             * @return The Table Column object that controls this column
0163:             * @throws DataStoreException if the expression is invalid
0164:             */
0165:            public STableColumn addDisplayColumn(String name,
0166:                    String columnExpression, String caption)
0167:                    throws DataStoreException {
0168:                return addDisplayColumn(name, new DataStoreEvaluator(_ds,
0169:                        columnExpression), caption);
0170:            }
0171:
0172:            /**
0173:             * Adds a column from the datastore to the visual grid.
0174:             * @param columnExpression The expression to evaluate to display on the grid
0175:             * @param caption The caption to display
0176:             * @return The Table Column object that controls this column
0177:             * @throws DataStoreException if the expression is invalid
0178:             */
0179:            public STableColumn addDisplayColumn(
0180:                    DataStoreExpression columnExpression, String caption)
0181:                    throws DataStoreException {
0182:                return addDisplayColumn(genName(), new DataStoreEvaluator(_ds,
0183:                        columnExpression), caption);
0184:            }
0185:
0186:            /**
0187:             * Adds a column from the datastore to the visual grid.
0188:             * @param name The identifier for the column
0189:             * @param columnExpression The expression to evaluate to display on the grid
0190:             * @param caption The caption to display
0191:             * @return The Table Column object that controls this column
0192:             * @throws DataStoreException if the expression is invalid
0193:             */
0194:            public STableColumn addDisplayColumn(String name,
0195:                    DataStoreExpression columnExpression, String caption)
0196:                    throws DataStoreException {
0197:                return addDisplayColumn(name, new DataStoreEvaluator(_ds,
0198:                        columnExpression), caption);
0199:            }
0200:
0201:            /**
0202:             * Adds a column from the datastore to the visual grid.
0203:             * @param name The identifier for the column
0204:             * @param columnExpression The expression to evaluate to display on the grid
0205:             * @param caption The caption to display
0206:             * @return The Table Column object that controls this column
0207:             * @throws DataStoreException if the expression is invalid
0208:             */
0209:            public STableColumn addDisplayColumn(String name,
0210:                    DataStoreEvaluator columnExpression, String caption)
0211:                    throws DataStoreException {
0212:                TableColumnModel mod = getColumnModel();
0213:                STableColumn col = new STableColumn(name, this );
0214:                col.setHeaderValue(caption);
0215:                int modelIndex = -1;
0216:                if (columnExpression.getExpression() != null)
0217:                    modelIndex = _ds.getColumnIndex(columnExpression
0218:                            .getExpression());
0219:                col.setModelIndex(modelIndex);
0220:                ExpressionCellRenderer rend = new ExpressionCellRenderer(this );
0221:                if (modelIndex == -1)
0222:                    rend.setExpression(columnExpression);
0223:                else
0224:                    rend.setExpression(columnExpression.getExpression());
0225:                col.setCellRenderer(rend);
0226:                mod.addColumn(col);
0227:                return col;
0228:            }
0229:
0230:            /**
0231:             * Adds a column from the datastore to the visual grid.
0232:             * @param columnExpression The expression to evaluate to display on the grid
0233:             * @param caption The caption to display
0234:             * @return The Table Column object that controls this column
0235:             * @throws DataStoreException if the expression is invalid
0236:             */
0237:            public STableColumn addDisplayColumn(
0238:                    DataStoreEvaluator columnExpression, String caption)
0239:                    throws DataStoreException {
0240:                return addDisplayColumn(genName(), columnExpression, caption);
0241:            }
0242:
0243:            /**
0244:             * Adds a column from the datastore to the visual grid as an image.
0245:             * @param name The identifier for the column
0246:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0247:             * @param caption The caption to display
0248:             * @return The Table Column object that controls this column
0249:             */
0250:            private STableColumn addDisplayImage(String name,
0251:                    DataStoreEvaluator imageExpression, String caption) {
0252:                TableColumnModel mod = getColumnModel();
0253:                STableColumn col = new STableColumn(name, this );
0254:                col.setHeaderValue(caption);
0255:                col.setModelIndex(_ds.getColumnIndex(imageExpression
0256:                        .getExpression()));
0257:                ImageCellRenderer rend = new ImageCellRenderer(imageExpression);
0258:                col.setCellRenderer(rend);
0259:                mod.addColumn(col);
0260:                return col;
0261:            }
0262:
0263:            /**
0264:             * Adds a column from the datastore to the visual grid as an image.
0265:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0266:             * @param caption The caption to display
0267:             * @return The Table Column object that controls this column
0268:             */
0269:            private STableColumn addDisplayImage(
0270:                    DataStoreEvaluator imageExpression, String caption) {
0271:                return addDisplayImage(genName(), imageExpression, caption);
0272:            }
0273:
0274:            /**
0275:             * Adds a column from the datastore to the visual grid as an image.
0276:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0277:             * @param caption The caption to display
0278:             * @return The Table Column object that controls this column
0279:             * @throws DataStoreException if the expression is invalid
0280:             */
0281:            public STableColumn addDisplayImage(String imageExpression,
0282:                    String caption) throws DataStoreException {
0283:                return addDisplayImage(new DataStoreEvaluator(_ds,
0284:                        imageExpression), caption);
0285:            }
0286:
0287:            /**
0288:             * Adds a column from the datastore to the visual grid as an image.
0289:             * @param name The name of the column
0290:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0291:             * @param caption The caption to display
0292:             * @return The Table Column object that controls this column
0293:             * @throws DataStoreException if the expression is invalid
0294:             */
0295:            public STableColumn addDisplayImage(String name,
0296:                    String imageExpression, String caption)
0297:                    throws DataStoreException {
0298:                return addDisplayImage(name, new DataStoreEvaluator(_ds,
0299:                        imageExpression), caption);
0300:            }
0301:
0302:            /**
0303:             * Adds a column from the datastore to the visual grid as an image.
0304:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0305:             * @param caption The caption to display
0306:             * @return The Table Column object that controls this column
0307:             * @throws DataStoreException if the expression is invalid
0308:             */
0309:            public STableColumn addDisplayImage(
0310:                    DataStoreExpression imageExpression, String caption)
0311:                    throws DataStoreException {
0312:                return addDisplayImage(new DataStoreEvaluator(_ds,
0313:                        imageExpression), caption);
0314:            }
0315:
0316:            /**
0317:             * Adds a column from the datastore to the visual grid as an image.
0318:             * @param name The name of the column
0319:             * @param imageExpression The expression to evaluate to get the URL or byte array of the image
0320:             * @param caption The caption to display
0321:             * @return The Table Column object that controls this column
0322:             * @throws DataStoreException if the expression is invalid
0323:             */
0324:            public STableColumn addDisplayImage(String name,
0325:                    DataStoreExpression imageExpression, String caption)
0326:                    throws DataStoreException {
0327:                return addDisplayImage(name, new DataStoreEvaluator(_ds,
0328:                        imageExpression), caption);
0329:            }
0330:
0331:            /**
0332:             * Adds a column from the datastore to the visual grid. The column can be edited.
0333:             * @param name The internal identifier used in the STable
0334:             * @param column The name of the database column to display and edit on the grid
0335:             * @param caption The caption to display
0336:             * @return The Table Column object that controls this column
0337:             * @throws DataStoreException if the expression is invalid
0338:             */
0339:            public STableColumn addEditColumn(String name, String column,
0340:                    String caption) throws DataStoreException {
0341:                STableColumn col = addDisplayColumn(name, column, caption);
0342:                STextField f = new STextField();
0343:                col.setCellEditor(new SComponentCellEditor(f));
0344:                if (col.getModelIndex() != -1)
0345:                    ((DataStoreTableModel) getModel()).setColumnEditable(col
0346:                            .getModelIndex(), true);
0347:                return col;
0348:            }
0349:
0350:            /**
0351:             * Adds a column from the datastore to the visual grid. The column can be edited.
0352:             * @param column The name of the database column to display and edit on the grid
0353:             * @param caption The caption to display
0354:             * @return The Table Column object that controls this column
0355:             * @throws DataStoreException if the expression is invalid
0356:             */
0357:
0358:            public STableColumn addEditColumn(String column, String caption)
0359:                    throws DataStoreException {
0360:                return addEditColumn(column, column, caption);
0361:            }
0362:
0363:            /**
0364:             * Adds a column from the datastore to the visual grid. The column can be edited in an SComboBox.
0365:             * @param name The internal identifier used in the STable
0366:             * @param column The name of the database column to display and edit on the grid
0367:             * @param caption The caption to display
0368:             * @param combo The combo box to use as an editor
0369:             * @return The Table Column object that controls this column
0370:             * @throws DataStoreException if the expression is invalid
0371:             */
0372:
0373:            public STableColumn addEditColumn(String name, String column,
0374:                    String caption, SComboBox combo) throws DataStoreException {
0375:                STableColumn col = addDisplayColumn(name, column, caption,
0376:                        combo);
0377:                combo.setLightWeightPopupEnabled(false);
0378:                col.setCellEditor(new ComboBoxCellEditor(combo));
0379:                if (col.getModelIndex() != -1)
0380:                    ((DataStoreTableModel) getModel()).setColumnEditable(col
0381:                            .getModelIndex(), true);
0382:                return col;
0383:            }
0384:
0385:            /**
0386:             * Adds a column from the datastore to the visual grid. The column can be edited in an SComboBox.
0387:             * @param column The name of the database column to display and edit on the grid
0388:             * @param caption The caption to display
0389:             * @param combo The combo box to use as an editor
0390:             * @return The Table Column object that controls this column
0391:             * @throws DataStoreException if the expression is invalid
0392:             */
0393:
0394:            public STableColumn addEditColumn(String column, String caption,
0395:                    SComboBox combo) throws DataStoreException {
0396:                return addEditColumn(column, column, caption, combo);
0397:            }
0398:
0399:            /**
0400:             * Adds a column from the datastore to the visual grid. The column can be edited using an SCheckBox.
0401:             * @param name The internal identifier used in the STable
0402:             * @param column The name of the database column to display and edit on the grid
0403:             * @param caption The caption to display
0404:             * @param cbx The combo box to use as an editor
0405:             * @return The Table Column object that controls this column
0406:             * @throws DataStoreException if the expression is invalid
0407:             */
0408:
0409:            public STableColumn addEditColumn(String name, String column,
0410:                    String caption, SCheckBox cbx) throws DataStoreException {
0411:                STableColumn col = addDisplayColumn(name, column, caption, cbx);
0412:                col.setCellEditor(new CheckBoxCellEditor(cbx));
0413:                cbx.setBackground(getBackground());
0414:                if (col.getModelIndex() != -1)
0415:                    ((DataStoreTableModel) getModel()).setColumnEditable(col
0416:                            .getModelIndex(), true);
0417:                return col;
0418:            }
0419:
0420:            /**
0421:             * Adds a column from the datastore to the visual grid. The column can be edited using an SCheckBox.
0422:             * @param column The column display and edit on the grid
0423:             * @param caption The caption to display
0424:             * @param cbx The combo box to use as an editor
0425:             * @return The Table Column object that controls this column
0426:             * @throws DataStoreException if the expression is invalid
0427:             */
0428:
0429:            public STableColumn addEditColumn(String column, String caption,
0430:                    SCheckBox cbx) throws DataStoreException {
0431:                return addEditColumn(column, column, caption, cbx);
0432:            }
0433:
0434:            /**
0435:             * This method is used by the framework and should not be called directly
0436:             */
0437:            public ValueChangedEvent generateValueChangedEvent() {
0438:                int row = editingRow;
0439:                int col = editingColumn;
0440:                try {
0441:                    String oldVal = _ds.getFormattedString(row, col);
0442:                    String newVal = getCellEditor().getCellEditorValue()
0443:                            .toString();
0444:                    return new ValueChangedEvent(this , oldVal, newVal, _ds,
0445:                            row, col);
0446:                } catch (DataStoreException ex) {
0447:                    ex.printStackTrace();
0448:                    return null;
0449:                }
0450:            }
0451:
0452:            /**
0453:             * This method is used by the framework and should not be called directly
0454:             */
0455:            public SComponentHelper getHelper() {
0456:                return _helper;
0457:            }
0458:
0459:            /**
0460:             * This method adds a listener the will be notified when the value in this component changes.
0461:             * @param l The listener to add.
0462:             */
0463:            public void addValueChangedListener(ValueChangedListener l) {
0464:                _helper.addValueChangedListener(l);
0465:            }
0466:
0467:            /**
0468:             * This method removes a listener from the list that will be notified if the text in the component changes.
0469:             * @param l The listener to remove.
0470:             */
0471:            public void removeValueChangedListener(ValueChangedListener l) {
0472:                _helper.removeValueChangedListener(l);
0473:            }
0474:
0475:            /**
0476:             * Returns whether or not the table can be sorted by clicking on a column
0477:             */
0478:            public boolean getClickSort() {
0479:                return _clickSort;
0480:            }
0481:
0482:            /**
0483:             * Sets whether or not the table can be sorted by clicking on a column
0484:             */
0485:            public void setClickSort(boolean clickSort) {
0486:                _clickSort = clickSort;
0487:            }
0488:
0489:            /**
0490:             * Returns true if the STable row selection is bound to the current row in the DataStore. If so, the selected row in the STable will be the same as the current row in the DataStore.
0491:             */
0492:            public boolean getAutoSelectCurrentRow() {
0493:                return _selectCurrentRow;
0494:            }
0495:
0496:            /**
0497:             * Sets whether or not the STable row selection is bound to the current row in the DataStore. If so, the selected row in the STable will be the same as the current row in the DataStore.
0498:             */
0499:            public void setAutoSelectCurrentRow(boolean selectCurrentRow) {
0500:                _selectCurrentRow = selectCurrentRow;
0501:                setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0502:            }
0503:
0504:            /**
0505:             * Selects a row (single selection only)
0506:             */
0507:            public void selectRow(int row) {
0508:                clearSelection();
0509:                boolean oldAutoScroll = getAutoscrolls();
0510:                setAutoscrolls(true);
0511:                changeSelection(row, -1, false, false);
0512:                setAutoscrolls(oldAutoScroll);
0513:            }
0514:
0515:            /**
0516:             * Sets the table's selection mode to allow only single selections, a single
0517:             * contiguous interval, or multiple intervals.
0518:             * <P>
0519:             * <bold>Note:</bold>
0520:             * <code>JTable</code> provides all the methods for handling
0521:             * column and row selection.  When setting states,
0522:             * such as <code>setSelectionMode</code>, it not only
0523:             * updates the mode for the row selection model but also sets similar
0524:             * values in the selection model of the <code>columnModel</code>.
0525:             * If you want to have the row and column selection models operating
0526:             * in different modes, set them both directly.
0527:             * <p>
0528:             * Both the row and column selection models for <code>JTable</code>
0529:             * default to using a <code>DefaultListSelectionModel</code>
0530:             * so that <code>JTable</code> works the same way as the
0531:             * <code>JList</code>. See the <code>setSelectionMode</code> method
0532:             * in <code>JList</code> for details about the modes.
0533:             *
0534:             * @see JList#setSelectionMode
0535:             */
0536:            public void setSelectionMode(int selectionMode) {
0537:                super .setSelectionMode(selectionMode);
0538:                if (selectionMode != ListSelectionModel.SINGLE_SELECTION)
0539:                    setAutoSelectCurrentRow(false);
0540:            }
0541:
0542:            /**
0543:             * Adds the validations for a column in the datastore to a table column. The icon will display if ther are any validation errors for the specified column and the tooltip will show the error message.
0544:             * @param tabCol The column to set the expression for
0545:             * @param columnName The name of the column in the datastore
0546:             * @param icon the icon to show if a rule is volated
0547:             */
0548:            public void addColumnValidations(TableColumn tabCol,
0549:                    String columnName, Icon icon) {
0550:                ExpressionCellRenderer rend = (ExpressionCellRenderer) tabCol
0551:                        .getCellRenderer();
0552:                rend.addColumnValidations(columnName, icon);
0553:            }
0554:
0555:            /**
0556:             * Adds the validations for a column in the datastore to a table column. This method will bind to the column that the TableColumn is already bound to.
0557:             * @param tabCol The column to set the expression for
0558:             * @param icon the icon to show if a rule is volated
0559:             */
0560:            public void addColumnValidations(TableColumn tabCol, Icon icon) {
0561:                ExpressionCellRenderer rend = (ExpressionCellRenderer) tabCol
0562:                        .getCellRenderer();
0563:                if (rend.getColName() != null)
0564:                    rend.addColumnValidations(rend.getColName(), icon);
0565:            }
0566:
0567:            /**
0568:             * Displays the icon and message expression for each cell where the expression evaluates to false
0569:             * @param tabCol The TableColumn object to set the expression for number in the stable
0570:             * @param exp The expression to evaluate
0571:             * @param message The message to display if the rule is false
0572:             * @param icon the icon to show if a rule is volated
0573:             * @throws DataStoreException
0574:             */
0575:            public void addValidateExpression(TableColumn tabCol,
0576:                    DataStoreEvaluator exp, String message, Icon icon)
0577:                    throws DataStoreException {
0578:                ExpressionCellRenderer rend = (ExpressionCellRenderer) tabCol
0579:                        .getCellRenderer();
0580:                rend.addValidateExpression(exp, message, icon);
0581:            }
0582:
0583:            /**
0584:             * Displays the icon and message expression for each cell where the expression evaluates to false
0585:             * @param tabCol The TableColumn object to set the expression for number in the stable
0586:             * @param exp The expression to evaluate
0587:             * @param message The message to display if the rule is false
0588:             * @param icon the icon to show if a rule is volated
0589:             * @throws DataStoreException
0590:             */
0591:            public void addValidateExpression(TableColumn tabCol, String exp,
0592:                    String message, Icon icon) throws DataStoreException {
0593:                ExpressionCellRenderer rend = (ExpressionCellRenderer) tabCol
0594:                        .getCellRenderer();
0595:                rend.addValidateExpression(exp, message, icon);
0596:            }
0597:
0598:            /**
0599:             * Displays the icon and message expression for each cell where the expression evaluates to false
0600:             * @param tabCol The TableColumn object to set the expression for number in the stable
0601:             * @param exp The expression to evaluate
0602:             * @param message The message to display if the rule is false
0603:             * @param icon the icon to show if a rule is volated
0604:             * @throws DataStoreException
0605:             */
0606:            public void addValidateExpression(TableColumn tabCol,
0607:                    DataStoreExpression exp, String message, Icon icon)
0608:                    throws DataStoreException {
0609:                ExpressionCellRenderer rend = (ExpressionCellRenderer) tabCol
0610:                        .getCellRenderer();
0611:                rend.addValidateExpression(exp, message, icon);
0612:            }
0613:
0614:            /**
0615:             * Returns the table column object at the specified column index
0616:             */
0617:            public TableColumn getTableColumn(int tableColNo) {
0618:                return getColumnModel().getColumn(tableColNo);
0619:            }
0620:
0621:            //inner class that handles click sorting
0622:            private class ListMouseListener extends MouseAdapter {
0623:                Object _lastSort = null;
0624:                int _lastDir = DataStoreBuffer.SORT_ASC;
0625:
0626:                public void mouseClicked(MouseEvent e) {
0627:                    if (!_clickSort)
0628:                        return;
0629:                    if (isEditing())
0630:                        getCellEditor().stopCellEditing();
0631:                    TableColumnModel columnModel = getColumnModel();
0632:                    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
0633:                    TableColumn col = columnModel.getColumn(viewColumn);
0634:
0635:                    int colNo = convertColumnIndexToModel(viewColumn);
0636:                    try {
0637:                        if (colNo != -1) {
0638:                            _ds.sort(colNo, getDir(new Integer(colNo)));
0639:                        } else {
0640:                            ExpressionCellRenderer rend = (ExpressionCellRenderer) col
0641:                                    .getCellRenderer();
0642:                            DataStoreEvaluator eval = rend.getEvaluator();
0643:                            _ds.sort(eval, getDir(eval));
0644:                        }
0645:                    } catch (Exception ex) {
0646:                    }
0647:                    if (_ds.gotoFirst()) {
0648:                        getSelectionModel().clearSelection();
0649:                        getSelectionModel().setAnchorSelectionIndex(0);
0650:                        getSelectionModel().setLeadSelectionIndex(0);
0651:                    }
0652:                }
0653:
0654:                int getDir(Object o) {
0655:                    if (_lastSort == null) {
0656:                        _lastDir = DataStoreBuffer.SORT_ASC;
0657:                    } else if (o instanceof  Integer
0658:                            && _lastSort instanceof  Integer) {
0659:                        if (o.equals(_lastSort)) {
0660:                            if (_lastDir == DataStoreBuffer.SORT_ASC)
0661:                                _lastDir = DataStoreBuffer.SORT_DES;
0662:                            else
0663:                                _lastDir = DataStoreBuffer.SORT_ASC;
0664:                        } else
0665:                            _lastDir = DataStoreBuffer.SORT_ASC;
0666:                    } else if (o instanceof  DataStoreEvaluator
0667:                            && _lastSort instanceof  DataStoreEvaluator) {
0668:                        if (o == _lastSort) {
0669:                            if (_lastDir == DataStoreBuffer.SORT_ASC)
0670:                                _lastDir = DataStoreBuffer.SORT_DES;
0671:                            else
0672:                                _lastDir = DataStoreBuffer.SORT_ASC;
0673:                        } else
0674:                            _lastDir = DataStoreBuffer.SORT_ASC;
0675:                    } else
0676:                        _lastDir = DataStoreBuffer.SORT_ASC;
0677:
0678:                    _lastSort = o;
0679:                    return _lastDir;
0680:
0681:                }
0682:            };
0683:
0684:            private String genName() {
0685:                return "column" + getColumnCount();
0686:            }
0687:
0688:            /**
0689:             * Makes a column with the specified name invisible
0690:             * @param name The name of the column to hide
0691:             */
0692:            public void hideColumn(String name) {
0693:                TableColumnModel mod = getColumnModel();
0694:                try {
0695:                    int ndx = mod.getColumnIndex(name);
0696:                    if (ndx > -1) {
0697:                        STableColumn col = (STableColumn) mod.getColumn(ndx);
0698:                        mod.removeColumn(col);
0699:                        _invisibleColumns.add(col);
0700:                    }
0701:                } catch (Exception e) {
0702:                    e.printStackTrace();
0703:                }
0704:            }
0705:
0706:            /**
0707:             * Makes a column visible and adds it to the end of the table
0708:             */
0709:            public void showColumn(String name) {
0710:                TableColumnModel mod = getColumnModel();
0711:                for (int i = 0; i < _invisibleColumns.size(); i++) {
0712:                    STableColumn col = (STableColumn) _invisibleColumns
0713:                            .elementAt(i);
0714:                    if (col != null && col.getName().equals(name)) {
0715:                        mod.addColumn(col);
0716:                        _invisibleColumns.removeElementAt(i);
0717:                        return;
0718:                    }
0719:                }
0720:            }
0721:
0722:            /**
0723:             * Move the named column to a new position on the table
0724:             */
0725:            public void moveColumn(String name, int newIndex) {
0726:                TableColumnModel mod = getColumnModel();
0727:                try {
0728:                    int ndx = mod.getColumnIndex(name);
0729:                    mod.moveColumn(ndx, newIndex);
0730:                } catch (Exception e) {
0731:                    e.printStackTrace();
0732:                }
0733:            }
0734:
0735:            /**
0736:             * Returns the index of the named column or -1 if not found
0737:             */
0738:            public int getColumnIndex(String name) {
0739:                TableColumnModel mod = getColumnModel();
0740:                try {
0741:                    return mod.getColumnIndex(name);
0742:
0743:                } catch (Exception e) {
0744:                    return -1;
0745:                }
0746:            }
0747:
0748:            /**
0749:             *  Sets the width for a column. Autoresize must be set to off for this to work.
0750:             */
0751:            public void setColumnWidth(String name, int width) {
0752:                TableColumnModel mod = getColumnModel();
0753:                try {
0754:                    TableColumn col = mod.getColumn(mod.getColumnIndex(name));
0755:                    col.setPreferredWidth(width);
0756:                    col.setWidth(width);
0757:                } catch (Exception e) {
0758:                    e.printStackTrace();
0759:                }
0760:            }
0761:
0762:            /**
0763:             * Sets a column to editable, the column must have been added with an addEditColumn method for this to have an effect
0764:             */
0765:            public void setColumnEditable(String name, boolean editable) {
0766:                TableColumnModel mod = getColumnModel();
0767:                try {
0768:                    if (isEditing()) {
0769:                        DefaultCellEditor ed = (DefaultCellEditor) getCellEditor();
0770:                        ed.stopCellEditing();
0771:                    }
0772:
0773:                    STableColumn col = (STableColumn) mod.getColumn(mod
0774:                            .getColumnIndex(name));
0775:                    col.setEditable(editable);
0776:                } catch (Exception e) {
0777:                    e.printStackTrace();
0778:                }
0779:            }
0780:
0781:            /**
0782:             * Returns true if the named column is editable
0783:             */
0784:            public boolean isColumnEditable(String name) {
0785:                TableColumnModel mod = getColumnModel();
0786:                try {
0787:                    STableColumn col = (STableColumn) mod.getColumn(mod
0788:                            .getColumnIndex(name));
0789:                    return col.isEditable();
0790:                } catch (Exception e) {
0791:                    e.printStackTrace();
0792:                    return false;
0793:                }
0794:            }
0795:
0796:            /**
0797:             * Returns a list of all the columns in the table
0798:             */
0799:            public STableColumn[] getColumns() {
0800:                TableColumnModel mod = getColumnModel();
0801:                int count = mod.getColumnCount();
0802:                STableColumn ret[] = new STableColumn[count];
0803:                for (int i = 0; i < count; i++)
0804:                    ret[i] = (STableColumn) mod.getColumn(i);
0805:                return ret;
0806:            }
0807:
0808:            /**
0809:             * Updates the selection models of the table, depending on the state of the
0810:             * two flags: <code>toggle</code> and <code>extend</code>. All changes
0811:             * to the selection that are the result of keyboard or mouse events received
0812:             * by the UI are channeled through this method so that the behavior may be
0813:             * overridden by a subclass.
0814:             * <p>
0815:             * This implementation uses the following conventions:
0816:             * <ul>
0817:             * <li> <code>toggle</code>: <em>false</em>, <code>extend</code>: <em>false</em>.
0818:             *      Clear the previous selection and ensure the new cell is selected.
0819:             * <li> <code>toggle</code>: <em>false</em>, <code>extend</code>: <em>true</em>.
0820:             *      Extend the previous selection to include the specified cell.
0821:             * <li> <code>toggle</code>: <em>true</em>, <code>extend</code>: <em>false</em>.
0822:             *      If the specified cell is selected, deselect it. If it is not selected, select it.
0823:             * <li> <code>toggle</code>: <em>true</em>, <code>extend</code>: <em>true</em>.
0824:             *      Leave the selection state as it is, but move the anchor index to the specified location.
0825:             * </ul>
0826:             * @param  rowIndex   affects the selection at <code>row</code>
0827:             * @param  columnIndex  affects the selection at <code>column</code>
0828:             * @param  toggle  see description above
0829:             * @param  extend  if true, extend the current selection
0830:             *
0831:             */
0832:            public void changeSelection(int rowIndex, int columnIndex,
0833:                    boolean toggle, boolean extend) {
0834:                SComponentHelper.acceptCurrentValue();
0835:                super .changeSelection(rowIndex, columnIndex, toggle, extend);
0836:            }
0837:
0838:            /* (non-Javadoc)
0839:             * @see javax.swing.JTable#editCellAt(int, int)
0840:             */
0841:            public boolean editCellAt(int row, int column) {
0842:                boolean ret = super .editCellAt(row, column);
0843:                if (ret) {
0844:                    getEditorComponent().requestFocus();
0845:                }
0846:                return ret;
0847:            }
0848:
0849:            /* (non-Javadoc)
0850:             * @see javax.swing.JTable#editCellAt(int, int, java.util.EventObject)
0851:             */
0852:            public boolean editCellAt(int row, int column, EventObject e) {
0853:                boolean ret = super .editCellAt(row, column, e);
0854:                if (ret)
0855:                    getEditorComponent().requestFocus();
0856:                return ret;
0857:            }
0858:
0859:            /**
0860:             * Set a datastore expression for the selected background color of each row. The expression must return a String in the form "red,green,blue" 
0861:             * @param expression
0862:             * @throws DataStoreException
0863:             */
0864:            public void setSelectedBackgroundColorExpression(String expression)
0865:                    throws DataStoreException {
0866:                _selectedBackgroundColorExpression = new DataStoreEvaluator(
0867:                        _ds, expression);
0868:            }
0869:
0870:            /**
0871:             * Set a datastore expression for the selected background color of each row. The expression must return a Color object or a String in the form "red,green,blue"
0872:             * @param expression
0873:             * @throws DataStoreException
0874:             */
0875:            public void setSelectedBackgroundColorExpression(
0876:                    DataStoreExpression expression) throws DataStoreException {
0877:                _selectedBackgroundColorExpression = new DataStoreEvaluator(
0878:                        _ds, expression);
0879:            }
0880:
0881:            /**
0882:             * Set a datastore expression for the selected foreground color of each row. The expression must return a String in the form "red,green,blue" 
0883:             * @param expression
0884:             * @throws DataStoreException
0885:             */
0886:            public void setSelectedForegroundColorExpression(String expression)
0887:                    throws DataStoreException {
0888:                _selectedForegroundColorExpression = new DataStoreEvaluator(
0889:                        _ds, expression);
0890:            }
0891:
0892:            /**
0893:             * Set a datastore expression for the selected foreground color of each row. The expression must return a Color object or a String in the form "red,green,blue"
0894:             * @param expression
0895:             * @throws DataStoreException
0896:             */
0897:            public void setSelectedForegroundColorExpression(
0898:                    DataStoreExpression expression) throws DataStoreException {
0899:                _selectedForegroundColorExpression = new DataStoreEvaluator(
0900:                        _ds, expression);
0901:            }
0902:
0903:            /**
0904:             * Set a datastore expression for the background color of each row. The expression must return a String in the form "red,green,blue" 
0905:             * @param expression
0906:             * @throws DataStoreException
0907:             */
0908:            public void setBackgroundColorExpression(String expression)
0909:                    throws DataStoreException {
0910:                _backgroundColorExpression = new DataStoreEvaluator(_ds,
0911:                        expression);
0912:            }
0913:
0914:            /**
0915:             * Set a datastore expression for the background color of each row. The expression must return a Color object or a String in the form "red,green,blue"
0916:             * @param expression
0917:             * @throws DataStoreException
0918:             */
0919:            public void setBackgroundColorExpression(
0920:                    DataStoreExpression expression) throws DataStoreException {
0921:                _backgroundColorExpression = new DataStoreEvaluator(_ds,
0922:                        expression);
0923:            }
0924:
0925:            /**
0926:             * Set a datastore expression for the foreground color of each row. The expression must return a String in the form "red,green,blue" 
0927:             * @param expression
0928:             * @throws DataStoreException
0929:             */
0930:            public void setForegroundColorExpression(String expression)
0931:                    throws DataStoreException {
0932:                _foregroundColorExpression = new DataStoreEvaluator(_ds,
0933:                        expression);
0934:            }
0935:
0936:            /**
0937:             * Set a datastore expression for the foreground color of each row. The expression must return a Color object or a String in the form "red,green,blue"
0938:             * @param expression
0939:             * @throws DataStoreException
0940:             */
0941:            public void setForegroundColorExpression(
0942:                    DataStoreExpression expression) throws DataStoreException {
0943:                _foregroundColorExpression = new DataStoreEvaluator(_ds,
0944:                        expression);
0945:            }
0946:
0947:            /* (non-Javadoc)
0948:             * @see javax.swing.JTable#getSelectionBackground()
0949:             */
0950:            public Color getSelectionBackground(int row) {
0951:                Color c = getColor(_selectedBackgroundColorExpression, row);
0952:                if (c == null)
0953:                    return super .getSelectionBackground();
0954:                else
0955:                    return c;
0956:            }
0957:
0958:            /* (non-Javadoc)
0959:             * @see javax.swing.JTable#getSelectionForeground()
0960:             */
0961:            public Color getSelectionForeground(int row) {
0962:                Color c = getColor(_selectedForegroundColorExpression, row);
0963:                if (c == null)
0964:                    return super .getSelectionForeground();
0965:                else
0966:                    return c;
0967:            }
0968:
0969:            /* (non-Javadoc)
0970:             * @see java.awt.Component#getBackground()
0971:             */
0972:            public Color getBackground(int row) {
0973:                Color c = getColor(_backgroundColorExpression, row);
0974:                if (c == null)
0975:                    return super .getBackground();
0976:                else
0977:                    return c;
0978:            }
0979:
0980:            /* (non-Javadoc)
0981:             * @see java.awt.Component#getForeground()
0982:             */
0983:            public Color getForeground(int row) {
0984:                Color c = getColor(_foregroundColorExpression, row);
0985:                if (c == null)
0986:                    return super .getForeground();
0987:                else
0988:                    return c;
0989:            }
0990:
0991:            private Color getColor(DataStoreEvaluator eval, int row) {
0992:                if (eval == null)
0993:                    return null;
0994:                Object ret;
0995:                try {
0996:                    ret = eval.evaluateRow(row);
0997:                } catch (DataStoreException e1) {
0998:                    return null;
0999:                }
1000:                if (ret == null)
1001:                    return null;
1002:                if (ret instanceof  Color)
1003:                    return (Color) ret;
1004:                String col = "";
1005:                if (ret instanceof  String)
1006:                    col = (String) ret;
1007:                if (col.indexOf(",") == -1)
1008:                    return null;
1009:                StringTokenizer tok = new StringTokenizer(col, ",");
1010:                try {
1011:                    int red = Integer.parseInt(tok.nextToken().trim());
1012:                    int green = Integer.parseInt(tok.nextToken().trim());
1013:                    int blue = Integer.parseInt(tok.nextToken().trim());
1014:                    return new Color(red, green, blue);
1015:                } catch (Exception e) {
1016:                    return null;
1017:                }
1018:            }
1019:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.