/[classpath]/classpath/javax/swing/plaf/basic/BasicTableUI.java
ViewVC logotype

Diff of /classpath/javax/swing/plaf/basic/BasicTableUI.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.5.2.9 by gnu_andrew, Tue Sep 20 18:46:33 2005 UTC revision 1.5.2.10 by gnu_andrew, Wed Nov 2 00:43:59 2005 UTC
# Line 49  import java.awt.event.ActionEvent; Line 49  import java.awt.event.ActionEvent;
49  import java.awt.event.ActionListener;  import java.awt.event.ActionListener;
50  import java.awt.event.FocusEvent;  import java.awt.event.FocusEvent;
51  import java.awt.event.FocusListener;  import java.awt.event.FocusListener;
 import java.awt.event.InputEvent;  
52  import java.awt.event.KeyEvent;  import java.awt.event.KeyEvent;
53  import java.awt.event.KeyListener;  import java.awt.event.KeyListener;
54  import java.awt.event.MouseEvent;  import java.awt.event.MouseEvent;
55    import java.beans.PropertyChangeEvent;
56    import java.beans.PropertyChangeListener;
57    
58  import javax.swing.AbstractAction;  import javax.swing.AbstractAction;
59  import javax.swing.ActionMap;  import javax.swing.ActionMap;
 import javax.swing.BorderFactory;  
60  import javax.swing.CellRendererPane;  import javax.swing.CellRendererPane;
61    import javax.swing.DefaultListSelectionModel;
62  import javax.swing.InputMap;  import javax.swing.InputMap;
63  import javax.swing.JComponent;  import javax.swing.JComponent;
64  import javax.swing.JTable;  import javax.swing.JTable;
65  import javax.swing.JTextField;  import javax.swing.JTextField;
66  import javax.swing.KeyStroke;  import javax.swing.KeyStroke;
67  import javax.swing.ListSelectionModel;  import javax.swing.ListSelectionModel;
68    import javax.swing.LookAndFeel;
69  import javax.swing.UIDefaults;  import javax.swing.UIDefaults;
70  import javax.swing.UIManager;  import javax.swing.UIManager;
71  import javax.swing.border.Border;  import javax.swing.border.Border;
# Line 75  import javax.swing.plaf.TableUI; Line 77  import javax.swing.plaf.TableUI;
77  import javax.swing.table.TableCellRenderer;  import javax.swing.table.TableCellRenderer;
78  import javax.swing.table.TableColumn;  import javax.swing.table.TableColumn;
79  import javax.swing.table.TableColumnModel;  import javax.swing.table.TableColumnModel;
80    import javax.swing.table.TableModel;
81    
82  public class BasicTableUI  public class BasicTableUI extends TableUI
   extends TableUI  
83  {  {
84    public static ComponentUI createUI(JComponent comp)    public static ComponentUI createUI(JComponent comp)
85    {    {
# Line 93  public class BasicTableUI Line 95  public class BasicTableUI
95    /** The normal cell border. */    /** The normal cell border. */
96    Border cellBorder;    Border cellBorder;
97    
   /** The cell border for selected/highlighted cells. */  
   Border highlightCellBorder;  
   
98    /** The action bound to KeyStrokes. */    /** The action bound to KeyStrokes. */
99    TableAction action;    TableAction action;
100    
101    class FocusHandler implements FocusListener    /**
102       * Listens for changes to the tables properties.
103       */
104      private PropertyChangeListener propertyChangeListener;
105    
106      /**
107       * Handles key events for the JTable. Key events should be handled through
108       * the InputMap/ActionMap mechanism since JDK1.3. This class is only there
109       * for backwards compatibility.
110       *
111       * @author Roman Kennke (kennke@aicas.com)
112       */
113      public class KeyHandler implements KeyListener
114      {
115    
116        /**
117         * Receives notification that a key has been pressed and released.
118         *
119         * @param event the key event
120         */
121        public void keyTyped(KeyEvent event)
122        {
123          // Key events should be handled through the InputMap/ActionMap mechanism
124          // since JDK1.3. This class is only there for backwards compatibility.
125        }
126    
127        /**
128         * Receives notification that a key has been pressed.
129         *
130         * @param event the key event
131         */
132        public void keyPressed(KeyEvent event)
133        {
134          // Key events should be handled through the InputMap/ActionMap mechanism
135          // since JDK1.3. This class is only there for backwards compatibility.
136        }
137    
138        /**
139         * Receives notification that a key has been released.
140         *
141         * @param event the key event
142         */
143        public void keyReleased(KeyEvent event)
144        {
145          // Key events should be handled through the InputMap/ActionMap mechanism
146          // since JDK1.3. This class is only there for backwards compatibility.
147        }
148      }
149    
150      public class FocusHandler implements FocusListener
151    {    {
152      public void focusGained(FocusEvent e)      public void focusGained(FocusEvent e)
153      {      {
154          // TODO: Implement this properly.
155      }      }
156    
157      public void focusLost(FocusEvent e)      public void focusLost(FocusEvent e)
158      {      {
159          // TODO: Implement this properly.
160      }      }
161    }    }
162    
163    class MouseInputHandler implements MouseInputListener    public class MouseInputHandler implements MouseInputListener
164    {    {
165      Point begin, curr;      Point begin, curr;
166    
# Line 145  public class BasicTableUI Line 196  public class BasicTableUI
196    
197      public void mouseClicked(MouseEvent e)      public void mouseClicked(MouseEvent e)
198      {      {
199          // TODO: What should be done here, if anything?
200      }      }
201    
202      public void mouseDragged(MouseEvent e)      public void mouseDragged(MouseEvent e)
203      {      {
204        curr = new Point(e.getX(), e.getY());        if (table.isEnabled())
205        updateSelection(e.isControlDown());                {
206              curr = new Point(e.getX(), e.getY());
207              updateSelection(e.isControlDown());
208            }
209      }      }
210    
211      public void mouseEntered(MouseEvent e)      public void mouseEntered(MouseEvent e)
212      {      {
213          // TODO: What should be done here, if anything?
214      }      }
215    
216      public void mouseExited(MouseEvent e)      public void mouseExited(MouseEvent e)
217      {      {
218          // TODO: What should be done here, if anything?
219      }      }
220    
221      public void mouseMoved(MouseEvent e)      public void mouseMoved(MouseEvent e)
222      {      {
223          // TODO: What should be done here, if anything?
224      }      }
225    
226      public void mousePressed(MouseEvent e)      public void mousePressed(MouseEvent e)
227      {      {
228        ListSelectionModel rowModel = table.getSelectionModel();        if (table.isEnabled())
229        ListSelectionModel colModel = table.getColumnModel().getSelectionModel();          {
230        int rowLead = rowModel.getLeadSelectionIndex();            ListSelectionModel rowModel = table.getSelectionModel();
231        int colLead = colModel.getLeadSelectionIndex();            ListSelectionModel colModel = table.getColumnModel().getSelectionModel();
232              int rowLead = rowModel.getLeadSelectionIndex();
233              int colLead = colModel.getLeadSelectionIndex();
234    
235              begin = new Point(e.getX(), e.getY());
236              curr = new Point(e.getX(), e.getY());
237              //if control is pressed and the cell is already selected, deselect it
238              if (e.isControlDown() && table.
239                  isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin)))
240                {                                      
241                  table.getSelectionModel().
242                  removeSelectionInterval(table.rowAtPoint(begin),
243                                          table.rowAtPoint(begin));
244                  table.getColumnModel().getSelectionModel().
245                  removeSelectionInterval(table.columnAtPoint(begin),
246                                          table.columnAtPoint(begin));
247                }
248              else
249                updateSelection(e.isControlDown());
250    
251        begin = new Point(e.getX(), e.getY());            // If we were editing, but the moved to another cell, stop editing
252        curr = new Point(e.getX(), e.getY());            if (rowLead != rowModel.getLeadSelectionIndex() ||
253        //if control is pressed and the cell is already selected, deselect it                colLead != colModel.getLeadSelectionIndex())
254        if (e.isControlDown() && table.              if (table.isEditing())
255            isCellSelected(table.rowAtPoint(begin),table.columnAtPoint(begin)))                table.editingStopped(new ChangeEvent(e));
         {                                        
           table.getSelectionModel().  
             removeSelectionInterval(table.rowAtPoint(begin),  
                                     table.rowAtPoint(begin));  
           table.getColumnModel().getSelectionModel().  
             removeSelectionInterval(table.columnAtPoint(begin),  
                                     table.columnAtPoint(begin));  
256          }          }
       else  
         updateSelection(e.isControlDown());  
   
       // If we were editing, but the moved to another cell, stop editing  
       if (rowLead != rowModel.getLeadSelectionIndex() ||  
           colLead != colModel.getLeadSelectionIndex())  
         if (table.isEditing())  
           table.editingStopped(new ChangeEvent(e));  
257      }      }
258    
259      public void mouseReleased(MouseEvent e)      public void mouseReleased(MouseEvent e)
260      {      {
261        begin = null;        if (table.isEnabled())
262        curr = null;          {
263              begin = null;
264              curr = null;
265            }
266        }
267      }
268    
269      /**
270       * Listens for changes to the model property of the JTable and adjusts some
271       * settings.
272       *
273       * @author Roman Kennke (kennke@aicas.com)
274       */
275      private class PropertyChangeHandler implements PropertyChangeListener
276      {
277        /**
278         * Receives notification if one of the JTable's properties changes.
279         *
280         * @param ev the property change event
281         */
282        public void propertyChange(PropertyChangeEvent ev)
283        {
284          String propName = ev.getPropertyName();
285          if (propName.equals("model"))
286            {
287              ListSelectionModel rowSel = table.getSelectionModel();
288              rowSel.clearSelection();
289              ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
290              colSel.clearSelection();
291              TableModel model = table.getModel();
292    
293              // Adjust lead and anchor selection indices of the row and column
294              // selection models.
295              if (model.getRowCount() > 0)
296                {
297                  rowSel.setAnchorSelectionIndex(0);
298                  rowSel.setLeadSelectionIndex(0);
299                }
300              else
301                {
302                  rowSel.setAnchorSelectionIndex(-1);
303                  rowSel.setLeadSelectionIndex(-1);
304                }
305              if (model.getColumnCount() > 0)
306                {
307                  colSel.setAnchorSelectionIndex(0);
308                  colSel.setLeadSelectionIndex(0);
309                }
310              else
311                {
312                  colSel.setAnchorSelectionIndex(-1);
313                  colSel.setLeadSelectionIndex(-1);
314                }
315            }
316      }      }
317    }    }
318    
# Line 206  public class BasicTableUI Line 326  public class BasicTableUI
326      return new MouseInputHandler();      return new MouseInputHandler();
327    }    }
328    
329    
330      /**
331       * Creates and returns a key listener for the JTable.
332       *
333       * @return a key listener for the JTable
334       */
335      protected KeyListener createKeyListener()
336      {
337        return new KeyHandler();
338      }
339    
340    /**    /**
341     * Return the maximum size of the table. The maximum height is the row     * Return the maximum size of the table. The maximum height is the row
342      * height times the number of rows. The maximum width is the sum of      * height times the number of rows. The maximum width is the sum of
# Line 255  public class BasicTableUI Line 386  public class BasicTableUI
386    
387    protected void installDefaults()    protected void installDefaults()
388    {    {
389      UIDefaults defaults = UIManager.getLookAndFeelDefaults();      LookAndFeel.installColorsAndFont(table, "Table.background",
390      table.setFont(defaults.getFont("Table.font"));                                       "Table.foreground", "Table.font");
391      table.setGridColor(defaults.getColor("Table.gridColor"));      table.setGridColor(UIManager.getColor("Table.gridColor"));
392      table.setForeground(defaults.getColor("Table.foreground"));      table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
393      table.setBackground(defaults.getColor("Table.background"));      table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
     table.setSelectionForeground(defaults.getColor("Table.selectionForeground"));  
     table.setSelectionBackground(defaults.getColor("Table.selectionBackground"));  
394      table.setOpaque(true);      table.setOpaque(true);
395        rendererPane = new CellRendererPane();
     highlightCellBorder = defaults.getBorder("Table.focusCellHighlightBorder");  
     cellBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1);  
   }  
   
   private int convertModifiers(int mod)  
   {  
     if ((mod & KeyEvent.SHIFT_DOWN_MASK) != 0)  
       {  
         mod |= KeyEvent.SHIFT_MASK;  
         mod &= ~KeyEvent.SHIFT_DOWN_MASK;  
       }  
     if ((mod & KeyEvent.CTRL_DOWN_MASK) != 0)  
       {  
         mod |= KeyEvent.CTRL_MASK;  
         mod &= ~KeyEvent.CTRL_DOWN_MASK;  
       }  
     if ((mod & KeyEvent.META_DOWN_MASK) != 0)  
       {  
         mod |= KeyEvent.META_MASK;  
         mod &= ~KeyEvent.META_DOWN_MASK;  
       }  
     if ((mod & KeyEvent.ALT_DOWN_MASK) != 0)  
       {  
         mod |= KeyEvent.ALT_MASK;  
         mod &= ~KeyEvent.ALT_DOWN_MASK;  
       }  
     if ((mod & KeyEvent.ALT_GRAPH_DOWN_MASK) != 0)  
       {  
         mod |= KeyEvent.ALT_GRAPH_MASK;  
         mod &= ~KeyEvent.ALT_GRAPH_DOWN_MASK;  
       }  
     return mod;  
396    }    }
397    
398    protected void installKeyboardActions()    protected void installKeyboardActions()
# Line 308  public class BasicTableUI Line 405  public class BasicTableUI
405      action = new TableAction();      action = new TableAction();
406      Object keys[] = ancestorMap.allKeys();      Object keys[] = ancestorMap.allKeys();
407      // Register key bindings in the UI InputMap-ActionMap pair      // Register key bindings in the UI InputMap-ActionMap pair
     // Note that we register key bindings with both the old and new modifier  
     // masks: InputEvent.SHIFT_MASK and InputEvent.SHIFT_DOWN_MASK and so on.  
408      for (int i = 0; i < keys.length; i++)      for (int i = 0; i < keys.length; i++)
409        {        {
410          parentInputMap.put(KeyStroke.getKeyStroke          KeyStroke stroke = (KeyStroke)keys[i];
411                        (((KeyStroke)keys[i]).getKeyCode(), convertModifiers          String actionString = (String) ancestorMap.get(stroke);
412                         (((KeyStroke)keys[i]).getModifiers())),  
413                             (String)ancestorMap.get((KeyStroke)keys[i]));          parentInputMap.put(KeyStroke.getKeyStroke(stroke.getKeyCode(),
414                                                      stroke.getModifiers()),
415          parentInputMap.put(KeyStroke.getKeyStroke                             actionString);
416                        (((KeyStroke)keys[i]).getKeyCode(),  
417                         ((KeyStroke)keys[i]).getModifiers()),          parentActionMap.put (actionString,
418                             (String)ancestorMap.get((KeyStroke)keys[i]));                               new ActionListenerProxy (action, actionString));
   
         parentActionMap.put  
           ((String)ancestorMap.get((KeyStroke)keys[i]), new ActionListenerProxy  
            (action, (String)ancestorMap.get((KeyStroke)keys[i])));  
419    
420        }        }
421      // Set the UI InputMap-ActionMap pair to be the parents of the      // Set the UI InputMap-ActionMap pair to be the parents of the
# Line 383  public class BasicTableUI Line 474  public class BasicTableUI
474       */       */
475      public void actionPerformed (ActionEvent e)      public void actionPerformed (ActionEvent e)
476      {      {
477        ListSelectionModel rowModel = table.getSelectionModel();        DefaultListSelectionModel rowModel = (DefaultListSelectionModel) table.getSelectionModel();
478        ListSelectionModel colModel = table.getColumnModel().getSelectionModel();        DefaultListSelectionModel colModel = (DefaultListSelectionModel) table.getColumnModel().getSelectionModel();
479    
480        int rowLead = rowModel.getLeadSelectionIndex();        int rowLead = rowModel.getLeadSelectionIndex();
481        int rowMax = table.getModel().getRowCount() - 1;        int rowMax = table.getModel().getRowCount() - 1;
# Line 392  public class BasicTableUI Line 483  public class BasicTableUI
483        int colLead = colModel.getLeadSelectionIndex();        int colLead = colModel.getLeadSelectionIndex();
484        int colMax = table.getModel().getColumnCount() - 1;        int colMax = table.getModel().getColumnCount() - 1;
485                
486        if (e.getActionCommand().equals("selectPreviousRowExtendSelection"))        String command = e.getActionCommand();
487          
488          if (command.equals("selectPreviousRowExtendSelection"))
489          {          {
490            rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));            rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));
491            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
492          }          }
493        else if (e.getActionCommand().equals("selectLastColumn"))        else if (command.equals("selectLastColumn"))
494          {          {
           table.clearSelection();  
495            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
496            colModel.setSelectionInterval(colMax, colMax);            colModel.setSelectionInterval(colMax, colMax);
497          }          }
498        else if (e.getActionCommand().equals("startEditing"))        else if (command.equals("startEditing"))
499          {          {
500            if (table.isCellEditable(rowLead, colLead))            if (table.isCellEditable(rowLead, colLead))
501              table.editCellAt(rowLead,colLead);              table.editCellAt(rowLead,colLead);
502          }          }
503        else if (e.getActionCommand().equals("selectFirstRowExtendSelection"))        else if (command.equals("selectFirstRowExtendSelection"))
504          {                        {              
505            rowModel.setLeadSelectionIndex(0);            rowModel.setLeadSelectionIndex(0);
506            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
507          }          }
508        else if (e.getActionCommand().equals("selectFirstColumn"))        else if (command.equals("selectFirstColumn"))
509          {          {
510            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
511            colModel.setSelectionInterval(0, 0);            colModel.setSelectionInterval(0, 0);
512          }          }
513        else if (e.getActionCommand().equals("selectFirstColumnExtendSelection"))        else if (command.equals("selectFirstColumnExtendSelection"))
514          {          {
515            colModel.setLeadSelectionIndex(0);            colModel.setLeadSelectionIndex(0);
516            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
517          }          }      
518        else if (e.getActionCommand().equals("selectLastRow"))        else if (command.equals("selectLastRow"))
519          {          {
520            rowModel.setSelectionInterval(rowMax,rowMax);            rowModel.setSelectionInterval(rowMax,rowMax);
521            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
522          }          }
523        else if (e.getActionCommand().equals("selectNextRowExtendSelection"))        else if (command.equals("selectNextRowExtendSelection"))
524          {          {
525            rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));            rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
526            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
527          }          }
528        else if (e.getActionCommand().equals("selectFirstRow"))        else if (command.equals("selectFirstRow"))
529          {          {
530            rowModel.setSelectionInterval(0,0);            rowModel.setSelectionInterval(0,0);
531            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
532          }          }
533        else if (e.getActionCommand().equals("selectNextColumnExtendSelection"))        else if (command.equals("selectNextColumnExtendSelection"))
534          {          {
535            colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));            colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));
536            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
537          }          }
538        else if (e.getActionCommand().equals("selectLastColumnExtendSelection"))        else if (command.equals("selectLastColumnExtendSelection"))
539          {          {
540            colModel.setLeadSelectionIndex(colMax);            colModel.setLeadSelectionIndex(colMax);
541            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
542          }          }
543        else if (e.getActionCommand().equals("selectPreviousColumnExtendSelection"))        else if (command.equals("selectPreviousColumnExtendSelection"))
544          {          {
545            colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));            colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));
546            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
547          }          }
548        else if (e.getActionCommand().equals("selectNextRow"))        else if (command.equals("selectNextRow"))
549          {          {
550            rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),            rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
551                                          Math.min(rowLead + 1, rowMax));                                          Math.min(rowLead + 1, rowMax));
552            colModel.setSelectionInterval(colLead,colLead);            colModel.setSelectionInterval(colLead,colLead);
553          }          }
554        else if (e.getActionCommand().equals("scrollUpExtendSelection"))        else if (command.equals("scrollUpExtendSelection"))
555          {          {
556            int target;            int target;
557            if (rowLead == getFirstVisibleRowIndex())            if (rowLead == getFirstVisibleRowIndex())
# Line 472  public class BasicTableUI Line 564  public class BasicTableUI
564            rowModel.setLeadSelectionIndex(target);            rowModel.setLeadSelectionIndex(target);
565            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
566          }          }
567        else if (e.getActionCommand().equals("selectPreviousRow"))        else if (command.equals("selectPreviousRow"))
568          {          {
569            rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),            rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
570                                          Math.max(rowLead - 1, 0));                                          Math.max(rowLead - 1, 0));
571            colModel.setSelectionInterval(colLead,colLead);            colModel.setSelectionInterval(colLead,colLead);
572          }          }
573        else if (e.getActionCommand().equals("scrollRightChangeSelection"))        else if (command.equals("scrollRightChangeSelection"))
574          {          {
575            int target;            int target;
576            if (colLead == getLastVisibleColumnIndex())            if (colLead == getLastVisibleColumnIndex())
# Line 491  public class BasicTableUI Line 583  public class BasicTableUI
583            colModel.setSelectionInterval(target, target);            colModel.setSelectionInterval(target, target);
584            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
585          }          }
586        else if (e.getActionCommand().equals("selectPreviousColumn"))        else if (command.equals("selectPreviousColumn"))
587          {          {
588            rowModel.setSelectionInterval(rowLead,rowLead);            rowModel.setSelectionInterval(rowLead,rowLead);
589            colModel.setSelectionInterval(Math.max(colLead - 1, 0),            colModel.setSelectionInterval(Math.max(colLead - 1, 0),
590                                          Math.max(colLead - 1, 0));                                          Math.max(colLead - 1, 0));
591          }          }
592        else if (e.getActionCommand().equals("scrollLeftChangeSelection"))        else if (command.equals("scrollLeftChangeSelection"))
593          {          {
594            int target;            int target;
595            if (colLead == getFirstVisibleColumnIndex())            if (colLead == getFirstVisibleColumnIndex())
# Line 510  public class BasicTableUI Line 602  public class BasicTableUI
602            colModel.setSelectionInterval(target, target);            colModel.setSelectionInterval(target, target);
603            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
604          }          }
605        else if (e.getActionCommand().equals("clearSelection"))        else if (command.equals("clearSelection"))
606          {          {
607            table.clearSelection();            table.clearSelection();
608          }          }
609        else if (e.getActionCommand().equals("cancel"))        else if (command.equals("cancel"))
610          {          {
611            // FIXME: implement other parts of "cancel" like undo-ing last            // FIXME: implement other parts of "cancel" like undo-ing last
612            // selection.  Right now it just calls editingCancelled if            // selection.  Right now it just calls editingCancelled if
# Line 522  public class BasicTableUI Line 614  public class BasicTableUI
614            if (table.isEditing())            if (table.isEditing())
615              table.editingCanceled(new ChangeEvent("cancel"));              table.editingCanceled(new ChangeEvent("cancel"));
616          }          }
617        else if (e.getActionCommand().equals("selectNextRowCell")        else if (command.equals("selectNextRowCell")
618                 || e.getActionCommand().equals("selectPreviousRowCell")                 || command.equals("selectPreviousRowCell")
619                 || e.getActionCommand().equals("selectNextColumnCell")                 || command.equals("selectNextColumnCell")
620                 || e.getActionCommand().equals("selectPreviousColumnCell"))                 || command.equals("selectPreviousColumnCell"))
621          {          {
622            // If nothing is selected, select the first cell in the table            // If nothing is selected, select the first cell in the table
623            if (table.getSelectedRowCount() == 0 &&            if (table.getSelectedRowCount() == 0 &&
# Line 561  public class BasicTableUI Line 653  public class BasicTableUI
653            // when you get to the edges of the table.            // when you get to the edges of the table.
654            if (!multColsSelected && !multRowsSelected)            if (!multColsSelected && !multRowsSelected)
655              {              {
656                if (e.getActionCommand().indexOf("Column") != -1)                if (command.indexOf("Column") != -1)
657                  advanceSingleSelection(colModel, colMax, rowModel, rowMax,                  advanceSingleSelection(colModel, colMax, rowModel, rowMax,
658                                         (e.getActionCommand().equals                                         (command.equals
659                                          ("selectPreviousColumnCell")));                                          ("selectPreviousColumnCell")));
660                else                else
661                  advanceSingleSelection(rowModel, rowMax, colModel, colMax,                  advanceSingleSelection(rowModel, rowMax, colModel, colMax,
662                                         (e.getActionCommand().equals                                         (command.equals
663                                          ("selectPreviousRowCell")));                                          ("selectPreviousRowCell")));
664                return;                return;
665              }              }
# Line 588  public class BasicTableUI Line 680  public class BasicTableUI
680                        
681            // If there are multiple rows and columns selected, select the next            // If there are multiple rows and columns selected, select the next
682            // cell and wrap at the edges of the selection.              // cell and wrap at the edges of the selection.  
683            if (e.getActionCommand().indexOf("Column") != -1)            if (command.indexOf("Column") != -1)
684              advanceMultipleSelection(colModel, colMinSelected, colMaxSelected,              advanceMultipleSelection(colModel, colMinSelected, colMaxSelected,
685                                       rowModel, rowMinSelected, rowMaxSelected,                                       rowModel, rowMinSelected, rowMaxSelected,
686                                       (e.getActionCommand().equals                                       (command.equals
687                                        ("selectPreviousColumnCell")), true);                                        ("selectPreviousColumnCell")), true);
688                        
689            else            else
690              advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected,              advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected,
691                                       colModel, colMinSelected, colMaxSelected,                                       colModel, colMinSelected, colMaxSelected,
692                                       (e.getActionCommand().equals                                       (command.equals
693                                        ("selectPreviousRowCell")), false);                                        ("selectPreviousRowCell")), false);
694          }          }
695        else if (e.getActionCommand().equals("selectNextColumn"))        else if (command.equals("selectNextColumn"))
696          {          {
697            rowModel.setSelectionInterval(rowLead,rowLead);            rowModel.setSelectionInterval(rowLead,rowLead);
698            colModel.setSelectionInterval(Math.min(colLead + 1, colMax),            colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
699                                          Math.min(colLead + 1, colMax));                                          Math.min(colLead + 1, colMax));
700          }          }
701        else if (e.getActionCommand().equals("scrollLeftExtendSelection"))        else if (command.equals("scrollLeftExtendSelection"))
702          {          {
703            int target;            int target;
704            if (colLead == getFirstVisibleColumnIndex())            if (colLead == getFirstVisibleColumnIndex())
# Line 619  public class BasicTableUI Line 711  public class BasicTableUI
711            colModel.setLeadSelectionIndex(target);            colModel.setLeadSelectionIndex(target);
712            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
713          }          }
714        else if (e.getActionCommand().equals("scrollDownChangeSelection"))        else if (command.equals("scrollDownChangeSelection"))
715          {          {
716            int target;            int target;
717            if (rowLead == getLastVisibleRowIndex())            if (rowLead == getLastVisibleRowIndex())
# Line 632  public class BasicTableUI Line 724  public class BasicTableUI
724            rowModel.setSelectionInterval(target, target);            rowModel.setSelectionInterval(target, target);
725            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
726          }          }
727        else if (e.getActionCommand().equals("scrollRightExtendSelection"))        else if (command.equals("scrollRightExtendSelection"))
728          {          {
729            int target;            int target;
730            if (colLead == getLastVisibleColumnIndex())            if (colLead == getLastVisibleColumnIndex())
# Line 645  public class BasicTableUI Line 737  public class BasicTableUI
737            colModel.setLeadSelectionIndex(target);            colModel.setLeadSelectionIndex(target);
738            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
739          }          }
740        else if (e.getActionCommand().equals("selectAll"))        else if (command.equals("selectAll"))
741          {          {
742            table.selectAll();            table.selectAll();
743          }          }
744        else if (e.getActionCommand().equals("selectLastRowExtendSelection"))        else if (command.equals("selectLastRowExtendSelection"))
745          {          {
746            rowModel.setLeadSelectionIndex(rowMax);            rowModel.setLeadSelectionIndex(rowMax);
747            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
748          }          }
749        else if (e.getActionCommand().equals("scrollDownExtendSelection"))        else if (command.equals("scrollDownExtendSelection"))
750          {          {
751            int target;            int target;
752            if (rowLead == getLastVisibleRowIndex())            if (rowLead == getLastVisibleRowIndex())
# Line 666  public class BasicTableUI Line 758  public class BasicTableUI
758                        
759            rowModel.setLeadSelectionIndex(target);            rowModel.setLeadSelectionIndex(target);
760            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
761          }          }      
762        else if (e.getActionCommand().equals("scrollUpChangeSelection"))        else if (command.equals("scrollUpChangeSelection"))
763          {          {
764            int target;            int target;
765            if (rowLead == getFirstVisibleRowIndex())            if (rowLead == getFirstVisibleRowIndex())
# Line 680  public class BasicTableUI Line 772  public class BasicTableUI
772            rowModel.setSelectionInterval(target, target);            rowModel.setSelectionInterval(target, target);
773            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
774          }          }
775          else if (command.equals("selectNextRowChangeLead"))
776              {
777                if (rowModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
778                  {
779                    // just "selectNextRow"
780                    rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
781                                                  Math.min(rowLead + 1, rowMax));
782                    colModel.setSelectionInterval(colLead,colLead);
783                  }
784                else
785                  rowModel.moveLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
786              }
787          else if (command.equals("selectPreviousRowChangeLead"))
788            {
789              if (rowModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
790                {
791                  // just selectPreviousRow
792                  rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
793                                                Math.min(rowLead -1, 0));
794                  colModel.setSelectionInterval(colLead,colLead);
795                }
796              else
797                rowModel.moveLeadSelectionIndex(Math.max(rowLead - 1, 0));
798            }
799          else if (command.equals("selectNextColumnChangeLead"))
800            {
801              if (colModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)            
802                {
803                  // just selectNextColumn
804                  rowModel.setSelectionInterval(rowLead,rowLead);
805                  colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
806                                                Math.min(colLead + 1, colMax));
807                }
808              else
809                colModel.moveLeadSelectionIndex(Math.min(colLead + 1, colMax));
810            }
811          else if (command.equals("selectPreviousColumnChangeLead"))
812            {
813              if (colModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)            
814                {
815                  // just selectPreviousColumn
816                  rowModel.setSelectionInterval(rowLead,rowLead);
817                  colModel.setSelectionInterval(Math.max(colLead - 1, 0),
818                                                Math.max(colLead - 1, 0));
819                  
820                }
821              else
822                colModel.moveLeadSelectionIndex(Math.max(colLead - 1, 0));
823            }
824          else if (command.equals("addToSelection"))
825              {
826                if (!table.isEditing())
827                  {
828                    int oldRowAnchor = rowModel.getAnchorSelectionIndex();
829                    int oldColAnchor = colModel.getAnchorSelectionIndex();
830                    rowModel.addSelectionInterval(rowLead, rowLead);
831                    colModel.addSelectionInterval(colLead, colLead);
832                    rowModel.setAnchorSelectionIndex(oldRowAnchor);
833                    colModel.setAnchorSelectionIndex(oldColAnchor);
834                  }
835              }
836          else if (command.equals("extendTo"))
837            {
838              rowModel.setSelectionInterval(rowModel.getAnchorSelectionIndex(),
839                                            rowLead);
840              colModel.setSelectionInterval(colModel.getAnchorSelectionIndex(),
841                                            colLead);
842            }
843          else if (command.equals("toggleAndAnchor"))
844            {
845              if (rowModel.isSelectedIndex(rowLead))
846                rowModel.removeSelectionInterval(rowLead, rowLead);
847              else
848                rowModel.addSelectionInterval(rowLead, rowLead);
849              
850              if (colModel.isSelectedIndex(colLead))
851                colModel.removeSelectionInterval(colLead, colLead);
852              else
853                colModel.addSelectionInterval(colLead, colLead);
854              
855              rowModel.setAnchorSelectionIndex(rowLead);
856              colModel.setAnchorSelectionIndex(colLead);
857            }
858        else        else
859          {          {
860            // If we're here that means we bound this TableAction class            // If we're here that means we bound this TableAction class
861            // to a keyboard input but we either want to ignore that input            // to a keyboard input but we either want to ignore that input
862            // or we just haven't implemented its action yet.            // or we just haven't implemented its action yet.
863              
864              // Uncomment the following line to print the names of unused bindings
865              // when their keys are pressed
866              
867              // System.out.println ("not implemented: "+e.getActionCommand());
868          }          }
869    
870        if (table.isEditing() && e.getActionCommand() != "startEditing")        // Any commands whose keyStrokes should be used by the Editor should not
871          table.editingCanceled(new ChangeEvent("update"));        // cause editing to be stopped: ie, the SPACE sends "addToSelection" but
872        table.repaint();        // if the table is in editing mode, the space should not cause us to stop
873                // editing because it should be used by the Editor.
874          if (table.isEditing() && command != "startEditing"
875              && command != "addToSelection")
876            table.editingStopped(new ChangeEvent("update"));
877                
878        table.scrollRectToVisible        table.scrollRectToVisible
879          (table.getCellRect(rowModel.getLeadSelectionIndex(),          (table.getCellRect(rowModel.getLeadSelectionIndex(),
880                             colModel.getLeadSelectionIndex(), false));                             colModel.getLeadSelectionIndex(), false));
881          table.repaint();
882      }      }
883            
884        /**
885         * Returns the column index of the first visible column.
886         * @return the column index of the first visible column.
887         */
888      int getFirstVisibleColumnIndex()      int getFirstVisibleColumnIndex()
889      {      {
890        ComponentOrientation or = table.getComponentOrientation();        ComponentOrientation or = table.getComponentOrientation();
# Line 922  public class BasicTableUI Line 1111  public class BasicTableUI
1111    
1112    protected void installListeners()    protected void installListeners()
1113    {    {
1114      table.addFocusListener(focusListener);        if (focusListener == null)
1115          focusListener = createFocusListener();
1116        table.addFocusListener(focusListener);
1117        if (keyListener == null)
1118          keyListener = createKeyListener();
1119      table.addKeyListener(keyListener);      table.addKeyListener(keyListener);
1120        if (mouseInputListener == null)
1121          mouseInputListener = createMouseInputListener();
1122      table.addMouseListener(mouseInputListener);          table.addMouseListener(mouseInputListener);    
1123      table.addMouseMotionListener(mouseInputListener);      table.addMouseMotionListener(mouseInputListener);
1124        if (propertyChangeListener == null)
1125          propertyChangeListener = new PropertyChangeHandler();
1126        table.addPropertyChangeListener(propertyChangeListener);
1127    }    }
1128    
1129    protected void uninstallDefaults()    protected void uninstallDefaults()
# Line 950  public class BasicTableUI Line 1148  public class BasicTableUI
1148    
1149    protected void uninstallKeyboardActions()    protected void uninstallKeyboardActions()
1150    {    {
1151        // TODO: Implement this properly.
1152    }    }
1153    
1154    protected void uninstallListeners()    protected void uninstallListeners()
# Line 958  public class BasicTableUI Line 1157  public class BasicTableUI
1157      table.removeKeyListener(keyListener);      table.removeKeyListener(keyListener);
1158      table.removeMouseListener(mouseInputListener);          table.removeMouseListener(mouseInputListener);    
1159      table.removeMouseMotionListener(mouseInputListener);      table.removeMouseMotionListener(mouseInputListener);
1160        table.removePropertyChangeListener(propertyChangeListener);
1161        propertyChangeListener = null;
1162    }    }
1163    
1164    public void installUI(JComponent comp)    public void installUI(JComponent comp)
1165    {    {
1166      table = (JTable)comp;      table = (JTable)comp;
     focusListener = createFocusListener();    
     mouseInputListener = createMouseInputListener();  
1167      installDefaults();      installDefaults();
1168      installKeyboardActions();      installKeyboardActions();
1169      installListeners();      installListeners();
# Line 977  public class BasicTableUI Line 1176  public class BasicTableUI
1176      uninstallDefaults();          uninstallDefaults();    
1177    }    }
1178    
1179      /**
1180       * Paints a single cell in the table.
1181       *
1182       * @param g The graphics context to paint in
1183       * @param row The row number to paint
1184       * @param col The column number to paint
1185       * @param bounds The bounds of the cell to paint, assuming a coordinate
1186       * system beginning at <code>(0,0)</code> in the upper left corner of the
1187       * table
1188       * @param rend A cell renderer to paint with
1189       * @param data The data to provide to the cell renderer
1190       * @param rowLead The lead selection for the rows of the table.
1191       * @param colLead The lead selection for the columns of the table.
1192       */
1193      void paintCell(Graphics g, int row, int col, Rectangle bounds,
1194                     TableCellRenderer rend, TableModel data,
1195                     int rowLead, int colLead)
1196      {
1197        boolean rowSelAllowed = table.getRowSelectionAllowed();
1198        boolean colSelAllowed = table.getColumnSelectionAllowed();
1199        boolean isSel = false;
1200        if (rowSelAllowed && colSelAllowed || !rowSelAllowed && !colSelAllowed)
1201          isSel = table.isCellSelected(row, col);
1202        else
1203          isSel = table.isRowSelected(row) && table.getRowSelectionAllowed()
1204               || table.isColumnSelected(col) && table.getColumnSelectionAllowed();
1205    
1206        // Determine the focused cell. The focused cell is the cell at the
1207        // leadSelectionIndices of the row and column selection model.
1208        ListSelectionModel rowSel = table.getSelectionModel();
1209        ListSelectionModel colSel = table.getColumnModel().getSelectionModel();
1210        boolean hasFocus = table.hasFocus() && table.isEnabled()
1211                           && rowSel.getLeadSelectionIndex() == row
1212                           && colSel.getLeadSelectionIndex() == col;
1213    
1214        Component comp = rend.getTableCellRendererComponent(table,
1215                                                           data.getValueAt(row, col),
1216                                                           isSel, hasFocus, row, col);
1217        
1218        rendererPane.paintComponent(g, comp, table, bounds);
1219        
1220        // FIXME: this is manual painting of the Caret, why doesn't the
1221        // JTextField take care of this itself?
1222        if (comp instanceof JTextField)
1223          {
1224            Rectangle oldClip = g.getClipBounds();
1225            g.translate(bounds.x, bounds.y);
1226            g.clipRect(0, 0, bounds.width, bounds.height);
1227            ((JTextField)comp).getCaret().paint(g);
1228            g.translate(-bounds.x, -bounds.y);
1229            g.setClip(oldClip);
1230          }
1231      }
1232      
1233    public void paint(Graphics gfx, JComponent ignored)    public void paint(Graphics gfx, JComponent ignored)
1234    {    {
1235      int ncols = table.getColumnCount();      int ncols = table.getColumnCount();
# Line 1002  public class BasicTableUI Line 1255  public class BasicTableUI
1255          y = y0;          y = y0;
1256          TableColumn col = cols.getColumn(c);          TableColumn col = cols.getColumn(c);
1257          int width = col.getWidth();          int width = col.getWidth();
1258          int modelCol = col.getModelIndex();          int halfGapWidth = gap.width / 2;
1259            int halfGapHeight = gap.height / 2;
1260          for (int r = 0; r < nrows && y < ymax; ++r)          for (int r = 0; r < nrows && y < ymax; ++r)
1261            {            {
1262              Rectangle bounds = new Rectangle(x, y, width, height);              Rectangle bounds = new Rectangle(x + halfGapWidth,
1263                if (bounds.intersects(clip))                                               y + halfGapHeight + 1,
1264                {                                               width - gap.width + 1,
1265                  TableCellRenderer rend = table.getCellRenderer(r, c);                                               height - gap.height);
1266                  Component comp = table.prepareRenderer(rend, r, c);              if (bounds.intersects(clip))
1267                  gfx.translate(x, y);                {                                                    
1268                  comp.setBounds(new Rectangle(0, 0, width, height));                  paintCell(gfx, r, c, bounds, table.getCellRenderer(r, c),
1269                  // Set correct border on cell renderer.                            table.getModel(),
1270                  // Only the lead selection cell gets a border                            table.getSelectionModel().getLeadSelectionIndex(),
1271                  if (comp instanceof JComponent)                            table.getColumnModel().getSelectionModel().getLeadSelectionIndex());
                   {  
                     if (table.getSelectionModel().getLeadSelectionIndex() == r  
                         && table.getColumnModel().getSelectionModel().  
                         getLeadSelectionIndex() == c)  
                       ((JComponent) comp).setBorder(highlightCellBorder);  
                     else  
                       ((JComponent) comp).setBorder(cellBorder);  
                   }  
                 comp.paint(gfx);  
                 if (comp instanceof JTextField)  
                   ((JTextField)comp).getCaret().paint(gfx);  
                 gfx.translate(-x, -y);  
1272                }                }
1273                y += height;              y += height;
1274            }            }
1275          x += width;          x += width;
1276        }        }
# Line 1040  public class BasicTableUI Line 1281  public class BasicTableUI
1281    
1282      Color grid = table.getGridColor();          Color grid = table.getGridColor();    
1283    
1284      // paint vertical grid lines          // paint vertical grid lines
1285      if (grid != null && table.getShowVerticalLines())      if (grid != null && table.getShowVerticalLines())
1286        {            {    
1287          x = x0;          x = x0;
# Line 1050  public class BasicTableUI Line 1291  public class BasicTableUI
1291          for (int c = 0; c < ncols && x < xmax; ++c)          for (int c = 0; c < ncols && x < xmax; ++c)
1292            {            {
1293              x += cols.getColumn(c).getWidth();              x += cols.getColumn(c).getWidth();
1294              gfx.drawLine(x - gap.width, y0, x - gap.width, ymax);              gfx.drawLine(x, y0, x, ymax);
1295              paintedLine = true;              paintedLine = true;
1296            }            }
1297          gfx.setColor(save);          gfx.setColor(save);

Legend:
Removed from v.1.5.2.9  
changed lines
  Added in v.1.5.2.10

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26