KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > swing > spreadsheet > Applet


1 // ============================================================================
2
// $Id: Applet.java,v 1.4 2005/08/02 23:45:21 davidahall Exp $
3
// Copyright (c) 2004-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.swing.spreadsheet;
34
35 import java.awt.Component JavaDoc;
36 import java.awt.Dimension JavaDoc;
37 import java.awt.Point JavaDoc;
38 import java.awt.event.MouseAdapter JavaDoc;
39 import java.awt.event.MouseEvent JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.net.URL JavaDoc;
43 import javax.swing.Icon JavaDoc;
44 import javax.swing.JApplet JavaDoc;
45 import javax.swing.JDesktopPane JavaDoc;
46 import javax.swing.JMenuItem JavaDoc;
47 import javax.swing.JOptionPane JavaDoc;
48 import javax.swing.JPopupMenu JavaDoc;
49 import javax.swing.JScrollPane JavaDoc;
50 import javax.swing.UIManager JavaDoc;
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 /**
62  * An applet wrapper for Spreadsheet.
63  * <p>
64  * Copyright &copy; 2004-2005 David A. Hall
65  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
66  */

67
68 public class Applet extends JApplet JavaDoc {
69
70     static final long serialVersionUID = 1879123030509834030L;
71
72     public Applet() {}
73
74     private Spreadsheet _sheet;
75     private JScrollPane JavaDoc _pane;
76     private JDesktopPane JavaDoc _desktop;
77     
78     // ------------------------
79
// JApplet Lifecycle
80
// ------------------------
81

82     public void init() {
83         printStartupHeader();
84
85         _desktop = new JDesktopPane JavaDoc();
86         setContentPane(_desktop);
87  
88         _sheet = new Spreadsheet(16,16);
89         _sheet.setPreferredScrollableViewportSize(new Dimension JavaDoc(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 JavaDoc popupMenu = new JPopupMenu JavaDoc("Popup Menu");
97         popupMenu.add(new JMenuItem JavaDoc(controller.getCellRenameCmd()));
98         popupMenu.add(new JMenuItem JavaDoc(controller.getCellFormatCmd()));
99         popupMenu.add(new JMenuItem JavaDoc(controller.getCellTypeCmd()));
100
101         // Add a rightclick mouse listener with a 'standard' popup menu
102
_sheet.addMouseListener(new MouseAdapter JavaDoc() {
103                 public void mousePressed(MouseEvent JavaDoc e) {
104                     // Right clicking should update selection
105
if (e.getButton() == e.BUTTON3) {
106                         Spreadsheet sht = (Spreadsheet) e.getComponent();
107                         Point JavaDoc 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 JavaDoc e) {
119                     if (e.isPopupTrigger()) {
120                         popupMenu.show(e.getComponent(), e.getX(), e.getY());
121                     }
122                 }
123             });
124         
125         
126         _pane = new JScrollPane JavaDoc(_sheet);
127         _desktop.add(_pane, javax.swing.JDesktopPane.DEFAULT_LAYER);
128
129
130         // "$1.showStatus($2)".bind1st(this)
131
_sheet.setStatusHandler(new InvokeMethod(JApplet JavaDoc.class, "showStatus", String JavaDoc.class)
132                                         .bind1st(this).compose(new ArrayUnary()));
133
134         controller.setPromptFunctor(buildPromptFunctor());
135         controller.setErrorFunctor(buildErrorFunctor());
136
137         String JavaDoc doc = getParameter("worksheet");
138         if (doc != null) {
139             try {
140                 URL JavaDoc docURL = new URL JavaDoc(getDocumentBase(), doc);
141                 Object JavaDoc obj = docURL.getContent();
142                 if (obj instanceof InputStream JavaDoc) {
143                     _sheet.readSpreadsheet((InputStream JavaDoc) obj);
144                 }
145             }
146             catch (IOException JavaDoc 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 JavaDoc,String JavaDoc,String JavaDoc> buildPromptFunctor() {
171         // Unlike Application, the Applet uses showInternalInputDialog, and the convenient
172
// three arg form of showInputDialog is not supported by showInternalInputDialog.
173

174         InvokeMethod<JOptionPane JavaDoc,String JavaDoc> showInput =
175             new InvokeMethod<JOptionPane JavaDoc,String JavaDoc>( JOptionPane JavaDoc.class, "showInternalInputDialog",
176                                              new Class JavaDoc[]{Component JavaDoc.class,Object JavaDoc.class,String JavaDoc.class,
177                                                          Integer.TYPE, Icon JavaDoc.class, Object JavaDoc[].class,
178                                                          Object JavaDoc.class});
179         
180         ApplyBinary<String JavaDoc,String JavaDoc> sevenArgs =
181             new ApplyBinary<String JavaDoc,String JavaDoc>(new BinaryFunctor[]
182                { new ConstantBinary<String JavaDoc,String JavaDoc,Component JavaDoc>(_desktop),
183                  new Project1st<String JavaDoc,String JavaDoc>(),
184                  new ConstantBinary<String JavaDoc,String JavaDoc,String JavaDoc>(UIManager.getString("OptionPane.inputDialogTitle")),
185                  new ConstantBinary<String JavaDoc,String JavaDoc,Integer JavaDoc>(JOptionPane.QUESTION_MESSAGE),
186                  new ConstantBinary<String JavaDoc,String JavaDoc,Icon JavaDoc>(null),
187                  new ConstantBinary<String JavaDoc,String JavaDoc,Object JavaDoc[]>(null),
188                  new Project2nd<String JavaDoc,String JavaDoc>()
189                });
190             
191         return showInput.bind1st(null).compose(sevenArgs);
192     }
193
194     
195     public BinaryFunctor<String JavaDoc,String JavaDoc,?> buildErrorFunctor() {
196         // This is fairly similar to the PromptFunctor. This one requires
197
// four parms, two of which are constants.
198

199         InvokeMethod showError =
200             new InvokeMethod(JOptionPane JavaDoc.class, "showInternalMessageDialog",
201                              new Class JavaDoc[]{Component JavaDoc.class, Object JavaDoc.class, String JavaDoc.class, Integer.TYPE});
202
203         ApplyBinary<String JavaDoc,String JavaDoc> fourArgs =
204             new ApplyBinary<String JavaDoc,String JavaDoc>(new BinaryFunctor[]
205                { new ConstantBinary<String JavaDoc,String JavaDoc,Component JavaDoc>(_desktop),
206                  new Project1st<String JavaDoc,String JavaDoc>(),
207                  new Project2nd<String JavaDoc,String JavaDoc>(),
208                  new ConstantBinary<String JavaDoc,String JavaDoc,Integer JavaDoc>(JOptionPane.ERROR_MESSAGE)
209                });
210
211         return showError.bind1st(null).compose(fourArgs);
212     }
213 }
214
Popular Tags