/*
* 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;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import geom.Position;
import ket.MathCollection;
import ket.Selection;
import ket.display.ImageTools;
import ket.math.*;
import ket.math.purpose.Word;
import ketUI.chord.Chord;
import ketUI.chord.KeyboardEventHandler;
import ketUI.modes.AddMode;
import ketUI.modes.DocumentState;
import ketUI.modes.NormalMode;
import ketUI.responder.MouseResponder;
class FunctionMenuFrame extends ButtonMenu {
static final int FUNCTIONS = 0x1;
static final int SYMBOLIC_FUNCTIONS = 0x2;
boolean pending;
int mark;
public FunctionMenuFrame(Document document, boolean pending, JComponent source, int mark) {
super(document);
this.pending = pending;
this.mark = mark;
int columns = 7;
setupUserInterface(source, columns, 7, 200, 200, "Insert a function");
setHold(true);
}
@Override
public void buttonSetup() {
Branch one = new Branch(Function.UNKNOWN, new Token(new Word("a")));
Branch two = new Branch(Function.UNKNOWN, new Token(new Word("x")), new Token(new Word("y")));
for (Function function : Function.OPERATORS) {
if (Type.STRICT_INVERSES.contains(function)) { // Don't bother with asin etc.
continue;
}
boolean symbolic = function instanceof SymbolicFunction;
ImageIcon icon = null;
if (mark==FUNCTIONS) {
if (symbolic) continue;
} else {
if ( ! symbolic ) continue;
}
JButton button = icon!=null ? new JButton(icon): new JButton(getLabel(function));
button.setFocusable(false);
button.addActionListener(this);
String toolTip = function.getName();
Character c = Type.asLetter(function);
if (c!=null) {
toolTip += ", '>"+c+"'";
}
if (function instanceof SymbolicFunction) {
SymbolicFunction sf = (SymbolicFunction) function;
two.setFunction(sf);
String ascii = "";
boolean oneValid = sf.validArgumentCount(1);
boolean twoValid = sf.validArgumentCount(2);
if (oneValid) {
one.setFunction(sf);
ascii += "\"" + one.toString() + "\"";
}
if (oneValid && twoValid) {
ascii += ", ";
}
if (twoValid) {
one.setFunction(sf);
ascii += "\"" + two.toString() + "\"";
}
if (ascii.length()>0) {
toolTip += ", " + ascii;
}
}
button.setToolTipText(toolTip);
add(button);
}
}
public String getLabel(Function function) {
if (function instanceof SymbolicFunction) {
return ((SymbolicFunction) function).toUnicodeString();
} else {
return function.toVerboseString();
}
}
@Override
public void replaceByText(String text) {
Function function = null;
for (Function f : Function.OPERATORS) {
if (getLabel(f).equals(text)) {
function = f;
}
}
replaceByFunction(function);
}
public void replaceByFunction(Function function) {
if (pending && getCurrent().isBranch()) {
getCurrent().asBranch().setFunction(function);
} else if (isEditing() && getCurrent().isBranch()) {
getCurrent().asBranch().setFunction(function);
} else {
getSelection().addIntermediaryParent(function);
}
}
}