Package ketUI.modes

Source Code of ketUI.modes.PasteMode

/*
* 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 PasteMode {
  final Modes modes;

  public PasteMode(Modes modes) {
    this.modes = modes;
  }

  // ---

  public void pasteFromClipboard() {
    Ket.out.println(" --- paste from clipboard --- ");
    Argument clipboardArgument = getClipboard().getArgument("*");
    if (clipboardArgument==null) {
      Ket.out.println(" !!! Can't read from clipboard !!! ");
      return;
    }
    Ket.out.println("clipboard argument = \"" + clipboardArgument + "\"");
    getSelection().appendEquation(new Equation(clipboardArgument));
  }

  public boolean pasteTextFromClipboard() {
    Ket.out.println(" --- paste from clipboard --- ");
    String clipboardString = getClipboard().readFromSystemClipboard();
    if (clipboardString==null) {
      Ket.out.println(" !!! Can't read from clipboard !!! ");
      return false;
    }
    String[] lines = clipboardString.split("\n");
    Ket.out.println("Clipboard:");
    for (String l : lines) {
      Ket.out.println("\t" + l);
      Token text = new Token(new Text(l.trim()));
      getSelection().appendEquation(new Equation(text));
    }
    return true;
  }

  public void copyToClipboard() {
    getClipboard().copyToClipboard(getCurrent());
  }

  // ---

  public boolean addChildAfterFromClipboard(int index) {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    if (getCurrent() instanceof VariableToken) {
      KnownArguments knownArguments = getMathCollection().getKnownArguments();
      getSelection().variableTokenToBranch(knownArguments);
      index = -1;
    }
    getSelection().addChildAfter(index, clipboardArgument);
    return true;
  }

  public boolean appendChildFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    if (getCurrent() instanceof VariableToken) {
      KnownArguments knownArguments = getMathCollection().getKnownArguments();
      getSelection().variableTokenToBranch(knownArguments);
    }
    getSelection().appendChild(clipboardArgument);
    return true;
  }

  public boolean prependUpFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().prependUp(clipboardArgument);
    return true;
  }


  public boolean appendDownFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().appendDown(clipboardArgument);
    return true;
  }

  public boolean appendEquationFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().appendEquation(new Equation(clipboardArgument));
    Ket.out.println("clipboard = " + getClipboard());
    return true;
  }

  public boolean appendSiblingFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().appendSibling(clipboardArgument);
    return true;
  }

  public boolean appendMapFromClipboard() {
    Ket.out.println(" --- append map from clipboard --- ");
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    KnownArguments knownArguments = getMathCollection().getKnownArguments();
    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;
    }
    Function function = knownArguments.toFunction(clipboardArgument.getPurpose());
    if (clipboardArgument.isToken() ) {
      return true
    }
    Branch clipboardBranch = clipboardArgument.asBranch();
    if (clipboardBranch.size()==0) {
      return true;
    }
    for (Argument child : currentBranch.getChildren()) {
      Branch intermediate = child.addIntermediaryParent(function);
      Branch clone = clipboardBranch.cloneBranch();
      intermediate.appendAll(clone.getChildren());
    }
    return true;
  }

  public boolean appendBranchFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    // BUG: This removes current from the equation tree.
    getCurrent().replace(clipboardArgument);
    setCurrent(clipboardArgument);
    return true;
  }

  public boolean appendCurrentFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getCurrent().replaceIntermediate(clipboardArgument);
    setCurrent(clipboardArgument);
    return true;
  }

  public boolean appendInverseParentFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().addIntermediaryParent(clipboardArgument.getFunction());
    getSelection().invert();
    return true;
  }

  public boolean appendParentFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    Argument oldCurrent = getCurrent();
    getSelection().replace(clipboardArgument);
    Branch clipboardBranch = clipboardArgument.asBranch();
    if (clipboardBranch!=null) {
      clipboardBranch.prepend(oldCurrent);
      setCurrent(oldCurrent);
    } else {
      State state = clipboardArgument.getState();
      KnownArguments knownArguments = getMathCollection().getKnownArguments();
      Function function = knownArguments.stateToFunction(state);
      if (function==null) return false;
      getSelection().addIntermediaryParent(function);
    }
    return true;
  }

  public boolean appendPurposeFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    // TODO: Fold into to addIntermediary or similar
    Function function = clipboardArgument.getFunction();
    if (getCurrent() instanceof Branch && function!=null) {
      ((Branch) getCurrent()).setFunction(function);
      return true;
    } else {
      // (no children)
      getSelection().replace(clipboardArgument);
      return true;
    }
  }
 
  public boolean appendSubstituteFromClipboard() {
    // TODO: Check clipboard format is valid and display a warning to the GUI if not.
    Argument oldCurrent = Argument.cloneArgument(getCurrent());
    Argument substitution = getClipboard().getArgument();
    if (substitution instanceof Token) {
      return false;
    }
    getSelection().substitute(substitution.asBranch());
    getClipboard().setDeletedArgument(oldCurrent);
    return true;
  }

  public boolean appendTraceFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    // BUG: using this on the root of an equation prepends rather than appends the clipboard argument.
    if ( ! (clipboardArgument instanceof Branch)) {
      modes.error(" !!! Cannot paste a non-branch as a parent. !!! ");
      return false;
    }
    Branch clipboardBranch = (Branch) clipboardArgument;
    getCurrent().addIntermediaryParent(clipboardBranch, true);
    setCurrent(clipboardBranch);
    return true;
  }

  public boolean appendWithFunctionFromClipboard(Function function) {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().addParentAndNewSibling(function, true); // TODO: Always add right.
    getSelection().replace(clipboardArgument);
    return true;
  }

  public boolean prependChildFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    if (getCurrent() instanceof VariableToken) {
      KnownArguments knownArguments = getMathCollection().getKnownArguments();
      getSelection().variableTokenToBranch(knownArguments);
    }
    getSelection().prependChild(clipboardArgument);
    return true;
  }

  public boolean prependEquationFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().prependEquation(new Equation(clipboardArgument));
    return true;
  }

  public boolean prependSiblingFromClipboard() {
    Argument clipboardArgument = getClipboard().getArgument();
    if (clipboardArgument==null) {
      modes.error(" !!! Clipboard was empty !!! ");
      return false;
    }
    getSelection().prependSibling(clipboardArgument);
    return true;
  }

  public void yank() {
    if (getMathCollection().isColumnSelectionSet()) {
      Argument selection = getSelection().cloneRange();
      getClipboard().setYankedArgument(selection);
      getMathCollection().setCursorSelection();
    } else if (getMathCollection().isRangeSelectionSet()) {
      Argument selection = getSelection().cloneRange();
      getClipboard().setYankedArgument(selection);
      getMathCollection().setCursorSelection();
    } else {
      getClipboard().setYankedArgument(getCurrent());
    }
  }

  public boolean yankCurrentWithParent() {
    if (getCursor().getParent() instanceof Branch) {
      Argument currentCopy = Argument.cloneArgument(getCurrent());
      Function function = ((Branch) getCursor().getParent()).getFunction();
      Branch parent = new Branch(function);
      parent.append(currentCopy);
      getClipboard().setYankedArgument(parent);
    }
    return false;
  }


  // --- Helper methods ---

  private MathCollection getMathCollection() {
    return modes.getMathCollection();
  }

  private Cursor getCursor() {
    return getMathCollection().getCursor();
  }

  public Argument getCurrent() {
    return getCursor().getCurrent();
  }

  private Clipboard getClipboard() {
    return   modes.getClipboard();
  }

  private Selection getSelection() {
    return getMathCollection().getSelection();
  }

  private void setCurrent(Argument current) {
    getCursor().setCurrent(current);
  }
}
TOP

Related Classes of ketUI.modes.PasteMode

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.