001: package org.swingml.component.ext;
002:
003: import java.awt.*;
004:
005: import javax.swing.*;
006: import javax.swing.table.*;
007:
008: import org.swingml.*;
009: import org.swingml.model.TableColumnModel;
010:
011: public class ColumnHeadingCellRenderer implements TableCellRenderer {
012:
013: private String direction;
014: private ImageIcon icon;
015: private TableColumnModel model;
016: private int sort;
017: private String sortType;
018:
019: public ColumnHeadingCellRenderer(TableColumnModel aModel) {
020: setModel(aModel);
021: }
022:
023: public ColumnHeadingCellRenderer(TableColumnModel aModel,
024: ImageIcon anIcon, int i) {
025: this (aModel);
026: setIcon(anIcon);
027: setSort(i);
028: }
029:
030: public ColumnHeadingCellRenderer(TableColumnModel aModel, String d,
031: int i) {
032: this (aModel);
033: setDirection(d);
034: setSort(i);
035: }
036:
037: private String getAlignment() {
038: String result = Constants.LEFT;
039: if (getModel() != null) {
040: result = getModel().getAlignment();
041: }
042: return result;
043: }
044:
045: public String getDirection() {
046: return direction;
047: }
048:
049: public ImageIcon getIcon() {
050: return icon;
051: }
052:
053: public TableColumnModel getModel() {
054: return model;
055: }
056:
057: public int getSort() {
058: return sort;
059: }
060:
061: /**
062: * @return Returns the sortType.
063: */
064: public String getSortType() {
065: return sortType;
066: }
067:
068: public Component getTableCellRendererComponent(JTable table,
069: Object value, boolean isSelected, boolean hasFocus,
070: int row, int column) {
071: JPanel panel = new JPanel(new GridBagLayout());
072: panel.setBorder(BorderFactory.createRaisedBevelBorder());
073:
074: // calcualate and set alignment options
075: GridBagConstraints constraints = new GridBagConstraints();
076: constraints.fill = GridBagConstraints.NONE;
077: constraints.weightx = 1.0;
078:
079: String alignment = getAlignment();
080: if (alignment != null) {
081: if (alignment.equals(Constants.RIGHT)) {
082: constraints.anchor = GridBagConstraints.LINE_END;
083: } else if (alignment.equals(Constants.LEFT)) {
084: constraints.anchor = GridBagConstraints.LINE_START;
085: } else {
086: constraints.weightx = 0.0;
087: constraints.anchor = GridBagConstraints.CENTER;
088: }
089: }
090:
091: JLabel label = new JLabel();
092: label.setBorder(BorderFactory.createEmptyBorder());
093: label.setText(value.toString());
094: label.setVerticalTextPosition(SwingConstants.CENTER);
095: panel.add(label, constraints);
096:
097: if (getSort() != -1 || getDirection() != null) {
098: JLabel sortIndicator = new JLabel();
099: sortIndicator.setText(" "
100: + getSort()
101: + " "
102: + (getDirection() != null ? " " + getDirection()
103: : ""));
104: panel.add(sortIndicator, constraints);
105: }
106:
107: if (getIcon() != null) {
108: JLabel arrow = new JLabel();
109: arrow.setIcon(getIcon());
110: panel.add(arrow, constraints);
111: }
112:
113: return panel;
114: }
115:
116: public void reset() {
117: setDirection(null);
118: setIcon(null);
119: setSortType(null);
120: setSort(-1);
121: }
122:
123: public void setDirection(String aDirection) {
124: this .direction = aDirection;
125: }
126:
127: public void setIcon(ImageIcon anIcon) {
128: this .icon = anIcon;
129: }
130:
131: public void setModel(TableColumnModel aModel) {
132: this .model = aModel;
133: }
134:
135: public void setSort(int sortOrder) {
136: this .sort = sortOrder;
137: }
138:
139: /**
140: * @param type The sortType to set.
141: */
142: public void setSortType(String type) {
143: this.sortType = type;
144: }
145:
146: }
|