/*
* 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.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 abstract class ButtonMenu extends JPopupMenu implements ActionListener {
final Document document;
public ButtonMenu(Document document) {
this.document = document;
}
protected DocumentState getDocumentState() {
return document.getModes().getDocumentState();
}
protected boolean isEditing() {
return getDocumentState().isEditing();
}
protected boolean isPending() {
return getDocumentState().isPending();
}
public void setHold(boolean hold) {
document.getKetPanel().setHold(hold);
}
protected NormalMode getNormalMode() {
return document.getModes().getNormalMode();
}
protected Chord getChord() {
KeyboardEventHandler keyboardEventHandler = document.getKeyboardEventHandler();
return keyboardEventHandler.getChord();
}
protected AddMode getAddMode() {
return document.getModes().getAddMode();
}
protected MathCollection getMathCollection() {
return document.getMathCollection();
}
protected Selection getSelection() {
return document.getSelection();
}
protected Argument getCurrent() {
return getSelection().getCurrent();
}
public void setupUserInterface(JComponent source, int columns, int rows, int width, int height, String string) {
setLayout(new GridLayout(columns, rows));
Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
if (string!=null) {
TitledBorder title = BorderFactory.createTitledBorder(loweredetched, string);
title.setTitleJustification(TitledBorder.RIGHT);
setBorder(title);
}
setSize(width, height);
buttonSetup();
pack();
show(source, 0, 0);
setHold(true);
}
public void actionPerformed(ActionEvent e) {
String text = ((JButton) e.getSource()).getText();
document.getKeyboardEventHandler().setToInitialState();
replaceByText(text);
getMathCollection().updateUndoStack();
document.getKetPanel().updateAndRepaint();
done();
}
public void done() {
setVisible(false);
setHold(false);
//! Dispose doesn't work.
// Should you make a new popup every time, or just hide/show a singleton?
}
public JPanel row(JButton...buttons) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, buttons.length));
for (JButton b : buttons) {
panel.add(b);
}
return panel;
}
public JButton makeButton(String text, String toolTip) {
JButton button = new JButton(text);
button.addActionListener(this);
if (toolTip!=null) {
button.setToolTipText(toolTip);
}
button.setFocusable(false);
add(button);
return button;
}
public abstract void buttonSetup();
public abstract void replaceByText(String text);
}