package org.openautonomousconnection.oacswing.component; import jdk.jshell.spi.ExecutionControl; import org.openautonomousconnection.oacswing.component.design.Design; import org.openautonomousconnection.oacswing.component.design.DesignManager; import javax.swing.*; import java.awt.*; import java.util.Optional; import java.util.OptionalInt; public interface OACComponent { default void initDesign() { DesignManager.apply(this); // Design design = DesignManager.getGlobalDesign(); // // this.setBackground(design.getElements().get(this.getClass()).getColor()); } void setBackground(Color color); default void initOther(Component comp) { if(comp instanceof OACComponent component) component.initDesign(); } default Component add(Optional name, Component comp, OptionalInt index, Optional constrains) throws ExecutionControl.NotImplementedException { JComponent superclass = this.getSuperclass(); if(comp instanceof OACComponent component) component.initDesign(); if(name.isPresent()) return superclass.add(name.get(), comp); else if(constrains.isPresent()) if(index.isPresent()) { superclass.add(comp, constrains.get(), index.getAsInt()); return null; } else { superclass.add(comp, constrains.get()); return null; } else if(index.isPresent()) return superclass.add(comp, index.getAsInt()); else return superclass.add(comp); } default Component add(Component comp) { try { return this.add(Optional.empty(), comp, OptionalInt.empty(), Optional.empty()); } catch (ExecutionControl.NotImplementedException e) { throw new RuntimeException(e); } } default Component add(String name, Component comp) { try { return this.add(Optional.of(name), comp, OptionalInt.empty(), Optional.empty()); } catch (ExecutionControl.NotImplementedException e) { throw new RuntimeException(e); } } default Component add(Component comp, int index) { try { return this.add(Optional.empty(), comp, OptionalInt.of(index), Optional.empty()); } catch (ExecutionControl.NotImplementedException e) { throw new RuntimeException(e); } } default void add(Component comp, Object constraints, int index) { try { this.add(Optional.empty(), comp, OptionalInt.of(index), Optional.of(constraints)); } catch (ExecutionControl.NotImplementedException e) { throw new RuntimeException(e); } } default void add(Component comp, Object constraints) { try { this.add(Optional.empty(), comp, OptionalInt.empty(), Optional.of(constraints)); } catch (ExecutionControl.NotImplementedException e) { throw new RuntimeException(e); } } /** * Needed to run JComponent / Component methods * @return JComponent type superclass * @throws ExecutionControl.NotImplementedException superclass is not of type JComponent */ default JComponent getSuperclass() throws ExecutionControl.NotImplementedException { if(this instanceof JComponent superClass) return superClass; else throw new ExecutionControl.NotImplementedException("Trying to implement OACComponent interface for non-JComponent class"); } }