Skip to content

Commit

Permalink
add on the fly feature to show the the icon as background image for b…
Browse files Browse the repository at this point in the history
…etter component placement
  • Loading branch information
Marcel Hoppe committed Nov 9, 2017
1 parent 5cf4f2b commit c125cdc
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import de.thm.mni.mote.mode.modelica.MoContainer;
import de.thm.mni.mote.mode.parser.ParserException;
import de.thm.mni.mote.mode.uiactor.control.modelica.FXMoGroup;
import de.thm.mni.mote.mode.uiactor.control.modelica.FXMoDiagramMoGroup;
import de.thm.mni.mote.mode.uiactor.control.modelica.FXMoGroup;
import de.thm.mni.mote.mode.uiactor.control.modelica.FXMoIconMoGroup;
import de.thm.mni.mote.mode.uiactor.editor.MenuManager;
import de.thm.mni.mote.mode.uiactor.editor.actionmanager.ActionManager;
import de.thm.mni.mote.mode.uiactor.editor.elementmanager.ElementManager;
import de.thm.mni.mote.mode.uiactor.editor.elementmanager.elements.ManagedFXMoDiagramMoGroup;
Expand Down Expand Up @@ -37,20 +38,14 @@
public class MainTabControl extends Tab implements Initializable {

private final MoContainer data;
private final Boolean imagesAsBackground;

@FXML private StackPane main;
@FXML private ScrollPane scroll;

private FXMLLoader loader;

public MainTabControl(MoContainer data) {
this(data, false);
}

public MainTabControl(MoContainer data, Boolean imagesAsBackground) {
this.data = data;
this.imagesAsBackground = imagesAsBackground;
loader = new FXMLLoader();
loader.setLocation(Utilities.getControlView("MainTab"));
loader.setRoot(this);
Expand All @@ -72,7 +67,7 @@ public void initialize(URL location, ResourceBundle resources) {
try {
this.setGraphic(new FXMoIconMoGroup(data).scaleToSize(20., 20.));

mp = new ManagedFXMoDiagramMoGroup(data, imagesAsBackground);
mp = new ManagedFXMoDiagramMoGroup(data);

updateText(Change.NONE);

Expand Down Expand Up @@ -110,6 +105,14 @@ public void lateInitialize(Scene scene) {

main.minWidthProperty().bind(Bindings.createDoubleBinding(() -> scroll.getViewportBounds().getWidth(), scroll.viewportBoundsProperty()));

if(data.getElement().hasIcon()) {
MenuManager.getInstance(data).getShowIconProperty().addListener((observable, oldValue, newValue) -> {
ManagedFXMoDiagramMoGroup fxdiagram = (ManagedFXMoDiagramMoGroup)main.getChildren().get(0);
if(newValue) fxdiagram.setImageAsBackground();
else fxdiagram.removeImageAsBackground();
});
}

this.setOnSelectionChanged(event -> {
if (MainTabControl.this.isSelected()) {
StateMachine.getInstance(data).enter();
Expand All @@ -122,6 +125,7 @@ public void lateInitialize(Scene scene) {

this.setOnClosed(event -> {
StateMachine.getInstance(data).leave();
MenuManager.removeInstance(data);
ActionManager.removeInstance(data);
ElementManager.removeInstance(data);
StateMachine.removeInstance(data);
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/de/thm/mni/mote/mode/uiactor/control/MoTreeCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MoTreeCell extends TreeCell<MoContainer> {
private ContextMenu cm = null;
private TabPane tabPane;

private ObjectProperty<EventHandler<ActionEvent>> onEditAction = new SimpleObjectProperty<>(null);
private ObjectProperty<EventHandler<ActionEvent>> onEditActionProperty = new SimpleObjectProperty<>(null);
private ObjectProperty<EventHandler<ActionEvent>> onAddNewActionProperty = new SimpleObjectProperty<>(null);

private ObjectProperty<EventHandler<MouseEvent>> onNonRootMouseClickedProperty = new SimpleObjectProperty<>(null);
Expand All @@ -41,7 +41,7 @@ public class MoTreeCell extends TreeCell<MoContainer> {
{
btnRootEdit.getStyleClass().add("no-border");
btnRootEdit.setGraphic(new FontIcon("gmi-edit"));
btnRootEdit.onActionProperty().bind(onEditAction);
btnRootEdit.onActionProperty().bind(onEditActionProperty);
}


Expand Down Expand Up @@ -120,27 +120,19 @@ private EventHandler<MouseEvent> createDragDetectEventHandler() {
};
}

private final ObjectProperty<EventHandler<ActionEvent>> onEditActionProperty() { return onEditAction; }
public final void setOnEditAction(EventHandler<ActionEvent> value) { onEditActionProperty.set(value); }

public final void setOnEditAction(EventHandler<ActionEvent> value) { onEditActionProperty().set(value); }
public final void setOnNonRootMouseClicked(EventHandler<MouseEvent> value) { onNonRootMouseClickedProperty.set(value); }

private final ObjectProperty<EventHandler<MouseEvent>> onNonRootMouseClickedProperty() { return onNonRootMouseClickedProperty; }
public final void setOnNonRootContextMenuRequest(EventHandler<? super ContextMenuEvent> value) { onNonRootContextMenuRequest.set(value); }

public final void setOnNonRootMouseClicked(EventHandler<MouseEvent> value) { onNonRootMouseClickedProperty().set(value); }

private final ObjectProperty<EventHandler<? super ContextMenuEvent>> onNonRootContextMenuRequest() { return onNonRootContextMenuRequest; }

public final void setOnNonRootContextMenuRequest(EventHandler<? super ContextMenuEvent> value) { onNonRootContextMenuRequest().set(value); }

private final ObjectProperty<EventHandler<ActionEvent>> onNonRootContextMenuItemAction() { return onNonRootContextMenuItemAction; }

public final void setOnNonRootContextMenuItemAction(EventHandler<ActionEvent> value) { onNonRootContextMenuItemAction().set(value); }
public final void setOnNonRootContextMenuItemAction(EventHandler<ActionEvent> value) { onNonRootContextMenuItemAction.set(value); }

private ContextMenu createLibraryMenu() {
if (cm != null) return cm;
cm = new ContextMenu();

for (String action : new String[]{"add_new.package", "add_new.model", "seperator", "open_as_diagram", "add_to_diagram", "open_as_icon"}) {
for (String action : new String[]{"add_new.package", "add_new.model", "seperator", "open", "add_to_diagram"}) {
createMenu(action, 0, cm.getItems());
}
return cm;
Expand All @@ -151,10 +143,10 @@ private void createMenu(String action, int part, ObservableList<MenuItem> items)
if (part + 1 >= splittedAction.length) {
items.add(createMenuItem(action));
} else {
String newAction = "";
StringBuilder newAction = new StringBuilder();
for (int i = 0; i <= part; i++) {
if (!newAction.isEmpty()) newAction += ".";
newAction += ((newAction.isEmpty()) ? "" : ".") + splittedAction[i];
if (newAction.length() > 0) newAction.append(".");
newAction.append((newAction.length() == 0) ? "" : ".").append(splittedAction[i]);
}
Menu submenu = null;
for (MenuItem item : items) {
Expand All @@ -164,7 +156,7 @@ private void createMenu(String action, int part, ObservableList<MenuItem> items)
}
}
if (submenu == null) {
submenu = new ContextSubMenu(tr("Main", "menu.context." + newAction), newAction);
submenu = new ContextSubMenu(tr("Main", "menu.context." + newAction), newAction.toString());
items.add(submenu);
}
createMenu(action, part + 1, submenu.getItems());
Expand All @@ -188,11 +180,11 @@ public boolean isLibrary() {
return isSystemLib() || isProjectLib();
}

public boolean isSystemLib() {
private boolean isSystemLib() {
return itemHasParentType(getItem(), "system_libraries");
}

public boolean isProjectLib() {
private boolean isProjectLib() {
return itemHasParentType(getItem(), "project_libraries");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.thm.mni.mote.mode.modelica.MoConnection;
import de.thm.mni.mote.mode.modelica.MoContainer;
import de.thm.mni.mote.mode.modelica.MoVariable;
import de.thm.mni.mote.mode.modelica.graphics.MoGraphic;
import de.thm.mni.mote.mode.parser.ParserException;
import de.thm.mni.mote.mode.uiactor.editor.elementmanager.elements.ManagedFXMoVariableIconMoGroup;
import javafx.collections.ListChangeListener;
Expand All @@ -12,34 +13,45 @@
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by hobbypunk on 19.09.16.
*/
public class FXMoDiagramMoGroup extends FXMoGroup {

private final Boolean imageAsBackground;
private Map<MoVariable, FXMoVariableIconMoGroup> variables = new HashMap<>();
private Map<MoConnection, FXMoConnectionGroup> connections = new HashMap<>();

public FXMoDiagramMoGroup(MoContainer container, Boolean imageAsBackground) throws ParserException {
public FXMoDiagramMoGroup(MoContainer container) throws ParserException {
super(container);
this.imageAsBackground = imageAsBackground;
init();
this.setId(container.getName().replaceAll("\\.", "_"));
}

@Override
protected void initImage() {
if(this.imageAsBackground) this.getMoClass().getIcon().getMoGraphics().forEach(node -> this.initImage(node, true));
initVariables();
initConnections();
if (this.getMoClass().getDiagram() != null) this.getMoClass().getDiagram().getMoGraphics().forEach(this::initImage);
coordianteSystem.setFill(Color.WHITE);
}

public void setImageAsBackground() {
if(this.getMoClass().hasIcon()) {
List<MoGraphic> list = this.getMoClass().getIcon().getMoGraphics();
Collections.reverse(list);
list.forEach(node -> this.initImage(node, true));
}
}

public void removeImageAsBackground() {
this.getMoClass().getIcon().getMoGraphics().forEach(node -> this.remove(node, true));
}

private void addListener(FXMoGroup group, EventHandler<InputEvent> eventHandler) {
if (group.equals(this)) {
group.addEventHandler(MouseEvent.ANY, eventHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ protected Double getScaleFactor(Boolean scaleUp) {
return (scaleUp) ? ZOOMFACTOR : (1 / ZOOMFACTOR);
}

protected FXMoGroup scaleDelta(Boolean scaleUp) throws ParserException {
return scaleToFactor(scale.getX() * getScaleFactor(scaleUp), scale.getY() * getScaleFactor(scaleUp), false);
protected void scaleDelta(Boolean scaleUp) throws ParserException {
scaleToFactor(scale.getX() * getScaleFactor(scaleUp), scale.getY() * getScaleFactor(scaleUp), false);
}


Expand All @@ -109,7 +109,7 @@ public FXMoGroup scaleToSize(Double newWidth, Double newHeight) {
return scaleToFactor(scale.getX() * newWidth / oldWidth, scale.getY() * newHeight / oldHeight, true);
}

protected FXMoGroup scaleToFactor(Double factorX, Double factorY, Boolean force) {
FXMoGroup scaleToFactor(Double factorX, Double factorY, Boolean force) {
if (force || factorX > 0.25) {
scale.setX(factorX);
scale.setY(factorY);
Expand All @@ -124,7 +124,7 @@ private void initCoordinateSystem() {
else initCoordinateSystem(this.getMoClass().getDiagramCoordinateSystem());
}

protected void initCoordinateSystem(MoCoordinateSystem mcs) {
private void initCoordinateSystem(MoCoordinateSystem mcs) {

Point2D extent0 = mcs.getExtent().getP1();
Point2D extent1 = mcs.getExtent().getP2();
Expand Down Expand Up @@ -167,11 +167,13 @@ public void remove(Node node) {
getChildren().remove(node);
}

public void remove(MoGraphic mg) {
public void remove(MoGraphic mg) {remove(mg, false);}
@SuppressWarnings("SuspiciousMethodCalls")
public void remove(MoGraphic mg, Boolean force) {
for (int i = 0, size = getChildren().size(); i < size; i++) {
if (getChildren().get(i) instanceof Element) {
Element child = (Element) getChildren().get(i);
if (child.getData().equals(mg) && child instanceof Deletable) {
if (child.getData().equals(mg) && (child instanceof Deletable || force)) {
getChildren().remove(child);
return;
}
Expand All @@ -195,7 +197,7 @@ void initImage(MoGraphic mg) {
initImage(mg, false);
}

void initImage(MoGraphic mg, Boolean grayOut) {
void initImage(MoGraphic mg, Boolean asBackground) {
Node elem = null;
if (mg instanceof MoText) this.add(new Text(this, (MoText) mg));
else if (mg instanceof MoRectangle) {
Expand All @@ -211,13 +213,15 @@ else if (mg instanceof MoRectangle) {
}

if(elem != null) {
if(grayOut) {
if (!asBackground) {
this.add(elem);
} else {
ColorAdjust effect = new ColorAdjust();
effect.setSaturation(-0.8);
effect.setSaturation(-0.8);
elem.setEffect(effect);
this.add(1, elem);
}
this.add(elem);
preventScaling(elem,1., 1.);
}
}
Expand Down Expand Up @@ -252,6 +256,7 @@ public Point2D convertTo(Point2D scenePoint) {
return null;
}

@SuppressWarnings("WeakerAccess")
public Point2D convertFrom(Point2D p) {

try {
Expand Down
Loading

0 comments on commit c125cdc

Please sign in to comment.