Search in sources :

Example 31 with Tree

use of ini.trakem2.display.Tree in project TrakEM2 by trakem2.

the class DBLoader method getTemplateRoot.

/**
 * Fetch the root of the TemplateThing tree from the database-stored hierarchy of TemplateThing objects defined in the original XML file .
 */
public TemplateThing getTemplateRoot(Project project) {
    // connect if disconnected
    if (!connectToDatabase()) {
        return null;
    }
    TemplateThing root = null;
    synchronized (db_lock) {
        // New way: TemplateThing instances are saved in the ab_things table
        try {
            // fetch TemplateThings, which have no stored object.
            // signature of the root TemplateThing is parent_id=-1 and object_id=-1
            ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND parent_id=-1 AND object_id=-1").executeQuery();
            if (r.next()) {
                long id = r.getLong("id");
                String type = r.getString("type");
                root = new TemplateThing(type, project, id);
                root.setup(getChildrenTemplateThings(project, id));
            }
            r.close();
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
    }
    return root;
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Example 32 with Tree

use of ini.trakem2.display.Tree in project TrakEM2 by trakem2.

the class Search method executeSearch.

private void executeSearch() {
    final Project project = Project.getProjects().get(projects.getSelectedIndex());
    if (null == project) {
        // Should not happen
        return;
    }
    Bureaucrat.createAndStart(new Worker.Task("Searching") {

        public void exec() {
            String pattern = search_field.getText();
            if (null == pattern || 0 == pattern.length()) {
                return;
            }
            // fix pattern
            final String typed_pattern = pattern;
            // I hate java
            final StringBuilder sb = new StringBuilder();
            if (!pattern.startsWith("^"))
                sb.append("^.*");
            for (int i = 0; i < pattern.length(); i++) {
                sb.append(pattern.charAt(i));
            }
            if (!pattern.endsWith("$"))
                sb.append(".*$");
            pattern = sb.toString();
            final Pattern pat = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            // Utils.log2("pattern after: " + pattern);
            final ArrayList<DBObject> al = new ArrayList<DBObject>();
            // Utils.log("types[pulldown] = " +
            // types[pulldown.getSelectedIndex()]);
            find(project.getRootLayerSet(), al, types[pulldown.getSelectedIndex()]);
            // Utils.log2("found labels: " + al.size());
            if (0 == al.size())
                return;
            final Vector<DBObject> v_obs = new Vector<DBObject>();
            final Vector<String> v_txt = new Vector<String>();
            final Vector<Coordinate<?>> v_co = new Vector<Coordinate<?>>();
            Coordinate<?> co = null;
            for (final DBObject dbo : al) {
                if (Thread.currentThread().isInterrupted()) {
                    return;
                }
                boolean matched = false;
                // Search in its title
                Displayable d = null;
                if (dbo instanceof Displayable) {
                    d = (Displayable) dbo;
                }
                String txt;
                String meaningful_title = null;
                if (null == d || Patch.class == d.getClass())
                    txt = dbo.getTitle();
                else {
                    txt = meaningful_title = dbo.getProject().getMeaningfulTitle(d);
                }
                if (null == txt || 0 == txt.trim().length())
                    continue;
                matched = pat.matcher(txt).matches();
                if (!matched && null != d) {
                    // Search also in its annotation
                    txt = d.getAnnotation();
                    if (null != txt)
                        matched = pat.matcher(txt).matches();
                }
                if (!matched) {
                    // Search also in its toString()
                    txt = dbo.toString();
                    matched = pat.matcher(txt).matches();
                }
                if (!matched) {
                    // Search also in its id
                    txt = Long.toString(dbo.getId());
                    matched = pat.matcher(txt).matches();
                    if (matched)
                        txt = "id: #" + txt;
                }
                if (!matched && null != d) {
                    // Search also in its properties
                    Map<String, String> props = d.getProperties();
                    if (null != props) {
                        for (final Map.Entry<String, String> e : props.entrySet()) {
                            if (pat.matcher(e.getKey()).matches() || pat.matcher(e.getValue()).matches()) {
                                matched = true;
                                txt = e.getKey() + " => " + e.getValue() + " [property]";
                                break;
                            }
                        }
                    }
                    if (!matched) {
                        Map<Displayable, Map<String, String>> linked_props = ((Displayable) dbo).getLinkedProperties();
                        if (null != linked_props) {
                            for (final Map.Entry<Displayable, Map<String, String>> e : linked_props.entrySet()) {
                                for (final Map.Entry<String, String> ee : e.getValue().entrySet()) {
                                    if (pat.matcher(ee.getKey()).matches() || pat.matcher(ee.getValue()).matches()) {
                                        matched = true;
                                        txt = ee.getKey() + " => " + e.getValue() + " [linked property]";
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (!matched && dbo instanceof Tree<?>) {
                    // search Node tags
                    Node<?> root = ((Tree<?>) dbo).getRoot();
                    if (null == root)
                        continue;
                    for (final Node<?> nd : root.getSubtreeNodes()) {
                        Set<Tag> tags = nd.getTags();
                        if (null == tags)
                            continue;
                        for (final Tag tag : tags) {
                            if (pat.matcher(tag.toString()).matches()) {
                                v_obs.add(dbo);
                                v_txt.add(new StringBuilder(tag.toString()).append(" (").append(null == meaningful_title ? dbo.toString() : meaningful_title).append(')').toString());
                                v_co.add(createCoordinate((Tree<?>) dbo, nd));
                            }
                        }
                    }
                    // all added if any
                    continue;
                }
                if (!matched)
                    continue;
                // txt = txt.length() > 30 ? txt.substring(0, 27) + "..." :
                // txt;
                v_obs.add(dbo);
                v_txt.add(txt);
                v_co.add(co);
            }
            if (0 == v_obs.size()) {
                Utils.showMessage("Nothing found.");
                return;
            }
            final JPanel result = new JPanel();
            GridBagLayout gb = new GridBagLayout();
            result.setLayout(gb);
            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.insets = new Insets(5, 10, 5, 10);
            String xml = "";
            if (project.getLoader() instanceof FSLoader) {
                String path = ((FSLoader) project.getLoader()).getProjectXMLPath();
                if (null != path) {
                    xml = " [" + new File(path).getName() + "]";
                }
            }
            JLabel projectTitle = new JLabel(project.getTitle() + xml);
            gb.setConstraints(projectTitle, c);
            result.add(projectTitle);
            c.insets = new Insets(0, 0, 0, 0);
            JPanel padding = new JPanel();
            c.weightx = 1;
            gb.setConstraints(padding, c);
            result.add(padding);
            c.gridy = 1;
            c.gridwidth = 2;
            c.fill = GridBagConstraints.BOTH;
            c.weighty = 1;
            JScrollPane jsp = makeTable(new DisplayableTableModel(v_obs, v_txt, v_co), project);
            gb.setConstraints(jsp, c);
            result.add(jsp);
            search_tabs.addTab(typed_pattern, result);
            search_tabs.setSelectedComponent(result);
            synchronized (tabMap) {
                List<JPanel> cs = tabMap.get(project);
                if (null == cs) {
                    cs = new ArrayList<JPanel>();
                    tabMap.put(project, cs);
                }
                cs.add(result);
            }
        }
    }, project);
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Set(java.util.Set) HashSet(java.util.HashSet) LayerSet(ini.trakem2.display.LayerSet) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) Node(ini.trakem2.display.Node) ArrayList(java.util.ArrayList) DBObject(ini.trakem2.persistence.DBObject) AreaTree(ini.trakem2.display.AreaTree) Tree(ini.trakem2.display.Tree) AreaList(ini.trakem2.display.AreaList) List(java.util.List) ArrayList(java.util.ArrayList) Vector(java.util.Vector) JScrollPane(javax.swing.JScrollPane) Pattern(java.util.regex.Pattern) Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) JLabel(javax.swing.JLabel) Project(ini.trakem2.Project) FSLoader(ini.trakem2.persistence.FSLoader) Coordinate(ini.trakem2.display.Coordinate) Tag(ini.trakem2.display.Tag) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File)

Aggregations

ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 HashSet (java.util.HashSet)10 Displayable (ini.trakem2.display.Displayable)9 ZDisplayable (ini.trakem2.display.ZDisplayable)9 ProjectThing (ini.trakem2.tree.ProjectThing)9 Project (ini.trakem2.Project)8 DBObject (ini.trakem2.persistence.DBObject)8 LayerSet (ini.trakem2.display.LayerSet)7 TemplateThing (ini.trakem2.tree.TemplateThing)7 TreePath (javax.swing.tree.TreePath)7 GenericDialog (ij.gui.GenericDialog)6 Layer (ini.trakem2.display.Layer)6 LayerThing (ini.trakem2.tree.LayerThing)6 List (java.util.List)6 Map (java.util.Map)6 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)6 ProjectTree (ini.trakem2.tree.ProjectTree)5 Worker (ini.trakem2.utils.Worker)5 ImagePlus (ij.ImagePlus)4