001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.contrib.view;
019:
020: import java.awt.*;
021: import java.awt.event.*;
022: import java.net.*;
023: import java.applet.*;
024: import java.io.*;
025: import javax.swing.*;
026:
027: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
028: import org.apache.poi.hssf.usermodel.HSSFSheet;
029: import org.apache.poi.hssf.usermodel.HSSFCell;
030:
031: /**
032: * Sheet Viewer - Views XLS files via HSSF. Can be used as an applet with
033: * filename="" or as a applications (pass the filename as the first parameter).
034: * Or you can pass it a URL in a "url" parameter when run as an applet or just
035: * that first parameter must start with http:// and it will guess its a url. I
036: * only tested it as an applet though, so it probably won't work...you fix it.
037: *
038: * @author Andrew C. Oliver
039: * @author Jason Height
040: */
041: public class SViewer extends JApplet {
042: private SViewerPanel panel;
043: boolean isStandalone = false;
044: String filename = null;
045:
046: /**Get a parameter value*/
047: public String getParameter(String key, String def) {
048: return isStandalone ? System.getProperty(key, def)
049: : (getParameter(key) != null ? getParameter(key) : def);
050: }
051:
052: /**Construct the applet*/
053: public SViewer() {
054: }
055:
056: /**Initialize the applet*/
057: public void init() {
058: try {
059: jbInit();
060: } catch (Exception e) {
061: e.printStackTrace();
062: System.exit(1);
063: }
064: }
065:
066: /**Component initialization*/
067: private void jbInit() throws Exception {
068: InputStream i = null;
069: boolean isurl = false;
070: if (filename == null)
071: filename = getParameter("filename");
072:
073: if (filename == null
074: || filename.substring(0, 7).equals("http://")) {
075: isurl = true;
076: if (filename == null)
077: filename = getParameter("url");
078: i = getXLSFromURL(filename);
079: }
080:
081: HSSFWorkbook wb = null;
082: if (isurl) {
083: wb = constructWorkbook(i);
084: } else {
085: wb = constructWorkbook(filename);
086: }
087: panel = new SViewerPanel(wb, false);
088: getContentPane().setLayout(new BorderLayout());
089: getContentPane().add(panel, BorderLayout.CENTER);
090: }
091:
092: private HSSFWorkbook constructWorkbook(String filename)
093: throws FileNotFoundException, IOException {
094: HSSFWorkbook wb = null;
095: FileInputStream in = new FileInputStream(filename);
096: wb = new HSSFWorkbook(in);
097: in.close();
098: return wb;
099: }
100:
101: private HSSFWorkbook constructWorkbook(InputStream in)
102: throws IOException {
103: HSSFWorkbook wb = null;
104:
105: wb = new HSSFWorkbook(in);
106: in.close();
107: return wb;
108: }
109:
110: /**Start the applet*/
111: public void start() {
112: }
113:
114: /**Stop the applet*/
115: public void stop() {
116: }
117:
118: /**Destroy the applet*/
119: public void destroy() {
120: }
121:
122: /**Get Applet information*/
123: public String getAppletInfo() {
124: return "Applet Information";
125: }
126:
127: /**Get parameter info*/
128: public String[][] getParameterInfo() {
129: return null;
130: }
131:
132: /**
133: * opens a url and returns an inputstream
134: *
135: */
136: private InputStream getXLSFromURL(String urlstring)
137: throws MalformedURLException, IOException {
138: URL url = new URL(urlstring);
139: URLConnection uc = url.openConnection();
140: String field = uc.getHeaderField(0);
141: for (int i = 0; field != null; i++) {
142: System.out.println(field);
143: field = uc.getHeaderField(i);
144: }
145: BufferedInputStream is = new BufferedInputStream(uc
146: .getInputStream());
147: return is;
148: }
149:
150: /**Main method*/
151: public static void main(String[] args) {
152: SViewer applet = new SViewer();
153: applet.isStandalone = true;
154: applet.filename = args[0];
155: Frame frame;
156: frame = new Frame() {
157: protected void processWindowEvent(WindowEvent e) {
158: super .processWindowEvent(e);
159: if (e.getID() == WindowEvent.WINDOW_CLOSING) {
160: System.exit(0);
161: }
162: }
163:
164: public synchronized void setTitle(String title) {
165: super .setTitle(title);
166: enableEvents(AWTEvent.WINDOW_EVENT_MASK);
167: }
168: };
169: frame.setTitle("Applet Frame");
170: frame.add(applet, BorderLayout.CENTER);
171: applet.init();
172: applet.start();
173: frame.setSize(400, 320);
174: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
175: frame.setLocation((d.width - frame.getSize().width) / 2,
176: (d.height - frame.getSize().height) / 2);
177: frame.setVisible(true);
178: }
179: }
|