/*
 * Copyright (c) 2002-2008 Pixware. 
 *
 * Author: Hussein Shafie
 *
 * This file is part of the XMLmind XML Editor project.
 * For conditions of distribution and use, see the accompanying legal.txt file.
 */

import java.awt.event.KeyEvent;

import com.xmlmind.guiutil.ShowStatus;

import com.xmlmind.xmledit.gadget.UserInput;

import com.xmlmind.xmledit.edit.Commands;

import com.xmlmind.xmledit.view.BindingSpec;
import com.xmlmind.xmledit.view.DocumentView;

import com.xmlmind.xmledit.cmd.CommandBase;

public class StyledEditor2 extends StyledEditor 
                                   implements ShowStatus.StatusWindow {
    private static final BindingSpec[] EXTRA_BINDINGS = {
        new BindingSpec(UserInput.get(KeyEvent.VK_ESCAPE, 0),
                        UserInput.get('u'),
                        "ConvertCaseCmd", "upper"),
        new BindingSpec(UserInput.get(KeyEvent.VK_ESCAPE, 0),
                        UserInput.get('l'),
                        "ConvertCaseCmd", "lower"),
        new BindingSpec(UserInput.get(KeyEvent.VK_ESCAPE, 0),
                        UserInput.get('c'),
                        "ConvertCaseCmd", "capital"),

        new BindingSpec(UserInput.get(KeyEvent.VK_ESCAPE, 0),
                        UserInput.get('t'),
                        "WrapElementCmd", null),

        new BindingSpec(UserInput.get(')'),
                        "ShowMatchingCharCmd", ")"),
        new BindingSpec(UserInput.get('}'),
                        "ShowMatchingCharCmd", "}"),
        new BindingSpec(UserInput.get(']'),
                        "ShowMatchingCharCmd", "]"),

        new BindingSpec(UserInput.get(KeyEvent.VK_ESCAPE, 0),
                        UserInput.get('w'),
                        "InsertAfterAsP", null)
    };

    public static void main(String[] args) {
        StyledEditor2 app = new StyledEditor2();
        app.run(args);
    }

    public StyledEditor2() {
        Commands.register("ConvertCaseCmd", new ConvertCaseCmd());
        Commands.register("WrapElementCmd", new WrapElementCmd());
        Commands.register("ShowMatchingCharCmd", new ShowMatchingCharCmd());

        final MakeParagraphsCmd makeParagraphs = new MakeParagraphsCmd();
        Commands.register("MakeParagraphsCmd", makeParagraphs);

        Commands.register("InsertAfterAsP", new CommandBase() {
            public boolean prepare(DocumentView docView,
                                   String parameter, int x, int y) {
                return makeParagraphs.prepare(docView, 
                                             "{http://www.w3.org/1999/xhtml}p",
                                             x, y);
            }

            public Object execute(DocumentView docView, 
                                  String parameter, int x, int y) {
                makeParagraphs.prepare(docView, 
                                       "{http://www.w3.org/1999/xhtml}p", 
                                       x, y);
                String paragraphs = 
                    (String) makeParagraphs.execute(docView, "p", x, y);
                if (paragraphs == EXECUTION_FAILED) 
                    return EXECUTION_FAILED;

                docView.executeCommand("paste", 
                                       "after[implicitElement] " + paragraphs, 
                                       x, y);
                return null;
            }
        });

        ShowStatus.setStatusWindow(this);
    }

    public void showStatus(String message) {
        setMessage(message);
    }

    protected void configureDocumentView(DocumentView docView) {
        super.configureDocumentView(docView);
        docView.addBindings(EXTRA_BINDINGS);
    }
}
