/*
* 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.HTMLTools;
import ket.display.ImageTools;
import ket.math.Argument;
import ket.math.Branch;
import ket.math.Equation;
import ket.math.Function;
import ket.math.Symbol;
import ket.math.SymbolicFunction;
import ket.math.Token;
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;
/**
* Handle toolbar-button mouse clicks.
*/
public class ToolBarEventHandler implements ActionListener {
public static boolean HORIZONAL_TOOLBAR_AXIS = false;
public void setHold(boolean hold) { //-
document.getKetPanel().setHold(hold);
}
public static final int TOOL_BAR_SIZE = 40;
final JToolBar toolBar;
final Document document;
final JButton newFile;
final JButton operate;
final JButton appendEquation;
final JButton appendText;
final JButton undo;
final JButton redo;
//- final JButton upperCaseGreek;
final JButton lowerCaseGreek;
final JButton operands;
final JButton function;
final JButton symbolicFunctions;
//- final JButton expand;
final JButton delete;
final JButton change;
public ToolBarEventHandler(Document document) {
this.document = document;
toolBar = new JToolBar(HORIZONAL_TOOLBAR_AXIS ? SwingConstants.HORIZONTAL : SwingConstants.VERTICAL);
Dimension size = new Dimension(FrameManager.SCREEN_WIDTH, FrameManager.SCREEN_HEIGHT);
if (HORIZONAL_TOOLBAR_AXIS) {
size.height = TOOL_BAR_SIZE;
} else {
size.width = TOOL_BAR_SIZE;
}
toolBar.setPreferredSize(size);
toolBar.setFloatable(false);
newFile = ImageTools.makeNavigationButton("new", "Create a new document, 'Ctrl+w n'.", "New", this);
appendEquation = ImageTools.makeNavigationButton(
"equation",
HTMLTools.htmlLines(
"Append a new equation, 'ae',",
"<Enter> to stop,",
"<Esc> to cancel."),
"Append equation",
this);
appendText = ImageTools.makeNavigationButton(
"text",
HTMLTools.htmlLines(
"Append a text block, 'at',",
"<Esc> to finish editing."),
"Append text",
this);
undo = ImageTools.makeNavigationButton("left", "Undo the previous edit, 'u'.", "Undo", this);
redo = ImageTools.makeNavigationButton("right", "Redo the previous edit, 'Ctrl-R'.", "Redo", this);
//- upperCaseGreek = ImageTools.makeNavigationButton("upper", "Insert an uppercase greek letter.", "Uppercase Greek", this);
lowerCaseGreek = ImageTools.makeNavigationButton("lower", "Insert a lowercase greek letter.", "Lowercase Greek", this);
operands = ImageTools.makeNavigationButton("operand", "Insert an operand symbol.", "Operand", this);
function = ImageTools.makeNavigationButton("function", "Insert a function.", "Function", this);
symbolicFunctions = ImageTools.makeNavigationButton("operation", "Insert an operation.", Symbol.GREATER_EQUALS.toUnicode(), this);
//- expand = ImageTools.makeNavigationButton("expand", "Expand the current selection.", "Expand...", this); // TODO: Use a balloon icon?
delete = ImageTools.makeNavigationButton("delete", "Delete items relative to the current selection.", "Delete...", this);
//- change = ImageTools.makeNavigationButton("spanner", "Transform the current selection.", "Transform...", this);
change = ImageTools.makeNavigationButton("similar", "Transform the current selection.", "Transform...", this);
toolBar.add(newFile);
toolBar.addSeparator();
toolBar.add(appendEquation);
toolBar.add(appendText);
toolBar.addSeparator();
toolBar.add(undo);
toolBar.add(redo);
toolBar.addSeparator();
operate = ImageTools.makeNavigationButton("plus", "Perform an operation on the current selection, 'a+', 'a-', 'a*'...", "operate on", this);
toolBar.add(delete);
toolBar.add(operate);
//- toolBar.add(expand);
toolBar.addSeparator();
toolBar.add(change);
toolBar.addSeparator();
//- toolBar.add(upperCaseGreek);
toolBar.add(lowerCaseGreek);
toolBar.add(operands);
toolBar.add(function);
toolBar.add(symbolicFunctions);
}
private Argument cloneCurrentRoot() { // TODO: Use this here and in normal responder.
Equation currentEquation = document.getCursor().getEquation();
Argument root = currentEquation.getRoot();
return Argument.cloneArgument(root);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
Chord chord = getChord();
// Use functions, symbolic functions and operations as arguments to an existing edit.
if (source==function) {
/*<?
new FunctionMenuFrame(document, isPending(), function, FunctionMenuFrame.FUNCTIONS);
getMathCollection().updateUndoStack();
document.getKetPanel().updateAndRepaint();
return;*/
new FunctionList(document, document.getKetPanel());
} else if (source==symbolicFunctions) {
new FunctionMenuFrame(document, isPending(), symbolicFunctions, FunctionMenuFrame.SYMBOLIC_FUNCTIONS);
getMathCollection().updateUndoStack();
document.getKetPanel().updateAndRepaint();
return;
} else if (source==operate) {
new OperationMenuFrame(document, isPending(), operate);
return;
}
// otherwise:
document.getKeyboardEventHandler().setToInitialState();
if (source==newFile) {
getNormalMode().openNewWindow();
} else if (source==appendEquation) {
getAddMode().appendEquation(chord);
} else if (source==undo) {
getMathCollection().undo();
} else if (source==redo) {
getMathCollection().redo();
} else if (source==appendText) {
getAddMode().appendTextLine(chord);
//- } else if (source==upperCaseGreek) {
//- new UpperCaseGreekMenuFrame(document, upperCaseGreek);
} else if (source==lowerCaseGreek) {
new LowerCaseGreekMenuFrame(document, lowerCaseGreek);
} else if (source==operands) {
new OperandMenuFrame(document, operands);
} else if (source==delete) {
new DeleteMenuFrame(document, delete);
//- } else if (source==expand) {
//- new ExpandMenuFrame(document, expand);
} else if (source==change) {
new TransformationMenuFrame(document, change);
}
getMathCollection().updateUndoStack();
document.getKetPanel().updateAndRepaint();
}
private DocumentState getDocumentState() {
return document.getModes().getDocumentState();
}
private boolean isPending() {
return getDocumentState().isPending();
}
private MathCollection getMathCollection() {
return document.getMathCollection();
}
/**
* Return the current chord from the keyboard event handler.
*/
private Chord getChord() {
KeyboardEventHandler keyboardEventHandler = document.getKeyboardEventHandler();
return keyboardEventHandler.getChord();
}
private Argument getCurrent() {
return getSelection().getCurrent();
}
private Selection getSelection() {
return document.getSelection();
}
private NormalMode getNormalMode() { //-
return document.getModes().getNormalMode();
}
private AddMode getAddMode() {
return document.getModes().getAddMode();
}
public JComponent getToolBar() {
return toolBar;
}
}