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);
}
Aggregations