001: // ============================================================================
002: // $Id: Applet.java,v 1.4 2005/08/02 23:45:21 davidahall Exp $
003: // Copyright (c) 2004-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.swing.spreadsheet;
034:
035: import java.awt.Component;
036: import java.awt.Dimension;
037: import java.awt.Point;
038: import java.awt.event.MouseAdapter;
039: import java.awt.event.MouseEvent;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.net.URL;
043: import javax.swing.Icon;
044: import javax.swing.JApplet;
045: import javax.swing.JDesktopPane;
046: import javax.swing.JMenuItem;
047: import javax.swing.JOptionPane;
048: import javax.swing.JPopupMenu;
049: import javax.swing.JScrollPane;
050: import javax.swing.UIManager;
051: import net.sf.jga.fn.BinaryFunctor;
052: import net.sf.jga.fn.adaptor.ApplyBinary;
053: import net.sf.jga.fn.adaptor.ConstantBinary;
054: import net.sf.jga.fn.adaptor.Project1st;
055: import net.sf.jga.fn.adaptor.Project2nd;
056: import net.sf.jga.fn.property.ArrayUnary;
057: import net.sf.jga.fn.property.InvokeMethod;
058: import net.sf.jga.parser.FunctorParser;
059: import net.sf.jga.parser.ParseException;
060:
061: /**
062: * An applet wrapper for Spreadsheet.
063: * <p>
064: * Copyright © 2004-2005 David A. Hall
065: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
066: */
067:
068: public class Applet extends JApplet {
069:
070: static final long serialVersionUID = 1879123030509834030L;
071:
072: public Applet() {
073: }
074:
075: private Spreadsheet _sheet;
076: private JScrollPane _pane;
077: private JDesktopPane _desktop;
078:
079: // ------------------------
080: // JApplet Lifecycle
081: // ------------------------
082:
083: public void init() {
084: printStartupHeader();
085:
086: _desktop = new JDesktopPane();
087: setContentPane(_desktop);
088:
089: _sheet = new Spreadsheet(16, 16);
090: _sheet.setPreferredScrollableViewportSize(new Dimension(400,
091: 250));
092: _sheet.setEditableByDefault(true);
093: _sheet.setRowSelectionInterval(0, 0);
094: _sheet.setColumnSelectionInterval(0, 0);
095:
096: Controller controller = new Controller(_sheet);
097:
098: final JPopupMenu popupMenu = new JPopupMenu("Popup Menu");
099: popupMenu.add(new JMenuItem(controller.getCellRenameCmd()));
100: popupMenu.add(new JMenuItem(controller.getCellFormatCmd()));
101: popupMenu.add(new JMenuItem(controller.getCellTypeCmd()));
102:
103: // Add a rightclick mouse listener with a 'standard' popup menu
104: _sheet.addMouseListener(new MouseAdapter() {
105: public void mousePressed(MouseEvent e) {
106: // Right clicking should update selection
107: if (e.getButton() == e.BUTTON3) {
108: Spreadsheet sht = (Spreadsheet) e.getComponent();
109: Point p = e.getPoint();
110: int row = sht.rowAtPoint(p);
111: int col = sht.columnAtPoint(p);
112: sht.setRowSelectionInterval(row, row);
113: sht.setColumnSelectionInterval(col, col);
114:
115: if (e.isPopupTrigger()) {
116: popupMenu.show(e.getComponent(), p.x, p.y);
117: }
118: }
119: }
120:
121: public void mouseReleased(MouseEvent e) {
122: if (e.isPopupTrigger()) {
123: popupMenu
124: .show(e.getComponent(), e.getX(), e.getY());
125: }
126: }
127: });
128:
129: _pane = new JScrollPane(_sheet);
130: _desktop.add(_pane, javax.swing.JDesktopPane.DEFAULT_LAYER);
131:
132: // "$1.showStatus($2)".bind1st(this)
133: _sheet.setStatusHandler(new InvokeMethod(JApplet.class,
134: "showStatus", String.class).bind1st(this ).compose(
135: new ArrayUnary()));
136:
137: controller.setPromptFunctor(buildPromptFunctor());
138: controller.setErrorFunctor(buildErrorFunctor());
139:
140: String doc = getParameter("worksheet");
141: if (doc != null) {
142: try {
143: URL docURL = new URL(getDocumentBase(), doc);
144: Object obj = docURL.getContent();
145: if (obj instanceof InputStream) {
146: _sheet.readSpreadsheet((InputStream) obj);
147: }
148: } catch (IOException x) {
149: System.err.println(x.getMessage());
150: x.printStackTrace();
151: }
152: }
153: }
154:
155: public void start() {
156: _pane.setBounds(_desktop.getBounds());
157: _pane.setVisible(true);
158: _sheet.requestFocusInWindow();
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: // Unlike Application, the Applet uses showInternalInputDialog, and the convenient
172: // three arg form of showInputDialog is not supported by showInternalInputDialog.
173:
174: InvokeMethod<JOptionPane, String> showInput = new InvokeMethod<JOptionPane, String>(
175: JOptionPane.class, "showInternalInputDialog",
176: new Class[] { Component.class, Object.class,
177: String.class, Integer.TYPE, Icon.class,
178: Object[].class, Object.class });
179:
180: ApplyBinary<String, String> sevenArgs = new ApplyBinary<String, String>(
181: new BinaryFunctor[] {
182: new ConstantBinary<String, String, Component>(
183: _desktop),
184: new Project1st<String, String>(),
185: new ConstantBinary<String, String, String>(
186: UIManager
187: .getString("OptionPane.inputDialogTitle")),
188: new ConstantBinary<String, String, Integer>(
189: JOptionPane.QUESTION_MESSAGE),
190: new ConstantBinary<String, String, Icon>(null),
191: new ConstantBinary<String, String, Object[]>(
192: null), new Project2nd<String, String>() });
193:
194: return showInput.bind1st(null).compose(sevenArgs);
195: }
196:
197: public BinaryFunctor<String, String, ?> buildErrorFunctor() {
198: // This is fairly similar to the PromptFunctor. This one requires
199: // four parms, two of which are constants.
200:
201: InvokeMethod showError = new InvokeMethod(JOptionPane.class,
202: "showInternalMessageDialog", new Class[] {
203: Component.class, Object.class, String.class,
204: Integer.TYPE });
205:
206: ApplyBinary<String, String> fourArgs = new ApplyBinary<String, String>(
207: new BinaryFunctor[] {
208: new ConstantBinary<String, String, Component>(
209: _desktop),
210: new Project1st<String, String>(),
211: new Project2nd<String, String>(),
212: new ConstantBinary<String, String, Integer>(
213: JOptionPane.ERROR_MESSAGE) });
214:
215: return showError.bind1st(null).compose(fourArgs);
216: }
217: }
|