package jetbrains.mps.lang.editor.diagram.runtime.jetpad.views;
/*Generated by MPS */
import jetbrains.jetpad.model.property.Property;
import jetbrains.jetpad.geometry.Rectangle;
import jetbrains.jetpad.model.property.ValueProperty;
import jetbrains.jetpad.mapper.Mapper;
import jetbrains.jetpad.mapper.Synchronizers;
import jetbrains.jetpad.model.property.WritableProperty;
import jetbrains.jetpad.mapper.MapperFactory;
import jetbrains.jetpad.projectional.view.GroupView;
import jetbrains.mps.lang.editor.diagram.runtime.jetpad.property.DependentProperty;
import jetbrains.jetpad.geometry.Vector;
import jetbrains.jetpad.model.property.Properties;
import jetbrains.jetpad.values.Color;
public abstract class RectDecoratorView extends AbstractDecoratorView {
private static final int ERROR_FRAME_WIDTH = 3;
public Property<Rectangle> bounds = new ValueProperty<Rectangle>(new Rectangle(0, 0, 0, 0));
public Property<Rectangle> boundsDelta = new ValueProperty<Rectangle>();
public Property<Boolean> resizable = new ValueProperty<Boolean>(false);
private Property<Rectangle> myInternalsBounds = new ValueProperty<Rectangle>();
public RectDecoratorView() {
new Mapper<RectDecoratorView, RectDecoratorView>(this, this) {
@Override
protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
super.registerSynchronizers(configuration);
configuration.add(Synchronizers.forProperty(bounds, new WritableProperty<Rectangle>() {
public void set(Rectangle newBounds) {
myInternalsBounds.set(new Rectangle(newBounds.origin.x, newBounds.origin.y, newBounds.dimension.x - 1, newBounds.dimension.y - 1));
}
}));
configuration.add(Synchronizers.forObservableRole(this, selectionDecorator, childSubList(), createSelectionDecoratorMapperFactory()));
configuration.add(Synchronizers.forObservableRole(this, errorDecorator, childSubList(), createErrorDecoratorMapperFactory()));
}
}.attachRoot();
}
private MapperFactory<Boolean, GroupView> createSelectionDecoratorMapperFactory() {
return new MapperFactory<Boolean, GroupView>() {
public Mapper<? extends Boolean, ? extends GroupView> createMapper(Boolean selected) {
return new Mapper<Boolean, ResizableSelectionFrameView>(selected, new ResizableSelectionFrameView()) {
@Override
protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
super.registerSynchronizers(configuration);
configuration.add(Synchronizers.forProperty(new DependentProperty<Rectangle>(myInternalsBounds, boundsDelta), getTarget().internalsBounds));
configuration.add(Synchronizers.forProperty(getTarget().boundsDelta, new WritableProperty<Rectangle>() {
public void set(Rectangle internalsBoundsDelta) {
if (internalsBoundsDelta == null) {
return;
}
Rectangle oldBounds = bounds.get();
Rectangle newBounds = new Rectangle(oldBounds.origin.add(internalsBoundsDelta.origin), oldBounds.dimension.add(internalsBoundsDelta.dimension));
Vector min = null;
Vector max = null;
for (Vector nextPoint : newBounds.getBoundPoints()) {
min = (min == null ? nextPoint : min.min(nextPoint));
max = (max == null ? nextPoint : max.max(nextPoint));
}
newBounds = new Rectangle(min, max.sub(min));
boundsDelta.set(new Rectangle(newBounds.origin.sub(oldBounds.origin), newBounds.dimension.sub(oldBounds.dimension)));
}
}));
configuration.add(Synchronizers.forProperty(resizable, getTarget().resizable));
configuration.add(Synchronizers.forProperty(Properties.ifProp(hasError, AbstractExternalFrameView.getHalfWidth(ERROR_FRAME_WIDTH), AbstractExternalFrameView.getHalfWidth(getTarget().selectionLineWidth.get())), getTarget().frameWidth));
}
};
}
};
}
private MapperFactory<Boolean, SelectionFrameView> createErrorDecoratorMapperFactory() {
return new MapperFactory<Boolean, SelectionFrameView>() {
public Mapper<? extends Boolean, ? extends SelectionFrameView> createMapper(Boolean error) {
return new Mapper<Boolean, SelectionFrameView>(error, createErrorView()) {
@Override
protected void registerSynchronizers(Mapper.SynchronizersConfiguration configuration) {
super.registerSynchronizers(configuration);
configuration.add(Synchronizers.forProperty(myInternalsBounds, getTarget().internalsBounds));
}
};
}
};
}
private SelectionFrameView createErrorView() {
SelectionFrameView result = new SelectionFrameView();
result.color.set(Color.RED);
result.setLineWidth(ERROR_FRAME_WIDTH);
return result;
}
}