Changeset 161


Ignore:
Timestamp:
07/25/08 16:37:58 (16 years ago)
Author:
ra33
Message:

Adding calculation stuff...

Location:
trunk/src/org/expeditee
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Misc.java

    r160 r161  
    2929import org.expeditee.items.Item;
    3030import org.expeditee.items.Text;
     31import org.expeditee.simple.ExpediteeJEP;
    3132import org.expeditee.stats.CometStats;
    3233import org.expeditee.stats.SessionStats;
     
    660661                if (toCalculate instanceof Text) {
    661662                        Text text = (Text) toCalculate;
    662                         org.nfunk.jep.JEP myParser = new org.nfunk.jep.JEP();
    663                         myParser.addStandardFunctions();
    664                         myParser.addStandardConstants();
    665                         myParser.setImplicitMul(true);
    666 
    667                         Observer observer = new Observer() {
    668                                 private String _attribute = "";
    669 
    670                                 public void update(Observable ob, Object o) {
    671                                         _attribute = ((Variable) o).getName() + ": ";
    672                                 }
    673 
    674                                 @Override
    675                                 public String toString() {
    676                                         return _attribute;
    677                                 }
    678                         };
    679 
    680                         // Check for variables
    681                         for (Text t : frame.getTextItems()) {
    682                                 AttributeValuePair avp = AttributeUtils.getPair(t.getText());
    683                                 if (avp != null) {
    684                                         myParser.addVariable(avp.getAttribute(), avp
    685                                                         .getDoubleValue());
    686                                 }
     663                        ExpediteeJEP myParser = new ExpediteeJEP();
     664                        myParser.addVariables(frame);
     665                        String linkedFrame = toCalculate.getAbsoluteLink();
     666                        if (linkedFrame != null) {
     667                                myParser.addVariables(FrameIO.LoadFrame(linkedFrame));
    687668                        }
    688 
    689                         myParser.getSymbolTable().addObserver(observer);
    690                         myParser.setAllowAssignment(true);
    691                         myParser.setAllowUndeclared(true);
     669                        myParser.addObserver();
    692670
    693671                        // Do the calculation
    694672                        Node node = myParser.parseExpression(text.getText());
    695673                        if (node == null) {
    696                                 for (String s : myParser.getErrorInfo().split("\n")) {
    697                                         MessageBay.errorMessage(s);
     674                                MessageBay.errorMessage(myParser.getErrorInfo().replace("\n",
     675                                                ""));
     676                        } else {
     677                                String result = myParser.getResult();
     678                                if (result == null) {
     679                                        MessageBay.errorMessage(myParser.getErrorInfo().replace("\n",
     680                                        ""));
     681                                } else {
     682                                        text.setText(result);
     683                                        if (text.isFloating()) {
     684                                                text.setPosition(FrameMouseActions.MouseX,
     685                                                                FrameMouseActions.MouseY);
     686                                                FrameMouseActions.resetOffset();
     687                                        }
    698688                                }
    699                         } else {
    700                                 Double result = myParser.getValue();
    701                                 NumberFormat nf = NumberFormat.getInstance();
    702                                 nf.setMinimumFractionDigits(0);
    703                                 nf.setMaximumFractionDigits(15);
    704                                 text.setText(observer.toString() + nf.format(result));
    705                                 text.setPosition(FrameMouseActions.MouseX,
    706                                                 FrameMouseActions.MouseY);
    707                                 FrameMouseActions.resetOffset();
    708689                        }
    709690
  • trunk/src/org/expeditee/actions/Simple.java

    r156 r161  
    4444import org.expeditee.simple.BelowMinParametreCountException;
    4545import org.expeditee.simple.Context;
     46import org.expeditee.simple.ExpediteeJEP;
    4647import org.expeditee.simple.IncorrectParametreCountException;
    4748import org.expeditee.simple.IncorrectTypeException;
     
    5960import org.expeditee.stats.AgentStats;
    6061import org.expeditee.stats.SessionStats;
     62import org.nfunk.jep.Node;
    6163
    6264public class Simple implements Runnable {
     
    313315                        t.start();
    314316                } else {
    315                         if (!testSuite.runSuite()){
     317                        if (!testSuite.runSuite()) {
    316318                                throw new RuntimeException(frameset + " failed");
    317319                        }
     
    780782                        // Check if the user wants to display a message
    781783                        // Check for set statements
    782                 } else if (tokens[0].startsWith("issearchpatternvalid")) {
     784                } else if (tokens[0].startsWith("calculatestring")) {
     785                        assertMinParametreCount(tokens, 2);
     786                        String toCalculate = context.getPrimitives().getStringValue(tokens[1]);
     787                        boolean result = true;
     788                        ExpediteeJEP myParser = new ExpediteeJEP();
     789                        //Add the variables in the system
     790                        context.getPrimitives().addToParser(myParser);
     791                        Node equation = myParser.parseExpression(toCalculate);
     792                        //TODO get the answer
     793                       
     794                } else if (tokens[0].startsWith("calculateitem")) {
     795                        assertMinParametreCount(tokens, 1);
     796                        assertVariableType(tokens[1], 1, SPointer.itemPrefix);
     797                        Item item = (Item)context.getPointers().getVariable(tokens[1]).getValue();
     798                        Frame frame = null;
     799                        if(tokens.length > 2){
     800                                assertVariableType(tokens[2], 2, SPointer.framePrefix);
     801                                frame = (Frame)context.getPointers().getVariable(tokens[2]).getValue();
     802                        }
     803                        Misc.calculate(frame, item);
     804                }else if (tokens[0].startsWith("issearchpatternvalid")) {
    783805                        assertExactParametreCount(tokens, 2);
    784806                        boolean result = true;
     
    901923                                SearchAgent searchAgent = new SearchTree();
    902924                                _agent = searchAgent;
    903                                 searchAgent.initialise(null, null, topFrameName, resultsFrameset,
    904                                                 replacementString, pattern);
     925                                searchAgent.initialise(null, null, topFrameName,
     926                                                resultsFrameset, replacementString, pattern);
    905927                                searchAgent.run();
    906928                                _agent = null;
     
    964986                                Item valueItem = null;
    965987                                // Begin the search
    966                                 for (Text text : targetFrame.getTextItems()){
     988                                for (Text text : targetFrame.getTextItems()) {
    967989                                        String s = text.getText().toLowerCase();
    968990
     
    12711293                                        context.getPointers().setObject(frameTitleVar,
    12721294                                                        frame.getTitleItem());
    1273                                 }else if (tokens[0].startsWith("getframefilepath")) {
     1295                                } else if (tokens[0].startsWith("getframefilepath")) {
    12741296                                        assertExactParametreCount(tokens, 2);
    12751297                                        String frameName = context.getPrimitives().getStringValue(
     
    19992021                        } else
    20002022                                context.readFrame(frameName, frameVar, null);
    2001                 }else if (tokens[0].equals("exitexpeditee")) {
     2023                } else if (tokens[0].equals("exitexpeditee")) {
    20022024                        Browser._theBrowser.exit();
    2003                 }else if (tokens[0].equals("readkbdcond")) {
     2025                } else if (tokens[0].equals("readkbdcond")) {
    20042026
    20052027                        String nextCharVarName = DEFAULT_CHAR;
     
    24832505                        assertExactParametreCount(tokens, 0);
    24842506                        FrameMouseActions.leftButton();
    2485                         //DisplayIO.clickMouse(InputEvent.BUTTON1_MASK);
     2507                        // DisplayIO.clickMouse(InputEvent.BUTTON1_MASK);
    24862508                } else if (tokens[0].equals("clickmiddlebutton")) {
    24872509                        assertExactParametreCount(tokens, 0);
    24882510                        FrameMouseActions.middleButton();
    2489                         //DisplayIO.clickMouse(InputEvent.BUTTON2_MASK);
     2511                        // DisplayIO.clickMouse(InputEvent.BUTTON2_MASK);
    24902512                } else if (tokens[0].equals("clickrightbutton")) {
    24912513                        assertExactParametreCount(tokens, 0);
    24922514                        FrameMouseActions.rightButton();
    2493                         //DisplayIO.clickMouse(InputEvent.BUTTON3_MASK);
     2515                        // DisplayIO.clickMouse(InputEvent.BUTTON3_MASK);
    24942516                } else if (tokens[0].equals("repaint")) {
    24952517                        assertExactParametreCount(tokens, 0);
  • trunk/src/org/expeditee/gui/FrameKeyboardActions.java

    r156 r161  
    1919
    2020import org.expeditee.actions.Actions;
     21import org.expeditee.actions.Misc;
    2122import org.expeditee.actions.NavigationActions;
    2223import org.expeditee.actions.Simple;
     
    380381                case KeyEvent.VK_ESCAPE:
    381382                        // Do escape after control so Ctl+Escape does not perform DropDown
    382                         functionKey(FunctionKey.DropDown, false, false);
     383                        functionKey(FunctionKey.DropDown, e.isShiftDown(), e
     384                                        .isControlDown());
    383385                        SessionStats.Escape();
    384386                        break;
     
    588590                int distance = isShiftDown ? 5 : 20;
    589591                switch (key) {
     592                case KeyEvent.VK_TAB:
     593                        Misc.calculate(DisplayIO.getCurrentFrame(), FrameUtils.getCurrentItem());
     594                        break;
     595                case KeyEvent.VK_ESCAPE:
     596                        // Do escape after control so Ctl+Escape does not perform DropDown
     597                        functionKey(FunctionKey.DropDown, isShiftDown, true);
     598                        SessionStats.Escape();
     599                        break;
    590600                case KeyEvent.VK_1:
    591601                        FrameMouseActions.leftButton();
     
    896906                // get whatever the user is pointing at
    897907                Item on = FrameUtils.getCurrentItem();
    898                
     908
    899909                // check for enclosed mode
    900910                if (on == null && key.ordinal() < FunctionKey.AudienceMode.ordinal()) {
    901911                        Collection<Item> enclosed = FrameUtils.getCurrentItems(on);
    902                        
     912
    903913                        if (enclosed != null && enclosed.size() > 0) {
    904914                                // ensure only one dot\line is present in the list
     
    961971                switch (key) {
    962972                case DropDown:
     973                        if (isShiftDown || isControlDown) {
     974                                Misc.calculate(DisplayIO.getCurrentFrame(), on);
     975                        }
    963976                        Drop(on);
    964977                        return;
  • trunk/src/org/expeditee/simple/Primitives.java

    r115 r161  
    139139                // If one of them is a real and one is an integer then do a numeric
    140140                // comparison
    141                 if ((var1.startsWith(SReal.prefix) || var2
    142                                 .startsWith(SReal.prefix))) {
     141                if ((var1.startsWith(SReal.prefix) || var2.startsWith(SReal.prefix))) {
    143142                        double v1 = getDoubleValue(var1);
    144143                        double v2 = getDoubleValue(var2);
     
    182181        /**
    183182         * Adds two variables to gether returning the value in the first.
     183         *
    184184         * @param toSet
    185185         * @param amount
     
    265265                setValue(answer, new SBoolean(!getVariable(variable).booleanValue()));
    266266        }
     267
     268        public void addToParser(ExpediteeJEP myParser) {
     269                for (SPrimitive var : list_) {
     270                        try {
     271                                Double value = var.doubleValue();
     272                                myParser.addVariable(var.getName(), value);
     273                        } catch (Exception e) {
     274                        }
     275                }
     276        }
    267277}
Note: See TracChangeset for help on using the changeset viewer.