/[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.31 by abalkiss, Thu Oct 13 18:43:50 2005 UTC revision 1.32 by abalkiss, Tue Oct 18 17:48:03 2005 UTC
# Line 57  import javax.swing.AbstractAction; Line 57  import javax.swing.AbstractAction;
57  import javax.swing.ActionMap;  import javax.swing.ActionMap;
58  import javax.swing.BorderFactory;  import javax.swing.BorderFactory;
59  import javax.swing.CellRendererPane;  import javax.swing.CellRendererPane;
60    import javax.swing.DefaultListSelectionModel;
61  import javax.swing.InputMap;  import javax.swing.InputMap;
62  import javax.swing.JComponent;  import javax.swing.JComponent;
63  import javax.swing.JTable;  import javax.swing.JTable;
# Line 437  public class BasicTableUI extends TableU Line 438  public class BasicTableUI extends TableU
438       */       */
439      public void actionPerformed (ActionEvent e)      public void actionPerformed (ActionEvent e)
440      {      {
441        ListSelectionModel rowModel = table.getSelectionModel();        DefaultListSelectionModel rowModel = (DefaultListSelectionModel) table.getSelectionModel();
442        ListSelectionModel colModel = table.getColumnModel().getSelectionModel();        DefaultListSelectionModel colModel = (DefaultListSelectionModel) table.getColumnModel().getSelectionModel();
443    
444        int rowLead = rowModel.getLeadSelectionIndex();        int rowLead = rowModel.getLeadSelectionIndex();
445        int rowMax = table.getModel().getRowCount() - 1;        int rowMax = table.getModel().getRowCount() - 1;
# Line 446  public class BasicTableUI extends TableU Line 447  public class BasicTableUI extends TableU
447        int colLead = colModel.getLeadSelectionIndex();        int colLead = colModel.getLeadSelectionIndex();
448        int colMax = table.getModel().getColumnCount() - 1;        int colMax = table.getModel().getColumnCount() - 1;
449                
450        if (e.getActionCommand().equals("selectPreviousRowExtendSelection"))        String command = e.getActionCommand();
451          
452          if (command.equals("selectPreviousRowExtendSelection"))
453          {          {
454            rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));            rowModel.setLeadSelectionIndex(Math.max(rowLead - 1, 0));
455            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
456          }          }
457        else if (e.getActionCommand().equals("selectLastColumn"))        else if (command.equals("selectLastColumn"))
458          {          {
           table.clearSelection();  
459            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
460            colModel.setSelectionInterval(colMax, colMax);            colModel.setSelectionInterval(colMax, colMax);
461          }          }
462        else if (e.getActionCommand().equals("startEditing"))        else if (command.equals("startEditing"))
463          {          {
464            if (table.isCellEditable(rowLead, colLead))            if (table.isCellEditable(rowLead, colLead))
465              table.editCellAt(rowLead,colLead);              table.editCellAt(rowLead,colLead);
466          }          }
467        else if (e.getActionCommand().equals("selectFirstRowExtendSelection"))        else if (command.equals("selectFirstRowExtendSelection"))
468          {                        {              
469            rowModel.setLeadSelectionIndex(0);            rowModel.setLeadSelectionIndex(0);
470            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
471          }          }
472        else if (e.getActionCommand().equals("selectFirstColumn"))        else if (command.equals("selectFirstColumn"))
473          {          {
474            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
475            colModel.setSelectionInterval(0, 0);            colModel.setSelectionInterval(0, 0);
476          }          }
477        else if (e.getActionCommand().equals("selectFirstColumnExtendSelection"))        else if (command.equals("selectFirstColumnExtendSelection"))
478          {          {
479            colModel.setLeadSelectionIndex(0);            colModel.setLeadSelectionIndex(0);
480            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
481          }          }      
482        else if (e.getActionCommand().equals("selectLastRow"))        else if (command.equals("selectLastRow"))
483          {          {
484            rowModel.setSelectionInterval(rowMax,rowMax);            rowModel.setSelectionInterval(rowMax,rowMax);
485            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
486          }          }
487        else if (e.getActionCommand().equals("selectNextRowExtendSelection"))        else if (command.equals("selectNextRowExtendSelection"))
488          {          {
489            rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));            rowModel.setLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
490            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
491          }          }
492        else if (e.getActionCommand().equals("selectFirstRow"))        else if (command.equals("selectFirstRow"))
493          {          {
494            rowModel.setSelectionInterval(0,0);            rowModel.setSelectionInterval(0,0);
495            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
496          }          }
497        else if (e.getActionCommand().equals("selectNextColumnExtendSelection"))        else if (command.equals("selectNextColumnExtendSelection"))
498          {          {
499            colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));            colModel.setLeadSelectionIndex(Math.min(colLead + 1, colMax));
500            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
501          }          }
502        else if (e.getActionCommand().equals("selectLastColumnExtendSelection"))        else if (command.equals("selectLastColumnExtendSelection"))
503          {          {
504            colModel.setLeadSelectionIndex(colMax);            colModel.setLeadSelectionIndex(colMax);
505            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
506          }          }
507        else if (e.getActionCommand().equals("selectPreviousColumnExtendSelection"))        else if (command.equals("selectPreviousColumnExtendSelection"))
508          {          {
509            colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));            colModel.setLeadSelectionIndex(Math.max(colLead - 1, 0));
510            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
511          }          }
512        else if (e.getActionCommand().equals("selectNextRow"))        else if (command.equals("selectNextRow"))
513          {          {
514            rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),            rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
515                                          Math.min(rowLead + 1, rowMax));                                          Math.min(rowLead + 1, rowMax));
516            colModel.setSelectionInterval(colLead,colLead);            colModel.setSelectionInterval(colLead,colLead);
517          }          }
518        else if (e.getActionCommand().equals("scrollUpExtendSelection"))        else if (command.equals("scrollUpExtendSelection"))
519          {          {
520            int target;            int target;
521            if (rowLead == getFirstVisibleRowIndex())            if (rowLead == getFirstVisibleRowIndex())
# Line 526  public class BasicTableUI extends TableU Line 528  public class BasicTableUI extends TableU
528            rowModel.setLeadSelectionIndex(target);            rowModel.setLeadSelectionIndex(target);
529            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
530          }          }
531        else if (e.getActionCommand().equals("selectPreviousRow"))        else if (command.equals("selectPreviousRow"))
532          {          {
533            rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),            rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
534                                          Math.max(rowLead - 1, 0));                                          Math.max(rowLead - 1, 0));
535            colModel.setSelectionInterval(colLead,colLead);            colModel.setSelectionInterval(colLead,colLead);
536          }          }
537        else if (e.getActionCommand().equals("scrollRightChangeSelection"))        else if (command.equals("scrollRightChangeSelection"))
538          {          {
539            int target;            int target;
540            if (colLead == getLastVisibleColumnIndex())            if (colLead == getLastVisibleColumnIndex())
# Line 545  public class BasicTableUI extends TableU Line 547  public class BasicTableUI extends TableU
547            colModel.setSelectionInterval(target, target);            colModel.setSelectionInterval(target, target);
548            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
549          }          }
550        else if (e.getActionCommand().equals("selectPreviousColumn"))        else if (command.equals("selectPreviousColumn"))
551          {          {
552            rowModel.setSelectionInterval(rowLead,rowLead);            rowModel.setSelectionInterval(rowLead,rowLead);
553            colModel.setSelectionInterval(Math.max(colLead - 1, 0),            colModel.setSelectionInterval(Math.max(colLead - 1, 0),
554                                          Math.max(colLead - 1, 0));                                          Math.max(colLead - 1, 0));
555          }          }
556        else if (e.getActionCommand().equals("scrollLeftChangeSelection"))        else if (command.equals("scrollLeftChangeSelection"))
557          {          {
558            int target;            int target;
559            if (colLead == getFirstVisibleColumnIndex())            if (colLead == getFirstVisibleColumnIndex())
# Line 564  public class BasicTableUI extends TableU Line 566  public class BasicTableUI extends TableU
566            colModel.setSelectionInterval(target, target);            colModel.setSelectionInterval(target, target);
567            rowModel.setSelectionInterval(rowLead, rowLead);            rowModel.setSelectionInterval(rowLead, rowLead);
568          }          }
569        else if (e.getActionCommand().equals("clearSelection"))        else if (command.equals("clearSelection"))
570          {          {
571            table.clearSelection();            table.clearSelection();
572          }          }
573        else if (e.getActionCommand().equals("cancel"))        else if (command.equals("cancel"))
574          {          {
575            // FIXME: implement other parts of "cancel" like undo-ing last            // FIXME: implement other parts of "cancel" like undo-ing last
576            // selection.  Right now it just calls editingCancelled if            // selection.  Right now it just calls editingCancelled if
# Line 576  public class BasicTableUI extends TableU Line 578  public class BasicTableUI extends TableU
578            if (table.isEditing())            if (table.isEditing())
579              table.editingCanceled(new ChangeEvent("cancel"));              table.editingCanceled(new ChangeEvent("cancel"));
580          }          }
581        else if (e.getActionCommand().equals("selectNextRowCell")        else if (command.equals("selectNextRowCell")
582                 || e.getActionCommand().equals("selectPreviousRowCell")                 || command.equals("selectPreviousRowCell")
583                 || e.getActionCommand().equals("selectNextColumnCell")                 || command.equals("selectNextColumnCell")
584                 || e.getActionCommand().equals("selectPreviousColumnCell"))                 || command.equals("selectPreviousColumnCell"))
585          {          {
586            // If nothing is selected, select the first cell in the table            // If nothing is selected, select the first cell in the table
587            if (table.getSelectedRowCount() == 0 &&            if (table.getSelectedRowCount() == 0 &&
# Line 615  public class BasicTableUI extends TableU Line 617  public class BasicTableUI extends TableU
617            // when you get to the edges of the table.            // when you get to the edges of the table.
618            if (!multColsSelected && !multRowsSelected)            if (!multColsSelected && !multRowsSelected)
619              {              {
620                if (e.getActionCommand().indexOf("Column") != -1)                if (command.indexOf("Column") != -1)
621                  advanceSingleSelection(colModel, colMax, rowModel, rowMax,                  advanceSingleSelection(colModel, colMax, rowModel, rowMax,
622                                         (e.getActionCommand().equals                                         (command.equals
623                                          ("selectPreviousColumnCell")));                                          ("selectPreviousColumnCell")));
624                else                else
625                  advanceSingleSelection(rowModel, rowMax, colModel, colMax,                  advanceSingleSelection(rowModel, rowMax, colModel, colMax,
626                                         (e.getActionCommand().equals                                         (command.equals
627                                          ("selectPreviousRowCell")));                                          ("selectPreviousRowCell")));
628                return;                return;
629              }              }
# Line 642  public class BasicTableUI extends TableU Line 644  public class BasicTableUI extends TableU
644                        
645            // If there are multiple rows and columns selected, select the next            // If there are multiple rows and columns selected, select the next
646            // cell and wrap at the edges of the selection.              // cell and wrap at the edges of the selection.  
647            if (e.getActionCommand().indexOf("Column") != -1)            if (command.indexOf("Column") != -1)
648              advanceMultipleSelection(colModel, colMinSelected, colMaxSelected,              advanceMultipleSelection(colModel, colMinSelected, colMaxSelected,
649                                       rowModel, rowMinSelected, rowMaxSelected,                                       rowModel, rowMinSelected, rowMaxSelected,
650                                       (e.getActionCommand().equals                                       (command.equals
651                                        ("selectPreviousColumnCell")), true);                                        ("selectPreviousColumnCell")), true);
652                        
653            else            else
654              advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected,              advanceMultipleSelection(rowModel, rowMinSelected, rowMaxSelected,
655                                       colModel, colMinSelected, colMaxSelected,                                       colModel, colMinSelected, colMaxSelected,
656                                       (e.getActionCommand().equals                                       (command.equals
657                                        ("selectPreviousRowCell")), false);                                        ("selectPreviousRowCell")), false);
658          }          }
659        else if (e.getActionCommand().equals("selectNextColumn"))        else if (command.equals("selectNextColumn"))
660          {          {
661            rowModel.setSelectionInterval(rowLead,rowLead);            rowModel.setSelectionInterval(rowLead,rowLead);
662            colModel.setSelectionInterval(Math.min(colLead + 1, colMax),            colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
663                                          Math.min(colLead + 1, colMax));                                          Math.min(colLead + 1, colMax));
664          }          }
665        else if (e.getActionCommand().equals("scrollLeftExtendSelection"))        else if (command.equals("scrollLeftExtendSelection"))
666          {          {
667            int target;            int target;
668            if (colLead == getFirstVisibleColumnIndex())            if (colLead == getFirstVisibleColumnIndex())
# Line 673  public class BasicTableUI extends TableU Line 675  public class BasicTableUI extends TableU
675            colModel.setLeadSelectionIndex(target);            colModel.setLeadSelectionIndex(target);
676            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
677          }          }
678        else if (e.getActionCommand().equals("scrollDownChangeSelection"))        else if (command.equals("scrollDownChangeSelection"))
679          {          {
680            int target;            int target;
681            if (rowLead == getLastVisibleRowIndex())            if (rowLead == getLastVisibleRowIndex())
# Line 686  public class BasicTableUI extends TableU Line 688  public class BasicTableUI extends TableU
688            rowModel.setSelectionInterval(target, target);            rowModel.setSelectionInterval(target, target);
689            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
690          }          }
691        else if (e.getActionCommand().equals("scrollRightExtendSelection"))        else if (command.equals("scrollRightExtendSelection"))
692          {          {
693            int target;            int target;
694            if (colLead == getLastVisibleColumnIndex())            if (colLead == getLastVisibleColumnIndex())
# Line 699  public class BasicTableUI extends TableU Line 701  public class BasicTableUI extends TableU
701            colModel.setLeadSelectionIndex(target);            colModel.setLeadSelectionIndex(target);
702            rowModel.setLeadSelectionIndex(rowLead);            rowModel.setLeadSelectionIndex(rowLead);
703          }          }
704        else if (e.getActionCommand().equals("selectAll"))        else if (command.equals("selectAll"))
705          {          {
706            table.selectAll();            table.selectAll();
707          }          }
708        else if (e.getActionCommand().equals("selectLastRowExtendSelection"))        else if (command.equals("selectLastRowExtendSelection"))
709          {          {
710            rowModel.setLeadSelectionIndex(rowMax);            rowModel.setLeadSelectionIndex(rowMax);
711            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
712          }          }
713        else if (e.getActionCommand().equals("scrollDownExtendSelection"))        else if (command.equals("scrollDownExtendSelection"))
714          {          {
715            int target;            int target;
716            if (rowLead == getLastVisibleRowIndex())            if (rowLead == getLastVisibleRowIndex())
# Line 720  public class BasicTableUI extends TableU Line 722  public class BasicTableUI extends TableU
722                        
723            rowModel.setLeadSelectionIndex(target);            rowModel.setLeadSelectionIndex(target);
724            colModel.setLeadSelectionIndex(colLead);            colModel.setLeadSelectionIndex(colLead);
725          }          }      
726        else if (e.getActionCommand().equals("scrollUpChangeSelection"))        else if (command.equals("scrollUpChangeSelection"))
727          {          {
728            int target;            int target;
729            if (rowLead == getFirstVisibleRowIndex())            if (rowLead == getFirstVisibleRowIndex())
# Line 734  public class BasicTableUI extends TableU Line 736  public class BasicTableUI extends TableU
736            rowModel.setSelectionInterval(target, target);            rowModel.setSelectionInterval(target, target);
737            colModel.setSelectionInterval(colLead, colLead);            colModel.setSelectionInterval(colLead, colLead);
738          }          }
739          else if (command.equals("selectNextRowChangeLead"))
740              {
741                if (rowModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
742                  {
743                    // just "selectNextRow"
744                    rowModel.setSelectionInterval(Math.min(rowLead + 1, rowMax),
745                                                  Math.min(rowLead + 1, rowMax));
746                    colModel.setSelectionInterval(colLead,colLead);
747                  }
748                else
749                  rowModel.moveLeadSelectionIndex(Math.min(rowLead + 1, rowMax));
750              }
751          else if (command.equals("selectPreviousRowChangeLead"))
752            {
753              if (rowModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
754                {
755                  // just selectPreviousRow
756                  rowModel.setSelectionInterval(Math.max(rowLead - 1, 0),
757                                                Math.min(rowLead -1, 0));
758                  colModel.setSelectionInterval(colLead,colLead);
759                }
760              else
761                rowModel.moveLeadSelectionIndex(Math.max(rowLead - 1, 0));
762            }
763          else if (command.equals("selectNextColumnChangeLead"))
764            {
765              if (colModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)            
766                {
767                  // just selectNextColumn
768                  rowModel.setSelectionInterval(rowLead,rowLead);
769                  colModel.setSelectionInterval(Math.min(colLead + 1, colMax),
770                                                Math.min(colLead + 1, colMax));
771                }
772              else
773                colModel.moveLeadSelectionIndex(Math.min(colLead + 1, colMax));
774            }
775          else if (command.equals("selectPreviousColumnChangeLead"))
776            {
777              if (colModel.getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)            
778                {
779                  // just selectPreviousColumn
780                  rowModel.setSelectionInterval(rowLead,rowLead);
781                  colModel.setSelectionInterval(Math.max(colLead - 1, 0),
782                                                Math.max(colLead - 1, 0));
783                  
784                }
785              else
786                colModel.moveLeadSelectionIndex(Math.max(colLead - 1, 0));
787            }
788          else if (command.equals("addToSelection"))
789              {
790                if (!table.isEditing())
791                  {
792                    int oldRowAnchor = rowModel.getAnchorSelectionIndex();
793                    int oldColAnchor = colModel.getAnchorSelectionIndex();
794                    rowModel.addSelectionInterval(rowLead, rowLead);
795                    colModel.addSelectionInterval(colLead, colLead);
796                    rowModel.setAnchorSelectionIndex(oldRowAnchor);
797                    colModel.setAnchorSelectionIndex(oldColAnchor);
798                  }
799              }
800          else if (command.equals("extendTo"))
801            {
802              rowModel.setSelectionInterval(rowModel.getAnchorSelectionIndex(),
803                                            rowLead);
804              colModel.setSelectionInterval(colModel.getAnchorSelectionIndex(),
805                                            colLead);
806            }
807          else if (command.equals("toggleAndAnchor"))
808            {
809              if (rowModel.isSelectedIndex(rowLead))
810                rowModel.removeSelectionInterval(rowLead, rowLead);
811              else
812                rowModel.addSelectionInterval(rowLead, rowLead);
813              
814              if (colModel.isSelectedIndex(colLead))
815                colModel.removeSelectionInterval(colLead, colLead);
816              else
817                colModel.addSelectionInterval(colLead, colLead);
818              
819              rowModel.setAnchorSelectionIndex(rowLead);
820              colModel.setAnchorSelectionIndex(colLead);
821            }
822        else        else
823          {          {
824            // If we're here that means we bound this TableAction class            // If we're here that means we bound this TableAction class
825            // to a keyboard input but we either want to ignore that input            // to a keyboard input but we either want to ignore that input
826            // or we just haven't implemented its action yet.            // or we just haven't implemented its action yet.
827              
828              // Uncomment the following line to print the names of unused bindings
829              // when their keys are pressed
830              
831              // System.out.println ("not implemented: "+e.getActionCommand());
832          }          }
833    
834        if (table.isEditing() && e.getActionCommand() != "startEditing")        // Any commands whose keyStrokes should be used by the Editor should not
835          table.editingCanceled(new ChangeEvent("update"));        // cause editing to be stopped: ie, the SPACE sends "addToSelection" but
836        table.repaint();        // if the table is in editing mode, the space should not cause us to stop
837                // editing because it should be used by the Editor.
838          if (table.isEditing() && command != "startEditing"
839              && command != "addToSelection")
840            table.editingStopped(new ChangeEvent("update"));
841                
842        table.scrollRectToVisible        table.scrollRectToVisible
843          (table.getCellRect(rowModel.getLeadSelectionIndex(),          (table.getCellRect(rowModel.getLeadSelectionIndex(),
844                             colModel.getLeadSelectionIndex(), false));                             colModel.getLeadSelectionIndex(), false));
845          table.repaint();
846      }      }
847            
848        /**
849         * Returns the column index of the first visible column.
850         * @return the column index of the first visible column.
851         */
852      int getFirstVisibleColumnIndex()      int getFirstVisibleColumnIndex()
853      {      {
854        ComponentOrientation or = table.getComponentOrientation();        ComponentOrientation or = table.getComponentOrientation();

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.32

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