Wrote a utility previewing every component
[Mograsim.git] / plugins / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / OpenPreviewOfEveryComponent.java
1 package net.mograsim.logic.model.examples;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.List;
8
9 import com.google.gson.JsonObject;
10
11 import net.mograsim.logic.model.SimpleLogicUIStandalone;
12 import net.mograsim.logic.model.am2900.Am2900Loader;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.components.atomic.ModelTextComponent;
15 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
16 import net.mograsim.logic.model.util.JsonHandler;
17
18 public class OpenPreviewOfEveryComponent
19 {
20         public static void main(String[] args) throws IOException
21         {
22                 Am2900Loader.setup();
23
24                 List<String> allComponents = new ArrayList<>();
25
26                 // TODO this is ugly. Is there a better way?
27                 try (InputStream resourceAsStream = Am2900Loader.class.getResourceAsStream("standardComponentIDMapping.json"))
28                 {
29                         allComponents.addAll(JsonHandler.readJson(resourceAsStream, JsonObject.class).keySet());
30                 }
31                 try (InputStream resourceAsStream = IndirectModelComponentCreator.class.getResourceAsStream("standardComponentIDMapping.json"))
32                 {
33                         allComponents.addAll(JsonHandler.readJson(resourceAsStream, JsonObject.class).keySet());
34                 }
35
36                 Collections.sort(allComponents, String::compareToIgnoreCase);
37
38                 SimpleLogicUIStandalone.executeVisualisation(model ->
39                 {
40                         int y = 0;
41                         for (String componentID : allComponents)
42                                 try
43                                 {
44                                         ModelComponent createComponent = IndirectModelComponentCreator.createComponent(model, componentID);
45                                         createComponent.moveTo(0, y);
46                                         new ModelTextComponent(model, componentID).moveTo(createComponent.getWidth() + 10, y);
47                                         y += createComponent.getHeight() + 10;
48                                 }
49                                 catch (RuntimeException e)
50                                 {
51                                         new ModelTextComponent(model, "Error creating " + componentID + ": " + e).moveTo(0, y);
52                                         y += 20;
53                                 }
54                 });
55         }
56 }