Changeset 296


Ignore:
Timestamp:
09/09/08 12:12:25 (16 years ago)
Author:
bjn8
Message:

3rd party agents can load themselves into expeditee agent collection at runtime

Location:
trunk/src/org/expeditee
Files:
3 edited

Legend:

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

    r294 r296  
    1313import java.util.LinkedList;
    1414import java.util.List;
     15import java.util.Set;
    1516import java.util.jar.JarFile;
    1617import java.util.zip.ZipEntry;
     
    6364        private static HashMap<String, String> _Fonts = new HashMap<String, String>();
    6465
    65         // maps lower case JAG class names to capitalized JAG names
     66        // maps lower case JAG class names to capitalized JAG full class names
    6667        private static HashMap<String, String> _JAGs = new HashMap<String, String>();
    6768
     
    207208                                String name = classes[i].getSimpleName();
    208209                                // maps lower case name to correct capitalised name
    209                                 _JAGs.put(name.toLowerCase(), name);
     210                                _JAGs.put(name.toLowerCase(), classes[i].getName());
    210211                        }
    211212
     
    254255
    255256        /**
     257         * Temporary, if a plugin system is devised then this would porbably become redundant.
     258         * For now this allows external agents to be included.
     259         *
     260         * @param fullClassNames
     261         *              A set of full class names, that is, the class package and name. For example"
     262         *              "org.myplugin.agents.SerializedSearch"
     263         *
     264         * @return
     265         *              A collection of classes their were omitted because either there was a name clash
     266         *              with existing agents or did not exist.
     267         *              i.e. is completely successful this will be empty. Never null.
     268         *
     269         * @throws NullPointerException
     270         *              If fullClassNames is null.
     271         *             
     272         */
     273        public static Collection<String> addAgents(Set<String> fullClassNames) {
     274                if (fullClassNames == null) throw new NullPointerException("fullClassNames");
     275               
     276                List<String> omittedAgents = new LinkedList<String>();
     277               
     278                for (String fullName : fullClassNames) {
     279                       
     280                        if (fullName == null || fullName.length() == 0) continue;
     281                       
     282                        boolean didAdd = false;
     283               
     284                        try {
     285                                // Does the class even exist?
     286                                Class c = Class.forName(fullName);
     287                               
     288                                String name = c.getSimpleName().toLowerCase();
     289
     290                                if (!_JAGs.containsKey(name)) {
     291                                       
     292                                        _JAGs.put(name, fullName);
     293                                        didAdd = true;
     294                                       
     295                                }
     296
     297                        } catch (ClassNotFoundException e) { // Nope it does not exist
     298                                e.printStackTrace();
     299                        }
     300                       
     301                        if (!didAdd) omittedAgents.add(fullName);
     302       
     303                }
     304                       
     305       
     306                return omittedAgents;
     307        }
     308       
     309        /**
    256310         * Loads all the Methods that meet the requirements checked by MethodCheck
    257311         * into the hashtable.
     
    413467                String nameWithCorrectCase = name;
    414468                name = name.toLowerCase();
     469               
     470                String fullClassName = AGENTS_PACKAGE + name;
    415471
    416472                try {
    417473                        // check for stored capitalisation
    418474                        if (_JAGs.containsKey(name)) {
    419                                 name = _JAGs.get(name);
     475                                fullClassName = _JAGs.get(name);
    420476                        } else if (name.endsWith("tree")) {
    421477                                parameters = name.substring(0, name.length() - "tree".length())
    422478                                                + " " + parameters;
    423                                 name = "writetree";
     479                                fullClassName = AGENTS_PACKAGE + "writetree";
     480                               
    424481                        } else if (name.endsWith("frame")) {
    425482                                parameters = name
    426483                                                .substring(0, name.length() - "frame".length())
    427484                                                + " " + parameters;
    428                                 name = "writeframe";
     485                                fullClassName = AGENTS_PACKAGE + "writeframe";
    429486                        }
    430487
    431488                        // load the JAG class
    432                         Class agentClass = Class.forName(AGENTS_PACKAGE + name);
     489                        Class agentClass = Class.forName(fullClassName);
    433490
    434491                        // get the constructor for the JAG class
     
    491548
    492549                        // create the JAG
    493                         _Agent = (Agent) con.newInstance(params);
    494 
     550                        Agent toLaunch = (Agent) con.newInstance(params);
     551                       
     552                        LaunchAgent(toLaunch, source, clicked);
     553
     554                } catch (ClassNotFoundException cnf) {
     555                        _Agent = null;
     556                        throw new RuntimeException(nameWithCorrectCase
     557                                        + "' is not an action or agent.");
     558                }
     559        }
     560       
     561        /**
     562         * Launches an agent from a aleady instantiated object.
     563         *
     564         * @param agent
     565         *              The agent to launch. Must not be null.
     566         *
     567         * @param source
     568         *              The calling frame that launched it. Must not be null.
     569         *
     570         * @param itemParam
     571         *              The item paremeter for the agent. Must not be null.
     572         *
     573         * @throws NullPointerException
     574         *              if any of the arguments are null.
     575         */
     576        public static void LaunchAgent(
     577                        Agent agent,
     578                        Frame source,
     579                        Item itemParam) {
     580               
     581                if (agent == null) throw new NullPointerException("agent");
     582                if (source == null) throw new NullPointerException("source");
     583                if (itemParam == null) throw new NullPointerException("itemParam");
     584               
     585                String nameWithCorrectCase = agent.getClass().getSimpleName();
     586               
     587                try {
     588       
     589       
     590                        // create the JAG
     591                        _Agent = agent;
     592       
    495593                        Thread t = new Thread(_Agent);
    496594                        t.setPriority(Thread.MIN_PRIORITY);
    497595
    498                         Item itemParam = clicked;
    499596                        if (FreeItems.textOnlyAttachedToCursor()) {
    500597                                itemParam = FreeItems.getItemAttachedToCursor();
    501598                        }
    502 
     599       
    503600                        // check for errors during initialisation
    504601                        if (!_Agent.initialise(source, itemParam)) {
     
    507604                                                + nameWithCorrectCase);
    508605                        }
    509 
     606       
    510607                        // save the current frame (if necesssary)
    511608                        // TODO make this nicer... ie. make Format an action rather than an
    512609                        // agent and save frames only before running agents
    513                         if (!name.equals("format") && !name.equals("sort")) {
     610                        if (!nameWithCorrectCase.equalsIgnoreCase("format") && !nameWithCorrectCase.equalsIgnoreCase("sort")) {
    514611                                FrameUtils.LeavingFrame(source);
    515612                        }
    516 
     613       
    517614                        if (_Agent.hasResultString()) {
    518615                                // Just run the agent on this thread... dont run it in the
     
    535632                                }
    536633                        }
    537                 } catch (ClassNotFoundException cnf) {
    538                         _Agent = null;
    539                         throw new RuntimeException(nameWithCorrectCase
    540                                         + "' is not an action or agent.");
    541634                } catch (Exception e) {
    542635                        _Agent = null;
     
    546639                }
    547640                FrameGraphics.refresh(false);
    548 
    549                 return;
    550641        }
    551642
  • trunk/src/org/expeditee/gui/FrameGraphics.java

    r286 r296  
    3838
    3939        // Final passes to renderering the current frame
    40         private static LinkedList<FinalFrameRenderPass> _finalPasses = new LinkedList<FinalFrameRenderPass>();
     40        private static LinkedList<FrameRenderPass> _frameRenderPasses = new LinkedList<FrameRenderPass>();
    4141
    4242        // modes
     
    307307        public static void paintFrame(Frame toPaint, Area clip,
    308308                        boolean isActualFrame, boolean createVolitile, Graphics2D bg) {
     309               
     310                // Prepare render passes
     311                if (isActualFrame) {
     312                        currentClip = clip;
     313                        for (FrameRenderPass pass : _frameRenderPasses) {
     314                                currentClip = pass.paintStarted(currentClip);
     315                                clip = currentClip;
     316                        }
     317                }
     318               
    309319                bg.setClip(clip);
    310320
     
    479489                // Repaint popups / drags... As well as final passes
    480490                if (isActualFrame) {
     491                        for (FrameRenderPass pass : _frameRenderPasses) {
     492                                pass.paintPreLayeredPanePass(bg);
     493                        }
    481494                        PopupManager.getInstance().paintLayeredPane(bg, clip);
    482                         for (FinalFrameRenderPass pass : _finalPasses) {
    483                                 pass.paint(bg);
     495                        for (FrameRenderPass pass : _frameRenderPasses) {
     496                                pass.paintFinalPass(bg);
    484497                        }
    485498                }
     
    968981         *             If pass is null.
    969982         */
    970         public static void addFinalFrameRenderPass(FinalFrameRenderPass pass) {
     983        public static void addFrameRenderPass(FrameRenderPass pass) {
    971984                if (pass == null)
    972985                        throw new NullPointerException("pass");
    973986
    974                 if (!_finalPasses.contains(pass))
    975                         _finalPasses.add(pass);
     987                if (!_frameRenderPasses.contains(pass))
     988                        _frameRenderPasses.add(pass);
    976989        }
    977990
     
    986999         *
    9871000         */
    988         public static void removeFinalFrameRenderPass(FinalFrameRenderPass pass) {
    989                 _finalPasses.remove(pass);
     1001        public static void removeFrameRenderPass(FrameRenderPass pass) {
     1002                _frameRenderPasses.remove(pass);
    9901003        }
    9911004
     
    10011014         * frame painter then it is not garaunteed to be rendered very last.
    10021015         *
    1003          * @see FrameGraphics#addFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FinalFrameRenderPass)
    1004          * @see FrameGraphics#removeFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FinalFrameRenderPass)
     1016         * @see FrameGraphics#addFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FrameRenderPass)
     1017         * @see FrameGraphics#removeFinalFrameRenderPass(org.expeditee.gui.FrameGraphics.FrameRenderPass)
    10051018         *
    10061019         * @author Brook Novak
    10071020         */
    1008         public interface FinalFrameRenderPass {
    1009                 void paint(Graphics g);
     1021        public interface FrameRenderPass {
     1022               
     1023                /**
     1024                 *
     1025                 * @param currentClip
     1026                 *
     1027                 * @return
     1028                 *              The clip that the pass should use instead.
     1029                 *              i.e. if there are any effects that cannot invladate prior to paint call.
     1030                 */
     1031                Area paintStarted(Area currentClip);
     1032               
     1033                void paintFinalPass(Graphics g);
     1034               
     1035                void paintPreLayeredPanePass(Graphics g);
    10101036        }
    10111037
  • trunk/src/org/expeditee/items/widgets/InteractiveWidget.java

    r294 r296  
    12611261                FrameGraphics.invalidateArea(dirty);
    12621262                invalidateLink();
    1263                 FrameGraphics.refresh(true);
     1263                //FrameGraphics.refresh(true);
    12641264        }
    12651265
     
    14541454        }
    14551455
     1456        protected void addDataIfCaseInsensitiveNotExists(String tag) {
     1457                if (tag == null) throw new NullPointerException("tag");
     1458               
     1459                List<String> data = getCurrentRepresentation().getData();
     1460               
     1461                if (data == null) {
     1462                        data = new LinkedList<String>();
     1463                }
     1464               
     1465                for (String s : data) {
     1466                        if (s != null && s.equalsIgnoreCase(tag)) {
     1467                                return;
     1468                        }
     1469                }
     1470               
     1471                data.add(tag);
     1472                getCurrentRepresentation().setData(data);
     1473        }
     1474
     1475       
    14561476        /**
    14571477         * Updates the data with a given tag. All data is removed that is prefixed
     
    15061526                }
    15071527        }
     1528       
     1529        public boolean containsData(String str) {
     1530                assert(str != null);
     1531                if (getCurrentRepresentation().getData() != null)
     1532                        return getCurrentRepresentation().getData().contains(str);
     1533                return false;
     1534        }
     1535       
     1536        public boolean containsDataTrimmedIgnoreCase(String str) {
     1537                assert(str != null);
     1538                if (getCurrentRepresentation().getData() != null) {
     1539                        for (String data : getCurrentRepresentation().getData()) {
     1540                                if (data != null && data.trim().equalsIgnoreCase(str)) {
     1541                                        return true;
     1542                                }
     1543                        }
     1544                }
     1545                       
     1546                return false;
     1547        }
    15081548
    15091549        /**
     
    15551595
    15561596                if (FrameIO.isPositiveInteger(link)) { // relative - convert to
    1557                         // absolute
     1597                                                                                                // absolute
    15581598
    15591599                        // Get the frameset of this item
     
    15771617                return null;
    15781618        }
    1579 
    1580         /**
    1581          * Sets the border color for the widget. That is, for the source (so it is
    1582          * remembered) and also for all the corners/edges.
     1619       
     1620        /**
     1621         * Sets the border color for the widget.
     1622         * That is, for the source (so it is remembered) and also for all the
     1623         * corners/edges.
    15831624         *
    15841625         * @param c
    1585          *            The color to set.
     1626         *              The color to set.
    15861627         */
    15871628        public void setWidgetEdgeColor(Color c) {
    1588                 for (Item i : _expediteeItems)
    1589                         i.setColor(c);
     1629                for (Item i : _expediteeItems) i.setColor(c);
    15901630                // Above indirectly invokes setSourceBorderColor accordingly
    15911631        }
    1592 
     1632       
    15931633        /**
    15941634         * Sets the thickness of the widget edge.
     
    15971637         *
    15981638         * @param thickness
    1599          *            The new thickness to set.
     1639         *              The new thickness to set.
    16001640         */
    16011641        public void setWidgetEdgeThickness(float thickness) {
    1602                 for (Item i : _expediteeItems)
    1603                         i.setThickness(thickness);
    1604                 // Above indirectly invokes setSourceThickness accordingly
    1605         }
    1606 
    1607         // TODO: Maybe rename setSource* .. to update* ... These should actually be
    1608         // friendly!
     1642                for (Item i : _expediteeItems) i.setThickness(thickness);
     1643//               Above indirectly invokes setSourceThickness accordingly
     1644        }
     1645
     1646        // TODO: Maybe rename setSource* ..  to update* ... These should actually be friendly!
    16091647        public void setSourceColor(Color c) {
    16101648                _textRepresentation.setColor(c);
     
    16281666
    16291667        protected Point getOrigin() {
    1630                 return _d1.getPosition(); // BROOK: This flips around ... the origin
    1631                 // can be any point
     1668                return _d1.getPosition(); // BROOK: This flips around ... the origin can be any point
    16321669        }
    16331670
Note: See TracChangeset for help on using the changeset viewer.