• Better ChildFactory


    http://blogs.oracle.com/geertjan/entry/better_childfactory

    ————————————————————————————————————————————————————————————————————————

    Thanks to Jesse, here's a corrected & improved ChildFactory from a few days ago, using ChildFactory.Detachable so that you have addNotify/removeNotify to add/remove the LookupListener and thus avoid a memory leak, together with a createKeys/resultChanged that is simplified too.
    private class FirstLevelChildFactory extends ChildFactory.Detachable<ResultType> implements LookupListener {

        Result<ResultSet> resultRetrievedFromYahoo;

        @Override
        protected void addNotify() {
            resultRetrievedFromYahoo = Utilities.actionsGlobalContext().lookupResult(ResultSet.class);
            resultRetrievedFromYahoo.addLookupListener(this);
        }

        @Override
        protected void removeNotify() {
            resultRetrievedFromYahoo.removeLookupListener(this);
            resultRetrievedFromYahoo = null;
        }

        @Override
        protected boolean createKeys(List<ResultType> list) {
            for (ResultSet rs : resultRetrievedFromYahoo.allInstances()) {
                list.addAll(rs.getResult());
            }
            return true;
        }

        @Override
        protected Node createNodeForKey(ResultType key) {
            String[] split = key.getTitle().split(" ");
            String theNextWordToBeSearched = split[split.length - 1];
            AbstractNode firstLevelNode =
                    new AbstractNode(
                    Children.create(new GenericChildFactory(theNextWordToBeSearched), true),
                    Lookups.singleton(key));
            firstLevelNode.setShortDescription(key.getSummary());
            firstLevelNode.setDisplayName(key.getTitle());
            return firstLevelNode;
        }

        @Override
        public void resultChanged(LookupEvent le) {
            refresh(true);
        }

    }
    This is a very simple & elegant solution.

  • 相关阅读:
    Nacos 1.3.0版本部署连接mysql 8+
    Java Certificate证书问题
    UIKit之浅析UIButton
    Xcode Coule not launch "aaa" press launch failed:timed out waiting for app launch
    Cocos2d-x 安装教程for mac(Xcode)
    关于继承UITableViewController若干问题
    Table的分割线偏移量设置 及其 UIEdgeInset详解
    retain、strong、weak、assign区别
    iOS 使用xib创建cell的两种初始化方式
    No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2189950.html
Copyright © 2020-2023  润新知