1 /*******************************************************************************
\r
2 * Copyright (c) 2016 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * THTH ry - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.browser.content;
\r
14 import gnu.trove.map.hash.THashMap;
\r
16 import java.io.PrintWriter;
\r
17 import java.util.ArrayList;
\r
18 import java.util.Collections;
\r
19 import java.util.Comparator;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.debug.browser.sections.ResourceBrowserSection;
\r
26 public class ResourceBrowserContent {
\r
27 public final Resource resource;
\r
28 private final THashMap<Class<?>, ResourceBrowserSection> sectionMap =
\r
29 new THashMap<Class<?>, ResourceBrowserSection>();
\r
31 private ResourceBrowserContent(Resource resource) {
\r
32 this.resource = resource;
\r
35 @SuppressWarnings("unchecked")
\r
36 public <T extends ResourceBrowserSection> T getSection(Class<T> clazz) {
\r
37 return (T)sectionMap.get(clazz);
\r
40 public <T extends ResourceBrowserSection> void putSection(Class<T> clazz, T section) {
\r
41 sectionMap.put(clazz, section);
\r
44 @SuppressWarnings("unchecked")
\r
45 public <T extends ResourceBrowserSection> T removeSection(Class<T> clazz) {
\r
46 return (T)sectionMap.remove(clazz);
\r
49 private static final Comparator<ResourceBrowserSection> SECTION_COMPARATOR = new Comparator<ResourceBrowserSection>() {
\r
51 public int compare(ResourceBrowserSection o1, ResourceBrowserSection o2) {
\r
52 return Double.compare(o1.getPriority(), o2.getPriority());
\r
56 public void toHtml(ReadGraph graph, PrintWriter stream) throws DatabaseException {
\r
57 ArrayList<ResourceBrowserSection> sections = new ArrayList<ResourceBrowserSection>(sectionMap.values());
\r
58 Collections.sort(sections, SECTION_COMPARATOR);
\r
59 for(ResourceBrowserSection section : sections)
\r
60 section.toHtml(graph, stream);
\r
63 public static ResourceBrowserContent createContentFor(ReadGraph graph, Resource resource) throws DatabaseException {
\r
64 ResourceBrowserContent content = new ResourceBrowserContent(resource);
\r
65 for(ResourceBrowserRewriter rewriter : ResourceBrowserRewriterRepository.getRewriters())
\r
66 rewriter.rewrite(graph, content);
\r