/*
* 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 ket.*;
import ket.display.box.Box;
import ket.math.*;
import ket.math.convert.*;
import ket.math.purpose.DoubleValue;
import ket.math.purpose.IntegerValue;
import ket.math.purpose.NumberValue;
import ket.math.purpose.Text;
import ket.math.purpose.VariableToken;
import ket.math.purpose.Word;
import ket.treeDiff.*;
import ketUI.Clipboard;
import ketUI.Document;
import ketUI.DocumentManager;
import ketUI.Ket;
import ketUI.chord.Chord;
import ketUI.chord.KeyPress;
import ketUI.chord.KeyboardEventHandler;
import ketUI.chord.Macros;
import ketUI.panel.KetPanel;
/**
* This class provides a range of operations associated with adding arguments to a given equation.
*/
public class AddMode {
final Modes modes;
public AddMode(Modes modes) {
this.modes = modes;
}
public boolean insertBlankLine(Chord chord, boolean prepend) {
Token token = new Token(new Text(""));
Equation equation = new Equation(token);
if (prepend) {
getSelection().prependEquation(equation);
} else {
getSelection().appendEquation(equation);
}
chord.setComplete(true);
modes.setDocumentState(DocumentState.NORMAL);
return true;
}
public boolean prependNewChild(Chord chord) { // 'ai' on a root "?"
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
if (getCurrent() instanceof Token) {
KnownArguments knownArguments = getMathCollection().getKnownArguments();
boolean ok = getSelection().variableTokenToBranch(knownArguments);
if ( ! ok ) {
modes.error(" !!! Can't add a child to the non-branch, non-word current token !!! ");
return false;
}
}
getSelection().prependNewChild();
return true;
}
public boolean appendNewChild(Chord chord) {
getSelection().shatterText();
if (getCurrent() instanceof Token) {
KnownArguments knownArguments = getMathCollection().getKnownArguments();
boolean ok = getSelection().variableTokenToBranch(knownArguments);
if ( ! ok ) {
modes.error(" !!! Can't add a child to the non-branch, non-word current token !!! ");
return false;
}
}
getSelection().appendNewChild();
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
return true;
}
public boolean appendNewSibling(Chord chord) {
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
if (getCurrent().isRoot()) {
getSelection().appendEquation(new Equation());
} else {
getSelection().appendNewSibling();
}
return true;
}
public boolean replaceTextLine(Chord chord) {
Text text = new Text("");
Argument root = new Token(text);
getSelection().replace(getCurrent().getRoot(), root);
modes.setDocumentState(DocumentState.UPDATE_TEXT);
chord.setComplete(false);
return true;
}
public boolean replaceSelectionWithText(Chord chord) {
Argument text = new Token(new Text(""));
getSelection().replace(getCurrent(), text);
modes.setDocumentState(DocumentState.UPDATE_TEXT);
chord.setComplete(false);
return true;
}
public boolean appendTextLine(Chord chord) {
Text text = new Text("");
Argument root = new Token(text);
getSelection().appendEquation(new Equation(root));
modes.setDocumentState(DocumentState.UPDATE_TEXT);
chord.setComplete(false);
return true;
}
//! public boolean appendNewSibling(Chord chord) {
public boolean prependUp(Chord chord) {
getSelection().prependNewUp(); // new argument or equation
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
return true;
}
public boolean appendDown(Chord chord) {
getSelection().appendNewDown(); // new argument or equation
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
return true;
}
public boolean appendEquation(Chord chord) {
getSelection().appendEquation(new Equation());
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
return true;
}
public boolean prependEquation(Chord chord) {
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
getSelection().prependEquation(new Equation());
return true;
}
public boolean prependNewSibling(Chord chord) {
modes.setDocumentState(DocumentState.UPDATE_REPLACE);
chord.setComplete(false);
if (getCurrent().isRoot()) {
getSelection().prependEquation(new Equation());
} else {
getSelection().prependNewSibling();
}
return true;
}
public boolean prependTextLine(Chord chord) {
Text text = new Text("");
Argument root = new Token(text);
getSelection().prependEquation(new Equation(root));
modes.setDocumentState(DocumentState.UPDATE_TEXT);
chord.setComplete(false);
return true;
}
// Analogous to (map user-input-fn (.getChildren args)).
public boolean mapPrepend(Chord chord) {
if (getCurrent() instanceof Token) {
modes.echo("Can't map a function to a non-branch.");
return false;
}
Branch currentBranch = getCurrent().asBranch();
if (currentBranch.size()==0) {
modes.echo("Map requires child arguments to map over.");
return false;
}
modes.setDocumentState(DocumentState.MAP_PREPEND);
chord.setComplete(false);
return true;
}
// Analogous to (map user-input-fn (.getChildren args)).
public boolean mapAppend(Chord chord) {
if (getCurrent() instanceof Token) {
modes.echo("Can't map a function to a non-branch.");
return false;
}
Branch currentBranch = getCurrent().asBranch();
if (currentBranch.size()==0) {
modes.echo("Map requires child arguments to map over.");
return false;
}
modes.setDocumentState(DocumentState.MAP_APPEND);
chord.setComplete(false);
return true;
}
// Analogous to (map user-input-fn (.getChildren args)).
public boolean map2Prepend(Chord chord) {
if (getCurrent() instanceof Token) {
modes.echo("Can't map a function to a non-branch.");
return false;
}
Branch currentBranch = getCurrent().asBranch();
if (currentBranch.size()==0) {
modes.echo("Map requires child arguments to map over.");
return false;
}
modes.setDocumentState(DocumentState.MAP2_PREPEND);
chord.setComplete(false);
return true;
}
// Analogous to (map user-input-fn (.getChildren args)).
public boolean map2Append(Chord chord) {
if (getCurrent() instanceof Token) {
modes.echo("Can't map a function to a non-branch.");
return false;
}
Branch currentBranch = getCurrent().asBranch();
if (currentBranch.size()==0) {
modes.echo("Map requires child arguments to map over.");
return false;
}
modes.setDocumentState(DocumentState.MAP2_APPEND);
chord.setComplete(false);
return true;
}
/*
case 'n': // 'en' Prepend grandchildren.
return getAddMode().map2Prepend(chord);
case 'n': // 'an' Append grandchildren.
return getAddMode().map2Append(chord);
*/
public boolean appendPurpose(Chord chord) { // TODO: Rename to appendParentPurpose()
getSelection().addNewIntermediaryParent();
modes.setDocumentState(DocumentState.APPEND_PURPOSE);
chord.setComplete(false);
return true;
}
public boolean prependPurpose(Chord chord) { // TODO: Rename to prependParentPurpose()
getSelection().addNewIntermediaryParent();
modes.setDocumentState(DocumentState.PREPEND_PURPOSE);
chord.setComplete(false);
return true;
}
// --- Helper methods ---
private MathCollection getMathCollection() {
return modes.getMathCollection();
}
private Selection getSelection() {
return getMathCollection().getSelection();
}
public Argument getCurrent() {
return getCursor().getCurrent();
}
private Cursor getCursor() {
return getMathCollection().getCursor();
}
}