/*
* 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 java.util.*;
import ket.*;
import ket.math.*;
import ket.math.purpose.NumberValue;
public class Find {
public static final boolean FORWARDS = false;
public static final boolean BACKWARDS = true;
public static final boolean SAME_DIRECTION = false;
public static final boolean REVERSE_DIRECTION = true;
private Selection selection;
private char character;
private boolean lastDirection;
public Find(Selection selection) {
this.selection = selection;
// Space is equivalent to null here.
character = ' ';
lastDirection = FORWARDS;
}
/**
* Search through a given document for the next argument whose name
* begins with 'c' or is a function of type 'c'.
*/
public Argument find(char character, boolean findDirection, KnownArguments knownArguments) {
this.lastDirection = findDirection;
this.character = character;
return findAgain(SAME_DIRECTION, knownArguments);
}
/**
* Find the next occurrence of a given character (returning null if
* searching for ' ' or for no matches.
*/
public Argument findAgain(boolean consistentFindDirection, KnownArguments knownArguments) {
// Sort the current equation's arguments in search order.
Equation currentEquation = selection.getCursor().getEquation();
boolean reverse = lastDirection ^ consistentFindDirection;
ArgumentVector argsVector = new ArgumentVector(
currentEquation.getVisibleRoot(),
ArgumentVector.EXCLUDE_HIDDEN_CHILDREN|
ArgumentVector.INCLUDE_ROOT);
Vector<Argument> subVector = argsVector.subVector(
selection.getCurrent(),
reverse);
Function matchedFunction = knownArguments.getFunctionByChar(character);
// TODO: Handle special case of find '-' of a negative number.
for (Argument argument : subVector) {
Purpose purpose = argument.getPurpose();
if (purpose==null) {
continue;
}
boolean match = matchedFunction==purpose;
match |= purpose.matchTypeChar(character);
String name = purpose.getName();
if ((purpose!=Function.ADD) &&
(purpose!=Function.MINUS) &&
(purpose!=Function.TIMES) &&
(purpose!=Function.FRACTION) &&
(purpose!=Function.POWER)) {
match |= name.charAt(0)==character;
}
char first = name.charAt(0);
if (Character.isUpperCase(first)) {
match |= Character.toLowerCase(first)==character;
}
if (purpose instanceof NumberValue) {
NumberValue numberValue = (NumberValue) purpose;
match |= numberValue.matchChar(character);
}
if (match) {
selection.setCurrent(argument);
return argument;
}
}
return null;
}
}