Java tutorial
/* * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by * applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS * OF ANY KIND, either express or implied. See the License for the specific * language governing permissions and limitations under the License. */ package net.rptools.layercontrol; import java.util.List; import javafx.application.Platform; import javafx.scene.Node; import javafx.scene.layout.Pane; import org.apache.commons.lang3.StringUtils; import org.rptools.framework.Layer; import org.rptools.framework.ThreadPolicy; import org.rptools.layer.AbstractLayer; import org.rptools.layer.ControlledLayer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * In the stackpane (which holds the "business" layers) the top-most layer * (front to back) is shown at the top of this component's gallery. Which * reverses the movement direction, when a layer is shifted here. See the move * and addLayer methods. <br> * Since we do not control the startup order, we create a temporary pane, where * we keep all layers created thus far. * @author username */ public class LayerStackLayer extends AbstractLayer { // Utility private static final Logger LOGGER = LoggerFactory.getLogger(LayerStackLayer.class); // Back reference private LayerControlComponentImpl component; /** * Constructor. * @param aComponent "home" class */ public LayerStackLayer(final LayerControlComponentImpl aComponent) { component = aComponent; } @Override public String getName() { return "LayerControlStack"; } @Override protected void redraw() { } /** * Move the layer. * @param offset offset by which to move * @param layer layer to move */ @ThreadPolicy(ThreadPolicy.ThreadId.JFX) public void move(final int offset, final Layer layer) { final Node node = getDrawable().lookup("#" + StringUtils.uncapitalize(layer.getName())); final int idx = getDrawable().getChildren().indexOf(node); getDrawable().getChildren().remove(node); getDrawable().getChildren().add(idx - offset, node); } /** * Add a layer. * @param i index to add at * @param layer layer to add */ @ThreadPolicy(ThreadPolicy.ThreadId.ANY) public synchronized void addLayer(final int i, final Layer layer) { if (!Platform.isFxApplicationThread()) { Platform.runLater(new Runnable() { @Override public void run() { addLayer(i, layer); } }); } // Now on JFX thread Pane pane = new Pane(); final String resource = "layer" + layer.getName() + ".fxml"; try { pane = component.getFramework().getFXMLLoader(layer.getClass().getResource(resource)).load(); } catch (final Exception e) { LOGGER.warn("no layer pane found resource={}", resource); } pane.getStyleClass().add(layer.getName().toLowerCase()); pane.setId(StringUtils.uncapitalize(layer.getName())); pane.minWidthProperty().bind(getDrawable().widthProperty()); pane.minHeightProperty().bind(getDrawable().heightProperty()); final int size = getDrawable().getChildren().size(); getDrawable().getChildren().add(size - i, pane); layer.setDrawable(pane); } /** * Remove a layer. * @param layer layer to remove */ @ThreadPolicy(ThreadPolicy.ThreadId.ANY) public synchronized void removeLayer(final Layer layer) { if (!Platform.isFxApplicationThread()) { Platform.runLater(new Runnable() { @Override public void run() { removeLayer(layer); } }); } // Now on JFX thread getDrawable().getChildren().remove(layer.getDrawable()); } /** * Reset the layers. * @param controlledLayers new layer set. */ @ThreadPolicy(ThreadPolicy.ThreadId.ANY) public void setLayers(final List<ControlledLayer> controlledLayers) { getDrawable().getChildren().clear(); for (Layer layer : controlledLayers) { addLayer(0, layer); } } @Override protected void globalTransformChanged() { // This is not a transformed layer } /** * Helper for tracing. * @param layer layer to trace index of * @return index in children list */ @ThreadPolicy(ThreadPolicy.ThreadId.JFX) public int indexOf(final Layer layer) { return getDrawable().getChildren().indexOf(layer.getDrawable()); } }