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: DocumentNode.java,v 1.6 2006/02/02 18:53:46 bradford Exp $
*/
import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.DocWrapper;
import com.dbxml.db.client.CollectionClient;
import com.dbxml.util.Stopwatch;
import com.dbxml.util.dbXMLException;
import java.awt.Cursor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* DocumentNode
*/
public final class DocumentNode extends AdminNode implements HasCollection, HasMenu, HasDocWrapper {
private static final Icon DocumentIcon = new ImageIcon(DocumentNode.class.getResource("doc_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 CollectionClient col;
private String id;
public DocumentNode(AdminNode parentNode, CollectionClient col, String id) {
super(parentNode);
this.col = col;
this.id = id;
}
public String getLabel() {
return id;
}
public Icon getIcon() {
return DocumentIcon;
}
public CollectionClient getCollection() {
return col;
}
public DocWrapper getDocWrapper() {
try {
if ( col.getCollectionType() == CollectionClient.TYPE_DOCUMENTS )
return new DocWrapper(col.getDocumentAsText(id), col, null, id, true);
else {
return new DocWrapper(new String(col.getValue(id)), col, null, id, true, true);
}
}
catch ( Exception e ) {
Admin.getInstance().addMessage(e.toString());
return null;
}
}
public String getMenuTitle() {
return "Document";
}
public MenuItem[] getMenu() {
return Menu;
}
public int menuAction(int action) {
Admin admin = Admin.getInstance();
switch ( action ) {
case ACTION_DELETE:
try {
String title = "Delete Document";
String message = "Are you sure you want to delete the\n"
+ "Document '"+id+"'?";
int res = JOptionPane.showConfirmDialog(Admin.getInstance(), message, title, JOptionPane.YES_NO_OPTION);
if ( res == JOptionPane.YES_OPTION ) {
col.remove(id);
return REMOVE_SELF;
}
}
catch ( Exception e ) {
Admin.getInstance().addMessage(e.toString());
}
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:
try {
String name = col.getCanonicalName() + '/' + id;
admin.xsltViewer.setStylesheet(name, getDocWrapper());
}
catch ( dbXMLException e ) {
Admin.getInstance().addMessage(e.getMessage());
}
break;
}
return REFRESH_NONE;
}
}