Back to index

wims  3.65+svn20090927
TableOp.java
Go to the documentation of this file.
00001  /*
00002   * @(#)TableOp.java
00003   *
00004   * $Id: TableOp.java,v 1.40 2002/08/08 05:15:09 huaz Exp $
00005   *
00006   * Created on November 5, 2000, 11:21 PM
00007 */
00008 package SharpTools;
00009 import java.awt.*;
00010 import java.awt.event.*;
00011 import javax.swing.*;
00012 import java.util.*;
00013 
00021 public class TableOp implements ActionListener {     
00022     
00023     // these variables correspond to the variables in SharpTools
00024     private SharpTools sharp;
00025     public SharpOptionPane sharpoptionpane;
00026     private JTable table;
00027     private SharpTableModel tableModel;
00028     private History history;
00029     private JToolBar toolBar;
00030     final ImageIcon colwidthIcon=new ImageIcon(getClass().getResource("/images/colwidth.gif"));
00031     //final static private ImageIcon colwidthIcon = SharpTools.getImageIcon("colwidth.gif");
00032     //    private Histogram histo;
00033     
00034     // constructor
00035     TableOp(SharpTools sharp) {
00036        init(sharp);
00037        sharp.checkFindNextState();
00038        
00039        // Identifying the Insert/Delete KeyStroke user can modify this
00040        // to some other Key combination.
00041 
00042        KeyStroke insertrow = KeyStroke.getKeyStroke
00043            (KeyEvent.VK_INSERT,
00044             ActionEvent.CTRL_MASK,
00045             false);
00046 
00047        KeyStroke insertcolumn = KeyStroke.getKeyStroke
00048            (KeyEvent.VK_INSERT,
00049             ActionEvent.CTRL_MASK|ActionEvent.SHIFT_MASK,
00050             false);
00051 
00052        KeyStroke deleterow = KeyStroke.getKeyStroke
00053            (KeyEvent.VK_DELETE,
00054             ActionEvent.CTRL_MASK,
00055             false);
00056 
00057        KeyStroke deletecolumn = KeyStroke.getKeyStroke
00058            (KeyEvent.VK_DELETE,
00059             ActionEvent.CTRL_MASK|ActionEvent.SHIFT_MASK,
00060             false);
00061 
00062        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Insert_Row"),insertrow,
00063                                  JComponent.WHEN_FOCUSED);
00064 
00065        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Insert_Column"),insertcolumn,
00066                                  JComponent.WHEN_FOCUSED);
00067 
00068        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Delete_Row"),deleterow,
00069                                  JComponent.WHEN_FOCUSED);
00070 
00071        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Delete_Column"),deletecolumn,
00072                                  JComponent.WHEN_FOCUSED);
00073        
00074     }
00075 
00081     private void init(SharpTools sharp) {
00082        this.sharp = sharp;
00083        table = sharp.getTable();
00084        tableModel = sharp.getTableModel();
00085        history = sharp.getHistory();
00086        toolBar = sharp.getToolBar();
00087     }
00088     
00089     
00090     /* insert a row or column */
00091     public void insert(boolean byRow) {
00092         //checks if anything is selected
00093        int insertNum = table.getSelectedRowCount();
00094         if (insertNum != 0) {
00095            
00096             if (byRow) {
00097               int row = table.getSelectedRow();
00098               CellRange range =
00099                   new CellRange(new CellPoint(row, SharpTools.baseCol),
00100                               new CellPoint(row+insertNum-1,
00101                                           tableModel.getColumnCount()-1));
00102               history.add(tableModel, range, History.INSERTROW);
00103                tableModel.insertRow(range);
00104             }
00105            else {
00106               insertNum = table.getSelectedColumnCount();
00107               int column = table.getSelectedColumn();
00108               CellRange range =
00109                   new CellRange(new CellPoint(SharpTools.baseRow, column),
00110                               new CellPoint(tableModel.getRowCount()-1,
00111                                           column+insertNum-1));
00112 
00113               history.add(tableModel, range, History.INSERTCOLUMN);
00114                tableModel.insertColumn(range);
00115                 
00116             }
00117         }else { 
00118            sharp.noCellsSelected("Insert");
00119         }
00120     }
00121 
00122     public void remove(boolean byRow) {
00123        int removeNum = table.getSelectedRowCount();
00124        
00125         //checks if anything is selected
00126        
00127         if (removeNum != 0) {
00128 
00129             if (byRow) {
00130 
00131               if (removeNum == table.getRowCount()-1) {
00132                   tooMuchDeletion();
00133                   return;
00134               }
00135               
00136               int row = table.getSelectedRow();
00137               CellRange range =
00138                   new CellRange(new CellPoint(row, SharpTools.baseCol),
00139                               new CellPoint(row+removeNum-1,
00140                                           tableModel.getColumnCount()-1));
00141 
00142               if (tableModel.isDeletionSafe(range, true) || unsafeDeletion()) {
00143                   history.add(tableModel, range, History.REMOVEROW);
00144                   Debug.println(range);
00145                   tableModel.removeRow(range);
00146                     Debug.println("Delete row range "+range);
00147               }
00148             }
00149            else {
00150               int column = table.getSelectedColumn();
00151 
00152               removeNum = table.getSelectedColumnCount();
00153 
00154               if (removeNum == table.getColumnCount()-1) {
00155                   tooMuchDeletion();
00156                   return;
00157               }
00158               
00159               CellRange range =
00160                   new CellRange(new CellPoint(SharpTools.baseRow, column),
00161                               new CellPoint(tableModel.getRowCount()-1,
00162                                           column+removeNum-1));
00163                 Debug.println("Delete column range "+range);
00164               if (tableModel.isDeletionSafe(range, false) || unsafeDeletion()) {
00165                   history.add(tableModel, range, History.REMOVECOLUMN);
00166                   Debug.println(range);
00167                   tableModel.removeColumn(range);
00168               }
00169             }        
00170               
00171         }else { 
00172            sharp.noCellsSelected(SharpTools.translation.getProperty("Remove"));
00173         }
00174     }
00175     
00176     public void sort(boolean byRow) {
00177        //checks if anything is selected   
00178        if (table.getSelectedRowCount() != 0) { 
00179            CellRange range = new CellRange
00180               (table.getSelectedRows(), table.getSelectedColumns());
00181              
00182 
00183            //gets parameters for combo box in dialog
00184            Vector primary = new Vector();
00185            Vector secondary = new Vector();
00186             secondary.add("None");
00187            if (byRow) {
00188               for(int i = range.getStartRow(); i <= range.getEndRow(); i++) {
00189                   primary.add(SharpTools.translation.getProperty("Row")+" "+ Node.translateRow(i));
00190                   secondary.add(SharpTools.translation.getProperty("Row")+" "+ Node.translateRow(i));
00191               }
00192            }else {
00193               for(int i = range.getStartCol(); i <= range.getEndCol(); i++) {
00194                   primary.add(SharpTools.translation.getProperty("Column")+" "+ Node.translateColumn(i));
00195                   secondary.add(SharpTools.translation.getProperty("Column")+" "+ Node.translateColumn(i));
00196               }
00197            }
00198 
00199            //create and show the sort dialog
00200            SortDialog sortDialog = new SortDialog(sharp, primary, secondary);
00201            sortDialog.pack();
00202            sortDialog.setLocationRelativeTo(sharp);
00203            sortDialog.setVisible(true);
00204        
00205            
00206            Debug.println("Here we go " + sortDialog.getCriteriaA() + ": And : " + sortDialog.getCriteriaB());
00207            Debug.println(sortDialog.firstAscending() + " : " + sortDialog.secondAscending());
00208            int first = sortDialog.getCriteriaA();
00209            //check to see if there is sorting criteria
00210            if (first >= 0) {
00211                 history.add(tableModel, range);    
00212               //translate first to col/row number
00213               if (byRow) {
00214                   first += range.getStartRow();
00215               }else {
00216                   first += range.getStartCol();
00217               }
00218 
00219               int second = sortDialog.getCriteriaB();
00220               //check to see if there is second sorting criteria
00221               if (second > 0) {
00222                   //translate second to col/row number
00223                   if (byRow) {
00224                      //takes into account "none" choice
00225                      second += range.getStartRow() - 1;
00226                   }else {
00227                      second += range.getStartCol() -  1;
00228                   }
00229 
00230                   tableModel.sort(range, first, second, byRow, 
00231                                 sortDialog.firstAscending(),
00232                                 sortDialog.secondAscending());
00233               }else {
00234                   //sort according to first only
00235                   tableModel.sort(range, first, first, byRow,
00236                                 sortDialog.firstAscending(),
00237                                 sortDialog.secondAscending());
00238               }
00239            }
00240        } else {
00241             sharp.noCellsSelected(SharpTools.translation.getProperty("Sort"));
00242         }
00243     }
00244 
00245     // set the column width
00246     public void setColumnWidth() {
00247        int width;
00248        if(!SharpTools.inapplet){
00249            width = SharpTools.getConfig().getInt("COLUMNWIDTH");
00250        }
00251        else
00252        {
00253            width = MainApplet.COLUMNWIDTH;
00254        }
00255         String inValue = JOptionPane.showInputDialog(SharpTools.translation.getProperty("Default_width")+"\n"+SharpTools.translation.getProperty("rem1")+":");
00256        //System.out.println("ingetiept:"+inValue);
00257        if (inValue != null) {
00258            try {
00259               int newwidth = Integer.parseInt(inValue,10);
00260               if (newwidth != width && newwidth>=0) {
00261                   if(!SharpTools.inapplet){
00262                      SharpTools.getConfig().setInt("COLUMNWIDTH", newwidth);
00263                   }
00264                   sharp.setColumnWidth(newwidth);
00265               }
00266            }
00267            catch (Exception e) {
00268            }
00269        }
00270     }
00271 
00272     public void setFirstColumnWidth() {
00273        int width;
00274        if(!SharpTools.inapplet){
00275            width = SharpTools.getConfig().getInt("FIRSTCOLUMNWIDTH");
00276        }
00277        else
00278        {
00279            width = MainApplet.FIRSTCOLUMNWIDTH;
00280        }
00281         String inValue = JOptionPane.showInputDialog(SharpTools.translation.getProperty("Default_width")+"\n"+SharpTools.translation.getProperty("rem2")+":");
00282        //System.out.println("ingetiept:"+inValue);
00283        if (inValue != null) {
00284            try {
00285               int newwidth = Integer.parseInt(inValue,10);
00286               if (newwidth != width && newwidth>=0) {
00287                   if(!SharpTools.inapplet){
00288                      SharpTools.getConfig().setInt("FIRSTCOLUMNWIDTH", newwidth);
00289                   }
00290                   sharp.setBaseColumnWidth(newwidth);
00291               }
00292            }
00293            catch (Exception e) {
00294            }
00295        }
00296     }
00297     
00298     public int getColumnWidth(){
00299        int width=0;
00300        return width;
00301     }
00302     
00303     private boolean unsafeDeletion() {
00304         int choice = sharpoptionpane.showOptionDialog(
00305               sharp,
00306               SharpTools.translation.getProperty("rem4") +"\n"+SharpTools.translation.getProperty("rem3")+"\n\n",
00307               SharpTools.translation.getProperty("Delete"),
00308               JOptionPane.YES_NO_OPTION,
00309                JOptionPane.WARNING_MESSAGE,
00310                null, 
00311               1
00312            );
00313 
00314        return choice == JOptionPane.YES_OPTION;
00315     }
00316 
00317     private void tooMuchDeletion() {
00318         sharpoptionpane.showMessageDialog
00319            (sharp, SharpTools.translation.getProperty("rem5"),SharpTools.translation.getProperty("Delete"),
00320             JOptionPane.ERROR_MESSAGE);
00321     }
00322 
00333     public void actionPerformed(ActionEvent e) {
00334 
00335        if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Insert_Row"))==0) {
00336            insert(true);
00337        }
00338        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Insert_Column"))==0) {
00339            insert(false);
00340        }      
00341        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Delete_Row"))==0) {
00342            remove(true);
00343        }      
00344        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Delete_Column"))==0) {
00345            remove(false);
00346        }             
00347     }
00348     
00349 }