]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/content/ResourceBrowserContent.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / content / ResourceBrowserContent.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.content;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.io.PrintWriter;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20
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;
25
26 public class ResourceBrowserContent {
27     public final Resource resource;
28     private final THashMap<Class<?>, ResourceBrowserSection> sectionMap =
29             new THashMap<Class<?>, ResourceBrowserSection>();
30     
31     private ResourceBrowserContent(Resource resource) {
32         this.resource = resource;
33     }
34     
35     @SuppressWarnings("unchecked")
36     public <T extends ResourceBrowserSection> T getSection(Class<T> clazz) {
37         return (T)sectionMap.get(clazz);
38     }
39     
40     public <T extends ResourceBrowserSection> void putSection(Class<T> clazz, T section) {
41         sectionMap.put(clazz, section);
42     }
43     
44     @SuppressWarnings("unchecked")
45     public <T extends ResourceBrowserSection> T removeSection(Class<T> clazz) {
46         return (T)sectionMap.remove(clazz);
47     }
48     
49     private static final Comparator<ResourceBrowserSection> SECTION_COMPARATOR = new Comparator<ResourceBrowserSection>() {
50         @Override
51         public int compare(ResourceBrowserSection o1, ResourceBrowserSection o2) {
52             return Double.compare(o1.getPriority(), o2.getPriority());
53         }
54     };
55     
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);
61     }
62     
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);
67         return content;
68     }
69 }