0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package javax.swing;
0019:
0020: import java.awt.Color;
0021: import java.awt.Component;
0022: import java.awt.Cursor;
0023: import java.awt.Dimension;
0024: import java.awt.Font;
0025: import java.awt.FontMetrics;
0026: import java.awt.GraphicsEnvironment;
0027: import java.awt.HeadlessException;
0028: import java.awt.Point;
0029: import java.awt.Rectangle;
0030: import java.awt.event.FocusListener;
0031: import java.awt.event.KeyEvent;
0032: import java.awt.event.MouseEvent;
0033: import java.awt.print.Printable;
0034: import java.awt.print.PrinterException;
0035: import java.beans.PropertyChangeEvent;
0036: import java.beans.PropertyChangeListener;
0037: import java.lang.reflect.Constructor;
0038: import java.text.DateFormat;
0039: import java.text.MessageFormat;
0040: import java.text.NumberFormat;
0041: import java.util.Date;
0042: import java.util.EventObject;
0043: import java.util.Hashtable;
0044: import java.util.Locale;
0045: import java.util.Vector;
0046: import javax.accessibility.Accessible;
0047: import javax.accessibility.AccessibleAction;
0048: import javax.accessibility.AccessibleComponent;
0049: import javax.accessibility.AccessibleContext;
0050: import javax.accessibility.AccessibleExtendedTable;
0051: import javax.accessibility.AccessibleRole;
0052: import javax.accessibility.AccessibleSelection;
0053: import javax.accessibility.AccessibleStateSet;
0054: import javax.accessibility.AccessibleTable;
0055: import javax.accessibility.AccessibleTableModelChange;
0056: import javax.accessibility.AccessibleText;
0057: import javax.accessibility.AccessibleValue;
0058: import javax.print.attribute.PrintRequestAttributeSet;
0059: import javax.swing.border.Border;
0060: import javax.swing.event.CellEditorListener;
0061: import javax.swing.event.ChangeEvent;
0062: import javax.swing.event.ListSelectionEvent;
0063: import javax.swing.event.ListSelectionListener;
0064: import javax.swing.event.TableColumnModelEvent;
0065: import javax.swing.event.TableColumnModelListener;
0066: import javax.swing.event.TableModelEvent;
0067: import javax.swing.event.TableModelListener;
0068: import javax.swing.plaf.TableUI;
0069: import javax.swing.table.DefaultTableCellRenderer;
0070: import javax.swing.table.DefaultTableColumnModel;
0071: import javax.swing.table.DefaultTableModel;
0072: import javax.swing.table.JTableHeader;
0073: import javax.swing.table.TableCellEditor;
0074: import javax.swing.table.TableCellRenderer;
0075: import javax.swing.table.TableColumn;
0076: import javax.swing.table.TableColumnModel;
0077: import javax.swing.table.TableModel;
0078: import org.apache.harmony.luni.util.NotImplementedException;
0079: import org.apache.harmony.x.swing.StringConstants;
0080:
0081: import org.apache.harmony.x.swing.internal.nls.Messages;
0082:
0083: /**
0084: * <p>
0085: * <i>JTable</i>
0086: * </p>
0087: * <h3>Implementation Notes:</h3>
0088: * <ul>
0089: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
0090: * optimization, not as a guarantee of serialization compatibility.</li>
0091: * </ul>
0092: */
0093: @SuppressWarnings("unchecked")
0094: public class JTable extends JComponent implements TableModelListener,
0095: Scrollable, TableColumnModelListener, ListSelectionListener,
0096: CellEditorListener, Accessible {
0097: private static final long serialVersionUID = -506121678825692843L;
0098:
0099: public static final int AUTO_RESIZE_OFF = 0;
0100:
0101: public static final int AUTO_RESIZE_NEXT_COLUMN = 1;
0102:
0103: public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS = 2;
0104:
0105: public static final int AUTO_RESIZE_LAST_COLUMN = 3;
0106:
0107: public static final int AUTO_RESIZE_ALL_COLUMNS = 4;
0108:
0109: protected class AccessibleJTable extends AccessibleJComponent
0110: implements AccessibleSelection, ListSelectionListener,
0111: TableModelListener, TableColumnModelListener,
0112: CellEditorListener, PropertyChangeListener,
0113: AccessibleExtendedTable {
0114: private static final long serialVersionUID = 1L;
0115:
0116: protected class AccessibleJTableCell extends AccessibleContext
0117: implements Accessible, AccessibleComponent {
0118: public AccessibleJTableCell(JTable t, int r, int c, int i)
0119: throws NotImplementedException {
0120: throw new NotImplementedException();
0121: }
0122:
0123: public AccessibleContext getAccessibleContext()
0124: throws NotImplementedException {
0125: throw new NotImplementedException();
0126: }
0127:
0128: @Override
0129: public String getAccessibleName()
0130: throws NotImplementedException {
0131: throw new NotImplementedException();
0132: }
0133:
0134: @Override
0135: public void setAccessibleName(String s)
0136: throws NotImplementedException {
0137: throw new NotImplementedException();
0138: }
0139:
0140: @Override
0141: public String getAccessibleDescription()
0142: throws NotImplementedException {
0143: throw new NotImplementedException();
0144: }
0145:
0146: @Override
0147: public void setAccessibleDescription(String s)
0148: throws NotImplementedException {
0149: throw new NotImplementedException();
0150: }
0151:
0152: @Override
0153: public AccessibleRole getAccessibleRole()
0154: throws NotImplementedException {
0155: throw new NotImplementedException();
0156: }
0157:
0158: @Override
0159: public AccessibleStateSet getAccessibleStateSet()
0160: throws NotImplementedException {
0161: throw new NotImplementedException();
0162: }
0163:
0164: @Override
0165: public Accessible getAccessibleParent()
0166: throws NotImplementedException {
0167: throw new NotImplementedException();
0168: }
0169:
0170: @Override
0171: public int getAccessibleIndexInParent()
0172: throws NotImplementedException {
0173: throw new NotImplementedException();
0174: }
0175:
0176: @Override
0177: public int getAccessibleChildrenCount()
0178: throws NotImplementedException {
0179: throw new NotImplementedException();
0180: }
0181:
0182: @Override
0183: public Accessible getAccessibleChild(int i)
0184: throws NotImplementedException {
0185: throw new NotImplementedException();
0186: }
0187:
0188: @Override
0189: public Locale getLocale() throws NotImplementedException {
0190: throw new NotImplementedException();
0191: }
0192:
0193: @Override
0194: public void addPropertyChangeListener(
0195: PropertyChangeListener l)
0196: throws NotImplementedException {
0197: throw new NotImplementedException();
0198: }
0199:
0200: @Override
0201: public void removePropertyChangeListener(
0202: PropertyChangeListener l)
0203: throws NotImplementedException {
0204: throw new NotImplementedException();
0205: }
0206:
0207: @Override
0208: public AccessibleAction getAccessibleAction()
0209: throws NotImplementedException {
0210: throw new NotImplementedException();
0211: }
0212:
0213: @Override
0214: public AccessibleComponent getAccessibleComponent()
0215: throws NotImplementedException {
0216: throw new NotImplementedException();
0217: }
0218:
0219: @Override
0220: public AccessibleSelection getAccessibleSelection()
0221: throws NotImplementedException {
0222: throw new NotImplementedException();
0223: }
0224:
0225: @Override
0226: public AccessibleText getAccessibleText()
0227: throws NotImplementedException {
0228: throw new NotImplementedException();
0229: }
0230:
0231: @Override
0232: public AccessibleValue getAccessibleValue()
0233: throws NotImplementedException {
0234: throw new NotImplementedException();
0235: }
0236:
0237: public Color getBackground() throws NotImplementedException {
0238: throw new NotImplementedException();
0239: }
0240:
0241: public void setBackground(Color c)
0242: throws NotImplementedException {
0243: throw new NotImplementedException();
0244: }
0245:
0246: public Color getForeground() throws NotImplementedException {
0247: throw new NotImplementedException();
0248: }
0249:
0250: public void setForeground(Color c)
0251: throws NotImplementedException {
0252: throw new NotImplementedException();
0253: }
0254:
0255: public Cursor getCursor() throws NotImplementedException {
0256: throw new NotImplementedException();
0257: }
0258:
0259: public void setCursor(Cursor c)
0260: throws NotImplementedException {
0261: throw new NotImplementedException();
0262: }
0263:
0264: public Font getFont() throws NotImplementedException {
0265: throw new NotImplementedException();
0266: }
0267:
0268: public void setFont(Font f) throws NotImplementedException {
0269: throw new NotImplementedException();
0270: }
0271:
0272: public FontMetrics getFontMetrics(Font f)
0273: throws NotImplementedException {
0274: throw new NotImplementedException();
0275: }
0276:
0277: public boolean isEnabled() throws NotImplementedException {
0278: throw new NotImplementedException();
0279: }
0280:
0281: public void setEnabled(boolean b)
0282: throws NotImplementedException {
0283: throw new NotImplementedException();
0284: }
0285:
0286: public boolean isVisible() throws NotImplementedException {
0287: throw new NotImplementedException();
0288: }
0289:
0290: public void setVisible(boolean b)
0291: throws NotImplementedException {
0292: throw new NotImplementedException();
0293: }
0294:
0295: public boolean isShowing() throws NotImplementedException {
0296: throw new NotImplementedException();
0297: }
0298:
0299: public boolean contains(Point p)
0300: throws NotImplementedException {
0301: throw new NotImplementedException();
0302: }
0303:
0304: public Point getLocationOnScreen()
0305: throws NotImplementedException {
0306: throw new NotImplementedException();
0307: }
0308:
0309: public Point getLocation() throws NotImplementedException {
0310: throw new NotImplementedException();
0311: }
0312:
0313: public void setLocation(Point p)
0314: throws NotImplementedException {
0315: throw new NotImplementedException();
0316: }
0317:
0318: public Rectangle getBounds() throws NotImplementedException {
0319: throw new NotImplementedException();
0320: }
0321:
0322: public void setBounds(Rectangle r)
0323: throws NotImplementedException {
0324: throw new NotImplementedException();
0325: }
0326:
0327: public Dimension getSize() throws NotImplementedException {
0328: throw new NotImplementedException();
0329: }
0330:
0331: public void setSize(Dimension d)
0332: throws NotImplementedException {
0333: throw new NotImplementedException();
0334: }
0335:
0336: public Accessible getAccessibleAt(Point p)
0337: throws NotImplementedException {
0338: throw new NotImplementedException();
0339: }
0340:
0341: public boolean isFocusTraversable()
0342: throws NotImplementedException {
0343: throw new NotImplementedException();
0344: }
0345:
0346: public void requestFocus() throws NotImplementedException {
0347: throw new NotImplementedException();
0348: }
0349:
0350: public void addFocusListener(FocusListener l)
0351: throws NotImplementedException {
0352: throw new NotImplementedException();
0353: }
0354:
0355: public void removeFocusListener(FocusListener l)
0356: throws NotImplementedException {
0357: throw new NotImplementedException();
0358: }
0359: }
0360:
0361: protected class AccessibleJTableModelChange implements
0362: AccessibleTableModelChange {
0363: protected int type;
0364:
0365: protected int firstRow;
0366:
0367: protected int lastRow;
0368:
0369: protected int firstColumn;
0370:
0371: protected int lastColumn;
0372:
0373: protected AccessibleJTableModelChange(int type,
0374: int firstRow, int lastRow, int firstColumn,
0375: int lastColumn) throws NotImplementedException {
0376: throw new NotImplementedException();
0377: }
0378:
0379: public int getType() throws NotImplementedException {
0380: throw new NotImplementedException();
0381: }
0382:
0383: public int getFirstRow() throws NotImplementedException {
0384: throw new NotImplementedException();
0385: }
0386:
0387: public int getLastRow() throws NotImplementedException {
0388: throw new NotImplementedException();
0389: }
0390:
0391: public int getFirstColumn() throws NotImplementedException {
0392: throw new NotImplementedException();
0393: }
0394:
0395: public int getLastColumn() throws NotImplementedException {
0396: throw new NotImplementedException();
0397: }
0398: }
0399:
0400: public void propertyChange(PropertyChangeEvent e)
0401: throws NotImplementedException {
0402: throw new NotImplementedException();
0403: }
0404:
0405: public void tableChanged(TableModelEvent e)
0406: throws NotImplementedException {
0407: throw new NotImplementedException();
0408: }
0409:
0410: public void tableRowsInserted(TableModelEvent e)
0411: throws NotImplementedException {
0412: throw new NotImplementedException();
0413: }
0414:
0415: public void tableRowsDeleted(TableModelEvent e)
0416: throws NotImplementedException {
0417: throw new NotImplementedException();
0418: }
0419:
0420: public void columnAdded(TableColumnModelEvent e)
0421: throws NotImplementedException {
0422: throw new NotImplementedException();
0423: }
0424:
0425: public void columnRemoved(TableColumnModelEvent e)
0426: throws NotImplementedException {
0427: throw new NotImplementedException();
0428: }
0429:
0430: public void columnMoved(TableColumnModelEvent e)
0431: throws NotImplementedException {
0432: throw new NotImplementedException();
0433: }
0434:
0435: public void columnMarginChanged(ChangeEvent e)
0436: throws NotImplementedException {
0437: throw new NotImplementedException();
0438: }
0439:
0440: public void columnSelectionChanged(ListSelectionEvent e)
0441: throws NotImplementedException {
0442: throw new NotImplementedException();
0443: }
0444:
0445: public void editingStopped(ChangeEvent e)
0446: throws NotImplementedException {
0447: throw new NotImplementedException();
0448: }
0449:
0450: public void editingCanceled(ChangeEvent e)
0451: throws NotImplementedException {
0452: throw new NotImplementedException();
0453: }
0454:
0455: public void valueChanged(ListSelectionEvent e)
0456: throws NotImplementedException {
0457: throw new NotImplementedException();
0458: }
0459:
0460: @Override
0461: public AccessibleSelection getAccessibleSelection()
0462: throws NotImplementedException {
0463: throw new NotImplementedException();
0464: }
0465:
0466: @Override
0467: public AccessibleRole getAccessibleRole()
0468: throws NotImplementedException {
0469: throw new NotImplementedException();
0470: }
0471:
0472: @Override
0473: public Accessible getAccessibleAt(Point p)
0474: throws NotImplementedException {
0475: throw new NotImplementedException();
0476: }
0477:
0478: @Override
0479: public int getAccessibleChildrenCount()
0480: throws NotImplementedException {
0481: throw new NotImplementedException();
0482: }
0483:
0484: @Override
0485: public Accessible getAccessibleChild(int i)
0486: throws NotImplementedException {
0487: throw new NotImplementedException();
0488: }
0489:
0490: public int getAccessibleSelectionCount()
0491: throws NotImplementedException {
0492: throw new NotImplementedException();
0493: }
0494:
0495: public Accessible getAccessibleSelection(int i)
0496: throws NotImplementedException {
0497: throw new NotImplementedException();
0498: }
0499:
0500: public boolean isAccessibleChildSelected(int i)
0501: throws NotImplementedException {
0502: throw new NotImplementedException();
0503: }
0504:
0505: public void addAccessibleSelection(int i)
0506: throws NotImplementedException {
0507: throw new NotImplementedException();
0508: }
0509:
0510: public void removeAccessibleSelection(int i)
0511: throws NotImplementedException {
0512: throw new NotImplementedException();
0513: }
0514:
0515: public void clearAccessibleSelection()
0516: throws NotImplementedException {
0517: throw new NotImplementedException();
0518: }
0519:
0520: public void selectAllAccessibleSelection()
0521: throws NotImplementedException {
0522: throw new NotImplementedException();
0523: }
0524:
0525: public int getAccessibleRow(int index)
0526: throws NotImplementedException {
0527: throw new NotImplementedException();
0528: }
0529:
0530: public int getAccessibleColumn(int index)
0531: throws NotImplementedException {
0532: throw new NotImplementedException();
0533: }
0534:
0535: public int getAccessibleIndex(int r, int c)
0536: throws NotImplementedException {
0537: throw new NotImplementedException();
0538: }
0539:
0540: @Override
0541: public AccessibleTable getAccessibleTable()
0542: throws NotImplementedException {
0543: throw new NotImplementedException();
0544: }
0545:
0546: public Accessible getAccessibleCaption()
0547: throws NotImplementedException {
0548: throw new NotImplementedException();
0549: }
0550:
0551: public void setAccessibleCaption(Accessible a)
0552: throws NotImplementedException {
0553: throw new NotImplementedException();
0554: }
0555:
0556: public Accessible getAccessibleSummary()
0557: throws NotImplementedException {
0558: throw new NotImplementedException();
0559: }
0560:
0561: public void setAccessibleSummary(Accessible a)
0562: throws NotImplementedException {
0563: throw new NotImplementedException();
0564: }
0565:
0566: public int getAccessibleRowCount()
0567: throws NotImplementedException {
0568: throw new NotImplementedException();
0569: }
0570:
0571: public int getAccessibleColumnCount()
0572: throws NotImplementedException {
0573: throw new NotImplementedException();
0574: }
0575:
0576: public Accessible getAccessibleAt(int r, int c)
0577: throws NotImplementedException {
0578: throw new NotImplementedException();
0579: }
0580:
0581: public int getAccessibleRowExtentAt(int r, int c)
0582: throws NotImplementedException {
0583: throw new NotImplementedException();
0584: }
0585:
0586: public int getAccessibleColumnExtentAt(int r, int c)
0587: throws NotImplementedException {
0588: throw new NotImplementedException();
0589: }
0590:
0591: public AccessibleTable getAccessibleRowHeader()
0592: throws NotImplementedException {
0593: throw new NotImplementedException();
0594: }
0595:
0596: public void setAccessibleRowHeader(AccessibleTable a)
0597: throws NotImplementedException {
0598: throw new NotImplementedException();
0599: }
0600:
0601: public AccessibleTable getAccessibleColumnHeader()
0602: throws NotImplementedException {
0603: throw new NotImplementedException();
0604: }
0605:
0606: public void setAccessibleColumnHeader(AccessibleTable a)
0607: throws NotImplementedException {
0608: throw new NotImplementedException();
0609: }
0610:
0611: public Accessible getAccessibleRowDescription(int r)
0612: throws NotImplementedException {
0613: throw new NotImplementedException();
0614: }
0615:
0616: public void setAccessibleRowDescription(int r, Accessible a)
0617: throws NotImplementedException {
0618: throw new NotImplementedException();
0619: }
0620:
0621: public Accessible getAccessibleColumnDescription(int c)
0622: throws NotImplementedException {
0623: throw new NotImplementedException();
0624: }
0625:
0626: public void setAccessibleColumnDescription(int c, Accessible a)
0627: throws NotImplementedException {
0628: throw new NotImplementedException();
0629: }
0630:
0631: public boolean isAccessibleSelected(int r, int c)
0632: throws NotImplementedException {
0633: throw new NotImplementedException();
0634: }
0635:
0636: public boolean isAccessibleRowSelected(int r)
0637: throws NotImplementedException {
0638: throw new NotImplementedException();
0639: }
0640:
0641: public boolean isAccessibleColumnSelected(int c)
0642: throws NotImplementedException {
0643: throw new NotImplementedException();
0644: }
0645:
0646: public int[] getSelectedAccessibleRows()
0647: throws NotImplementedException {
0648: throw new NotImplementedException();
0649: }
0650:
0651: public int[] getSelectedAccessibleColumns()
0652: throws NotImplementedException {
0653: throw new NotImplementedException();
0654: }
0655:
0656: public int getAccessibleRowAtIndex(int i)
0657: throws NotImplementedException {
0658: throw new NotImplementedException();
0659: }
0660:
0661: public int getAccessibleColumnAtIndex(int i)
0662: throws NotImplementedException {
0663: throw new NotImplementedException();
0664: }
0665:
0666: public int getAccessibleIndexAt(int r, int c)
0667: throws NotImplementedException {
0668: throw new NotImplementedException();
0669: }
0670: }
0671:
0672: private class TableEditor extends DefaultCellEditor {
0673: private static final long serialVersionUID = -1840776223246975853L;
0674:
0675: public TableEditor(JCheckBox checkBox) {
0676: super (checkBox);
0677: setNormalEditorView();
0678: }
0679:
0680: public TableEditor(JTextField textField) {
0681: super (textField);
0682: setNormalEditorView();
0683: }
0684:
0685: public boolean isInputValid() {
0686: return verifyInput(delegate.getCellEditorValue());
0687: }
0688:
0689: protected boolean verifyInput(Object value) {
0690: return true;
0691: }
0692:
0693: @Override
0694: protected void fireEditingStopped() {
0695: if (!isInputValid()) {
0696: setErrorEditorView();
0697: return;
0698: }
0699: setNormalEditorView();
0700: super .fireEditingStopped();
0701: }
0702:
0703: @Override
0704: protected void fireEditingCanceled() {
0705: setNormalEditorView();
0706: super .fireEditingCanceled();
0707: }
0708:
0709: protected void setNormalEditorView() {
0710: ((JComponent) getComponent()).setBorder(BorderFactory
0711: .createLineBorder(Color.BLACK));
0712: }
0713:
0714: protected void setErrorEditorView() {
0715: ((JComponent) getComponent()).setBorder(BorderFactory
0716: .createLineBorder(Color.RED));
0717: }
0718: }
0719:
0720: private class ObjectEditor extends TableEditor {
0721: private static final long serialVersionUID = 1L;
0722:
0723: public ObjectEditor() {
0724: super (new JTextField());
0725: }
0726:
0727: @Override
0728: public boolean isCellEditable(EventObject event) {
0729: return getObjectConstructor() != null
0730: && super .isCellEditable(event);
0731: }
0732:
0733: @Override
0734: public Object getCellEditorValue() {
0735: Object value = delegate.getCellEditorValue();
0736: if (value == null) {
0737: return null;
0738: }
0739: try {
0740: return getObjectConstructor().newInstance(
0741: new Object[] { value.toString() });
0742: } catch (Exception e) {
0743: return null;
0744: }
0745: }
0746:
0747: @Override
0748: protected boolean verifyInput(Object value) {
0749: if (value == null || value.toString().trim().equals("")) {
0750: return true;
0751: }
0752: try {
0753: getObjectConstructor().newInstance(
0754: new Object[] { value.toString() });
0755: return true;
0756: } catch (Exception e) {
0757: return false;
0758: }
0759: }
0760:
0761: private Constructor<?> getObjectConstructor() {
0762: try {
0763: return getColumnClass().getConstructor(
0764: new Class[] { String.class });
0765: } catch (NoSuchMethodException e) {
0766: return null;
0767: }
0768: }
0769:
0770: private Class<?> getColumnClass() {
0771: Class<?> columnClass = JTable.this
0772: .getColumnClass(getEditingColumn());
0773: return columnClass == Object.class ? String.class
0774: : columnClass;
0775: }
0776: }
0777:
0778: private class NumberEditor extends ObjectEditor {
0779: private static final long serialVersionUID = 1L;
0780:
0781: public NumberEditor() {
0782: ((JTextField) getComponent())
0783: .setHorizontalAlignment(SwingConstants.RIGHT);
0784: }
0785: }
0786:
0787: private class BooleanEditor extends TableEditor {
0788: private static final long serialVersionUID = 1L;
0789:
0790: public BooleanEditor() {
0791: super (new JCheckBox());
0792: ((JCheckBox) getComponent())
0793: .setHorizontalAlignment(SwingConstants.CENTER);
0794: }
0795:
0796: @Override
0797: protected void setNormalEditorView() {
0798: ((JComponent) getComponent()).setBorder(null);
0799: }
0800: }
0801:
0802: protected TableModel dataModel;
0803:
0804: protected TableColumnModel columnModel;
0805:
0806: protected ListSelectionModel selectionModel;
0807:
0808: protected JTableHeader tableHeader;
0809:
0810: protected int rowHeight;
0811:
0812: protected int rowMargin;
0813:
0814: protected Color gridColor;
0815:
0816: protected boolean showHorizontalLines;
0817:
0818: protected boolean showVerticalLines;
0819:
0820: protected int autoResizeMode;
0821:
0822: protected boolean autoCreateColumnsFromModel = true;
0823:
0824: protected Dimension preferredViewportSize;
0825:
0826: protected boolean rowSelectionAllowed;
0827:
0828: protected boolean cellSelectionEnabled;
0829:
0830: protected transient Component editorComp;
0831:
0832: protected transient TableCellEditor cellEditor;
0833:
0834: protected transient int editingColumn = -1;
0835:
0836: protected transient int editingRow = -1;
0837:
0838: protected transient Hashtable defaultEditorsByColumnClass;
0839:
0840: protected transient Hashtable defaultRenderersByColumnClass;
0841:
0842: protected Color selectionBackground;
0843:
0844: protected Color selectionForeground;
0845:
0846: private boolean dragEnabled;
0847:
0848: private final Vector rowHeights = new Vector();
0849:
0850: private boolean surrendersFocusOnKeystroke;
0851:
0852: private boolean wasConsumed;
0853:
0854: private static final String HEADER_PROPERTY = "tableHeader";
0855:
0856: private static final String ROW_HEIGHT_PROPERTY = "rowHeight";
0857:
0858: private static final String ROW_MARGIN_PROPERTY = "rowMargin";
0859:
0860: private static final String GRID_COLOR_PROPERTY = "gridColor";
0861:
0862: private static final String SHOW_HORIZONTAL_LINES_PROPERTY = "showHorizontalLines";
0863:
0864: private static final String SHOW_VERTICAL_LINES_PROPERTY = "showVerticalLines";
0865:
0866: private static final String AUTO_RESIZE_MODE_PROPERTY = "autoResizeMode";
0867:
0868: private static final String AUTO_CREATE_COLUMNS_FROM_MODEL_PROPERTY = "autoCreateColumnsFromModel";
0869:
0870: private static final String ROW_SELECTION_ALLOWED_PROPERTY = "rowSelectionAllowed";
0871:
0872: private static final String COLUMN_SELECTION_ALLOWED_PROPERTY = "columnSelectionAllowed";
0873:
0874: private static final String CELL_SELECTION_ENABLED_PROPERTY = "cellSelectionEnabled";
0875:
0876: private static final String SELECTION_FOREGROUND_PROPERTY = "selectionForeground";
0877:
0878: private static final String SELECTION_BACKGROUND_PROPERTY = "selectionBackground";
0879:
0880: private static final String UI_CLASS_ID = "TableUI";
0881:
0882: public static JScrollPane createScrollPaneForTable(JTable table) {
0883: return new JScrollPane(table);
0884: }
0885:
0886: public JTable() {
0887: this (null, null, null);
0888: }
0889:
0890: public JTable(TableModel model) {
0891: this (model, null, null);
0892: }
0893:
0894: public JTable(TableModel model, TableColumnModel columnModel) {
0895: this (model, columnModel, null);
0896: }
0897:
0898: public JTable(TableModel model, TableColumnModel columnModel,
0899: ListSelectionModel selectionModel) {
0900: setColumnModel(columnModel != null ? columnModel
0901: : createDefaultColumnModel());
0902: setModel(model != null ? model : createDefaultDataModel());
0903: setSelectionModel(selectionModel != null ? selectionModel
0904: : createDefaultSelectionModel());
0905: initializeLocalVars();
0906: updateUI();
0907: }
0908:
0909: public JTable(int numRows, int numColumns) {
0910: this (new DefaultTableModel(numRows, numColumns));
0911: if (getAutoCreateColumnsFromModel()) {
0912: createDefaultColumnsFromModel();
0913: }
0914: }
0915:
0916: public JTable(Vector rowData, Vector columnNames) {
0917: this (new DefaultTableModel(rowData, columnNames));
0918: if (getAutoCreateColumnsFromModel()) {
0919: createDefaultColumnsFromModel();
0920: }
0921: }
0922:
0923: public JTable(Object[][] rowData, Object[] columnNames) {
0924: this (new DefaultTableModel(rowData, columnNames));
0925: if (getAutoCreateColumnsFromModel()) {
0926: createDefaultColumnsFromModel();
0927: }
0928: }
0929:
0930: @Override
0931: public void addNotify() {
0932: configureEnclosingScrollPane();
0933: super .addNotify();
0934: }
0935:
0936: @Override
0937: public void removeNotify() {
0938: super .removeNotify();
0939: unconfigureEnclosingScrollPane();
0940: }
0941:
0942: public void setTableHeader(JTableHeader header) {
0943: JTableHeader oldValue = this .tableHeader;
0944: if (oldValue != null) {
0945: oldValue.setTable(null);
0946: }
0947: this .tableHeader = header;
0948: if (header != null) {
0949: this .tableHeader.setTable(this );
0950: }
0951: firePropertyChange(HEADER_PROPERTY, oldValue, header);
0952: }
0953:
0954: public JTableHeader getTableHeader() {
0955: return tableHeader;
0956: }
0957:
0958: public void setRowHeight(int rowHeight) {
0959: if (rowHeight <= 0) {
0960: throw new IllegalArgumentException(Messages
0961: .getString("swing.38")); //$NON-NLS-1$
0962: }
0963: int oldValue = this .rowHeight;
0964: this .rowHeight = rowHeight;
0965: firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, rowHeight);
0966: }
0967:
0968: public int getRowHeight() {
0969: return rowHeight;
0970: }
0971:
0972: public void setRowHeight(int row, int height) {
0973: if (height <= 0) {
0974: throw new IllegalArgumentException(Messages
0975: .getString("swing.38")); //$NON-NLS-1$
0976: }
0977: if (rowHeights.size() <= row) {
0978: rowHeights.setSize(row + 1);
0979: }
0980: if (rowHeights.get(row) == null) {
0981: rowHeights.set(row, new MutableInteger());
0982: }
0983: if (row < getRowCount()) {
0984: ((MutableInteger) rowHeights.get(row)).setValue(height);
0985: }
0986: }
0987:
0988: public int getRowHeight(int row) {
0989: int result = rowHeights.size() > row
0990: && rowHeights.get(row) != null ? ((MutableInteger) rowHeights
0991: .get(row)).getValue()
0992: : -1;
0993: return result != -1 ? result : getRowHeight();
0994: }
0995:
0996: public void setRowMargin(int margin) {
0997: int oldValue = this .rowMargin;
0998: this .rowMargin = margin;
0999: firePropertyChange(ROW_MARGIN_PROPERTY, oldValue, margin);
1000: }
1001:
1002: public int getRowMargin() {
1003: return rowMargin;
1004: }
1005:
1006: public void setIntercellSpacing(Dimension spacing) {
1007: setRowMargin(spacing.height);
1008: columnModel.setColumnMargin(spacing.width);
1009: }
1010:
1011: public Dimension getIntercellSpacing() {
1012: return new Dimension(columnModel.getColumnMargin(),
1013: getRowMargin());
1014: }
1015:
1016: public void setGridColor(Color color) {
1017: Color oldValue = this .gridColor;
1018: this .gridColor = color;
1019: firePropertyChange(GRID_COLOR_PROPERTY, oldValue, color);
1020: }
1021:
1022: public Color getGridColor() {
1023: return gridColor;
1024: }
1025:
1026: public void setShowGrid(boolean show) {
1027: setShowHorizontalLines(show);
1028: setShowVerticalLines(show);
1029: }
1030:
1031: public void setShowHorizontalLines(boolean show) {
1032: boolean oldValue = this .showHorizontalLines;
1033: this .showHorizontalLines = show;
1034: firePropertyChange(SHOW_HORIZONTAL_LINES_PROPERTY, oldValue,
1035: show);
1036: }
1037:
1038: public boolean getShowHorizontalLines() {
1039: return showHorizontalLines;
1040: }
1041:
1042: public void setShowVerticalLines(boolean show) {
1043: boolean oldValue = this .showVerticalLines;
1044: this .showVerticalLines = show;
1045: firePropertyChange(SHOW_VERTICAL_LINES_PROPERTY, oldValue, show);
1046: }
1047:
1048: public boolean getShowVerticalLines() {
1049: return showVerticalLines;
1050: }
1051:
1052: public void setAutoResizeMode(int mode) {
1053: if (mode < 0 || mode > 4) {
1054: return;
1055: }
1056: int oldValue = this .autoResizeMode;
1057: this .autoResizeMode = mode;
1058: firePropertyChange(AUTO_RESIZE_MODE_PROPERTY, oldValue, mode);
1059: }
1060:
1061: public int getAutoResizeMode() {
1062: return autoResizeMode;
1063: }
1064:
1065: public void setAutoCreateColumnsFromModel(boolean autoCreate) {
1066: boolean oldValue = autoCreateColumnsFromModel;
1067: autoCreateColumnsFromModel = autoCreate;
1068: if (getAutoCreateColumnsFromModel()) {
1069: createDefaultColumnsFromModel();
1070: }
1071: firePropertyChange(AUTO_CREATE_COLUMNS_FROM_MODEL_PROPERTY,
1072: oldValue, autoCreate);
1073: }
1074:
1075: public boolean getAutoCreateColumnsFromModel() {
1076: return autoCreateColumnsFromModel;
1077: }
1078:
1079: public void createDefaultColumnsFromModel() {
1080: int columnCount = columnModel.getColumnCount();
1081: for (int i = 0; i < columnCount; i++) {
1082: columnModel.removeColumn(columnModel.getColumn(0));
1083: }
1084: for (int i = 0; i < getModel().getColumnCount(); i++) {
1085: TableColumn column = new TableColumn(i);
1086: column.setHeaderValue(getModel().getColumnName(i));
1087: columnModel.addColumn(column);
1088: }
1089: }
1090:
1091: public void setDefaultRenderer(Class<?> columnClass,
1092: TableCellRenderer renderer) {
1093: if (renderer != null) {
1094: defaultRenderersByColumnClass.put(columnClass, renderer);
1095: } else {
1096: defaultRenderersByColumnClass.remove(columnClass);
1097: }
1098: }
1099:
1100: public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
1101: return (TableCellRenderer) getClosestClass(columnClass,
1102: defaultRenderersByColumnClass);
1103: }
1104:
1105: public void setDefaultEditor(Class<?> columnClass,
1106: TableCellEditor editor) {
1107: if (editor != null) {
1108: defaultEditorsByColumnClass.put(columnClass, editor);
1109: } else {
1110: defaultEditorsByColumnClass.remove(columnClass);
1111: }
1112: }
1113:
1114: public TableCellEditor getDefaultEditor(Class<?> columnClass) {
1115: return (TableCellEditor) getClosestClass(columnClass,
1116: defaultEditorsByColumnClass);
1117: }
1118:
1119: public void setDragEnabled(boolean enabled) {
1120: if (enabled && GraphicsEnvironment.isHeadless()) {
1121: throw new HeadlessException();
1122: }
1123: dragEnabled = enabled;
1124: }
1125:
1126: public boolean getDragEnabled() {
1127: return dragEnabled;
1128: }
1129:
1130: public void setSelectionMode(int mode) {
1131: getSelectionModel().setSelectionMode(mode);
1132: getColumnModel().getSelectionModel().setSelectionMode(mode);
1133: }
1134:
1135: public void setRowSelectionAllowed(boolean allowed) {
1136: boolean oldValue = rowSelectionAllowed;
1137: rowSelectionAllowed = allowed;
1138: firePropertyChange(ROW_SELECTION_ALLOWED_PROPERTY, oldValue,
1139: allowed);
1140: }
1141:
1142: public boolean getRowSelectionAllowed() {
1143: return rowSelectionAllowed;
1144: }
1145:
1146: public void setColumnSelectionAllowed(boolean allowed) {
1147: boolean oldValue = getColumnModel().getColumnSelectionAllowed();
1148: getColumnModel().setColumnSelectionAllowed(allowed);
1149: firePropertyChange(COLUMN_SELECTION_ALLOWED_PROPERTY, oldValue,
1150: allowed);
1151: }
1152:
1153: public boolean getColumnSelectionAllowed() {
1154: return getColumnModel().getColumnSelectionAllowed();
1155: }
1156:
1157: public void setCellSelectionEnabled(boolean enabled) {
1158: boolean oldValue = cellSelectionEnabled;
1159: cellSelectionEnabled = enabled;
1160: setRowSelectionAllowed(enabled);
1161: setColumnSelectionAllowed(enabled);
1162: firePropertyChange(CELL_SELECTION_ENABLED_PROPERTY, oldValue,
1163: enabled);
1164: }
1165:
1166: public boolean getCellSelectionEnabled() {
1167: return getRowSelectionAllowed() && getColumnSelectionAllowed();
1168: }
1169:
1170: public void selectAll() {
1171: int rowLead = getSelectionModel().getLeadSelectionIndex();
1172: int rowAnchor = getSelectionModel().getAnchorSelectionIndex();
1173: int columnLead = getColumnModel().getSelectionModel()
1174: .getLeadSelectionIndex();
1175: int columnAnchor = getColumnModel().getSelectionModel()
1176: .getAnchorSelectionIndex();
1177: getSelectionModel().setValueIsAdjusting(true);
1178: getColumnModel().getSelectionModel().setValueIsAdjusting(true);
1179: setRowSelectionInterval(0, getRowCount() - 1);
1180: getSelectionModel().addSelectionInterval(rowAnchor, rowLead);
1181: setColumnSelectionInterval(0, getColumnCount() - 1);
1182: getColumnModel().getSelectionModel().addSelectionInterval(
1183: columnAnchor, columnLead);
1184: getSelectionModel().setValueIsAdjusting(false);
1185: getColumnModel().getSelectionModel().setValueIsAdjusting(false);
1186: }
1187:
1188: public void clearSelection() {
1189: getSelectionModel().clearSelection();
1190: getColumnModel().getSelectionModel().clearSelection();
1191: }
1192:
1193: public void setRowSelectionInterval(int start, int end) {
1194: checkSelectionInterval(start, end, getRowCount());
1195: getSelectionModel().setSelectionInterval(start, end);
1196: }
1197:
1198: public void setColumnSelectionInterval(int start, int end) {
1199: checkSelectionInterval(start, end, getColumnCount());
1200: getColumnModel().getSelectionModel().setSelectionInterval(
1201: start, end);
1202: }
1203:
1204: public void addRowSelectionInterval(int start, int end) {
1205: checkSelectionInterval(start, end, getRowCount());
1206: getSelectionModel().addSelectionInterval(start, end);
1207: }
1208:
1209: public void addColumnSelectionInterval(int start, int end) {
1210: checkSelectionInterval(start, end, getColumnCount());
1211: getColumnModel().getSelectionModel().addSelectionInterval(
1212: start, end);
1213: }
1214:
1215: public void removeRowSelectionInterval(int start, int end) {
1216: checkSelectionInterval(start, end, getRowCount());
1217: getSelectionModel().removeSelectionInterval(start, end);
1218: }
1219:
1220: public void removeColumnSelectionInterval(int start, int end) {
1221: checkSelectionInterval(start, end, getColumnCount());
1222: getColumnModel().getSelectionModel().removeSelectionInterval(
1223: start, end);
1224: }
1225:
1226: public int getSelectedRow() {
1227: return getSelectionModel().getMinSelectionIndex();
1228: }
1229:
1230: public int getSelectedColumn() {
1231: return getColumnModel().getSelectionModel()
1232: .getMinSelectionIndex();
1233: }
1234:
1235: public int[] getSelectedRows() {
1236: return getSelectedIndices(getSelectionModel());
1237: }
1238:
1239: public int[] getSelectedColumns() {
1240: return getSelectedIndices(getColumnModel().getSelectionModel());
1241: }
1242:
1243: public int getSelectedRowCount() {
1244: return getSelectedCount(getSelectionModel());
1245: }
1246:
1247: public int getSelectedColumnCount() {
1248: return getSelectedCount(getColumnModel().getSelectionModel());
1249: }
1250:
1251: public boolean isRowSelected(int row) {
1252: return getSelectionModel().isSelectedIndex(row);
1253: }
1254:
1255: public boolean isColumnSelected(int column) {
1256: return getColumnModel().getSelectionModel().isSelectedIndex(
1257: column);
1258: }
1259:
1260: public boolean isCellSelected(int row, int column) {
1261: return getRowSelectionAllowed()
1262: && isRowSelected(row)
1263: && (!getColumnSelectionAllowed() || isColumnSelected(column))
1264: || getColumnSelectionAllowed()
1265: && isColumnSelected(column)
1266: && (!getRowSelectionAllowed() || isRowSelected(row));
1267: }
1268:
1269: public void changeSelection(int row, int column, boolean toggle,
1270: boolean extend) {
1271: if (!toggle && !extend) {
1272: setRowSelectionInterval(row, row);
1273: setColumnSelectionInterval(column, column);
1274: } else if (!toggle && extend) {
1275: setRowSelectionInterval(getSelectionModel()
1276: .getAnchorSelectionIndex(), row);
1277: setColumnSelectionInterval(getColumnModel()
1278: .getSelectionModel().getAnchorSelectionIndex(),
1279: column);
1280: } else if (toggle && !extend) {
1281: if (isCellSelected(row, column)) {
1282: removeRowSelectionInterval(row, row);
1283: removeColumnSelectionInterval(column, column);
1284: } else {
1285: addRowSelectionInterval(row, row);
1286: addColumnSelectionInterval(column, column);
1287: }
1288: } else {
1289: getSelectionModel().setAnchorSelectionIndex(row);
1290: getColumnModel().getSelectionModel()
1291: .setAnchorSelectionIndex(column);
1292: }
1293: int currentRow = getSelectionModel().getLeadSelectionIndex();
1294: int currentColumn = getColumnModel().getSelectionModel()
1295: .getLeadSelectionIndex();
1296: if (currentRow != -1 && currentColumn != -1) {
1297: scrollRectToVisible(getCellRect(currentRow, currentColumn,
1298: true));
1299: }
1300: }
1301:
1302: public Color getSelectionForeground() {
1303: return selectionForeground;
1304: }
1305:
1306: public void setSelectionForeground(Color fg) {
1307: Color oldValue = selectionForeground;
1308: selectionForeground = fg;
1309: firePropertyChange(SELECTION_FOREGROUND_PROPERTY, oldValue, fg);
1310: }
1311:
1312: public Color getSelectionBackground() {
1313: return selectionBackground;
1314: }
1315:
1316: public void setSelectionBackground(Color bg) {
1317: Color oldValue = selectionBackground;
1318: selectionBackground = bg;
1319: firePropertyChange(SELECTION_BACKGROUND_PROPERTY, oldValue, bg);
1320: }
1321:
1322: public TableColumn getColumn(Object identifier) {
1323: int index = getColumnModel().getColumnIndex(identifier);
1324: if (index == -1) {
1325: throw new IllegalArgumentException(Messages
1326: .getString("swing.39")); //$NON-NLS-1$
1327: }
1328: return getColumnModel().getColumn(index);
1329: }
1330:
1331: public int convertColumnIndexToModel(int viewIndex) {
1332: if (viewIndex < 0) {
1333: return viewIndex;
1334: }
1335: return getColumnModel().getColumn(viewIndex).getModelIndex();
1336: }
1337:
1338: public int convertColumnIndexToView(int modelIndex) {
1339: if (modelIndex < 0) {
1340: return modelIndex;
1341: }
1342: TableColumnModel columnModel = getColumnModel();
1343: for (int i = 0; i < columnModel.getColumnCount(); i++) {
1344: if (columnModel.getColumn(i).getModelIndex() == modelIndex) {
1345: return i;
1346: }
1347: }
1348: return -1;
1349: }
1350:
1351: public int getRowCount() {
1352: return getModel().getRowCount();
1353: }
1354:
1355: public int getColumnCount() {
1356: return getColumnModel().getColumnCount();
1357: }
1358:
1359: public String getColumnName(int viewIndex) {
1360: return getModel().getColumnName(
1361: convertColumnIndexToModel(viewIndex));
1362: }
1363:
1364: public Class<?> getColumnClass(int viewIndex) {
1365: return getModel().getColumnClass(
1366: convertColumnIndexToModel(viewIndex));
1367: }
1368:
1369: public Object getValueAt(int row, int viewColumn) {
1370: return getModel().getValueAt(row,
1371: convertColumnIndexToModel(viewColumn));
1372: }
1373:
1374: public void setValueAt(Object value, int row, int viewColumn) {
1375: getModel().setValueAt(value, row,
1376: convertColumnIndexToModel(viewColumn));
1377: }
1378:
1379: public boolean isCellEditable(int row, int viewColumn) {
1380: return getModel().isCellEditable(row,
1381: convertColumnIndexToModel(viewColumn));
1382: }
1383:
1384: public void addColumn(TableColumn column) {
1385: if (column.getHeaderValue() == null) {
1386: column.setHeaderValue(getModel().getColumnName(
1387: column.getModelIndex()));
1388: }
1389: getColumnModel().addColumn(column);
1390: }
1391:
1392: public void removeColumn(TableColumn column) {
1393: getColumnModel().removeColumn(column);
1394: }
1395:
1396: public void moveColumn(int viewColumn, int targetViewColumn) {
1397: getColumnModel().moveColumn(viewColumn, targetViewColumn);
1398: }
1399:
1400: public int columnAtPoint(Point p) {
1401: int x = p.x;
1402: if (!getComponentOrientation().isLeftToRight()) {
1403: x = getWidth() - x;
1404: }
1405: return getColumnModel().getColumnIndexAtX(x);
1406: }
1407:
1408: public int rowAtPoint(Point p) {
1409: int previousWidth = 0;
1410: for (int i = 0; i < getRowCount(); i++) {
1411: int height = getRowHeight(i);
1412: if (p.y >= previousWidth && p.y < previousWidth + height) {
1413: return i;
1414: }
1415: previousWidth += height;
1416: }
1417: return -1;
1418: }
1419:
1420: public Rectangle getCellRect(int row, int viewColumn,
1421: boolean includeSpacing) {
1422: Rectangle result = new Rectangle();
1423: boolean useSpacing = includeSpacing;
1424: if (row >= 0 && row < getRowCount()) {
1425: for (int i = 0; i < row; i++) {
1426: result.y += getRowHeight(i);
1427: }
1428: result.height = getRowHeight(row);
1429: } else {
1430: useSpacing = true;
1431: }
1432: if (viewColumn >= 0 && viewColumn < getColumnCount()) {
1433: TableColumnModel columnModel = getColumnModel();
1434: if (getComponentOrientation().isLeftToRight()) {
1435: for (int i = 0; i < viewColumn; i++) {
1436: result.x += columnModel.getColumn(i).getWidth();
1437: }
1438: } else {
1439: for (int i = 0; i < columnModel.getColumnCount(); i++) {
1440: result.x += columnModel.getColumn(i).getWidth();
1441: }
1442: for (int i = 0; i <= viewColumn; i++) {
1443: result.x -= columnModel.getColumn(i).getWidth();
1444: }
1445: }
1446: result.width = columnModel.getColumn(viewColumn).getWidth();
1447: } else {
1448: useSpacing = true;
1449: }
1450: if (!useSpacing) {
1451: Dimension spacing = getIntercellSpacing();
1452: result.x += spacing.width / 2;
1453: result.width -= spacing.width;
1454: result.y += spacing.height / 2;
1455: result.height -= spacing.height;
1456: }
1457: return result;
1458: }
1459:
1460: public void sizeColumnsToFit(boolean lastColumnOnly) {
1461: int previousSetting = getAutoResizeMode();
1462: if (lastColumnOnly) {
1463: setAutoResizeMode(AUTO_RESIZE_LAST_COLUMN);
1464: } else {
1465: setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
1466: }
1467: doLayout();
1468: setAutoResizeMode(previousSetting);
1469: }
1470:
1471: public void sizeColumnsToFit(int resizingColumn) {
1472: }
1473:
1474: @Override
1475: public String getToolTipText(MouseEvent me) {
1476: int row = rowAtPoint(me.getPoint());
1477: int column = columnAtPoint(me.getPoint());
1478: if (row == -1 || column == -1) {
1479: return null;
1480: }
1481: TableCellRenderer renderer = getCellRenderer(row, column);
1482: if (renderer == null) {
1483: return null;
1484: }
1485: Component renderingComponent = renderer
1486: .getTableCellRendererComponent(this , getValueAt(row,
1487: column), isCellSelected(row, column), false,
1488: row, column);
1489: return renderer instanceof JComponent ? ((JComponent) renderingComponent)
1490: .getToolTipText()
1491: : null;
1492: }
1493:
1494: public void setSurrendersFocusOnKeystroke(
1495: boolean surrendersFocusOnKeystroke) {
1496: this .surrendersFocusOnKeystroke = surrendersFocusOnKeystroke;
1497: }
1498:
1499: public boolean getSurrendersFocusOnKeystroke() {
1500: return surrendersFocusOnKeystroke;
1501: }
1502:
1503: public boolean editCellAt(int row, int column) {
1504: return editCellAt(row, column, null);
1505: }
1506:
1507: public boolean editCellAt(int row, int viewColumn, EventObject e) {
1508: if (isEditing()) {
1509: getCellEditor().stopCellEditing();
1510: if (isEditing()) {
1511: return false;
1512: }
1513: }
1514: if (row >= getRowCount()
1515: || viewColumn > getColumnModel().getColumnCount()) {
1516: return false;
1517: }
1518: if (!isCellEditable(row, viewColumn)) {
1519: return false;
1520: }
1521: TableCellEditor editor = getCellEditor(row, viewColumn);
1522: if (editor == null) {
1523: return false;
1524: }
1525: editingColumn = viewColumn;
1526: if (!editor.isCellEditable(e)) {
1527: editingColumn = -1;
1528: return false;
1529: }
1530: setCellEditor(editor);
1531: setEditingRow(row);
1532: setEditingColumn(viewColumn);
1533: editorComp = prepareEditor(getCellEditor(), row, viewColumn);
1534: getCellEditor().addCellEditorListener(this );
1535: add(editorComp);
1536: editorComp.setBounds(getCellRect(row, viewColumn, false));
1537: return true;
1538: }
1539:
1540: public boolean isEditing() {
1541: return getCellEditor() != null;
1542: }
1543:
1544: public Component getEditorComponent() {
1545: return editorComp;
1546: }
1547:
1548: public int getEditingRow() {
1549: return editingRow;
1550: }
1551:
1552: public void setEditingRow(int row) {
1553: editingRow = row;
1554: }
1555:
1556: public int getEditingColumn() {
1557: return editingColumn;
1558: }
1559:
1560: public void setEditingColumn(int column) {
1561: editingColumn = column;
1562: }
1563:
1564: public TableUI getUI() {
1565: return (TableUI) ui;
1566: }
1567:
1568: public void setUI(TableUI ui) {
1569: super .setUI(ui);
1570: }
1571:
1572: @Override
1573: public void updateUI() {
1574: setUI((TableUI) UIManager.getUI(this ));
1575: }
1576:
1577: @Override
1578: public String getUIClassID() {
1579: return UI_CLASS_ID;
1580: }
1581:
1582: public void setModel(TableModel model) {
1583: if (model == null) {
1584: throw new IllegalArgumentException(Messages
1585: .getString("swing.3A")); //$NON-NLS-1$
1586: }
1587: TableModel oldValue = dataModel;
1588: if (oldValue != null) {
1589: oldValue.removeTableModelListener(this );
1590: }
1591: dataModel = model;
1592: dataModel.addTableModelListener(this );
1593: firePropertyChange(StringConstants.MODEL_PROPERTY_CHANGED,
1594: oldValue, model);
1595: if (oldValue != dataModel) {
1596: tableChanged(new TableModelEvent(dataModel,
1597: TableModelEvent.HEADER_ROW,
1598: TableModelEvent.HEADER_ROW,
1599: TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
1600: }
1601: }
1602:
1603: public TableModel getModel() {
1604: return dataModel;
1605: }
1606:
1607: public void setColumnModel(TableColumnModel model) {
1608: if (model == null) {
1609: throw new IllegalArgumentException(Messages
1610: .getString("swing.3B")); //$NON-NLS-1$
1611: }
1612: TableColumnModel oldValue = columnModel;
1613: if (oldValue != null) {
1614: oldValue.removeColumnModelListener(this );
1615: oldValue.removeColumnModelListener(getTableHeader());
1616: }
1617: columnModel = model;
1618: JTableHeader header = getTableHeader();
1619: if (header != null) {
1620: columnModel.addColumnModelListener(header);
1621: }
1622: columnModel.addColumnModelListener(this );
1623: }
1624:
1625: public TableColumnModel getColumnModel() {
1626: return columnModel;
1627: }
1628:
1629: public void setSelectionModel(ListSelectionModel model) {
1630: if (model == null) {
1631: throw new IllegalArgumentException(Messages
1632: .getString("swing.17")); //$NON-NLS-1$
1633: }
1634: ListSelectionModel oldValue = selectionModel;
1635: if (oldValue != null) {
1636: oldValue.removeListSelectionListener(this );
1637: }
1638: selectionModel = model;
1639: selectionModel.addListSelectionListener(this );
1640: alignSelectionModelToRows();
1641: firePropertyChange(StringConstants.SELECTION_MODEL_PROPERTY,
1642: oldValue, model);
1643: }
1644:
1645: public ListSelectionModel getSelectionModel() {
1646: return selectionModel;
1647: }
1648:
1649: public void tableChanged(TableModelEvent e) {
1650: if (e.getType() == TableModelEvent.UPDATE
1651: && e.getFirstRow() == TableModelEvent.HEADER_ROW
1652: && e.getLastRow() == TableModelEvent.HEADER_ROW) {
1653: if (getAutoCreateColumnsFromModel()) {
1654: createDefaultColumnsFromModel();
1655: }
1656: }
1657: if (getSelectionModel() != null) {
1658: updateSelectionModel(getSelectionModel(), e);
1659: }
1660: if (getColumnModel().getSelectionModel() != null) {
1661: updateColumnSelectionModel(getColumnModel()
1662: .getSelectionModel(), e);
1663: }
1664: revalidate();
1665: repaint();
1666: }
1667:
1668: public void columnAdded(TableColumnModelEvent e) {
1669: if (isEditing()) {
1670: getCellEditor().cancelCellEditing();
1671: }
1672: resizeAndRepaint();
1673: }
1674:
1675: public void columnRemoved(TableColumnModelEvent e) {
1676: if (isEditing()) {
1677: getCellEditor().cancelCellEditing();
1678: }
1679: resizeAndRepaint();
1680: }
1681:
1682: public void columnMoved(TableColumnModelEvent e) {
1683: if (isEditing()) {
1684: getCellEditor().cancelCellEditing();
1685: }
1686: repaint();
1687: }
1688:
1689: public void columnMarginChanged(ChangeEvent e) {
1690: if (isEditing()) {
1691: getCellEditor().cancelCellEditing();
1692: }
1693: resizeAndRepaint();
1694: }
1695:
1696: public void columnSelectionChanged(ListSelectionEvent e) {
1697: repaint();
1698: }
1699:
1700: public void valueChanged(ListSelectionEvent e) {
1701: repaint();
1702: }
1703:
1704: public void editingStopped(ChangeEvent e) {
1705: if (isEditing()) {
1706: setValueAt(getCellEditor().getCellEditorValue(),
1707: getEditingRow(), getEditingColumn());
1708: cleanUpAfterEditing();
1709: }
1710: }
1711:
1712: public void editingCanceled(ChangeEvent e) {
1713: if (isEditing()) {
1714: cleanUpAfterEditing();
1715: }
1716: }
1717:
1718: public void setPreferredScrollableViewportSize(Dimension size) {
1719: preferredViewportSize = size;
1720: }
1721:
1722: public Dimension getPreferredScrollableViewportSize() {
1723: return preferredViewportSize;
1724: }
1725:
1726: public int getScrollableUnitIncrement(Rectangle visibleRect,
1727: int orientation, int direction) {
1728: if (orientation == SwingConstants.HORIZONTAL) {
1729: return 100;
1730: }
1731: return getRowHeight();
1732: }
1733:
1734: public int getScrollableBlockIncrement(Rectangle visibleRect,
1735: int orientation, int direction) {
1736: if (orientation == SwingConstants.HORIZONTAL) {
1737: return visibleRect.width;
1738: }
1739: return visibleRect.height;
1740: }
1741:
1742: public boolean getScrollableTracksViewportWidth() {
1743: return getAutoResizeMode() != AUTO_RESIZE_OFF;
1744: }
1745:
1746: public boolean getScrollableTracksViewportHeight() {
1747: return false;
1748: }
1749:
1750: public void setCellEditor(TableCellEditor editor) {
1751: cellEditor = editor;
1752: }
1753:
1754: public TableCellEditor getCellEditor() {
1755: return cellEditor;
1756: }
1757:
1758: public TableCellRenderer getCellRenderer(int row, int viewColumn) {
1759: TableCellRenderer result = getColumnModel().getColumn(
1760: viewColumn).getCellRenderer();
1761: if (result == null) {
1762: result = getDefaultRenderer(getColumnClass(viewColumn));
1763: }
1764: return result;
1765: }
1766:
1767: public Component prepareRenderer(TableCellRenderer renderer,
1768: int row, int viewColumn) {
1769: boolean hasFocus = false;
1770: return renderer.getTableCellRendererComponent(this , getValueAt(
1771: row, viewColumn), isCellSelected(row, viewColumn),
1772: hasFocus, row, viewColumn);
1773: }
1774:
1775: public TableCellEditor getCellEditor(int row, int viewColumn) {
1776: TableCellEditor result = getColumnModel().getColumn(viewColumn)
1777: .getCellEditor();
1778: if (result == null) {
1779: result = getDefaultEditor(getColumnClass(viewColumn));
1780: }
1781: return result;
1782: }
1783:
1784: public Component prepareEditor(TableCellEditor editor, int row,
1785: int viewColumn) {
1786: return editor.getTableCellEditorComponent(this , getValueAt(row,
1787: viewColumn), isCellSelected(row, viewColumn), row,
1788: viewColumn);
1789: }
1790:
1791: public void removeEditor() {
1792: if (!isEditing()) {
1793: return;
1794: }
1795: setCellEditor(null);
1796: remove(getEditorComponent());
1797: editorComp = null;
1798: setEditingRow(-1);
1799: setEditingColumn(-1);
1800: }
1801:
1802: @Override
1803: public void doLayout() {
1804: if (getAutoResizeMode() == AUTO_RESIZE_OFF) {
1805: return;
1806: }
1807: TableColumn resizingColumn = (getTableHeader() == null) ? null
1808: : getTableHeader().getResizingColumn();
1809: if (resizingColumn == null) {
1810: ResizableElements resizable = new ResizableElements() {
1811: public int getElementsCount() {
1812: return getColumnCount();
1813: }
1814:
1815: public TableColumn getElement(int i) {
1816: return getColumnModel().getColumn(i);
1817: }
1818: };
1819: adjustColumns(getWidth(), resizable);
1820: } else {
1821: int resizingColIndex = getColumnModel().getColumnIndex(
1822: resizingColumn.getIdentifier());
1823: if (resizingColIndex + 1 == getColumnCount()) {
1824: int remWidth = getWidth();
1825: for (int i = 0; i < getColumnCount() - 1; i++) {
1826: remWidth -= getColumnModel().getColumn(i)
1827: .getWidth();
1828: }
1829: resizingColumn.setWidth(remWidth);
1830: return;
1831: }
1832: if (getAutoResizeMode() == AUTO_RESIZE_NEXT_COLUMN) {
1833: autoResizeNextColumn(resizingColumn);
1834: return;
1835: }
1836: if (getAutoResizeMode() == AUTO_RESIZE_LAST_COLUMN) {
1837: autoResizeLastColumn(resizingColumn);
1838: return;
1839: }
1840: if (getAutoResizeMode() == AUTO_RESIZE_SUBSEQUENT_COLUMNS) {
1841: autoResizeSubsequentColumns(resizingColumn);
1842: return;
1843: }
1844: if (getAutoResizeMode() == AUTO_RESIZE_ALL_COLUMNS) {
1845: ResizableElements resizable = new ResizableElements() {
1846: public int getElementsCount() {
1847: return getColumnCount();
1848: }
1849:
1850: public TableColumn getElement(int i) {
1851: return getColumnModel().getColumn(i);
1852: }
1853: };
1854: adjustColumns(getWidth(), resizable);
1855: }
1856: }
1857: }
1858:
1859: @Override
1860: public AccessibleContext getAccessibleContext() {
1861: if (accessibleContext == null) {
1862: accessibleContext = new AccessibleJTable();
1863: }
1864: return accessibleContext;
1865: }
1866:
1867: public boolean print() throws PrinterException,
1868: NotImplementedException {
1869: throw new NotImplementedException();
1870: }
1871:
1872: public boolean print(PrintMode printMode) throws PrinterException,
1873: NotImplementedException {
1874: throw new NotImplementedException();
1875: }
1876:
1877: public boolean print(PrintMode printMode,
1878: MessageFormat headerFormat, MessageFormat footerFormat)
1879: throws PrinterException, NotImplementedException {
1880: throw new NotImplementedException();
1881: }
1882:
1883: public boolean print(PrintMode printMode,
1884: MessageFormat headerFormat, MessageFormat footerFormat,
1885: boolean showPrintDialog, PrintRequestAttributeSet attr,
1886: boolean interactive) throws PrinterException,
1887: HeadlessException, NotImplementedException {
1888: throw new NotImplementedException();
1889: }
1890:
1891: public Printable getPrintable(PrintMode printMode,
1892: MessageFormat headerFormat, MessageFormat footerFormat)
1893: throws NotImplementedException {
1894: throw new NotImplementedException();
1895: }
1896:
1897: public static enum PrintMode {
1898: NORMAL, FIT_WIDTH
1899: }
1900:
1901: protected void initializeLocalVars() {
1902: dragEnabled = false;
1903: setRowMargin(1);
1904: setTableHeader(createDefaultTableHeader());
1905: setRowHeight(16);
1906: setShowHorizontalLines(true);
1907: setShowVerticalLines(true);
1908: setAutoResizeMode(AUTO_RESIZE_SUBSEQUENT_COLUMNS);
1909: setPreferredScrollableViewportSize(new Dimension(450, 400));
1910: setRowSelectionAllowed(true);
1911: createDefaultRenderers();
1912: createDefaultEditors();
1913: ToolTipManager.sharedInstance().registerComponent(this );
1914: }
1915:
1916: protected void configureEnclosingScrollPane() {
1917: JScrollPane enclosingScrollPane = getEnclosingScrollPane();
1918: if (enclosingScrollPane == null) {
1919: return;
1920: }
1921: enclosingScrollPane.setColumnHeaderView(getTableHeader());
1922: enclosingScrollPane.setBorder(UIManager
1923: .getBorder("Table.scrollPaneBorder"));
1924: }
1925:
1926: protected void unconfigureEnclosingScrollPane() {
1927: JScrollPane enclosingScrollPane = getEnclosingScrollPane();
1928: if (enclosingScrollPane == null) {
1929: return;
1930: }
1931: enclosingScrollPane.setColumnHeaderView(null);
1932: enclosingScrollPane.setBorder(null);
1933: }
1934:
1935: protected void createDefaultRenderers() {
1936: defaultRenderersByColumnClass = new Hashtable();
1937: defaultRenderersByColumnClass.put(Date.class,
1938: new DateTableCellRenderer());
1939: defaultRenderersByColumnClass.put(ImageIcon.class,
1940: new IconTableCellRenderer());
1941: defaultRenderersByColumnClass.put(Icon.class,
1942: new IconTableCellRenderer());
1943: defaultRenderersByColumnClass.put(Float.class,
1944: new NumberTableCellRenderer());
1945: defaultRenderersByColumnClass.put(Double.class,
1946: new NumberTableCellRenderer());
1947: defaultRenderersByColumnClass.put(Number.class,
1948: new NumberTableCellRenderer());
1949: defaultRenderersByColumnClass.put(Boolean.class,
1950: new BooleanTableCellRenderer());
1951: defaultRenderersByColumnClass.put(Object.class,
1952: new DefaultTableCellRenderer());
1953: }
1954:
1955: protected void createDefaultEditors() {
1956: defaultEditorsByColumnClass = new Hashtable();
1957: defaultEditorsByColumnClass.put(Number.class,
1958: new NumberEditor());
1959: defaultEditorsByColumnClass.put(Boolean.class,
1960: new BooleanEditor());
1961: defaultEditorsByColumnClass.put(Object.class,
1962: new ObjectEditor());
1963: }
1964:
1965: protected TableModel createDefaultDataModel() {
1966: return new DefaultTableModel();
1967: }
1968:
1969: protected TableColumnModel createDefaultColumnModel() {
1970: return new DefaultTableColumnModel();
1971: }
1972:
1973: protected ListSelectionModel createDefaultSelectionModel() {
1974: return new DefaultListSelectionModel();
1975: }
1976:
1977: protected JTableHeader createDefaultTableHeader() {
1978: return new JTableHeader(getColumnModel());
1979: }
1980:
1981: protected void resizeAndRepaint() {
1982: revalidate();
1983: repaint();
1984: }
1985:
1986: @Override
1987: protected void processKeyEvent(KeyEvent event) {
1988: super .processKeyEvent(event);
1989: if (event.isConsumed()) {
1990: wasConsumed = true;
1991: }
1992: if (event.getID() == KeyEvent.KEY_RELEASED) {
1993: wasConsumed = false;
1994: return;
1995: }
1996: if (wasConsumed) {
1997: return;
1998: }
1999: if (event.getKeyCode() == KeyEvent.VK_SHIFT
2000: || event.getKeyCode() == KeyEvent.VK_ALT
2001: || event.getKeyCode() == KeyEvent.VK_ALT_GRAPH
2002: || event.getKeyCode() == KeyEvent.VK_CONTROL
2003: || event.getKeyCode() == KeyEvent.VK_PRINTSCREEN
2004: || event.getKeyCode() == KeyEvent.VK_CAPS_LOCK
2005: || event.getKeyCode() == KeyEvent.VK_NUM_LOCK
2006: || event.getKeyCode() == KeyEvent.VK_SCROLL_LOCK
2007: || event.isAltDown() || event.isControlDown()) {
2008: return;
2009: }
2010: if (!isEditing()) {
2011: int currentRow = getSelectionModel()
2012: .getLeadSelectionIndex();
2013: int currentColumn = getColumnModel().getSelectionModel()
2014: .getLeadSelectionIndex();
2015: if (currentRow == -1 || currentColumn == -1) {
2016: return;
2017: }
2018: if (!editCellAt(currentRow, currentColumn, event)) {
2019: return;
2020: }
2021: if (isEditing() && getSurrendersFocusOnKeystroke()) {
2022: getEditorComponent().requestFocus();
2023: }
2024: }
2025: if (isEditing() && getEditorComponent() instanceof JComponent) {
2026: KeyEvent editorEvent = new KeyEvent(getEditorComponent(),
2027: event.getID(), event.getWhen(), event
2028: .getModifiers(), event.getKeyCode(), event
2029: .getKeyChar(), event.getKeyLocation());
2030: ((JComponent) getEditorComponent())
2031: .processKeyEvent(editorEvent);
2032: }
2033: }
2034:
2035: private JScrollPane getEnclosingScrollPane() {
2036: if (getParent() instanceof JViewport
2037: && ((JViewport) getParent()).getView() == this ) {
2038: if (getParent().getParent() instanceof JScrollPane) {
2039: return (JScrollPane) getParent().getParent();
2040: }
2041: }
2042: return null;
2043: }
2044:
2045: private Object getClosestClass(Class<?> columnClass,
2046: Hashtable classes) {
2047: Class<?> currentClass = columnClass;
2048: do {
2049: Object value = classes.get(currentClass);
2050: if (value != null) {
2051: return value;
2052: }
2053: currentClass = currentClass.getSuperclass();
2054: } while (currentClass != null);
2055: return null;
2056: }
2057:
2058: private void checkSelectionInterval(int start, int end, int bound) {
2059: if (start < 0 || end < 0 || start >= bound || end >= bound) {
2060: throw new IllegalArgumentException(Messages.getString(
2061: "swing.31", (bound - 1))); //$NON-NLS-1$
2062: }
2063: }
2064:
2065: private int[] getSelectedIndices(ListSelectionModel selModel) {
2066: int size = getSelectedCount(selModel);
2067: int[] result = new int[size];
2068: if (size == 0) {
2069: return result;
2070: }
2071: int count = 0;
2072: for (int i = selModel.getMinSelectionIndex(); i <= selModel
2073: .getMaxSelectionIndex(); i++) {
2074: if (selModel.isSelectedIndex(i)) {
2075: result[count++] = i;
2076: }
2077: }
2078: return result;
2079: }
2080:
2081: private int getSelectedCount(ListSelectionModel selModel) {
2082: if (selModel.isSelectionEmpty()) {
2083: return 0;
2084: }
2085: int result = 0;
2086: for (int i = selModel.getMinSelectionIndex(); i <= selModel
2087: .getMaxSelectionIndex(); i++) {
2088: if (selModel.isSelectedIndex(i)) {
2089: result++;
2090: }
2091: }
2092: return result;
2093: }
2094:
2095: private void updateSelectionModel(ListSelectionModel model,
2096: TableModelEvent e) {
2097: if (e.getType() == TableModelEvent.INSERT) {
2098: model.insertIndexInterval(e.getFirstRow(), e.getLastRow()
2099: - e.getFirstRow() + 1, true);
2100: alignSelectionModelToRows();
2101: }
2102: if (e.getType() == TableModelEvent.DELETE) {
2103: model.removeIndexInterval(e.getFirstRow(), e.getLastRow());
2104: alignSelectionModelToRows();
2105: }
2106: if (e.getType() == TableModelEvent.UPDATE
2107: && e.getColumn() == TableModelEvent.ALL_COLUMNS) {
2108: model.clearSelection();
2109: }
2110: }
2111:
2112: private void updateColumnSelectionModel(ListSelectionModel model,
2113: TableModelEvent e) {
2114: if (e.getType() == TableModelEvent.UPDATE
2115: && e.getFirstRow() == TableModelEvent.HEADER_ROW
2116: && e.getLastRow() == TableModelEvent.HEADER_ROW) {
2117: model.setAnchorSelectionIndex(-1);
2118: model.setLeadSelectionIndex(0);
2119: model.clearSelection();
2120: }
2121: }
2122:
2123: private void autoResizeSubsequentColumns(TableColumn resizingColumn) {
2124: final int resizingColIndex = getColumnModel().getColumnIndex(
2125: resizingColumn.getIdentifier());
2126: ResizableElements resizable = new ResizableElements() {
2127: public int getElementsCount() {
2128: return getColumnCount() - (resizingColIndex + 1);
2129: }
2130:
2131: public TableColumn getElement(int i) {
2132: return getColumnModel().getColumn(
2133: i + (resizingColIndex + 1));
2134: }
2135: };
2136: int width = 0;
2137: for (int i = 0; i <= resizingColIndex; i++) {
2138: width += getColumnModel().getColumn(i).getWidth();
2139: }
2140: int minSize = 0;
2141: for (int i = resizingColIndex + 1; i < getColumnCount(); i++) {
2142: minSize += getColumnModel().getColumn(i).getMinWidth();
2143: }
2144: if (getWidth() - width > minSize) {
2145: adjustColumns(getWidth() - width, resizable);
2146: } else {
2147: width = 0;
2148: for (int i = 0; i < resizingColIndex; i++) {
2149: width += getColumnModel().getColumn(i).getWidth();
2150: }
2151: for (int i = resizingColIndex + 1; i < getColumnCount(); i++) {
2152: getColumnModel().getColumn(i).setWidth(
2153: getColumnModel().getColumn(i).getMinWidth());
2154: }
2155: resizingColumn.setWidth(getWidth() - width - minSize);
2156: }
2157: }
2158:
2159: private void autoResizeNextColumn(TableColumn resizingColumn) {
2160: int resColIndex = getColumnModel().getColumnIndex(
2161: resizingColumn.getIdentifier());
2162: TableColumn nextColumn = getColumnModel().getColumn(
2163: resColIndex + 1);
2164: int colsSumWidth = getWidth();
2165: for (int i = 0; i < getColumnCount(); i++) {
2166: if (i != resColIndex && i != resColIndex + 1) {
2167: colsSumWidth -= getColumnModel().getColumn(i)
2168: .getWidth();
2169: }
2170: }
2171: if (resizingColumn.getWidth() + nextColumn.getMinWidth() < colsSumWidth) {
2172: nextColumn.setWidth(colsSumWidth
2173: - resizingColumn.getWidth());
2174: } else {
2175: int resizingColWidth = colsSumWidth
2176: - nextColumn.getMinWidth();
2177: resizingColumn.setWidth(resizingColWidth);
2178: nextColumn.setWidth(nextColumn.getMinWidth());
2179: }
2180: }
2181:
2182: private void autoResizeLastColumn(TableColumn resizingColumn) {
2183: int resColIndex = getColumnModel().getColumnIndex(
2184: resizingColumn.getIdentifier());
2185: TableColumn lastColumn = getColumnModel().getColumn(
2186: getColumnCount() - 1);
2187: int colsSumWidth = getWidth();
2188: for (int i = 0; i < getColumnCount(); i++) {
2189: if (i != resColIndex && i != getColumnCount() - 1) {
2190: colsSumWidth -= getColumnModel().getColumn(i)
2191: .getWidth();
2192: }
2193: }
2194: if (resizingColumn.getWidth() + lastColumn.getMinWidth() < colsSumWidth) {
2195: lastColumn.setWidth(colsSumWidth
2196: - resizingColumn.getWidth());
2197: } else {
2198: int resizingColWidth = colsSumWidth
2199: - lastColumn.getMinWidth();
2200: resizingColumn.setWidth(resizingColWidth);
2201: lastColumn.setWidth(lastColumn.getMinWidth());
2202: }
2203: }
2204:
2205: private void adjustColumns(long targetSize,
2206: ResizableElements resizable) {
2207: if (resizable.getElementsCount() == 0) {
2208: return;
2209: }
2210: long minColsWidth = 0;
2211: long maxColsWidth = 0;
2212: long colsWidth = 0;
2213: for (int i = 0; i < resizable.getElementsCount(); i++) {
2214: TableColumn column = resizable.getElement(i);
2215: minColsWidth += column.getMinWidth();
2216: maxColsWidth += column.getMaxWidth();
2217: colsWidth += column.getPreferredWidth();
2218: }
2219: long colsDelta = targetSize - colsWidth;
2220: int[] newWidthes = new int[resizable.getElementsCount()];
2221: int newTableWidth = 0;
2222: for (int i = 0; i < resizable.getElementsCount(); i++) {
2223: TableColumn column = resizable.getElement(i);
2224: int maxWidth = column.getMaxWidth();
2225: int minWidth = column.getMinWidth();
2226: int curWidth = column.getPreferredWidth();
2227: double multiplier = (colsDelta > 0) ? (double) (maxWidth - curWidth)
2228: / (double) (maxColsWidth - colsWidth)
2229: : (double) (curWidth - minWidth)
2230: / (double) (colsWidth - minColsWidth);
2231: int delta = (int) (colsDelta * multiplier);
2232: int newWidth = curWidth + delta;
2233: if (newWidth > maxWidth) {
2234: newWidth = maxWidth;
2235: }
2236: if (newWidth < minWidth) {
2237: newWidth = minWidth;
2238: }
2239: newWidthes[i] = newWidth;
2240: newTableWidth += newWidthes[i];
2241: }
2242: int diff = (int) targetSize - newTableWidth;
2243: int absDiff = Math.abs(diff);
2244: while (absDiff != 0) {
2245: if (diff > 0) {
2246: adjustNewWidthesToIncreaseSize(newWidthes);
2247: } else {
2248: adjustNewWidthesToDecreaseSize(newWidthes);
2249: }
2250: absDiff--;
2251: }
2252: for (int i = 0; i < resizable.getElementsCount(); i++) {
2253: resizable.getElement(i).setWidth(newWidthes[i]);
2254: }
2255: }
2256:
2257: private void cleanUpAfterEditing() {
2258: getCellEditor().removeCellEditorListener(this );
2259: removeEditor();
2260: repaint(getCellRect(getEditingRow(), getEditingColumn(), false));
2261: requestFocus();
2262: }
2263:
2264: private void adjustNewWidthesToDecreaseSize(int[] widths) {
2265: int result = widths.length - 1;
2266: int max = widths[widths.length - 1];
2267: for (int i = widths.length - 1; i >= 0; i--) {
2268: if (widths[i] > max) {
2269: result = i;
2270: }
2271: }
2272: widths[result]--;
2273: }
2274:
2275: private void adjustNewWidthesToIncreaseSize(int[] widths) {
2276: int result = widths.length - 1;
2277: int min = widths[widths.length - 1];
2278: for (int i = widths.length - 1; i >= 0; i--) {
2279: if (widths[i] < min) {
2280: result = i;
2281: }
2282: }
2283: widths[result]++;
2284: }
2285:
2286: private void alignSelectionModelToRows() {
2287: if (getRowCount() == 0) {
2288: if (selectionModel.getAnchorSelectionIndex() >= 0) {
2289: selectionModel.setValueIsAdjusting(true);
2290: selectionModel.setAnchorSelectionIndex(-1);
2291: selectionModel.setLeadSelectionIndex(-1);
2292: selectionModel.setValueIsAdjusting(false);
2293: }
2294: } else if (selectionModel.getLeadSelectionIndex() < 0) {
2295: selectionModel.removeSelectionInterval(0, 0);
2296: }
2297: }
2298:
2299: private interface ResizableElements {
2300: int getElementsCount();
2301:
2302: TableColumn getElement(int i);
2303: }
2304:
2305: private class MutableInteger {
2306: private int value = -1;
2307:
2308: public void setValue(int value) {
2309: this .value = value;
2310: }
2311:
2312: public int getValue() {
2313: return value;
2314: }
2315: }
2316:
2317: private class BooleanTableCellRenderer extends JCheckBox implements
2318: TableCellRenderer {
2319: private static final long serialVersionUID = 1L;
2320:
2321: private final Border noFocusBorder = BorderFactory
2322: .createEmptyBorder(1, 1, 1, 1);
2323:
2324: private Border focusBorder;
2325:
2326: private Color focusCellBackground;
2327:
2328: private Color focusCellForeground;
2329:
2330: public BooleanTableCellRenderer() {
2331: updateUI();
2332: }
2333:
2334: @Override
2335: public void updateUI() {
2336: super .updateUI();
2337: focusBorder = UIManager
2338: .getBorder("Table.focusCellHighlightBorder");
2339: focusCellBackground = UIManager
2340: .getColor("Table.focusCellBackground");
2341: focusCellForeground = UIManager
2342: .getColor("Table.focusCellForeground");
2343: }
2344:
2345: public Component getTableCellRendererComponent(JTable table,
2346: Object value, boolean isSelected, boolean hasFocus,
2347: int row, int column) {
2348: setValue(value);
2349: setFont(table.getFont());
2350: setHorizontalAlignment(SwingConstants.CENTER);
2351: if (hasFocus) {
2352: setBorder(focusBorder);
2353: if (isSelected) {
2354: setBackground(table.getSelectionBackground());
2355: setForeground(table.getSelectionForeground());
2356: } else if (table.isCellEditable(row, column)) {
2357: setBackground(focusCellBackground);
2358: setForeground(focusCellForeground);
2359: } else {
2360: setBackground(table.getBackground());
2361: setForeground(table.getForeground());
2362: }
2363: } else {
2364: setBorder(noFocusBorder);
2365: if (isSelected) {
2366: setBackground(table.getSelectionBackground());
2367: setForeground(table.getSelectionForeground());
2368: } else {
2369: setBackground(table.getBackground());
2370: setForeground(table.getForeground());
2371: }
2372: }
2373: return this ;
2374: }
2375:
2376: @Override
2377: public boolean isOpaque() {
2378: return true;
2379: }
2380:
2381: private void setValue(Object value) {
2382: if (value == null) {
2383: setSelected(false);
2384: } else {
2385: setSelected(Boolean.valueOf(value.toString())
2386: .booleanValue());
2387: }
2388: }
2389: }
2390:
2391: private class DateTableCellRenderer extends
2392: DefaultTableCellRenderer.UIResource {
2393: private static final long serialVersionUID = 1L;
2394:
2395: @Override
2396: public Component getTableCellRendererComponent(JTable table,
2397: Object value, boolean isSelected, boolean hasFocus,
2398: int row, int column) {
2399: return super .getTableCellRendererComponent(table,
2400: value != null ? DateFormat.getDateInstance()
2401: .format((Date) value) : null, isSelected,
2402: hasFocus, row, column);
2403: }
2404: }
2405:
2406: private class NumberTableCellRenderer extends
2407: DefaultTableCellRenderer.UIResource {
2408: private static final long serialVersionUID = 1L;
2409:
2410: public NumberTableCellRenderer() {
2411: super ();
2412: setHorizontalAlignment(SwingConstants.RIGHT);
2413: }
2414:
2415: @Override
2416: public Component getTableCellRendererComponent(JTable table,
2417: Object value, boolean isSelected, boolean hasFocus,
2418: int row, int column) {
2419: return super .getTableCellRendererComponent(table,
2420: value != null ? NumberFormat.getNumberInstance()
2421: .format(value) : null, isSelected,
2422: hasFocus, row, column);
2423: }
2424: }
2425:
2426: private class IconTableCellRenderer extends
2427: DefaultTableCellRenderer {
2428: private static final long serialVersionUID = 1L;
2429:
2430: @Override
2431: public Component getTableCellRendererComponent(JTable table,
2432: Object value, boolean isSelected, boolean hasFocus,
2433: int row, int column) {
2434: JLabel result = (JLabel) super
2435: .getTableCellRendererComponent(table, null,
2436: isSelected, hasFocus, row, column);
2437: if (value != null) {
2438: if (value instanceof Icon) {
2439: result.setIcon((Icon) value);
2440: } else {
2441: result.setIcon(new ImageIcon(value.toString()));
2442: }
2443: result.setHorizontalAlignment(SwingConstants.CENTER);
2444: result.setVerticalAlignment(SwingConstants.CENTER);
2445: } else {
2446: result.setIcon(null);
2447: }
2448: return result;
2449: }
2450: }
2451: }
|