Files
OAC-Swing/src/main/java/org/openautonomousconnection/oacswing/component/OACComponent.java
2026-02-11 23:28:02 +01:00

107 lines
3.4 KiB
Java

package org.openautonomousconnection.oacswing.component;
import jdk.jshell.spi.ExecutionControl;
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());
}
default void init() {
}
default void initOther(Component comp) {
if (comp instanceof OACComponent component)
component.initDesign();
}
default Component add(Optional<String> 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");
}
}