KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > contrib > view > SViewer


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19
20 package org.apache.poi.hssf.contrib.view;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.net.*;
25 import java.applet.*;
26 import java.io.*;
27 import javax.swing.*;
28
29 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30 import org.apache.poi.hssf.usermodel.HSSFSheet;
31 import org.apache.poi.hssf.usermodel.HSSFCell;
32
33 /**
34  * Sheet Viewer - Views XLS files via HSSF. Can be used as an applet with
35  * filename="" or as a applications (pass the filename as the first parameter).
36  * Or you can pass it a URL in a "url" parameter when run as an applet or just
37  * that first parameter must start with http:// and it will guess its a url. I
38  * only tested it as an applet though, so it probably won't work...you fix it.
39  *
40  * @author Andrew C. Oliver
41  * @author Jason Height
42  */

43 public class SViewer extends JApplet {
44   private SViewerPanel panel;
45   boolean isStandalone = false;
46   String JavaDoc filename = null;
47
48   /**Get a parameter value*/
49   public String JavaDoc getParameter(String JavaDoc key, String JavaDoc def) {
50     return isStandalone ? System.getProperty(key, def) :
51       (getParameter(key) != null ? getParameter(key) : def);
52   }
53
54   /**Construct the applet*/
55   public SViewer() {
56   }
57
58   /**Initialize the applet*/
59   public void init() {
60     try {
61       jbInit();
62     }
63     catch(Exception JavaDoc e) {
64       e.printStackTrace();
65       System.exit(1);
66     }
67   }
68
69   /**Component initialization*/
70   private void jbInit() throws Exception JavaDoc {
71     InputStream i = null;
72     boolean isurl = false;
73     if (filename == null) filename = getParameter("filename");
74
75     if (filename == null || filename.substring(0,7).equals("http://")) {
76       isurl = true;
77       if (filename == null) filename = getParameter("url");
78       i = getXLSFromURL(filename);
79     }
80
81     HSSFWorkbook wb = null;
82     if (isurl) {
83       wb = constructWorkbook(i);
84     } else {
85       wb = constructWorkbook(filename);
86     }
87     panel = new SViewerPanel(wb, false);
88     getContentPane().setLayout(new BorderLayout());
89     getContentPane().add(panel, BorderLayout.CENTER);
90   }
91
92   private HSSFWorkbook constructWorkbook(String JavaDoc filename) throws FileNotFoundException, IOException {
93     HSSFWorkbook wb = null;
94       FileInputStream in = new FileInputStream(filename);
95       wb = new HSSFWorkbook(in);
96       in.close();
97     return wb;
98   }
99
100   private HSSFWorkbook constructWorkbook(InputStream in) throws IOException {
101     HSSFWorkbook wb = null;
102
103       wb = new HSSFWorkbook(in);
104       in.close();
105     return wb;
106   }
107
108   /**Start the applet*/
109   public void start() {
110   }
111   /**Stop the applet*/
112   public void stop() {
113   }
114   /**Destroy the applet*/
115   public void destroy() {
116   }
117   /**Get Applet information*/
118   public String JavaDoc getAppletInfo() {
119     return "Applet Information";
120   }
121   /**Get parameter info*/
122   public String JavaDoc[][] getParameterInfo() {
123     return null;
124   }
125
126   /**
127    * opens a url and returns an inputstream
128    *
129    */

130   private InputStream getXLSFromURL(String JavaDoc urlstring) throws MalformedURLException, IOException {
131     URL url = new URL(urlstring);
132     URLConnection uc = url.openConnection();
133     String JavaDoc field = uc.getHeaderField(0);
134     for (int i=0;field != null; i++) {
135       System.out.println(field);
136       field = uc.getHeaderField(i);
137   }
138     BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
139     return is;
140   }
141
142
143   /**Main method*/
144   public static void main(String JavaDoc[] args) {
145     SViewer applet = new SViewer();
146     applet.isStandalone = true;
147     applet.filename = args[0];
148     Frame frame;
149     frame = new Frame() {
150       protected void processWindowEvent(WindowEvent e) {
151         super.processWindowEvent(e);
152         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
153           System.exit(0);
154         }
155       }
156       public synchronized void setTitle(String JavaDoc title) {
157         super.setTitle(title);
158         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
159       }
160     };
161     frame.setTitle("Applet Frame");
162     frame.add(applet, BorderLayout.CENTER);
163     applet.init();
164     applet.start();
165     frame.setSize(400,320);
166     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
167     frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
168     frame.setVisible(true);
169   }
170 }
171
Popular Tags