1 33 package net.sf.jga.swing.spreadsheet; 34 35 import java.awt.Component ; 36 import java.awt.Dimension ; 37 import java.awt.Point ; 38 import java.awt.event.MouseAdapter ; 39 import java.awt.event.MouseEvent ; 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 import java.net.URL ; 43 import javax.swing.Icon ; 44 import javax.swing.JApplet ; 45 import javax.swing.JDesktopPane ; 46 import javax.swing.JMenuItem ; 47 import javax.swing.JOptionPane ; 48 import javax.swing.JPopupMenu ; 49 import javax.swing.JScrollPane ; 50 import javax.swing.UIManager ; 51 import net.sf.jga.fn.BinaryFunctor; 52 import net.sf.jga.fn.adaptor.ApplyBinary; 53 import net.sf.jga.fn.adaptor.ConstantBinary; 54 import net.sf.jga.fn.adaptor.Project1st; 55 import net.sf.jga.fn.adaptor.Project2nd; 56 import net.sf.jga.fn.property.ArrayUnary; 57 import net.sf.jga.fn.property.InvokeMethod; 58 import net.sf.jga.parser.FunctorParser; 59 import net.sf.jga.parser.ParseException; 60 61 67 68 public class Applet extends JApplet { 69 70 static final long serialVersionUID = 1879123030509834030L; 71 72 public Applet() {} 73 74 private Spreadsheet _sheet; 75 private JScrollPane _pane; 76 private JDesktopPane _desktop; 77 78 82 public void init() { 83 printStartupHeader(); 84 85 _desktop = new JDesktopPane (); 86 setContentPane(_desktop); 87 88 _sheet = new Spreadsheet(16,16); 89 _sheet.setPreferredScrollableViewportSize(new Dimension (400,250)); 90 _sheet.setEditableByDefault(true); 91 _sheet.setRowSelectionInterval(0,0); 92 _sheet.setColumnSelectionInterval(0,0); 93 94 Controller controller = new Controller(_sheet); 95 96 final JPopupMenu popupMenu = new JPopupMenu ("Popup Menu"); 97 popupMenu.add(new JMenuItem (controller.getCellRenameCmd())); 98 popupMenu.add(new JMenuItem (controller.getCellFormatCmd())); 99 popupMenu.add(new JMenuItem (controller.getCellTypeCmd())); 100 101 _sheet.addMouseListener(new MouseAdapter () { 103 public void mousePressed(MouseEvent e) { 104 if (e.getButton() == e.BUTTON3) { 106 Spreadsheet sht = (Spreadsheet) e.getComponent(); 107 Point p = e.getPoint(); 108 int row = sht.rowAtPoint(p); 109 int col = sht.columnAtPoint(p); 110 sht.setRowSelectionInterval(row,row); 111 sht.setColumnSelectionInterval(col,col); 112 113 if (e.isPopupTrigger()) { 114 popupMenu.show(e.getComponent(), p.x, p.y); 115 } 116 } 117 } 118 public void mouseReleased(MouseEvent e) { 119 if (e.isPopupTrigger()) { 120 popupMenu.show(e.getComponent(), e.getX(), e.getY()); 121 } 122 } 123 }); 124 125 126 _pane = new JScrollPane (_sheet); 127 _desktop.add(_pane, javax.swing.JDesktopPane.DEFAULT_LAYER); 128 129 130 _sheet.setStatusHandler(new InvokeMethod(JApplet .class, "showStatus", String .class) 132 .bind1st(this).compose(new ArrayUnary())); 133 134 controller.setPromptFunctor(buildPromptFunctor()); 135 controller.setErrorFunctor(buildErrorFunctor()); 136 137 String doc = getParameter("worksheet"); 138 if (doc != null) { 139 try { 140 URL docURL = new URL (getDocumentBase(), doc); 141 Object obj = docURL.getContent(); 142 if (obj instanceof InputStream ) { 143 _sheet.readSpreadsheet((InputStream ) obj); 144 } 145 } 146 catch (IOException x) { 147 System.err.println(x.getMessage()); 148 x.printStackTrace(); 149 } 150 } 151 } 152 153 154 public void start() { 155 _pane.setBounds(_desktop.getBounds()); 156 _pane.setVisible(true); 157 _sheet.requestFocusInWindow(); 158 } 159 160 161 static private void printStartupHeader() { 162 System.out.println(""); 163 System.out.println("/**"); 164 System.out.println(" * A Java Hacker's Worksheet"); 165 System.out.println(" * Copyright (c) 2004-2005 David A. Hall"); 166 System.out.println(" */"); 167 System.out.println(""); 168 } 169 170 public BinaryFunctor<String ,String ,String > buildPromptFunctor() { 171 174 InvokeMethod<JOptionPane ,String > showInput = 175 new InvokeMethod<JOptionPane ,String >( JOptionPane .class, "showInternalInputDialog", 176 new Class []{Component .class,Object .class,String .class, 177 Integer.TYPE, Icon .class, Object [].class, 178 Object .class}); 179 180 ApplyBinary<String ,String > sevenArgs = 181 new ApplyBinary<String ,String >(new BinaryFunctor[] 182 { new ConstantBinary<String ,String ,Component >(_desktop), 183 new Project1st<String ,String >(), 184 new ConstantBinary<String ,String ,String >(UIManager.getString("OptionPane.inputDialogTitle")), 185 new ConstantBinary<String ,String ,Integer >(JOptionPane.QUESTION_MESSAGE), 186 new ConstantBinary<String ,String ,Icon >(null), 187 new ConstantBinary<String ,String ,Object []>(null), 188 new Project2nd<String ,String >() 189 }); 190 191 return showInput.bind1st(null).compose(sevenArgs); 192 } 193 194 195 public BinaryFunctor<String ,String ,?> buildErrorFunctor() { 196 199 InvokeMethod showError = 200 new InvokeMethod(JOptionPane .class, "showInternalMessageDialog", 201 new Class []{Component .class, Object .class, String .class, Integer.TYPE}); 202 203 ApplyBinary<String ,String > fourArgs = 204 new ApplyBinary<String ,String >(new BinaryFunctor[] 205 { new ConstantBinary<String ,String ,Component >(_desktop), 206 new Project1st<String ,String >(), 207 new Project2nd<String ,String >(), 208 new ConstantBinary<String ,String ,Integer >(JOptionPane.ERROR_MESSAGE) 209 }); 210 211 return showError.bind1st(null).compose(fourArgs); 212 } 213 } 214 | Popular Tags |