PageRenderTime 71ms CodeModel.GetById 64ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/TldXmlParsedData.java

#
Java | 116 lines | 83 code | 12 blank | 21 comment | 10 complexity | 403d3e25be0a5f8b0d0a860a0ec78aaf MD5 | raw file
  1package xml;
  2
  3import java.util.Comparator;
  4import java.util.Enumeration;
  5import javax.swing.tree.DefaultMutableTreeNode;
  6import javax.swing.tree.TreeNode;
  7
  8import sidekick.Asset;
  9import sidekick.IAsset;
 10import sidekick.ExpansionModel;
 11import xml.parser.TldXmlTag;
 12
 13import org.gjt.sp.jedit.View;
 14
 15/**
 16 * Special version of XmlParsedData for tld files.  Tld files have very few
 17 * tags with attributes, and the interesting things to see in the SideKick
 18 * tree are generally hidden from view, like the 'name' child tag of a 'tag'
 19 * or 'function' node,  This class renames those nodes with a child node
 20 * named 'name' so that the name is more readily visible in the tree.
 21 */
 22public class TldXmlParsedData extends XmlParsedData {
 23
 24
 25    public TldXmlParsedData(String filename, boolean html) {
 26        super(filename, html);
 27    }
 28
 29    /**
 30     * Rename the assets contained in the tree nodes to use the value
 31     * contained in the body of any child tags named 'name'.
 32     */ 
 33    @Override
 34    public void done(View view) {
 35        Enumeration children = root.children();
 36        while (children.hasMoreElements()) {
 37            processChild((TreeNode) children.nextElement());
 38        }
 39        tree.reload();
 40        sort(view);
 41    }
 42
 43    private void processChild(TreeNode node) {
 44        IAsset asset = (IAsset) ((DefaultMutableTreeNode) node).getUserObject();
 45        if (asset.getName().indexOf(":") == - 1) {
 46            renameAsset(node, asset);
 47            Enumeration children = node.children();
 48            while (children.hasMoreElements()) {
 49                processChild((TreeNode) children.nextElement());
 50            }
 51        }
 52    }
 53
 54    /**
 55     * @param node It had better be a DefaultMutableTreeNode and it had better
 56     * contain an Asset as the user object.  At the time of this writing, this
 57     * was true.
 58     */
 59    private void renameAsset(final TreeNode node, IAsset asset) {
 60        if (asset instanceof Asset && node instanceof DefaultMutableTreeNode) {
 61            Enumeration children = node.children();
 62            while (children.hasMoreElements()) {
 63                DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
 64                TldXmlTag uo = (TldXmlTag) child.getUserObject();
 65                if ("name".equals(uo.getName())) {
 66                    ((Asset) asset).setName(uo.getCharacters());
 67                    ((DefaultMutableTreeNode) node).setUserObject(asset);
 68                }
 69            }
 70        }
 71    }
 72
 73    // Overridden so name and type sorting work correctly for these files.
 74    // "original name" is the original name of the xml tag, like "tag-file" or "function".
 75    // "short string" is the name provided by the child element named "name". So sort
 76    // by name uses "short string" and sort by type uses "original name".
 77    @Override
 78    protected Comparator<DefaultMutableTreeNode> getSorter() {
 79        return new Comparator<DefaultMutableTreeNode>() {
 80            public int compare(DefaultMutableTreeNode tna, DefaultMutableTreeNode tnb) {
 81                int sortBy = getSortBy();
 82                switch (sortBy) {                // NOPMD, no breaks are necessary here
 83                    case SORT_BY_LINE:
 84                        Integer my_line = new Integer(((TldXmlTag)tna.getUserObject()).getStart().getOffset());
 85                        Integer other_line = new Integer(((TldXmlTag)tnb.getUserObject()).getStart().getOffset());
 86                        return my_line.compareTo(other_line) * (sortDown ? 1 : -1);
 87                    case SORT_BY_TYPE:
 88                        String my_on = ((TldXmlTag)tna.getUserObject()).getOriginalName().toLowerCase();
 89                        String other_on = ((TldXmlTag)tnb.getUserObject()).getOriginalName().toLowerCase();
 90                        return my_on.compareTo(other_on) * (sortDown ? 1 : -1);
 91                    case SORT_BY_NAME:
 92                    default:
 93                        return compareNames(tna, tnb);
 94                }
 95            }
 96    
 97            private int compareNames(DefaultMutableTreeNode tna, DefaultMutableTreeNode tnb) {
 98                // sort by name
 99                String my_name = ((TldXmlTag)tna.getUserObject()).getShortString().toLowerCase();
100                String other_name = ((TldXmlTag)tnb.getUserObject()).getShortString().toLowerCase();
101                return my_name.compareTo(other_name) * (sortDown ? 1 : -1);
102            }
103        } ;
104    }
105    
106    @Override
107    protected ExpansionModel createExpansionModel() {
108        ExpansionModel em = new ExpansionModel();
109        em.add();   // root (filename node)
110        em.add();   // main node
111        for (int i = 0; i < root.getChildAt(0).getChildCount(); i++) {
112            em.inc();   // tag, function, etc, nodes   
113        }
114        return em;
115    }
116}