/*
* JDataGridApplet.java
*
* Created on 2006��11��11��, ����8:19
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.zfqjava.swing;
import java.awt.BorderLayout;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
/**
* JDataGridApplet provides a standard applet based
* on the JDataGrid component.
*
* JDataGridApplet support the following parameters:
*
*
* | Parameter Name | Parameter Value | Parameter Description |
*
*
* | openUrl | url | URL load data into datagrid |
*
*
* | saveUrl | url | URL save data from datagrid |
*
*
* | saveFormat | onew of xls, xml, txt, csv, html, htm, ser | The file format to save |
*
*
* | saveFileName | string | The File name to save, the default is the file name of open url |
*
*
* | downloadPath | url | The path to download the saved file, relative to current applet webpage |
*
*
* | licenseUrl | url | The path to load the license data |
*
*
* Note:
*
* - The parameter value "url" is the url you want to load or save data,
* You should change it for your web server.
* - If you have not specified the saveFormat, it will be the format of openUrl file,
* if you have not specified the openUrl, it will not execute the save operation
*
* @author Administrator
*/
public class JDataGridApplet extends JApplet {
private static final Logger log = Logger.getLogger(JDataGridApplet.class.getName());
private URL openUrl;
private URL saveUrl;
private URL downloadPath;
private String saveFileName;
private String saveFormat;
private JDataGrid dataGrid;
private URL licenseUrl;
/** Creates a new instance of JDataGridApplet */
public JDataGridApplet() {
}
public void init() {
fetchParameters();
readLicense();
getContentPane().setLayout(new BorderLayout());
dataGrid = createDataGrid();
getContentPane().add(new JScrollPane(dataGrid), BorderLayout.CENTER);
doOpen();
}
/**
* Factory method to create the JDataGrid.
*/
protected JDataGrid createDataGrid() {
JDataGrid dataGrid = new JDataGrid();
dataGrid.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
dataGrid.setModel(new DefaultDataGridModel(99, 26));
return dataGrid;
}
private void fetchParameters() {
String openUrlString = getParameter("openUrl");
// openUrlString = "products/datagrid/applet/test.xls";
String saveUrlString = getParameter("saveUrl");
String saveFileNameString = getParameter("saveFileName");
String saveFormatString = getParameter("saveFormat");
String downloadPathString = getParameter("downloadPath");
String licenseUrlString = getParameter("licenseUrl");
URL url = this.getDocumentBase();
openUrl = AppletUtils.convertToAbsolute(url, openUrlString);
saveUrl = AppletUtils.convertToAbsolute(url, saveUrlString);
downloadPath = AppletUtils.convertToAbsoluteImpl(url, downloadPathString);
licenseUrl = AppletUtils.convertToAbsolute(url, licenseUrlString);
saveFileName = AppletUtils.getFileName(openUrl, saveFileNameString);
saveFormat = AppletUtils.getFileFormat(saveFileName, saveFormatString);
// saveFormat = checkSaveFormat(saveFormatString);
// System.out.println("documentBase="+this.getDocumentBase());
// System.out.println("codeBase="+this.getCodeBase());
// System.out.println("openUrl="+openUrl);
}
private void readLicense() {
try {
AppletUtils.readLicense(licenseUrl);
} catch (Exception ex) {
if(log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, "readLicense failed", ex);
}
}
}
public String[][] getParameterInfo() {
String pinfo[][] = {
{"openUrl", "url", "URL load data into datagrid, relative to current applet webpage"},
{"saveUrl", "url", "URL save data from datagrid, relative to current applet webpage"},
{"saveFormat", "xls, xml, txt, csv, html, htm, ser", "The file format to save, the default is the file format of open url"},
{"saveFileName", "string", "The File name to save, the default is the file name of open url"},
{"downloadPath", "url", "The path to download the saved file, relative to current applet webpage"},
{"licenseUrl", "url", "The path to load the license data"}
};
return pinfo;
}
private void doOpen() {
if(openUrl == null) {
return;
}
final String f = openUrl.getFile();
SwingWorker worker = new SwingWorker() {
protected Object doInBackground() throws Exception {
Object o = null;
String format = DefaultFileFilter.getExtensionName(f);
if("xls".equals(format) || "xml".equals(format)) {
Map m = new HashMap();
// m.put(ModelIO.PROGRESS_BAR, getStatusBar().getProgressBar());
// m.put(ModelIO.PREFERRED_WORK_BOOK_API, "org.apache.poi.hssf");
WorkBook book = ModelIO.readWorkBook(openUrl, format, m);
o = book;
} else if("txt".equals(format) || "csv".equals(format) || "html".equals(format) || "htm".equals(format)) {
// enable excel loose format
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
// map.put(ModelIO.PROGRESS_BAR, getStatusBar().getProgressBar());
// end
TableModel model = ModelIO.readTableModel(openUrl, format, map);
o = model;
} else if("ser".equals(format)) {
ObjectInputStream ios = null;
try {
// ios = new ObjectInputStream(new BufferedInputStream(new ProgressBarInputStream(getStatusBar().getProgressBar(), new FileInputStream(f))));
ios = new ObjectInputStream(new BufferedInputStream(openUrl.openStream()));
DefaultDataGridModel model = (DefaultDataGridModel)ios.readObject();
o = model;
} finally {
ios.close();
}
}
return o;
}
protected void done() {
try {
Object object = get();
setModel(object);
} catch (Exception ex) {
ex.printStackTrace();
// JOptionPane.showMessageDialog(JDataGridBean.this, ex.getMessage());
}
}
};
worker.execute();
}
private synchronized void setModel(Object model) {
if(dataGrid != null) {
TableModel tableModel = null;
if(model instanceof TableModel) {
tableModel = (TableModel)model;
} else if(model instanceof WorkBook) {
WorkBook workBook = (WorkBook)model;
if(workBook.getSelectedSheet() != null) {
tableModel = workBook.getSelectedSheet().getModel();
} else if(workBook.getSheetCount() > 0) {
tableModel = workBook.getSheet(0).getModel();
}
}
if(tableModel != null) {
dataGrid.setModel(tableModel);
}
}
}
private void doSave() {
if(!DataGridUtils.isFormatSupported(this.saveFormat)) {
if(log.isLoggable(Level.FINER)) {
log.log(Level.FINER, "the saveFormat is not supported: " + this.saveFormat);
}
return;
}
String fileName = AppletUtils.generateRandomFileName(this.saveFileName,this.saveFormat);
if(fileName == null) {
if(log.isLoggable(Level.FINER)) {
log.log(Level.FINER, "fileName is null, cannot execute save operation");
}
return;
}
URL urlObject = this.saveUrl;
if(urlObject == null) {
if(log.isLoggable(Level.FINER)) {
log.log(Level.FINER, "saveUrl is null, cannot execute save operation");
}
return;
}
String format = saveFormat;
TableModel model = dataGrid.getModel();
OutputStream os = null;
InputStream is = null;
try {
URLConnection connection = urlObject.openConnection();
connection.setRequestProperty(FILE_NAME_KEY, fileName);
connection.setDoOutput(true);
os = connection.getOutputStream();
// writeFileName(os, fileName);
if(format.equals("xls") || format.equals("xml")) {
WorkSheet[] sheets = new WorkSheet[1];
sheets[0] = new WorkSheet("sheet1", model);
WorkBook book = new WorkBook(sheets);
ModelIO.writeWorkBook(book, format, null, os);
is = connection.getInputStream();
} else if("txt".equals(format) || "csv".equals(format) || "html".equals(format) || "htm".equals(format)) {
// enable excel loose format
Map map = new HashMap(1);
map.put(ModelIO.ENABLE_LOOSE_FORMAT, Boolean.TRUE);
// end
ModelIO.writeTableModel(model, format, map, os);
is = connection.getInputStream();
} else if("ser".equals(format)) {
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(os));
oos.writeObject(model);
is = connection.getInputStream();
}
os.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
for(String line=reader.readLine(); line != null; line = reader.readLine()) {
if(log.isLoggable(Level.FINER)) {
log.log(Level.FINER, line);
}
}
URL downloadUrl = AppletUtils.getURLforBase(downloadPath, fileName);
if(downloadUrl != null) {
getAppletContext().showDocument(downloadUrl, "_blank");
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if(os != null) {
os.close();
}
if(is != null) {
is.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void doDownload() {
doSave();
}
private static final String FILE_NAME_KEY = "fileName";
}