Back to index

wims  3.65+svn20090927
EditOp.java
Go to the documentation of this file.
00001 /*
00002  * @(#)EditOp.java
00003  *
00004  * $Id: EditOp.java,v 1.37 2001/06/03 22:20:47 huaz Exp $
00005  *
00006  * Created on November 19, 2000, 02:16:16 AM
00007  */
00008 package SharpTools;
00009 import javax.swing.*;
00010 import java.util.*;
00011 import java.io.*;
00012 import java.awt.*;
00013 import java.awt.event.*;
00014 import java.awt.datatransfer.*;
00024 public class EditOp implements ActionListener {
00025 
00026     // these variables correspond to the variables in SharpTools
00027     private SharpTools sharp;
00028     private JTable table;
00029     private SharpTableModel tableModel;
00030     private History history;
00031     private JToolBar toolBar;
00032     private CellPoint copyPoint = new CellPoint(SharpTools.baseRow, SharpTools.baseCol);
00033     // jm.evers
00034     private Clipboard my_local_clipboard;
00035     // used for Find and Find Next
00036     //    private String fillValue;
00037     private String findValue;
00038     private boolean matchCase = false;
00039     private boolean matchCell = false;
00040     final ImageIcon fillIcon=new ImageIcon(getClass().getResource("/images/fill32.gif"));                                                  
00041     final ImageIcon findIcon=new ImageIcon(getClass().getResource("/images/find32.gif"));
00042     //final private static ImageIcon fillIcon = SharpTools.getImageIcon("fill32.gif");
00043     //final private static ImageIcon findIcon = SharpTools.getImageIcon("find32.gif");
00044     
00049     EditOp(SharpTools sharp) {
00050        this.sharp = sharp;
00051        table = sharp.getTable();
00052 
00053        // Identifying the undo KeyStroke user can modify this
00054        // to undo on some other Key combination.
00055        KeyStroke undo = KeyStroke.getKeyStroke
00056            (KeyEvent.VK_Z,ActionEvent.CTRL_MASK,false); 
00057 
00058        // Identifying the undo KeyStroke user can modify this
00059        // to undo on some other Key combination.
00060        KeyStroke redo = KeyStroke.getKeyStroke
00061            (KeyEvent.VK_Y,ActionEvent.CTRL_MASK,false);
00062        
00063        // Identifying the cut KeyStroke user can modify this
00064        // to cut on some other Key combination.
00065        KeyStroke cut = KeyStroke.getKeyStroke
00066            (KeyEvent.VK_X,ActionEvent.CTRL_MASK,false);
00067        
00068        // Identifying the copy KeyStroke user can modify this
00069        // to copy on some other Key combination.
00070        KeyStroke copy = KeyStroke.getKeyStroke
00071            (KeyEvent.VK_C,ActionEvent.CTRL_MASK,false); 
00072        
00073        // Identifying the Paste KeyStroke user can modify this
00074        // to copy on some other Key combination.
00075        KeyStroke paste = KeyStroke.getKeyStroke
00076            (KeyEvent.VK_V,ActionEvent.CTRL_MASK,false);
00077 
00078        KeyStroke fill = KeyStroke.getKeyStroke
00079            (KeyEvent.VK_L, ActionEvent.CTRL_MASK, false);
00080        
00081        KeyStroke clear = KeyStroke.getKeyStroke
00082            (KeyEvent.VK_DELETE, 0, false);
00083 
00084        // Identifying the find/findnext KeyStroke user can modify this
00085        // to find on some other Key combination.
00086        KeyStroke find = KeyStroke.getKeyStroke
00087            (KeyEvent.VK_F,ActionEvent.CTRL_MASK,false);
00088        
00089        KeyStroke findnext = KeyStroke.getKeyStroke
00090            (KeyEvent.VK_F3,0,false);
00091        
00092        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Undo"), undo,
00093                                  JComponent.WHEN_FOCUSED);
00094 
00095        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Redo"), redo,
00096                                  JComponent.WHEN_FOCUSED);     
00097        
00098        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Cut"), cut,
00099                                  JComponent.WHEN_FOCUSED);
00100 
00101        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Copy"), copy,
00102                                  JComponent.WHEN_FOCUSED);
00103 
00104        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Paste"),paste,
00105                                  JComponent.WHEN_FOCUSED);
00106 
00107        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Fill"),fill,
00108                                  JComponent.WHEN_FOCUSED);
00109        
00110        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Clear"),clear,
00111                                  JComponent.WHEN_FOCUSED);
00112 
00113        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Find"),find,
00114                                  JComponent.WHEN_FOCUSED);
00115        
00116        table.registerKeyboardAction(this,SharpTools.translation.getProperty("Find_Next"),findnext,
00117                                  JComponent.WHEN_FOCUSED);
00118 
00119 
00120        init(sharp);
00121     }
00122 
00130     public void init(SharpTools sharp) {
00131        tableModel = sharp.getTableModel();
00132        history = sharp.getHistory();
00133     }
00134     
00140     public void cut(){
00141        doCopy(true); //sets isCut to true
00142     }
00143 
00147     public void copy() {
00148        doCopy(false); //sets isCut to false
00149     }
00150     
00156     private void doCopy(boolean isCut) {
00157        if (table.getSelectedRowCount() != 0) { 
00158            CellRange range = new CellRange(
00159                          table.getSelectedRows(), table.getSelectedColumns());
00160 
00161            if (isCut) {
00162               history.add(tableModel, range);
00163            }      
00164 
00165            //     toolBar.getComponent(8).setEnabled(true);
00166            // now do the copy operation
00167            StringBuffer sbf=new StringBuffer();
00168 
00169            int startRow = table.getSelectedRow();
00170            int startCol = table.getSelectedColumn();
00171            
00172            int numrows=table.getSelectedRowCount();
00173            int numcols=table.getSelectedColumnCount();
00174            
00175            copyPoint = new CellPoint(table.getSelectedRow(),
00176                                   table.getSelectedColumn());
00177 
00178            String str = tableModel.toString(range, false);
00179            StringSelection stsel = new StringSelection(str);
00180            //jm.evers using a local clipboard to avoid jar-signing
00181            //Clipboard clipboard = new Clipboard ("name").
00182            my_local_clipboard = new Clipboard("my_local_clipboard");
00183            my_local_clipboard.setContents(stsel,stsel);
00184 
00185            if (isCut)
00186               tableModel.clearRange(range);
00187            
00188        } else {
00189             if (isCut) {
00190                sharp.noCellsSelected(SharpTools.translation.getProperty("Cut"));
00191             }else {
00192                 sharp.noCellsSelected(SharpTools.translation.getProperty("Copy"));
00193            }
00194         }
00195     }
00196 
00200     public void paste(){
00201         //checks if anything is selected  
00202         if (table.getSelectedRowCount() != 0) {
00203            int startRow=table.getSelectedRow();
00204            int startCol=table.getSelectedColumn(); 
00205            
00206            int rowOff = startRow-copyPoint.getRow();
00207            int colOff = startCol-copyPoint.getCol();
00208            
00209            try {
00210               
00211               String trstring=
00212                   (String)(my_local_clipboard.getContents(this).
00213                           getTransferData(DataFlavor.stringFlavor));
00214 
00215               CellPoint size = SharpTableModel.getSize(trstring);
00216               int endRow = Math.min(table.getRowCount()-1,
00217                                   startRow+size.getRow()-1);
00218               int endCol = Math.min(table.getColumnCount()-1,
00219                                   startCol+size.getCol()-1);
00220 
00221               CellRange affectedRange = new CellRange(startRow, endRow,
00222                                                  startCol, endCol);
00223               // add to history
00224               history.add(tableModel, affectedRange);
00225               //            System.out.println(affectedRange);
00226               
00227               tableModel.fromString(trstring, rowOff, colOff, affectedRange);
00228            }
00229            catch(Exception e){
00230                      System.out.println(e.toString());
00231            }
00232        } else {
00233            sharp.noCellsSelected(SharpTools.translation.getProperty("Paste"));
00234        }
00235     }    
00239     public void clear() {
00240        //checks if anything is selected
00241        if (table.getSelectedRowCount() != 0) {
00242            CellRange range = new CellRange(
00243                       table.getSelectedRows(), table.getSelectedColumns());
00244            history.add(tableModel, range);
00245            Debug.println("Clear");
00246            tableModel.clearRange(range);
00247        } else { 
00248            sharp.noCellsSelected(SharpTools.translation.getProperty("Clear"));
00249        }
00250     }
00251 
00252 
00256     public void fill() {
00257        //checks if anything is selected
00258        if (table.getSelectedRowCount() != 0) {
00259            CellRange range = new CellRange(
00260                          table.getSelectedRows(), table.getSelectedColumns());
00261 
00262            Cell first = tableModel.getCellAt(range.getStartRow(),
00263                                          range.getStartCol());
00264            String fillValue = null;
00265            if (first.isFormula())
00266               fillValue = "="+first.getFormula().toString();
00267            else
00268               fillValue = first.getValue().toString();
00269            
00270            Object inputValue =
00271               SharpOptionPane.showInputDialog
00272               (sharp, SharpTools.translation.getProperty("Please_enter_a_value_to_fill_the_range"),
00273                SharpTools.translation.getProperty("Fill"), JOptionPane.INFORMATION_MESSAGE,
00274                fillIcon, null, fillValue);
00275 
00276            //if input is cancelled or nothing is entered 
00277            //then don't change anything
00278            if (inputValue != null){
00279               if (((String)inputValue).length() != 0) {
00280                   history.add(tableModel, range);
00281                   tableModel.fillRange(range, (String)inputValue);
00282                   //            fillValue = inputValue;
00283               }
00284            }
00285        }else { 
00286            sharp.noCellsSelected(SharpTools.translation.getProperty("Fill"));
00287        }
00288     }
00289 
00296     public void find(boolean newValue) {
00297         CellPoint start;
00298         
00299        //checks if anything is selected
00300 
00301        if (table.getSelectedRowCount() != 0) {
00302            int x = table.getSelectedRow();
00303            int y = table.getSelectedColumn();
00304            // start from the next cell
00305            if (!newValue)
00306               if (y < table.getColumnCount()-1)
00307                   y++;
00308               else {
00309                   y = 1;
00310                   x++;
00311               }
00312            
00313            start = new CellPoint(x, y);
00314            
00315         }
00316        else {
00317            // or start from the beginning
00318             start = new CellPoint(SharpTools.baseRow, SharpTools.baseCol);
00319         }
00320 
00321        if (newValue) {
00322            // ask for new value
00323            //String inputValue = SharpOptionPane.showInputDialog
00324               //(sharp, "Find: ", "Find", findIcon, findValue, 10);
00325            FindDialog findDialog = new FindDialog(sharp, findValue, matchCase, matchCell);
00326            findDialog.pack();
00327            findDialog.setLocationRelativeTo(sharp);
00328            findDialog.setVisible(true);
00329 
00330            String inputValue = findDialog.getString();
00331            /*     
00332            matchCase = findDialog.isCaseSensitive();
00333            matchCell = findDialog.isCellMatching();
00334 
00335            Debug.println("case sens : " + findDialog.isCaseSensitive());
00336            Debug.println("match cell : " + findDialog.isCellMatching());
00337            */
00338            //if input is cancelled or nothing is entered 
00339            //then don't change anything
00340            if (inputValue == null || inputValue.length() == 0)
00341               return;
00342            else {
00343               setFindValue(inputValue);
00344               matchCase = findDialog.isCaseSensitive();
00345               matchCell = findDialog.isCellMatching();
00346 
00347               Debug.println("case sens : " + findDialog.isCaseSensitive());
00348               Debug.println("match cell : " + findDialog.isCellMatching());
00349            }
00350               
00351        }
00352        else if (!hasFindValue())
00353            return;
00354 
00355        CellPoint found = tableModel.look(start, SharpTableModel.fieldParser(findValue), matchCase, matchCell);
00356        if (found != null) {
00357            //System.out.println(found);
00358            table.setColumnSelectionInterval(found.getCol(), 
00359                                         found.getCol());
00360            table.setRowSelectionInterval(found.getRow(),
00361                                      found.getRow());
00362            // set it visible
00363            table.scrollRectToVisible
00364               (new Rectangle
00365                   (table.getCellRect(found.getRow(), found.getCol(), true)));
00366        }      
00367        else {
00368            SharpOptionPane.showMessageDialog
00369               (sharp, 
00370               SharpTools.translation.getProperty("SharpTools_has_finished_the_search") + "  : "+ findValue ,
00371                SharpTools.translation.getProperty("Find_Completed"),
00372                JOptionPane.INFORMATION_MESSAGE, findIcon); 
00373        }
00374     }
00375 
00381     public boolean hasFindValue() {
00382        return findValue != null && findValue.length() != 0;
00383     }
00384 
00385     private void setFindValue(String s) {
00386        findValue = s;
00387        sharp.checkFindNextState();
00388     }
00389     
00395     /*
00396       public boolean hasClipboard() {
00397       return 
00398       }
00399     */
00400     
00411     public void actionPerformed(ActionEvent e) {
00412 
00413        if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Undo"))==0) {
00414            history.undo(tableModel);
00415        }
00416        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Redo"))==0) {
00417            history.redo(tableModel);
00418        }      
00419        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Cut"))==0) {
00420            cut();
00421        }      
00422        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Copy"))==0) {
00423            copy();
00424        }             
00425        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Paste"))==0) {
00426            paste();
00427        }
00428        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Fill"))==0) {
00429            fill();
00430        }
00431        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Find"))==0) {
00432            find(true);
00433        }
00434        else if (e.getActionCommand().compareTo(SharpTools.translation.getProperty("Find_Next"))==0) {
00435            find(false);
00436        }
00437     }
00438 }
00439