Files
OAC-Swing/src/main/java/org/openautonomousconnection/oacswing/component/OACComponent.java

107 lines
3.4 KiB
Java
Raw Normal View History

2026-02-07 18:54:19 +01:00
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);
2026-02-07 18:54:19 +01:00
// Design design = DesignManager.getGlobalDesign();
//
// this.setBackground(design.getElements().get(this.getClass()).getColor());
}
2026-02-11 23:28:02 +01:00
default void init() {
}
2026-02-07 18:54:19 +01:00
default void initOther(Component comp) {
2026-02-11 23:28:02 +01:00
if (comp instanceof OACComponent component)
2026-02-07 18:54:19 +01:00
component.initDesign();
}
default Component add(Optional<String> name, Component comp, OptionalInt index, Optional<?> constrains) throws ExecutionControl.NotImplementedException {
2026-02-07 18:54:19 +01:00
JComponent superclass = this.getSuperclass();
2026-02-11 23:28:02 +01:00
if (comp instanceof OACComponent component)
2026-02-07 18:54:19 +01:00
component.initDesign();
2026-02-11 23:28:02 +01:00
if (name.isPresent())
2026-02-07 18:54:19 +01:00
return superclass.add(name.get(), comp);
2026-02-11 23:28:02 +01:00
else if (constrains.isPresent())
if (index.isPresent()) {
2026-02-07 18:54:19 +01:00
superclass.add(comp, constrains.get(), index.getAsInt());
return null;
2026-02-11 23:28:02 +01:00
} else {
2026-02-07 18:54:19 +01:00
superclass.add(comp, constrains.get());
return null;
}
2026-02-11 23:28:02 +01:00
else if (index.isPresent())
2026-02-07 18:54:19 +01:00
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
2026-02-11 23:28:02 +01:00
*
2026-02-07 18:54:19 +01:00
* @return JComponent type superclass
* @throws ExecutionControl.NotImplementedException superclass is not of type JComponent
*/
default JComponent getSuperclass() throws ExecutionControl.NotImplementedException {
2026-02-11 23:28:02 +01:00
if (this instanceof JComponent superClass)
2026-02-07 18:54:19 +01:00
return superClass;
else
throw new ExecutionControl.NotImplementedException("Trying to implement OACComponent interface for non-JComponent class");
}
}