/*
* CLoadSave.java
*
* Created on 14. Juli 2008, 19:14
*/
package zettelkasten;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.Icon;
import javax.swing.Timer;
import org.jdesktop.application.Action;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskMonitor;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class CLoadSave extends javax.swing.JDialog {
/**
* Global variables
*/
// of the CDaten object, which contains the XML data of the Zettelkasten
CDaten dataObj;
CProgressInputStream pis;
// get the strings for file descriptions from the resource map
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(zettelkasten.ZettelkastenApp.class).getContext().getResourceMap(ZettelkastenView.class);
Document zknFile; // stores the main data
int errorMessage; // in case error occured during important operations (loading/
// saving files ec.), this variable stores the error message which
// can be received via the "getErrorMessage()" method
/**
* Constants for error messages, received by getErrorMessage();
*/
int ERR_LOAD = 1;
int ERR_SAVE = 2;
private Timer messageTimer;
private Timer busyIconTimer;
private Icon idleIcon;
private Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
/**
* Creates new form CLoadSave
*
* Parameters:
* @parent the parent window
* @modal whether the dialog is modal or not
* @d CDaten class object, which manages the data storage
* @title the dialog's title, e.g. either "Open File" or "Save File"
*/
public CLoadSave(java.awt.Frame parent, boolean modal, CDaten d, String title ) {
super(parent, modal);
dataObj = d;
initComponents();
setTitle( title );
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
((hier kommt der automatisch generierteNetBeans-Matisse-Code...))
/**
* Initiates the status bar for background tasks.
* Catches messages from the doInBackground task
* and changes the progressbar state, the busy icon animation
* and - if necessary - the status message.
*/
private void initStatusBar() {
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(org.jdesktop.application.Application.getInstance(zettelkasten.ZettelkastenApp.class).getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
@Override
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
/**
* Method which returns the error message as a string,
* if an error occured during certain operations like
* loading or saveing files etc...
* @return
*/
public String getErrorMessage() {
String msg = new String("");
switch( errorMessage ) {
// loading error
case 1: msg = resourceMap.getString("ERR_MSG_LOAD");
break;
// saving error
case 2: msg = resourceMap.getString("ERR_MSG_SAVE");
break;
default: msg = "abc";
break;
}
return msg;
}
/**
* Action with background task, which imorts the file
* @return
*/
@Action
public Task loadFile() {
// initiate the "statusbar" (the loading splash screen), giving visiual
// feedback during open and save operations
initStatusBar();
return new LoadFileTask(org.jdesktop.application.Application.getInstance(zettelkasten.ZettelkastenApp.class));
}
private class LoadFileTask extends org.jdesktop.application.Task<Object, Void> {
LoadFileTask(org.jdesktop.application.Application app) {
// Runs on the EDT. Copy GUI state that
// doInBackground() depends on from parameters
// to ImportFileTask fields, here.
super(app);
}
@Override protected Object doInBackground() throws IOException, JDOMException {
// Your Task's code here. This method runs
// on a background thread, so don't reference
// the Swing GUI from here.
// prevent task from processing when the file path is incorrect
// get the file path from the data file which has to be opened
File fp = dataObj.getFilePath();
if( null == fp || !fp.exists() ) {
errorMessage = ERR_LOAD;
}
final long l = fp.length();
SAXBuilder builder = new SAXBuilder();
pis = new CProgressInputStream( new FileInputStream(fp) );
pis.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if (CProgressInputStream.PROPERTY_PROGRESS.equals(name)) {
long newVal = (Long) evt.getNewValue();
System.out.println(newVal + " bytes von " + l + " gelesen");
}
}
});
// open the file via saxbuilder input parser
// throws JDOMException
try {
builder.build(pis);
} catch (IOException ex) {
Logger.getLogger(CDaten.class.getName()).log(Level.SEVERE, null, ex);
}
return null; // return your result
}
@Override protected void succeeded(Object result) {
// Runs on the EDT. Update the GUI based on
// the result computed by doInBackground().
}
}
// Variables declaration - do not modify
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
// End of variables declaration
}