Search in sources :

Example 1 with CatalogTree

use of jmri.CatalogTree in project JMRI by JMRI.

the class CatalogPanel method createNewBranch.

/**
     * Create a new model and add it to the main root.
     * <p>
     * Can be called from off the GUI thread.
     *
     * @param systemName the system name for the catalog
     * @param userName   the user name for the catalog
     * @param path       the path on the new branch
     */
public void createNewBranch(String systemName, String userName, String path) {
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    CatalogTree tree = manager.getBySystemName(systemName);
    if (tree != null) {
        jmri.util.ThreadingUtil.runOnGUI(() -> {
            addTree(tree);
        });
    } else {
        final CatalogTree t = manager.newCatalogTree(systemName, userName);
        jmri.util.ThreadingUtil.runOnGUI(() -> {
            t.insertNodes(path);
            addTree(t);
        });
    }
}
Also used : CatalogTree(jmri.CatalogTree) CatalogTreeManager(jmri.CatalogTreeManager)

Example 2 with CatalogTree

use of jmri.CatalogTree in project JMRI by JMRI.

the class DefaultCatalogTreeManagerXml method writeCatalogTrees.

/*
     *  Writes out tree values to a file in the user's preferences directory
     */
public void writeCatalogTrees() throws java.io.IOException {
    if (log.isDebugEnabled()) {
        log.debug("entered writeCatalogTreeValues");
    }
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    List<String> trees = manager.getSystemNameList();
    boolean found = false;
    Iterator<String> iter = manager.getSystemNameList().iterator();
    while (iter.hasNext()) {
        String sname = iter.next();
        CatalogTree tree = manager.getBySystemName(sname);
        if (log.isDebugEnabled()) {
            log.debug("Tree: sysName= " + sname + ", userName= " + tree.getUserName());
            CatalogTreeNode root = tree.getRoot();
            log.debug("enumerateTree called for root= " + root.toString() + ", has " + root.getChildCount() + " children");
            // root.depthFirstEnumeration isn't fully typed in JDOM2
            @SuppressWarnings("unchecked") Enumeration<CatalogTreeNode> e = root.depthFirstEnumeration();
            while (e.hasMoreElements()) {
                CatalogTreeNode n = e.nextElement();
                log.debug("nodeName= " + n.getUserObject() + " has " + n.getLeaves().size() + " leaves and " + n.getChildCount() + " subnodes.");
            }
        }
        if (sname != null && sname.charAt(1) == CatalogTree.XML) {
            found = true;
            break;
        }
    }
    if (found) {
        // there are trees defined, create root element
        Element root = new Element("catalogTrees");
        Document doc = newDocument(root, dtdLocation + "catalogTree.dtd");
        // add XSLT processing instruction
        // <?xml-stylesheet type="text/xsl" href="XSLT/tree-values.xsl"?>
        java.util.Map<String, String> m = new java.util.HashMap<>();
        m.put("type", "text/xsl");
        m.put("href", xsltLocation + "panelfile.xsl");
        org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m);
        doc.addContent(0, p);
        store(root, trees);
        try {
            if (!checkFile(DEFAULT_FILE_NAME)) {
                // file does not exist, create it
                File file = new File(DEFAULT_FILE_NAME);
                if (!file.createNewFile()) {
                    log.error("createNewFile failed");
                }
            }
            // write content to file
            writeXML(findFile(DEFAULT_FILE_NAME), doc);
            // memory consistent with file
            jmri.jmrit.catalog.ImageIndexEditor.indexChanged(false);
        } catch (java.io.IOException ioe) {
            log.error("IO Exception " + ioe);
            throw (ioe);
        }
    }
}
Also used : Element(org.jdom2.Element) Document(org.jdom2.Document) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode) CatalogTree(jmri.CatalogTree) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 3 with CatalogTree

use of jmri.CatalogTree in project JMRI by JMRI.

the class IconAdder method initDefaultIcons.

public void initDefaultIcons() {
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    CatalogTree tree = manager.getBySystemName("NXDI");
    if (tree != null) {
        CatalogTreeNode node = tree.getRoot();
        Enumeration<CatalogTreeNode> e = node.children();
        while (e.hasMoreElements()) {
            CatalogTreeNode nChild = e.nextElement();
            if (_type.equals(nChild.toString())) {
                _defaultIcons = nChild;
                _userDefaults = true;
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("initDefaultIcons: type= " + _type + ", defaultIcons= " + _defaultIcons);
    }
}
Also used : CatalogTree(jmri.CatalogTree) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode)

Example 4 with CatalogTree

use of jmri.CatalogTree in project JMRI by JMRI.

the class IconAdder method updateCatalogTree.

/**
     * If icons are changed, update global tree
     */
private void updateCatalogTree() {
    CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
    // unfiltered, xml-stored, default icon tree
    CatalogTree tree = manager.getBySystemName("NXDI");
    if (tree == null) {
        // build a new Default Icons tree
        tree = manager.newCatalogTree("NXDI", "Default Icons");
    }
    CatalogTreeNode root = tree.getRoot();
    Enumeration<CatalogTreeNode> e = root.children();
    String name = _defaultIcons.toString();
    while (e.hasMoreElements()) {
        CatalogTreeNode nChild = e.nextElement();
        if (name.equals(nChild.toString())) {
            if (log.isDebugEnabled()) {
                log.debug("Remove node " + nChild);
            }
            root.remove(nChild);
            break;
        }
    }
    root.add(_defaultIcons);
    ImageIndexEditor.indexChanged(true);
}
Also used : CatalogTree(jmri.CatalogTree) CatalogTreeManager(jmri.CatalogTreeManager) CatalogTreeNode(jmri.jmrit.catalog.CatalogTreeNode)

Example 5 with CatalogTree

use of jmri.CatalogTree in project JMRI by JMRI.

the class DefaultCatalogTreeManager method newCatalogTree.

@Override
public CatalogTree newCatalogTree(String sysName, String userName) {
    if (log.isDebugEnabled()) {
        log.debug("new CatalogTree: systemName= " + sysName + ", userName= " + userName);
    }
    if (sysName == null) {
        log.error("SystemName cannot be null. UserName= " + userName);
        return null;
    }
    String systemName = sysName.toUpperCase();
    // return existing if there is one
    CatalogTree s;
    if ((userName != null) && ((s = getByUserName(userName)) != null)) {
        if (getBySystemName(systemName) != s) {
            log.error("inconsistent user (" + userName + ") and system name (" + systemName + ") results; userName related to (" + s.getSystemName() + ")");
        }
        return s;
    }
    if ((s = getBySystemName(systemName)) != null) {
        if ((s.getUserName() == null) && (userName != null)) {
            s.setUserName(userName);
        } else if (userName != null) {
            log.warn("Found memory via system name (" + systemName + ") with non-null user name (" + userName + ")");
        }
        return s;
    }
    // doesn't exist, make a new one
    s = createNewCatalogTree(systemName, userName);
    // save in the maps
    register(s);
    return s;
}
Also used : CatalogTree(jmri.CatalogTree)

Aggregations

CatalogTree (jmri.CatalogTree)10 CatalogTreeManager (jmri.CatalogTreeManager)7 CatalogTreeNode (jmri.jmrit.catalog.CatalogTreeNode)5 HashMap (java.util.HashMap)2 NamedIcon (jmri.jmrit.catalog.NamedIcon)2 Element (org.jdom2.Element)2 Point (java.awt.Point)1 File (java.io.File)1 Entry (java.util.Map.Entry)1 TreeNode (javax.swing.tree.TreeNode)1 XmlFile (jmri.jmrit.XmlFile)1 Document (org.jdom2.Document)1