Changeset 162


Ignore:
Timestamp:
07/28/08 15:08:13 (16 years ago)
Author:
ra33
Message:

Added faster versions of search frameset

Location:
trunk/src/org/expeditee
Files:
2 added
9 edited

Legend:

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

    r161 r162  
    2525import org.expeditee.gui.FrameIO;
    2626import org.expeditee.gui.FrameMouseActions;
     27import org.expeditee.gui.FrameUtils;
    2728import org.expeditee.gui.MessageBay;
    2829import org.expeditee.gui.TimeKeeper;
    2930import org.expeditee.items.Item;
     31import org.expeditee.items.ItemUtils;
    3032import org.expeditee.items.Text;
    3133import org.expeditee.simple.ExpediteeJEP;
     
    155157                getFromChildFrame(current, false);
    156158        }
     159       
     160        public static void GetItemsFromChildFrame(Item current) {
     161                getItemsFromChildFrame(current, false);
     162        }
    157163
    158164        /**
     
    180186                item.setPosition(DisplayIO.getMouseX(), FrameMouseActions.getY());
    181187                item.setParent(null);
    182 
    183188                FrameMouseActions.pickup(item);
     189                FrameGraphics.Repaint();
     190        }
     191       
     192        private static void getItemsFromChildFrame(Item current, boolean textOnly) {
     193                Collection<Item> items = getItemsOnChildFrame(current, textOnly);
     194                // if no item was found
     195                if (items == null || items.size() == 0) {
     196                        return;
     197                }
     198
     199                // copy the item and switch
     200                Collection<Item> copies = ItemUtils.CopyItems(items);
     201                float deltaX = DisplayIO.getMouseX();
     202                float deltaY = FrameMouseActions.getY();
     203                for(Item i: copies){
     204                        i.setXY(i.getX() + deltaX, i.getY() + deltaY);
     205                        i.setParent(null);
     206                }
     207                FrameMouseActions.pickup(copies);
    184208                FrameGraphics.Repaint();
    185209        }
     
    657681                return item;
    658682        }
     683       
     684        private static Collection<Item> getItemsOnChildFrame(Item current,
     685                        boolean textOnly) {
     686                // the item must link to a frame
     687                if (current.getLink() == null) {
     688                        MessageBay
     689                                        .displayMessage("Cannot get item from child - this item has no link");
     690                        return null;
     691                }
     692                Frame child = FrameIO.LoadFrame(current.getAbsoluteLink());
     693
     694                // if the frame could not be loaded
     695                if (child == null) {
     696                        MessageBay.errorMessage("Could not load child frame.");
     697                        return null;
     698                }
     699
     700                // find the first non-title and non-name item
     701                Collection<Item> body = new ArrayList<Item>();
     702                if (textOnly)
     703                        body.addAll(child.getBodyTextItems(false));
     704                else
     705                        body.addAll(child.getItems());
     706               
     707                return body;
     708        }
    659709
    660710        public static void calculate(Frame frame, Item toCalculate) {
     
    667717                                myParser.addVariables(FrameIO.LoadFrame(linkedFrame));
    668718                        }
    669                         myParser.addObserver();
     719                        myParser.resetObserver();
    670720
    671721                        // Do the calculation
     
    677727                                String result = myParser.getResult();
    678728                                if (result == null) {
    679                                         MessageBay.errorMessage(myParser.getErrorInfo().replace("\n",
    680                                         ""));
     729                                        MessageBay.errorMessage(myParser.getErrorInfo().replace(
     730                                                        "\n", ""));
    681731                                } else {
    682732                                        text.setText(result);
     
    685735                                                                FrameMouseActions.MouseY);
    686736                                                FrameMouseActions.resetOffset();
     737                                        } else {
     738                                                text.getParentOrCurrentFrame().change();
    687739                                        }
    688740                                }
  • trunk/src/org/expeditee/actions/NavigationActions.java

    r121 r162  
    55import org.expeditee.gui.DisplayIO;
    66import org.expeditee.gui.Frame;
     7import org.expeditee.gui.FrameCreator;
     8import org.expeditee.gui.FrameGraphics;
    79import org.expeditee.gui.FrameIO;
    810import org.expeditee.gui.FrameUtils;
    911import org.expeditee.gui.MessageBay;
    1012import org.expeditee.items.Item;
     13import org.expeditee.items.Text;
    1114
    1215/**
  • trunk/src/org/expeditee/actions/Simple.java

    r161 r162  
    162162                do {
    163163                        next = FrameIO.LoadNext(next);
    164                 } while (next != null && next.isTestFrame());
     164                } while (next != null && !next.isTestFrame());
    165165                FrameUtils.DisplayFrame(next, true);
    166166        }
     
    170170                do {
    171171                        prev = FrameIO.LoadPrevious(prev);
    172                 } while (prev != null && prev.isTestFrame());
     172                } while (prev != null && !prev.isTestFrame());
    173173
    174174                FrameUtils.DisplayFrame(prev, true);
     
    783783                        // Check for set statements
    784784                } else if (tokens[0].startsWith("calculatestring")) {
    785                         assertMinParametreCount(tokens, 2);
    786                         String toCalculate = context.getPrimitives().getStringValue(tokens[1]);
     785                        assertMinParametreCount(tokens, 1);
     786                        String toCalculate = context.getPrimitives().getStringValue(
     787                                        tokens[1]);
    787788                        boolean result = true;
     789                        String error = "";
    788790                        ExpediteeJEP myParser = new ExpediteeJEP();
    789                         //Add the variables in the system
     791                        // Add the variables in the system
    790792                        context.getPrimitives().addToParser(myParser);
    791793                        Node equation = myParser.parseExpression(toCalculate);
    792                         //TODO get the answer
    793                        
     794                        // TODO get the answer
     795                        if (equation == null) {
     796                                result = false;
     797                                error = myParser.getErrorInfo().replace("\n", "");
     798                        } else {
     799                                double value = myParser.getValue();
     800                                if (myParser.hasError()) {
     801                                        result = false;
     802                                        error = myParser.getErrorInfo().replace("\n", "");
     803                                } else {
     804                                        // Add a new variable if its an assignment statement
     805                                        String newVar = myParser.getNewVariable();
     806                                        if (newVar.length() > 0) {
     807                                                try {
     808                                                        context.getPrimitives().setValue(newVar,
     809                                                                        new SReal(value));
     810                                                } catch (IncorrectTypeException e) {
     811                                                        result = false;
     812                                                        error = e.getMessage();
     813                                                }
     814                                        }
     815                                        if (tokens.length > 2) {
     816                                                context.getPrimitives().setValue(tokens[2],
     817                                                                new SReal(value));
     818                                        }
     819                                }
     820                        }
     821                        // Set the result variable if there is one
     822                        if (tokens.length > 3) {
     823                                context.getPrimitives().setValue(tokens[3],
     824                                                new SBoolean(result));
     825                                // Set the result variable if there is one
     826                                if (tokens.length > 4 && !result) {
     827                                        context.getPrimitives().setValue(tokens[4], error);
     828                                }
     829                        }
    794830                } else if (tokens[0].startsWith("calculateitem")) {
    795831                        assertMinParametreCount(tokens, 1);
    796832                        assertVariableType(tokens[1], 1, SPointer.itemPrefix);
    797                         Item item = (Item)context.getPointers().getVariable(tokens[1]).getValue();
     833                        Item item = (Item) context.getPointers().getVariable(tokens[1])
     834                                        .getValue();
    798835                        Frame frame = null;
    799                         if(tokens.length > 2){
     836                        if (tokens.length > 2) {
    800837                                assertVariableType(tokens[2], 2, SPointer.framePrefix);
    801                                 frame = (Frame)context.getPointers().getVariable(tokens[2]).getValue();
     838                                frame = (Frame) context.getPointers().getVariable(tokens[2])
     839                                                .getValue();
    802840                        }
    803841                        Misc.calculate(frame, item);
    804                 }else if (tokens[0].startsWith("issearchpatternvalid")) {
     842                } else if (tokens[0].startsWith("issearchpatternvalid")) {
    805843                        assertExactParametreCount(tokens, 2);
    806844                        boolean result = true;
     
    885923                                String pattern = context.getPrimitives().getStringValue(
    886924                                                tokens[2]);
    887                                 SearchAgent searchAgent = new SearchFrameset();
     925                                long firstFrame = 1;
     926                                long maxFrame = Long.MAX_VALUE;
     927                                if(tokens.length > 7){
     928                                        firstFrame = context.getPrimitives().getIntegerValue(tokens[7]);
     929                                        if(tokens.length > 8){
     930                                                maxFrame = context.getPrimitives().getIntegerValue(tokens[8]);
     931                                        }
     932                                }
     933                               
     934                                SearchAgent searchAgent = new SearchFrameset(firstFrame, maxFrame);
    888935                                searchAgent.initialise(null, null, frameset, resultsFrameset,
    889936                                                replacementString, pattern);
     
    11871234                                                        .getStringValue(tokens[1]), context.getPrimitives()
    11881235                                                        .getStringValue(tokens[2]));
     1236                        } else if (tokens[0].equals("assertequalframes")) {
     1237                                assertExactParametreCount(tokens, 2);
     1238                                assertVariableType(tokens[1], 1, SPointer.framePrefix);
     1239                                assertVariableType(tokens[2], 2, SPointer.framePrefix);
     1240                                Frame frame1 = (Frame) context.getPointers().getVariable(
     1241                                                tokens[1]).getValue();
     1242                                Frame frame2 = (Frame) context.getPointers().getVariable(
     1243                                                tokens[2]).getValue();
     1244                                // Check that all the items on the frame are the same
     1245                                List<Item> items1 = frame1.getVisibleItems();
     1246                                List<Item> items2 = frame2.getVisibleItems();
     1247                                if (items1.size() != items2.size()) {
     1248                                        throw new UnitTestFailedException(items1.size() + " items",
     1249                                                        items2.size() + " items");
     1250                                } else {
     1251                                        for (int i = 0; i < items1.size(); i++) {
     1252                                                Item i1 = items1.get(i);
     1253                                                Item i2 = items2.get(i);
     1254                                                String s1 = i1.getText();
     1255                                                String s2 = i2.getText();
     1256                                                if (!s1.equals(s2)) {
     1257                                                        throw new UnitTestFailedException(s1, s2);
     1258                                                }
     1259                                        }
     1260                                }
    11891261                        } else if (tokens[0].equals("assertdefined")) {
    11901262                                assertExactParametreCount(tokens, 1);
     
    11931265                                                        "not defined");
    11941266                                }
     1267                        } else {
     1268                                throw new RuntimeException("Invalid Assert statement");
    11951269                        }
    11961270                } else if (tokens[0].startsWith("goto")) {
     
    19572031                                freshCopy.setFrameset(destinationFrameset);
    19582032                        }// Otherwise add it to the end of this frameset
    1959                         freshCopy.setFrameNumber(FrameIO.getLastNumber(freshCopy
    1960                                         .getFramesetName()) + 1);
    1961                         context.getPointers().setObject(tokens[2], freshCopy);
    1962                         String fileContents = FrameIO.ForceSaveFrame(freshCopy);
     2033                        int nextNumber = FrameIO.getLastNumber(freshCopy.getFramesetName()) + 1;
     2034                        // if the frameset doesnt already exist then create it
     2035                        if (nextNumber <= 0) {
     2036                                try {
     2037                                        FrameIO.CreateFrameset(freshCopy.getFramesetName(),
     2038                                                        frameToCopy.path);
     2039                                        nextNumber = 1;
     2040                                } catch (Exception e) {
     2041                                }
     2042                        }
     2043                        boolean success = false;
     2044                        if (nextNumber > 0) {
     2045                                freshCopy.setFrameNumber(nextNumber);
     2046                                context.getPointers().setObject(tokens[2], freshCopy);
     2047                                String fileContents = FrameIO.ForceSaveFrame(freshCopy);
     2048                                success = fileContents != null;
     2049                        }
    19632050                        FrameIO.ResumeCache();
    1964                         // Need to add the new copy to the cache in case it is edited by
    1965                         // other simple statements
    1966                         FrameIO.addToCache(freshCopy);
    1967                         boolean success = fileContents != null;
     2051                        if (success) {
     2052                                // Need to add the new copy to the cache in case it is edited by
     2053                                // other simple statements
     2054                                FrameIO.addToCache(freshCopy);
     2055                        }
    19682056                        if (!success && _verbose)
    19692057                                MessageBay.warningMessage("Error copying "
  • trunk/src/org/expeditee/agents/SearchAgent.java

    r156 r162  
    1111public abstract class SearchAgent extends DefaultAgent {
    1212
     13        private static final String DEFAULT_RESULTS_FRAMESET = "SearchResults";
     14       
    1315        protected FrameCreator _results;
    1416
     
    2628                //TODO use a results frame specified on the profile frame
    2729                if (item.getLink() == null) {
    28                         resultFrameset = frame.getFramesetName();
     30                        resultFrameset = DEFAULT_RESULTS_FRAMESET;
    2931                } else {
    3032                        resultFrameset = Conversion.getFramesetName(item.getAbsoluteLink(),
     
    3638        public boolean initialise(Frame frame, Item item, String startName, String resultsFrameset,
    3739                        String replacementString, String pattern) {
    38                 _pattern = pattern;
     40                _pattern = pattern.toLowerCase();
    3941                _replacementString = replacementString;
    4042                _startName = startName;
     
    5860                        String replacementString) {
    5961                String searchStr = itemToSearch.getText().toLowerCase();
    60                 String[] result = searchStr.split(pattern.toLowerCase(), 2);
    61                 boolean bFound = result.length > 1;
     62                boolean bFound = searchStr.matches(pattern.toLowerCase());
    6263                // If it is a find and replace... then replace with the replacement
    6364                // string
  • trunk/src/org/expeditee/agents/SearchFrameset.java

    r134 r162  
    55
    66public class SearchFrameset extends SearchAgent {
     7        private long _firstFrame = 1;
     8        private long _maxFrame = Integer.MAX_VALUE;
     9       
     10        public SearchFrameset(long firstFrame, long maxFrame) {
     11                _firstFrame = firstFrame;
     12                _maxFrame = maxFrame;
     13        }
     14       
     15        public SearchFrameset() {
     16                super();
     17        }
    718
    819        @Override
    920        protected Frame process(Frame frame) {
    1021                int count = FrameIO.getLastNumber(_startName);
    11                 for (int i = 1; i < count; i++) {
     22                for (long i = _firstFrame;i <= _maxFrame && i < count; i++) {
    1223                        if (_stop) {
    1324                                break;
  • trunk/src/org/expeditee/gui/DisplayIO.java

    r156 r162  
    6262         * The title to display in the Title bar.
    6363         */
    64         public static final String TITLE = "Exp22Jul2008A";
     64        public static final String TITLE = "Exp28Jul2008A";
    6565
    6666        private DisplayIO() {
     
    605605                Frame frame = FrameIO.LoadFrame(_VisitedFrames[side].pop());
    606606                // If the top frame on the backup stack is the current frame go back
    607                 // again...
    608                 if (frame.equals(getCurrentFrame())) {
     607                // again... or if it has been deleted
     608                //Recursively backup the stack
     609                if(frame == null || frame.equals(getCurrentFrame())) {
    609610                        Back();
    610611                        return;
  • trunk/src/org/expeditee/gui/FrameIO.java

    r156 r162  
    55import java.io.File;
    66import java.io.FileInputStream;
     7import java.io.FileNotFoundException;
    78import java.io.FileOutputStream;
    89import java.io.FileReader;
     
    1011import java.io.IOException;
    1112import java.sql.Time;
     13import java.util.Collection;
    1214import java.util.HashMap;
     15import java.util.LinkedList;
    1316
    1417import org.expeditee.agents.ExistingFramesetException;
     
    239242        }
    240243
     244        public static Collection<String> searchFrame(String frameName,
     245                        String pattern, String path) {
     246                String fullPath = null;
     247                if (path == null) {
     248                        for (String possiblePath : UserSettings.FrameDirs) {
     249                                fullPath = getFrameFullPathName(possiblePath, frameName);
     250                                if (fullPath != null)
     251                                        break;
     252                        }
     253                } else {
     254                        fullPath = getFrameFullPathName(path, frameName);
     255                        assert(fullPath != null);
     256                }
     257                // If the frame was not located return null
     258                if (fullPath == null)
     259                        return null;
     260                Collection<String> results = new LinkedList<String>();
     261                // Open the file and search the text items
     262                try {
     263                        BufferedReader reader = new BufferedReader(new FileReader(fullPath));
     264                        String next;
     265                        while (reader.ready() && ((next = reader.readLine()) != null)) {
     266                                if (next.startsWith("T")) {
     267                                        String toSearch = next.substring(2);
     268                                        if (toSearch.toLowerCase().matches(pattern))
     269                                                results.add(toSearch);
     270                                }
     271                        }
     272                } catch (FileNotFoundException e) {
     273                        e.printStackTrace();
     274                        return null;
     275                } catch (IOException e) {
     276                        e.printStackTrace();
     277                }
     278                return results;
     279        }
     280
    241281        private static Frame LoadFrame(String path, String frameName) {
    242282                String fullPath = getFrameFullPathName(path, frameName);
     
    349389                // if we did not find another Frame then this one must be the last one
    350390                // in the frameset
    351                 MessageBay
    352                                 .displayMessageOnce("This is the last frame in the frameset");
     391                MessageBay.displayMessageOnce("This is the last frame in the frameset");
    353392                return null;
    354393        }
     
    421460         * @param toDelete
    422461         *            The Frame to be deleted
    423          * @return The name the deleted frame was changed to, or null if the delete failed
     462         * @return The name the deleted frame was changed to, or null if the delete
     463         *         failed
    424464         */
    425465        public static String DeleteFrame(Frame toDelete) throws IOException,
     
    468508                ff.close();
    469509
    470                 if( del.delete()){
     510                if (del.delete()) {
    471511                        return toDelete.getName();
    472512                }
    473                
     513
    474514                return null;
    475515        }
     
    552592                                i.setLink(null);
    553593                }
    554                
     594
    555595                // do auto shrinking of the title IF not in twin frames mode
    556596                Item titleItem = template.getTitleItem();
     
    676716                        boolean checkBackup) {
    677717
    678                 // TODO When loading a frame maybe append onto the event history too- with a
     718                // TODO When loading a frame maybe append onto the event history too-
     719                // with a
    679720                // break to indicate the end of a session
    680721
    681                 if (toSave == null ||!toSave.hasChanged() || toSave.isSaved()){
     722                if (toSave == null || !toSave.hasChanged() || toSave.isSaved()) {
    682723                        SessionStats.NewFrameSession();
    683724                        return "";
     
    691732
    692733                // Dont save if the frame is protected and it exists
    693                 if (checkBackup
    694                                 && toSave.isReadOnly()) {
     734                if (checkBackup && toSave.isReadOnly()) {
    695735                        _Cache.remove(toSave.getName().toLowerCase());
    696736                        SessionStats.NewFrameSession();
     
    776816                        toSave.setActiveTime(activeTime);
    777817
    778                         //int oldMode = FrameGraphics.getMode();
    779                         //if (oldMode != FrameGraphics.MODE_XRAY)
    780                         //      FrameGraphics.setMode(FrameGraphics.MODE_XRAY, true);
     818                        // int oldMode = FrameGraphics.getMode();
     819                        // if (oldMode != FrameGraphics.MODE_XRAY)
     820                        // FrameGraphics.setMode(FrameGraphics.MODE_XRAY, true);
    781821                        writer.writeFrame(toSave);
    782                         //FrameGraphics.setMode(oldMode, true);
     822                        // FrameGraphics.setMode(oldMode, true);
    783823                        toSave.setSaved();
    784824                        if (inc) {
  • trunk/src/org/expeditee/gui/FrameMouseActions.java

    r156 r162  
    797797                                        refreshHighlights();
    798798                                }
     799                                FrameGraphics.requestRefresh(true);
    799800                                return;
    800801                                // otherwise, anchor the items
  • trunk/src/org/expeditee/simple/ExpediteeJEP.java

    r161 r162  
    1313
    1414public class ExpediteeJEP extends JEP {
    15         Observer observer = new Observer() {
    16                 private String _attribute = "";
    17 
    18                 public void update(Observable ob, Object o) {
    19                         _attribute = ((Variable) o).getName() + ": ";
    20                 }
    21 
    22                 @Override
    23                 public String toString() {
    24                         return _attribute;
    25                 }
    26         };
     15        Observer observer = null;
    2716       
    2817        public ExpediteeJEP() {
     
    3322                setAllowAssignment(true);
    3423                setAllowUndeclared(true);
     24                resetObserver();
    3525        }
    3626       
    37         public void addObserver() {
     27        public void resetObserver() {
     28                observer = new Observer() {
     29                        private String _attribute = "";
     30
     31                        public void update(Observable ob, Object o) {
     32                                _attribute = ((Variable) o).getName();
     33                        }
     34
     35                        @Override
     36                        public String toString() {
     37                                return _attribute;
     38                        }
     39                };
    3840                getSymbolTable().addObserver(observer);
    3941                getSymbolTable().addObserverToExistingVariables(observer);
     
    4850                nf.setMinimumFractionDigits(0);
    4951                nf.setMaximumFractionDigits(15);
    50                 return observer.toString() + nf.format(result);
     52                return observer.toString()+ ": " + nf.format(result);
    5153        }
    5254
     
    6365                }
    6466        }
     67
     68        public String getNewVariable() {
     69                return observer.toString();
     70        }
    6571}
Note: See TracChangeset for help on using the changeset viewer.