/*
* Copyright (C) 2011 Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ketUI.modes;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JMenu;
import ket.*;
import ket.display.HighlightCursor;
import ket.math.*;
import ket.math.purpose.Text;
import ketUI.*;
import ketUI.Clipboard;
import ketUI.Document;
import ketUI.chord.Chord;
import ketUI.chord.KeyboardEventHandler;
import ketUI.panel.KetPanel;
import ketUI.responder.*;
/**
* As well as storing the current responder and documentState that the editor is in, this
* provides a container for classes that provide functionality for specific
* modes.
*/
public class Modes implements HighlightCursor {
final Document document;
final Search search;
final Find find;
final CommandMode commandMode;
final NormalMode normalMode;
final AddMode addMode;
final DeleteMode deleteMode;
final PasteMode pasteMode;
final Message message;
final Clipboard clipboard;
final StringResponder stringResponder;
final TextResponder textResponder;
final NormalResponder normalResponder;
final EchoResponder echoResponder;
final LetterResponder letterResponder;
final AddResponder addResponder;
final MouseResponder mouseResponder;
Responder responder;
DocumentState documentState;
public Modes(Document document) {
this.document = document;
stringResponder = new StringResponder(this);
textResponder = new TextResponder(this);
normalResponder = new NormalResponder(this);
echoResponder = new EchoResponder(this);
letterResponder = new LetterResponder(this);
addResponder = new AddResponder(this);
mouseResponder = new MouseResponder(this);
if (getDocumentManager()!=null) {
clipboard = getDocumentManager().getClipboard();
} else {
clipboard = new Clipboard(Ket.KNOWN_ARGUMENTS);
}
message = new Message(document, getKnownArguments(), getMathCollection(), getClipboard());
search = new Search(getKnownArguments(), getClipboard(), this);
find = new Find(getSelection());
commandMode = new CommandMode(this, getMathCollection()); // +DocumentManager directly
normalMode = new NormalMode(this);
addMode = new AddMode(this);
deleteMode = new DeleteMode(this);
pasteMode = new PasteMode(this);
}
public StringResponder getStringResponder() {
return stringResponder;
}
public TextResponder getTextResponder() {
return textResponder;
}
public EchoResponder getEchoResponder() {
return echoResponder;
}
public LetterResponder getLetterResponder() {
return letterResponder;
}
public AddResponder getAddResponder() {
return addResponder;
}
public MouseResponder getMouseResponder() {
return mouseResponder;
}
public NormalResponder getNormalResponder() {
return normalResponder;
}
///////////////////////////////
// EXTERNAL ACCESSOR METHODS //
///////////////////////////////
public NormalMode getNormalMode() {
return normalMode;
}
public AddMode getAddMode() {
return addMode;
}
public DeleteMode getDeleteMode() {
return deleteMode;
}
public PasteMode getPasteMode() {
return pasteMode;
}
public CommandMode getCommandMode() {
return commandMode;
}
public Search getSearch() {
return search;
}
public Find getFind() {
return find;
}
public void echo(String string) {
Chord chord = getKeyboardEventHandler().getChord();
setDocumentState(DocumentState.ECHO_FEEDBACK);
message.setMessage(string);
chord.setComplete(false);
document.updateAndRepaint();
}
public void error(String string) {
// TODO: Remove chord from the argument list.
Chord chord = getKeyboardEventHandler().getChord();
setDocumentState(DocumentState.ECHO_ERROR);
message.setMessage(string);
// Note: Error responder is not finished until a key is pressed to
// clear the message.
if (chord!=null) {
// TODO: When called by mouse events, no ketUI.chord is given: fix.
chord.setComplete(false);
document.updateAndRepaint();
}
}
public Responder getResponder() {
return responder;
}
private Responder getResponder(DocumentState documentState) {
switch (documentState.getResponder()) {
case ADD_RESPONDER:
return addResponder;
case ECHO_RESPONDER:
return echoResponder;
case LETTER_RESPONDER:
return letterResponder;
case MOUSE_RESPONDER:
return mouseResponder;
case NORMAL_RESPONDER:
return normalResponder;
case STRING_RESPONDER:
return stringResponder;
case TEXT_RESPONDER:
return textResponder;
default:
throw new IllegalStateException("No known responder associated with state: " + documentState);
}
}
/**
* Modes are grouped into (responder, sub-responder) pairs and may be uniquely
* specified by the required sub-responder.
*/
public void setDocumentState(DocumentState documentState) {
if (responder!=null) {
responder.cleanAfterRespond();
}
this.documentState = documentState;
responder = getResponder(documentState);
FrameManager fm = document.getFrameManager();
MenuEventHandler menuEventHandler = fm!=null ? fm.getMenuEventHandler() : null;
if (menuEventHandler!=null) {
menuEventHandler.showFullMenu(responder!=stringResponder && responder!=textResponder);
}
responder.prepareToRespond();
}
/**
* Undo the previous edit.
*/
////////////////////////////
// LOCAL ACCESSOR METHODS //
////////////////////////////
// TODO: Should this be private?
public KeyboardEventHandler getKeyboardEventHandler() {
return document.getKeyboardEventHandler();
}
private Argument getCurrent() {
return getSelection().getCurrent();
}
public Selection getSelection() {
return getMathCollection().getSelection();
}
public Document getDocument() {
return document;
}
public Message getMessage() {
return message;
}
public DocumentState getDocumentState() {
return documentState;
}
public MathCollection getMathCollection() {
return document.getMathCollection();
}
public KnownArguments getKnownArguments() {
return getMathCollection().getKnownArguments();
}
public DocumentManager getDocumentManager() {
return document.getDocumentManager();
}
public Clipboard getClipboard() {
/*-
DocumentManager dm = getDocumentManager();
return dm!=null ? dm.getClipboard() : null;
*/
return clipboard;
}
public boolean highlightCursor() {
return documentState!=DocumentState.ECHO_VIEW;
}
}