Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » [javadoc | source]
    1   //
    2   //  Neuros Database Manipulator
    3   //  Copyright (C) 2003  Neuros Database Manipulator
    4   //
    5   //  This program is free software; you can redistribute it and/or modify
    6   //  it under the terms of the GNU General Public License as published by
    7   //  the Free Software Foundation; either version 2 of the License, or
    8   //  (at your option) any later version.
    9   //
   10   //  This program is distributed in the hope that it will be useful,
   11   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13   //  GNU General Public License for more details.
   14   //
   15   //  You should have received a copy of the GNU General Public License
   16   //  along with this program; if not, write to the Free Software
   17   //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18   //
   19   //  For information about Neuros Database Manipulator and its authors,
   20   //  please contact the Neuros Database Manipulator Web Site at
   21   //  http://neurosdbm.sourceforge.net
   22   //
   23   //
   24   
   25   package net.sourceforge.neurosdbm;
   26   
   27   import java.util.ArrayList;
   28   import java.awt.Component;
   29   import java.awt.Dimension;
   30   import java.awt.event.MouseEvent;
   31   import java.awt.event.MouseAdapter;
   32   import java.awt.event.InputEvent;
   33   import java.awt.event.MouseListener;
   34   
   35   import javax.swing.JPopupMenu;
   36   import javax.swing.JTable;
   37   import javax.swing.table.JTableHeader;
   38   import javax.swing.table.TableColumnModel;
   39   import javax.swing.table.TableColumn;
   40   import javax.swing.table.TableCellRenderer;
   41   import javax.swing.event.TableModelListener;
   42   import javax.swing.event.TableModelEvent;
   43   import net.sourceforge.neurosdbm.db.Database;
   44   
   45   class NeurosTable extends JTable {
   46   
   47     private Database database;
   48     private NeurosTableModel tableModel;
   49     private boolean doResizeColumns;
   50     private JPopupMenu popup;
   51     
   52     NeurosTable(Database database, MainFrame parent) {
   53       this.database = database;
   54       doResizeColumns = true;
   55   	popup = TrackPopupMenu.getMenu();
   56       tableModel = new NeurosTableModel(database, parent);
   57       setModel(tableModel);
   58       tableModel.addTableModelListener(new ResizeColumnListener());
   59       setColumnSelectionAllowed(false); 
   60       JTableHeader th = getTableHeader(); 
   61       th.addMouseListener(new ListMouseListener()); 
   62       
   63       setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   64       setDefaultRenderer(Object.class, new NeurosTableRowRenderer());
   65       
   66   	//Add listener to the text area so the popup menu can come up.
   67   	MouseListener popupListener = new PopupListener(popup);
   68   	this.addMouseListener(popupListener);
   69     }
   70   
   71   
   72     public void updateData() {
   73       tableModel.updateData();
   74     }
   75   
   76     
   77     public void doLayout() {
   78       super.doLayout();
   79       if (doResizeColumns) {
   80         doResizeColumns = false;
   81         resizeColumns();
   82       }
   83     }
   84     
   85     private void resizeColumns() {
   86       for (int col=0; col<getColumnCount(); col++) {
   87         TableColumn column = columnModel.getColumn(col);
   88         column.setPreferredWidth(50);
   89         for (int row=0; row<getRowCount(); row++) {
   90           TableCellRenderer renderer = getCellRenderer(row, col);
   91           Component component = renderer.getTableCellRendererComponent
   92             (this, tableModel.getValueAt(row, col), false, false, row, col);
   93   
   94           int columnWidth = column.getPreferredWidth();
   95           int componentWidth = component.getPreferredSize().width;
   96           int componentHeight = component.getPreferredSize().height;
   97           int columnMargin = getColumnModel().getColumnMargin();
   98   
   99           if (componentWidth >= columnWidth) {
  100             column.setPreferredWidth(componentWidth + columnMargin);
  101           }
  102         }
  103       }
  104     }
  105     
  106     class ResizeColumnListener implements TableModelListener {
  107       public void tableChanged(TableModelEvent event) {
  108       }
  109     }
  110   
  111     class ListMouseListener extends MouseAdapter { 
  112       public void mouseClicked(MouseEvent e) {
  113         TableColumnModel columnModel = getColumnModel();
  114         int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
  115         int column = convertColumnIndexToModel(viewColumn); 
  116         if (e.getClickCount() == 1 && column != -1) {
  117           int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK; 
  118           boolean ascending = (shiftPressed == 0); 
  119           tableModel.sortByColumn(column, ascending); 
  120         }
  121       }
  122     }
  123   
  124     ArrayList getSelectedTracks() {
  125       int[] rows = getSelectedRows();
  126       return tableModel.getSelectedTracks(rows);
  127     }
  128   
  129   
  130     void triggerResizeColumns() {
  131       doResizeColumns = true;
  132     }
  133     
  134     class PopupListener extends MouseAdapter {
  135   	  JPopupMenu popup;
  136   
  137   	  PopupListener(JPopupMenu popupMenu) {
  138   		  popup = popupMenu;
  139   	  }
  140   
  141   	  public void mousePressed(MouseEvent e) {
  142   		  maybeShowPopup(e);
  143   	  }
  144   
  145   	  public void mouseReleased(MouseEvent e) {
  146   		  maybeShowPopup(e);
  147   	  }
  148   
  149   	  private void maybeShowPopup(MouseEvent e) {
  150   		  if (e.isPopupTrigger()) {
  151   			  popup.show(e.getComponent(),
  152   						 e.getX(), e.getY());
  153   			popup.setInvoker(TrackPopupMenu.getMainMenu());
  154   		  }
  155   	  }
  156     }
  157   }

Save This Page
Home » openjdk-7 » net.sourceforge » neurosdbm » [javadoc | source]