Пример #1
0
  @Override
  public XMLNode write(Identified object, WriterContext context) {
    if (object == null) {
      return null;
    }

    XMLNode node = new XMLNode(DOMTags.ELEMENT_TAG);

    if (object.getId() != null && context.containsId(object.getId())) {
      node.setText(object.getId());
    } else {
      // Set id if necessary
      if (object.getId() == null) {
        object.setId(context.generateNewId());
      }

      object = (Identified) context.process(object, node);
      // If it's a reference
      if (object.getClass() == BasicElement.class) {
        node.setText(object.getId());
      } else {
        node.setAttribute(DOMTags.ID_AT, object.getId());
        ReflectionClass<?> clazz = ReflectionClassLoader.getReflectionClass(object.getClass());
        node.setAttribute(DOMTags.CLASS_AT, context.translateClass(clazz.getType()));
        while (clazz != null) {
          for (ReflectionField f : clazz.getFields()) {
            // Only store fields annotated with param
            if (f.getAnnotation(Param.class) != null) {
              Object value = f.getFieldValue(object);
              if (value != null) {
                modelVisitor.writeElement(
                    value,
                    object,
                    new ObjectWriterListener(context.translateField(f.getName()), node));
              }
            }
          }
          clazz = clazz.getSuperclass();
        }
      }
    }
    // Detect duplicated ids
    Object o = pointers.get(object.getId());
    if (o != null && o != object) {
      if (o.getClass() != BasicElement.class && object.getClass() != BasicElement.class) {
        logger.error("Duplicated id {}", object.getId());
      }
    } else {
      // We don't add references to the pointers list
      if (object.getClass() != BasicElement.class) {
        pointers.put(object.getId(), object);
      }
    }
    return node;
  }
Пример #2
0
  public static void main(String[] args) {
    Log4jConfig.configForConsole(Log4jConfig.Slf4jLevel.Info);

    // initialize launcher & reflection
    Injector injector =
        Guice.createInjector(new DesktopModule(), new EditorGuiceModule(), new JavaToolsModule());
    ReflectionClassLoader.init(injector.getInstance(ReflectionClassLoader.class));
    Controller c = injector.getInstance(Controller.class);
    EditorModelImpl mi = (EditorModelImpl) c.getModel();
    mi.getLoader().setSaveDir(new File("src/main/resources/"));

    final AssetViewer rootAssetViewer = c.createAssetViewer();

    final ArrayList<EditorNode> ians = new ArrayList<EditorNode>();
    Identified root = new BasicElement();
    root.setId("fakeRoot");
    mi.addNode(null, null, root, false);
    for (int i = 0; i < 100; i++) {
      ImageAssetNode ian = new ImageAssetNode(i);
      Image image = new Image("@drawable/EditorIcon128x128.png");
      image.setId("id" + i);
      mi.addNode(mi.getNodeFor(root), "mock", image, false);
      ian.addChild(mi.getNodeFor(image));
      ian.setBase(new File("src/main/resources/", AssetHandlerImpl.PROJECT_INTERNAL_PATH));
      ians.add(ian);
      mi.registerEditorNodeWithGraph(ian);
    }
    final ThumbnailPanel tnp = new ThumbnailPanel();

    JButton jb = new JButton("click to add nodes");
    jb.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            tnp.setNodes(ians);
          }
        });
    JButton jbg = new JButton("grab a capture");
    jbg.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            Image i = (Image) ians.get(0).getFirst().getContent();
            System.err.println("Setting drawable to something: " + i + " " + i.getUri());
            rootAssetViewer.setDrawable(i);
          }
        });

    PropertiesTablePanel ptp = new PropertiesTablePanel();
    ptp.setNodes(ians);

    ptp.setController(c);

    JTabbedPane jtp = new JTabbedPane();
    jtp.add("Icons", tnp);
    jtp.add("Table", ptp);

    JFrame jf = new JFrame();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(jtp);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(jb);
    buttonPanel.add(jbg);
    jf.add(buttonPanel, BorderLayout.SOUTH);
    jf.setSize(800, 600);
    jf.setLocationRelativeTo(null);
    jf.setVisible(true);
    jtp.add("assetViewer", rootAssetViewer.getCanvas());
  }