001: /*
002: * Created on 28/05/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: package br.com.igor.beans.table;
025:
026: import java.awt.Color;
027: import java.awt.Component;
028: import java.awt.Graphics;
029: import java.awt.Graphics2D;
030: import java.awt.RenderingHints;
031: import java.awt.event.MouseAdapter;
032: import java.awt.event.MouseEvent;
033: import java.sql.SQLException;
034:
035: import javax.swing.JTable;
036: import javax.swing.UIManager;
037: import javax.swing.table.DefaultTableCellRenderer;
038: import javax.swing.table.JTableHeader;
039:
040: import br.com.igor.db.ControllerCreationException;
041: import br.com.igor.db.PersistentObject;
042:
043: /**
044: * @author Igor Regis da Silva Simoes
045: */
046: /**
047: * Este componente é um Renderer para o Header de um JTable que controla a operação de "sort"
048: *
049: * @author Igor Regis da Silva Simoes
050: */
051: public class SortableTabelHeaderRenderer extends
052: DefaultTableCellRenderer {
053: /**
054: * Lista da ordem de sort das colunas
055: */
056: private boolean sorted[] = null;
057:
058: /**
059: * booleano indicando de uma coluna esta ou nao ordenada
060: */
061: private boolean sortedColumn = false;
062:
063: /**
064: * booleano indicando se uma caoluna esta em ordem crescente ou não
065: */
066: private boolean crescente = false;
067:
068: /**
069: * booleanos indicando se as colunas estão em ordem crescente ou descrescente
070: */
071: private boolean ordem[] = null;
072:
073: /**
074: *
075: */
076: private SortableMouseListener listener = null;
077:
078: /**
079: * Cria uma nova instância de SortableTabelHeader
080: */
081: public SortableTabelHeaderRenderer() {
082: //Não fazemos nada
083: }
084:
085: /**
086: * Sobrescreve o método da superclasse para retornar um renderer formatado, conforme descrito acima
087: *
088: * @param table JTable Tabela que será configurada por este renderer
089: * @param value Object Valor que será exibido pelo renderer
090: * @param isSelected boolean Indicando se a célula está selecionada
091: * @param hasFocus boolean indicando se a célual possui o foco
092: * @param row int Linha da célula
093: * @param column int Coluna da célula
094: * @return Retorna o Compoment responsável por renderizar o valor da célula
095: */
096: @Override
097: public Component getTableCellRendererComponent(JTable table,
098: Object value, boolean isSelected, boolean hasFocus,
099: int row, int column) {
100: if (listener == null) {
101: listener = new SortableMouseListener();
102: table.getTableHeader().addMouseListener(listener);
103: sorted = new boolean[table.getColumnCount()];
104: ordem = new boolean[table.getColumnCount()];
105: for (int i = 0; i < ordem.length; i++) {
106: ordem[i] = true;
107: }
108: }
109:
110: sortedColumn = sorted[column];
111: crescente = ordem[column];
112:
113: setValue(" " + value);
114: setToolTipText(value.toString());
115:
116: setBorder(UIManager.getBorder("TableHeader.cellBorder"));
117:
118: return this ;
119: }
120:
121: /**
122: * @see javax.swing.JComponent#paint(java.awt.Graphics)
123: */
124: @Override
125: public void paint(Graphics g) {
126: super .paint(g);
127:
128: if (sortedColumn) {
129: int base, ponta;
130:
131: if (crescente) {
132: ponta = 3;
133: base = 12;
134: } else {
135: ponta = 12;
136: base = 3;
137: }
138: Graphics2D g2d = (Graphics2D) g;
139: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
140: RenderingHints.VALUE_ANTIALIAS_ON);
141:
142: g2d.setColor(getBackground());
143: g2d.fillPolygon(new int[] { this .getWidth() - 15,
144: this .getWidth() - 10, this .getWidth() - 20 },
145: new int[] { ponta, base, base }, 3);
146:
147: g2d.setColor(Color.DARK_GRAY);
148: g2d.drawLine(this .getWidth() - 15, ponta,
149: this .getWidth() - 20, base);
150:
151: g2d.setColor(Color.WHITE);
152: g2d.drawLine(this .getWidth() - 15, ponta,
153: this .getWidth() - 10, base);
154:
155: g2d.setColor(Color.GRAY);
156: g2d.drawLine(this .getWidth() - 10, base,
157: this .getWidth() - 20, base);
158: }
159: }
160:
161: /**
162: * Getter for property sorted.
163: *
164: * @param column Coluna da tabela
165: * @return Value of property sorted.
166: */
167: public boolean isSorted(int column) {
168: return sorted[column];
169: }
170:
171: /**
172: * Setter for property sorted.
173: *
174: * @param sorted New value of property sorted.
175: * @param column Coluna da tabela
176: */
177: public void setSorted(boolean sorted, int column) {
178: this .sorted[column] = sorted;
179: }
180:
181: /**
182: * Classe que houve os eventos de clique no header
183: */
184: private class SortableMouseListener extends MouseAdapter {
185: /**
186: * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
187: */
188: @Override
189: public void mouseClicked(final MouseEvent e) {
190: JTableHeader header = (JTableHeader) e.getSource();
191: int coluna = header.getColumnModel().getColumnIndexAtX(
192: e.getX());
193:
194: for (int i = 0; i < sorted.length; i++) {
195: sorted[i] = false;
196: }
197:
198: sorted[coluna] = true;
199: ordem[coluna] = !ordem[coluna];
200:
201: PersistentObject order = ((DBTableModel) header.getTable()
202: .getModel()).getDataType();
203: order.setOrderBy(new String[] { header.getColumnModel()
204: .getColumn(
205: header.getColumnModel().getColumnIndexAtX(
206: e.getX())).getHeaderValue()
207: .toString() });
208: ((DBTableModel) header.getTable().getModel())
209: .setDataType(order);
210:
211: try {
212: ((DBTableModel) header.getTable().getModel()).filter();
213: } catch (ControllerCreationException cnfe) {
214: //TODO Excecao
215: cnfe.printStackTrace();
216: } catch (SQLException sqle) {
217: //TODO Excecao
218: sqle.printStackTrace();
219: }
220: }
221: }
222: }
|