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: DirectoryNode.java,v 1.6 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.AdminConfig;
import com.dbxml.db.admin.DocWrapper;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* DirectoryNode
*/
public final class DirectoryNode extends AdminNode implements HasMenu, HasChildren, HasStorage {
private static final Icon FSIcon = new ImageIcon(DirectoryNode.class.getResource("filesystem_mini.gif"));
private static final Icon DirIcon = new ImageIcon(DirectoryNode.class.getResource("directory_mini.gif"));
public static final int ACTION_DELETE = 0;
public static final int ACTION_DETACH = 1;
private String filename;
private String label;
private File file;
private Icon ico;
private boolean topLevel;
public DirectoryNode(AdminNode parentNode, String filename, String label) {
super(parentNode);
this.filename = filename;
this.label = label;
this.file = new File(filename);
ico = FSIcon;
topLevel = true;
}
public DirectoryNode(AdminNode parentNode, File file) {
super(parentNode);
this.file = file;
filename = file.getName();
label = filename;
ico = DirIcon;
topLevel = false;
}
public String getLabel() {
return label;
}
public Icon getIcon() {
return ico;
}
public File getFile() {
return file;
}
public boolean storeDocWrapper(DocWrapper doc) {
doc.setFile(file);
return doc.store();
}
public AdminNode getRefreshTarget() {
return this;
}
public AdminNode[] getChildren() {
try {
File[] list = file.listFiles(new FileFilter() {
public boolean accept(File f) {
return !f.isHidden() && (f.isDirectory() || f.isFile());
}
});
List l = new ArrayList();
List files = new ArrayList();
for ( int i = 0; i < list.length; i++ ) {
if ( list[i].isDirectory() )
l.add(new DirectoryNode(this, list[i]));
else
files.add(new FileNode(this, list[i]));
}
l.addAll(files);
return (AdminNode[])l.toArray(new AdminNode[0]);
}
catch ( Exception e ) {
return new AdminNode[0];
}
}
public String getCanonicalName() {
return file.getAbsolutePath();
}
public MenuItem[] getMenu() {
if ( topLevel )
return new MenuItem[] {new MenuItem(ACTION_DETACH, "Detach Filesystem")};
else
return new MenuItem[] {new MenuItem(ACTION_DELETE, "Delete Directory")};
}
public boolean isParentMenuInherited() {
return topLevel;
}
public String getMenuTitle() {
return "Directory";
}
public int menuAction(int action) {
Admin admin = Admin.getInstance();
switch ( action ) {
case ACTION_DETACH:
try {
AdminConfig cfg = admin.getAdminConfig();
cfg.removeFileSystem(label);
cfg.save();
return REMOVE_SELF;
}
catch ( Exception e ) {
admin.addMessage(e.getMessage());
}
break;
case ACTION_DELETE:
try {
String title = "Delete Directory";
String message = "Are you sure you want to delete the\n"
+ "Directory '"+file.getName()+"'?";
int res = JOptionPane.showConfirmDialog(admin, message, title, JOptionPane.YES_NO_OPTION);
if ( res == JOptionPane.YES_OPTION ) {
file.delete();
return REFRESH_PARENT;
}
}
catch ( Exception e ) {
admin.addMessage(e.getMessage());
}
break;
}
return REFRESH_NONE;
}
}