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