Ignore:
Timestamp:
08/01/08 16:38:58 (16 years ago)
Author:
ra33
Message:

Work on Bridging the Gap between SimpleStatements and Actions/Agents

File:
1 edited

Legend:

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

    r179 r181  
    1515import java.util.jar.JarFile;
    1616import java.util.zip.ZipEntry;
    17 
    18 import javax.swing.SwingUtilities;
    1917
    2018import org.expeditee.agents.Agent;
     
    266264         */
    267265        private static boolean MethodCheck(Method m) {
    268                 // the return type must be void
    269                 if (m.getReturnType() != void.class)
    270                         return false;
    271 
    272266                int mods = m.getModifiers();
    273267
     
    300294         *            The action to perform
    301295         */
    302         public static void PerformAction(Frame source, Item launcher, String command) {
     296        public static Object PerformAction(Frame source, Item launcher,
     297                        String command) throws Exception {
    303298                // if (!command.equalsIgnoreCase("Restore"))
    304299                // FrameIO.SaveFrame(source, false);
     
    320315                // check for protection on frame
    321316                if (ItemUtils.ContainsTag(source.getItems(), "@No" + mname)) {
    322                         MessageBay.displayMessage("Frame is protected by @No" + mname
     317                        throw new RuntimeException("Frame is protected by @No" + mname
    323318                                        + " tag.");
    324                         return;
    325319                }
    326320
     
    331325                if (toRun == null) {
    332326                        LaunchAgent(mname, command, source, launcher);
    333                         return;
     327                        return null;
    334328                }
    335329
     
    362356                                }
    363357
    364                                 possible.invoke(null, parameters);
    365                                 return;
     358                                return possible.invoke(null, parameters);
    366359                        } catch (Exception e) {
    367360                                Logger.Log(e);
     
    369362                        }
    370363                }
    371                 if (possibles.size() > 0) {
    372                         MessageBay.errorMessage("Incorrect parameters for " + mname);
    373                 } else {
    374                         assert (false);
    375                         MessageBay.errorMessage(mname + " action not found");
    376                 }
     364                //If the actions was not found... then it is run as an agent
     365                assert (possibles.size() > 0);
     366                throw new RuntimeException("Incorrect parameters for " + mname);
    377367        }
    378368
     
    388378         */
    389379        private static void LaunchAgent(String name, String parameters,
    390                         Frame source, Item clicked) {
     380                        Frame source, Item clicked) throws Exception {
    391381                // Use the correct case version for printing error messages
    392382                String nameWithCorrectCase = name;
    393383                name = name.toLowerCase();
    394                 // save the current frame (if necesssary)
    395                 // TODO make this nicer... ie. make Format an action rather than an
    396                 // agent and save frames only before running agents
    397                 if (!name.equalsIgnoreCase("format") && !name.equalsIgnoreCase("sort")) {
    398                         FrameUtils.LeavingFrame(source);
    399                 }
    400384
    401385                try {
    402386                        // check for stored capitalisation
    403                         if (_JAGs.containsKey(name.toLowerCase())) {
    404                                 name = _JAGs.get(name.toLowerCase());
    405                         } else if (name.toLowerCase().endsWith("tree")) {
     387                        if (_JAGs.containsKey(name)) {
     388                                name = _JAGs.get(name);
     389                        } else if (name.endsWith("tree")) {
    406390                                parameters = name.substring(0, name.length() - "tree".length())
    407391                                                + " " + parameters;
    408                                 name = "WriteTree";
    409                         } else if (name.toLowerCase().endsWith("frame")) {
     392                                name = "writetree";
     393                        } else if (name.endsWith("frame")) {
    410394                                parameters = name
    411395                                                .substring(0, name.length() - "frame".length())
    412396                                                + " " + parameters;
    413                                 name = "WriteFrame";
     397                                name = "writeframe";
    414398                        }
    415399
     
    437421                        // if there is no constructor, return
    438422                        if (con == null) {
    439                                 MessageBay.displayMessage(INVALID_PARAMETERS_ERROR
     423                                throw new RuntimeException(INVALID_PARAMETERS_ERROR
    440424                                                + nameWithCorrectCase);
    441                                 // System.out.println("Constructor not found...");
    442                                 return;
    443425                        }
    444426
     
    456438                        // check for errors during initialisation
    457439                        if (!_Agent.initialise(source, itemParam)) {
    458                                 MessageBay.errorMessage("Error initialising agent: "
     440                                _Agent = null;
     441                                throw new RuntimeException("Error initialising agent: "
    459442                                                + nameWithCorrectCase);
    460                                 _Agent = null;
    461                                 return;
     443                        }
     444
     445                        // save the current frame (if necesssary)
     446                        // TODO make this nicer... ie. make Format an action rather than an
     447                        // agent and save frames only before running agents
     448                        if (!name.equals("format") && !name.equals("sort")) {
     449                                FrameUtils.LeavingFrame(source);
    462450                        }
    463451
     
    478466                                        // TODO We want to be able to navigate through the frames as
    479467                                        // the results are loading
    480 
    481468                                        Frame next = _Agent.getResultFrame();
    482469                                        FrameUtils.DisplayFrame(next, true);
     
    485472
    486473                } catch (ClassNotFoundException cnf) {
    487                         MessageBay.errorMessage(nameWithCorrectCase
     474                        _Agent = null;
     475                        throw new RuntimeException(nameWithCorrectCase
    488476                                        + "' is not an action or agent.");
    489477                } catch (Exception e) {
    490                         MessageBay.errorMessage("Error creating Agent: '"
    491                                         + nameWithCorrectCase + "'");
    492                         System.out.println("Agent set to Null.");
    493478                        _Agent = null;
    494479                        e.printStackTrace();
    495                         Logger.Log(e);
     480                        throw new RuntimeException("Error creating Agent: '"
     481                                        + nameWithCorrectCase + "'");
    496482                }
    497483                FrameGraphics.refresh(false);
     
    554540
    555541                // if the first class in the list is a frame or item, it is the source
    556                 // or launcher
     542                //or launcher
    557543                // length must be at least one if we are still running
    558544                if (paramTypes[ind] == Frame.class) {
     
    561547                }
    562548
     549                //Check if the second item is an item
    563550                if (paramCount > ind && Item.class.isAssignableFrom(paramTypes[ind])) {
    564551                        objects[ind] = launcher;
    565552                        ind++;
     553                }//If there is stuff on the cursor use it for the rest of the params
     554                else if (launcher != null && launcher.isFloating()){
     555                        values = launcher.getText();
    566556                }
    567557
     
    576566                                        int endOfString = param.indexOf('"', 1);
    577567                                        if (endOfString > 0) {
    578                                                 param = param.substring(0, endOfString);
     568                                                param = param.substring(1, endOfString);
    579569                                        }
    580570                                }
     
    708698                return _Fonts;
    709699        }
     700
     701        public static Object PerformActionCatchErrors(Frame current, Item launcher, String command) {
     702                try{
     703                        return PerformAction(current, launcher, command);
     704                }catch(Exception e) {
     705                        MessageBay.errorMessage(e.getMessage());
     706                }
     707                return null;
     708        }
    710709}
Note: See TracChangeset for help on using the changeset viewer.