Update of /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui In directory usw-pr-cvs1:/tmp/cvs-serv31847/src/org/cdchamber/gui Modified Files: AboutDialog.java CDCMainFrame.java CDCMainFrameController.java FilesTable.java FilesTableModel.java Log Message: Tables can sort now. Index: AboutDialog.java =================================================================== RCS file: /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui/AboutDialog.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** AboutDialog.java 30 Jul 2002 02:26:17 -0000 1.1 --- AboutDialog.java 30 Jul 2002 05:12:12 -0000 1.2 *************** *** 8,11 **** --- 8,13 ---- import javax.swing.*; + import javax.swing.event.HyperlinkListener; + import javax.swing.event.HyperlinkEvent; import java.awt.event.*; import java.awt.*; *************** *** 13,17 **** import java.io.IOException; ! public class AboutDialog extends JDialog implements ActionListener { String product = "CDChamber $Revision$"; --- 15,19 ---- import java.io.IOException; ! public class AboutDialog extends JDialog implements ActionListener, HyperlinkListener { String product = "CDChamber $Revision$"; *************** *** 27,31 **** JLabel imageLabel= new JLabel( new ImageIcon( DirPath.getIconPath( "about.jpg"))); JTextPane textPane = new JTextPane(); ! try { textPane.setContentType("text/html"); --- 29,33 ---- JLabel imageLabel= new JLabel( new ImageIcon( DirPath.getIconPath( "about.jpg"))); JTextPane textPane = new JTextPane(); ! //HTMLDocument doc = new HTMLDocument(); try { textPane.setContentType("text/html"); *************** *** 36,45 **** sb.append( "</body></html>"); textPane.read( new StringReader( sb.toString()), "About CDChamber"); ! } catch ( IOException e) { e.printStackTrace(); } ! textPane.setEnabled( false); this.setTitle("About CDChamber"); this.setModal( true); --- 38,52 ---- sb.append( "</body></html>"); + //doc.insertString( doc.getEndPosition().getOffset(), sb.toString(), null); + + //textPane.setDocument( doc); textPane.read( new StringReader( sb.toString()), "About CDChamber"); ! ! } catch ( Exception e) { e.printStackTrace(); } ! textPane.addHyperlinkListener( this); ! textPane.setEditable( false); this.setTitle("About CDChamber"); this.setModal( true); *************** *** 76,78 **** --- 83,99 ---- cancel(); } + + public void hyperlinkUpdate(HyperlinkEvent e) { + if ( e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) return; + try { + Runtime.getRuntime().exec("start " + e.getURL()); + } catch (IOException e1) { + try { + Runtime.getRuntime().exec("cmd /c \"start " + e.getURL() + "\""); + } catch (IOException e2) { + e2.printStackTrace(); + } + } + } + } Index: CDCMainFrame.java =================================================================== RCS file: /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui/CDCMainFrame.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** CDCMainFrame.java 30 Jul 2002 02:26:17 -0000 1.23 --- CDCMainFrame.java 30 Jul 2002 05:12:12 -0000 1.24 *************** *** 199,202 **** --- 199,203 ---- public void setFilesTableModel(TableModel model) { filesTable.setModel( model); + mainPane.setDividerLocation( 0.3); } Index: CDCMainFrameController.java =================================================================== RCS file: /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui/CDCMainFrameController.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** CDCMainFrameController.java 30 Jul 2002 02:26:17 -0000 1.19 --- CDCMainFrameController.java 30 Jul 2002 05:12:13 -0000 1.20 *************** *** 130,134 **** if (e.getClickCount() >= 2) { FilesTableModel tableModel = (FilesTableModel) frame.getFilesTable().getModel(); ! FileType fileType = tableModel.dir.getFiles(frame.getFilesTable().getSelectedRow()); if (fileType instanceof DirectoryType) { Enumeration enu = selectedNode.children(); --- 130,134 ---- if (e.getClickCount() >= 2) { FilesTableModel tableModel = (FilesTableModel) frame.getFilesTable().getModel(); ! FileType fileType = (FileType)tableModel.files.elementAt(frame.getFilesTable().getSelectedRow()); if (fileType instanceof DirectoryType) { Enumeration enu = selectedNode.children(); Index: FilesTable.java =================================================================== RCS file: /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui/FilesTable.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** FilesTable.java 30 Jul 2002 02:26:17 -0000 1.1 --- FilesTable.java 30 Jul 2002 05:12:13 -0000 1.2 *************** *** 6,11 **** --- 6,14 ---- import javax.swing.*; + import javax.swing.event.TableModelEvent; import javax.swing.table.*; import java.awt.*; + import java.awt.event.MouseEvent; + import java.awt.event.MouseAdapter; public class FilesTable extends JTable { *************** *** 17,20 **** --- 20,28 ---- setRowHeight(18); + JTableHeader header = getTableHeader(); + header.setUpdateTableInRealTime(true); + header.addMouseListener(new ColumnListener( this)); + header.setReorderingAllowed(true); + } *************** *** 34,45 **** TypeIconCellRenderer typeRenderer = new TypeIconCellRenderer(); getColumn( FilesTableModel.columnHeads[0].title).setCellRenderer(typeRenderer); - - JTableHeader header = getTableHeader(); - header.setUpdateTableInRealTime(true); - // header.addMouseListener(new ColumnListener()); - header.setReorderingAllowed(true); - - // jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - } --- 42,45 ---- *************** *** 66,68 **** --- 66,103 ---- } + } + + class ColumnListener extends MouseAdapter { + + private JTable jTable; + public ColumnListener( JTable jTable) { + super(); + this.jTable = jTable; + } + + public void mouseClicked(MouseEvent e) { + if ( e.isConsumed()) return; + TableColumnModel colModel = jTable.getColumnModel(); + int columnModelIndex = colModel.getColumnIndexAtX(e.getX()); + int modelIndex = colModel.getColumn(columnModelIndex).getModelIndex(); + if (modelIndex < 0) return; + + FilesTableModel tmodel = (FilesTableModel) jTable.getModel(); + if (tmodel.sortCol == modelIndex) + tmodel.sortAsc = !tmodel.sortAsc; + else + tmodel.sortCol = modelIndex; + + for (int i = 0; i < tmodel.getColumnCount(); i++) { + TableColumn column = colModel.getColumn(i); + int index = column.getModelIndex(); + JLabel renderer = (JLabel) column.getHeaderRenderer(); + renderer.setIcon(tmodel.getColumnIcon(index)); + } + jTable.getTableHeader().repaint(); + + tmodel.sortData(); + jTable.tableChanged(new TableModelEvent(tmodel)); + jTable.repaint(); + } } Index: FilesTableModel.java =================================================================== RCS file: /cvsroot/cdchamber/CDChamber/src/org/cdchamber/gui/FilesTableModel.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FilesTableModel.java 22 Jul 2002 01:31:25 -0000 1.7 --- FilesTableModel.java 30 Jul 2002 05:12:13 -0000 1.8 *************** *** 10,13 **** --- 10,14 ---- import org.cdchamber.elements.DirectoryType; import org.cdchamber.elements.FileType; + import org.cdchamber.util.DirPath; import javax.swing.table.AbstractTableModel; *************** *** 16,20 **** import java.text.NumberFormat; import java.text.DateFormat; ! import java.util.Date; public class FilesTableModel extends AbstractTableModel { --- 17,21 ---- import java.text.NumberFormat; import java.text.DateFormat; ! import java.util.*; public class FilesTableModel extends AbstractTableModel { *************** *** 23,27 **** --- 24,44 ---- } + public int sortCol=0; + public boolean sortAsc=true; + public static ImageIcon COLUMN_UP = new ImageIcon( DirPath.getIconPath("SortUp.gif")); + public static ImageIcon COLUMN_DOWN = new ImageIcon( DirPath.getIconPath("SortDown.gif")); + + public ImageIcon getColumnIcon(int column) { // NEW + if (column==sortCol) + return sortAsc ? COLUMN_UP : COLUMN_DOWN; + return null; + } + + public void sortData() { + Collections.sort( files, new FileTypeComparator( sortCol, sortAsc)); + } + DirectoryType dir; + Vector files; public DirectoryType getDir() { return this.dir; *************** *** 30,37 **** public FilesTableModel( DefaultMutableTreeNode node) { dir = (DirectoryType)node.getUserObject(); } public int getRowCount() { ! return dir.getFilesCount(); } --- 47,62 ---- public FilesTableModel( DefaultMutableTreeNode node) { dir = (DirectoryType)node.getUserObject(); + + files = new Vector(); + for ( int i=0; i<dir.getFilesCount(); i++) { + files.addElement( dir.getFiles(i)); + } + sortCol = FilesTableModel.NAME; + sortAsc = true; + sortData(); } public int getRowCount() { ! return files.size(); } *************** *** 58,63 **** public Object getValueAt(int rowIndex, int columnIndex) { ! if ( rowIndex <0 || rowIndex > dir.getFilesCount()-1) return null; ! FileType ft = (FileType) dir.getFiles( rowIndex); switch ( columnIndex) { case NAME: --- 83,88 ---- public Object getValueAt(int rowIndex, int columnIndex) { ! if ( rowIndex <0 || rowIndex > files.size()-1) return null; ! FileType ft = (FileType) files.elementAt( rowIndex); switch ( columnIndex) { case NAME: *************** *** 79,87 **** public void setValueAt(Object value, int row, int col) { - if ( row<0 || row > dir.getFilesCount()-1) return; if ( col != DESC) return; ! // TODO: Maybe not right after sort. ! FileType ft = dir.getFiles( row); ft.setDesc( value.toString()); fireTableCellUpdated(row, col); --- 104,111 ---- public void setValueAt(Object value, int row, int col) { if ( col != DESC) return; + if ( row<0 || row > files.size()-1) return; ! FileType ft = (FileType)files.elementAt( row); ft.setDesc( value.toString()); fireTableCellUpdated(row, col); *************** *** 113,114 **** --- 137,211 ---- } } + + class FileTypeComparator implements Comparator { + + protected int m_sortCol; + protected boolean m_sortAsc; + + public FileTypeComparator(int sortCol, boolean sortAsc) { + m_sortCol = sortCol; + m_sortAsc = sortAsc; + } + + private int commonStringCompare( String n1, String n2) { + if ( n1 == null) return -1; + if ( n2 == null) return 1; + return n1.compareToIgnoreCase(n2); + } + + private boolean isDir( FileType fileType) { + if ( fileType instanceof DirectoryType) return true; + return false; + } + + public int compare(Object o1, Object o2) { + + if(!(o1 instanceof FileType) || !(o2 instanceof FileType)) + return 0; + FileType s1 = (FileType)o1; + FileType s2 = (FileType)o2; + int result = 0; + switch (m_sortCol) { + case FilesTableModel.NAME: + if ( isDir( s1) && isDir(s2)) { + result = commonStringCompare( s1.getName(), s2.getName()); + break; + } + if ( !(isDir( s1) || isDir(s2))) { + result = commonStringCompare( s1.getName(), s2.getName()); + break; + } + if ( isDir(s1)) { + result = -1; + break; + } + result = 1; + break; + case FilesTableModel.SIZE: + result = s1.getSize()<s2.getSize() ? -1 : 1; + break; + case FilesTableModel.DATE: + result = s1.getDate()<s2.getDate() ? -1 : 1; + break; + case FilesTableModel.DESC: + result = commonStringCompare( s1.getDesc(), s2.getDesc()); + break; + default: + result =0; + } + + if (!m_sortAsc) + result = -result; + return result; + } + + public boolean equals(Object obj) { + if (obj instanceof FileTypeComparator) { + FileTypeComparator compObj = (FileTypeComparator)obj; + return (compObj.m_sortCol==m_sortCol) && + (compObj.m_sortAsc==m_sortAsc); + } + return false; + } + } + |