Search in sources :

Example 36 with Project

use of org.openstack4j.model.identity.v3.Project 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

Project (ini.trakem2.Project)26 ArrayList (java.util.ArrayList)13 Project (org.openstack4j.model.identity.v3.Project)9 File (java.io.File)7 HashMap (java.util.HashMap)7 ImagePlus (ij.ImagePlus)6 Worker (ini.trakem2.utils.Worker)6 Map (java.util.Map)6 LayerSet (ini.trakem2.display.LayerSet)5 ProjectThing (ini.trakem2.tree.ProjectThing)5 HashSet (java.util.HashSet)5 DBObject (ini.trakem2.persistence.DBObject)4 VectorString3D (ini.trakem2.vector.VectorString3D)4 GenericDialog (ij.gui.GenericDialog)3 Patch (ini.trakem2.display.Patch)3 Rectangle (java.awt.Rectangle)3 ImageJ (ij.ImageJ)2 Roi (ij.gui.Roi)2 YesNoCancelDialog (ij.gui.YesNoCancelDialog)2 DirectoryChooser (ij.io.DirectoryChooser)2