Package ketUI

Source Code of ketUI.OperationMenuFrame

/*
* 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 OperationMenuFrame extends ButtonMenu {

  static final Function[] OPERATIONS = new Function[]{
    Function.TIMES, Function.FRACTION, Function.EQUALS,
    Function.ADD, Function.MINUS, Function.DERIVATIVE,
    Function.POWER, Function.SQRT, Function.INTEGRAL};

  public static final ImageIcon PLUS_ICON       = ImageTools.getIcon("grey/plus", "Add");
  public static final ImageIcon MINUS_ICON      = ImageTools.getIcon("grey/minus", "Minus");
  public static final ImageIcon TIMES_ICON      = ImageTools.getIcon("grey/times", "Times");
  public static final ImageIcon FRACTION_ICON   = ImageTools.getIcon("grey/fraction", "Fraction");
  public static final ImageIcon POWER_ICON      = ImageTools.getIcon("grey/power", "Power");
  public static final ImageIcon EQUALS_ICON     = ImageTools.getIcon("grey/equals", "Equals");
  public static final ImageIcon SQRT_ICON       = ImageTools.getIcon("grey/sqrt", "Square root")
  public static final ImageIcon INTEGRAL_ICON   = ImageTools.getIcon("grey/integral", "Integral");
  public static final ImageIcon DERIVATIVE_ICON = ImageTools.getIcon("grey/derivative", "Derivative");

  boolean pending;

  public OperationMenuFrame(Document document, boolean pending, JComponent source) {
    super(document);
    this.pending = pending;
    int columns = 3;
    setupUserInterface(source, columns, 7, 200, 200, "Operate");
    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")));
    /*
      * / =
      + - '
      ^ Q I
    */
    //- for (Function function : Function.OPERATORS) {
    for (Function function : OPERATIONS) {
      if (Type.STRICT_INVERSES.contains(function)) { // Don't bother with asin etc.
        continue;
      }
      boolean symbolic = function instanceof SymbolicFunction;
      ImageIcon icon = null;
      if (function==Function.ADD) {
        icon = PLUS_ICON;
      } else if (function==Function.MINUS) {
        icon = MINUS_ICON;
      } else if (function==Function.TIMES) {
        icon = TIMES_ICON;
      } else if (function==Function.FRACTION) {
        icon = FRACTION_ICON;
      } else if (function==Function.POWER) {
        icon = POWER_ICON;
      } else if (function==Function.EQUALS) {
        icon = EQUALS_ICON;
      } else if (function==Function.SQRT) {
        icon = SQRT_ICON;
      } else if (function==Function.INTEGRAL) {
        icon = INTEGRAL_ICON;
      } else if (function==Function.DERIVATIVE) {
        icon = DERIVATIVE_ICON;
      } else {
        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();
    }
  }

  public void actionPerformed(ActionEvent e) {
    Icon icon = ((JButton) e.getSource()).getIcon();
    document.getKeyboardEventHandler().setToInitialState()
    Function function = iconToFunction(icon);
    replaceByFunction(function);
    getMathCollection().updateUndoStack();
    document.getKetPanel().updateAndRepaint();
    done();
  }

  @Override
  public void replaceByText(String text) {
    // Do nothing
  }

  public Function iconToFunction(Icon icon) {
    if (icon==PLUS_ICON) return Function.ADD;
    if (icon==MINUS_ICON) return Function.MINUS;
    if (icon==TIMES_ICON) return Function.TIMES;
    if (icon==FRACTION_ICON) return Function.FRACTION;
    if (icon==POWER_ICON) return Function.POWER;
    if (icon==EQUALS_ICON) return Function.EQUALS;
    if (icon==SQRT_ICON) return Function.SQRT;
    if (icon==INTEGRAL_ICON) return Function.INTEGRAL;
    if (icon==DERIVATIVE_ICON) return Function.DERIVATIVE;
    else return null;
  }

  public void replaceByFunction(Function function) {
    boolean currentIsBranch = getCurrent().isBranch();
    if (pending && currentIsBranch) {
      getCurrent().asBranch().setFunction(function);
    } else {
      getSelection().addIntermediaryParent(function);
      if (function!=Function.SQRT) {
        getAddMode().appendNewChild(getChord());
      }
    }
  }

}
TOP

Related Classes of ketUI.OperationMenuFrame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.