package com.dbxml.db.admin.components;
/*
* 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: TextEditor.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.admin.TabbedNodeEditor;
import com.dbxml.db.admin.swing.SyntaxTextArea;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.awt.BorderLayout;
import java.awt.Cursor;
import javax.swing.JPanel;
import org.w3c.dom.Document;
/**
* TextEditor
*/
public class TextEditor extends JPanel implements TabbedNodeEditor {
private DocWrapper doc;
private boolean reset;
private String value = "";
private SyntaxTextArea textEdit = new SyntaxTextArea();
private BorderLayout thisLayout = new BorderLayout();
public TextEditor() {
try {
jbInit();
textEdit.setText(value);
}
catch ( Exception e ) {
Admin.getInstance().addMessage(e.toString());
}
}
private void jbInit() throws Exception {
this.setLayout(thisLayout);
this.add(textEdit, BorderLayout.CENTER);
}
public void requestFocus() {
textEdit.requestFocus();
}
public boolean isModified() {
return !textEdit.getText().equals(value);
}
public void reset() {
reset = true;
}
public void setLanguage(String language) {
textEdit.setLanguage(language);
}
public void setDocWrapper(DocWrapper doc) {
reset = this.doc != doc;
this.doc = doc;
}
public DocWrapper getDocWrapper() {
return doc;
}
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
textEdit.setEnabled(enabled);
}
public boolean isEnabled() {
return textEdit.isEnabled();
}
public void activate(boolean readOnly) {
Admin admin = Admin.getInstance();
admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
if ( readOnly == textEdit.isEditable() || reset ) {
value = doc.getText();
textEdit.setText(value);
textEdit.setCaretPosition(0);
textEdit.setEditable(!readOnly);
}
}
finally {
admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public boolean isCutEnabled() {
String sel = textEdit.getSelectedText();
return sel != null && sel.length() > 0 && textEdit.isEditable();
}
public void cut() {
textEdit.cut();
}
public boolean isCopyEnabled() {
String sel = textEdit.getSelectedText();
return sel != null && sel.length() > 0;
}
public void copy() {
textEdit.copy();
}
public boolean isPasteEnabled() {
return textEdit.isEditable();
}
public void paste() {
textEdit.paste();
}
public boolean isUndoEnabled() {
return false;
}
public void undo() {
/** @todo This */
}
public void find() {
/** @todo This */
}
public void findNext() {
/** @todo This */
}
public boolean isBeautifyEnabled() {
return value.length() > 0 && textEdit.getLanguage() == SyntaxTextArea.LANG_XML;
}
public void beautify() {
Admin admin = Admin.getInstance();
admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try {
String text = textEdit.getText();
Document d = DOMHelper.parseText(text);
String newText = TextWriter.toPrettyString(d).trim();
textEdit.setText(newText);
if ( !textEdit.isEditable() )
value = newText;
textEdit.setCaretPosition(0);
}
catch ( Exception e ) {
}
finally {
admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
public boolean deactivate() {
try {
if ( isModified() ) {
String text = textEdit.getText();
doc.setText(text);
}
return true;
}
catch ( Exception ex ) {
Admin.getInstance().setStatus("Error parsing Document text - Couldn't save Document");
return false;
}
}
}