package com.dbxml.db.admin.nodes;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: FileNode.java,v 1.8 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.DocWrapper;
import com.dbxml.util.Stopwatch;
import java.awt.Cursor;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* FileNode
*/
public final class FileNode extends AdminNode implements HasMenu, HasDocWrapper {
private static final Icon FileIcon = new ImageIcon(FileNode.class.getResource("file_mini.gif"));
public static final int ACTION_EDIT = 0;
public static final int ACTION_DELETE = 1;
public static final int ACTION_XSLT = 2;
private static final MenuItem[] Menu = new MenuItem[]{
new MenuItem(ACTION_EDIT, "Open Document"),
new MenuItem(ACTION_DELETE, "Delete Document"),
MenuItem.SEPARATOR,
new MenuItem(ACTION_XSLT, "Select as Stylesheet")
};
private String filename;
private File file;
public FileNode(AdminNode parentNode, File file) {
super(parentNode);
this.file = file;
filename = file.getName();
}
public String getLabel() {
return filename;
}
public File getFile() {
return file;
}
public Icon getIcon() {
return FileIcon;
}
public DocWrapper getDocWrapper() {
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis, 4096);
InputStreamReader isr = new InputStreamReader(bis, "UTF8");
char[] c = new char[(int)file.length()];
int size = isr.read(c);
isr.close();
return new DocWrapper(new String(c, 0, size), null, file.getParentFile(), file.getName(), true, true);
}
catch ( Exception e ) {
return null;
}
}
public String getMenuTitle() {
return "File";
}
public MenuItem[] getMenu() {
return Menu;
}
public int menuAction(int action) {
Admin admin = Admin.getInstance();
switch ( action ) {
case ACTION_DELETE:
try {
String title = "Delete File";
String message = "Are you sure you want to delete the\n"
+ "File '"+file.getName()+"'?";
int res = JOptionPane.showConfirmDialog(Admin.getInstance(), message, title, JOptionPane.YES_NO_OPTION);
if ( res == JOptionPane.YES_OPTION ) {
file.delete();
return REMOVE_SELF;
}
}
catch ( Exception e ) {
Admin.getInstance().addMessage(e.getMessage());
}
break;
case ACTION_EDIT:
admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Stopwatch sw = new Stopwatch("Document Retrieval", true);
DocWrapper dw = getDocWrapper();
sw.stop();
admin.setStatus(sw.toString());
admin.editDocument(dw);
admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
break;
case ACTION_XSLT:
String name = file.getAbsolutePath();
admin.xsltViewer.setStylesheet(name, getDocWrapper());
break;
}
return REFRESH_NONE;
}
}