public static void main(String[] args) { final Scene scene = new Scene(); LayerWidget layer = new LayerWidget(scene); scene.addChild(layer); Widget nodeWidget = new Widget(scene); nodeWidget.setBorder(BorderFactory.createLineBorder(1, Color.RED)); nodeWidget.setPreferredLocation(new Point(100, 100)); layer.addChild(nodeWidget); final Widget deferredWidget = new Widget(scene); deferredWidget.setLayout(LayoutFactory.createCardLayout(deferredWidget)); deferredWidget.setBorder(BorderFactory.createLineBorder(1, Color.BLUE)); nodeWidget.addChild(deferredWidget); final Widget label = new LabelWidget(scene, "Click me to add ComponentWidget"); label.setBorder(BorderFactory.createLineBorder(1, Color.GREEN)); deferredWidget.addChild(label); LayoutFactory.setActiveCard(deferredWidget, label); label .getActions() .addAction( ActionFactory.createEditAction( new EditProvider() { public void edit(Widget widget) { ComponentWidget component = new ComponentWidget(scene, new JButton("This is the new ComponentWidget")); component.setBorder(BorderFactory.createLineBorder(1, Color.GREEN)); deferredWidget.addChild(component); LayoutFactory.setActiveCard(deferredWidget, component); } })); scene .getActions() .addAction( ActionFactory.createEditAction( new EditProvider() { public void edit(Widget widget) { LayoutFactory.setActiveCard(deferredWidget, label); } })); // to force the boundary // nodeWidget.setPreferredBounds (new Rectangle (0, 0, 70, 30)); // nodeWidget.setPreferredSize (new Dimension (70, 30)); nodeWidget.setLayout(LayoutFactory.createOverlayLayout()); nodeWidget.setCheckClipping(true); // SceneSupport.show(scene); }
public static void main(String[] args) { final Scene scene = new Scene(); scene.setBorder(BorderFactory.createEmptyBorder(10)); scene.setLayout( LayoutFactory.createVerticalFlowLayout(LayoutFactory.SerialAlignment.JUSTIFY, 10)); JTextField textField = new JTextField( "Text for editing - try to edit me. When the JTextField component is hidden, then the Widget just renders it."); final ComponentWidget textFieldWidget = new ComponentWidget(scene, textField); JToggleButton button = new JToggleButton( "Click to hide/show JTextField component bellow. The ComponentWidget is still in the scene and rendered."); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { textFieldWidget.setComponentVisible(!textFieldWidget.isComponentVisible()); scene.validate(); } }); scene.addChild(new ComponentWidget(scene, button)); scene.addChild(textFieldWidget); SeparatorWidget separator = new SeparatorWidget(scene, SeparatorWidget.Orientation.HORIZONTAL); scene.addChild(separator); JTextField textField2 = new JTextField("Text for editing - try to edit me."); final ComponentWidget textFieldWidget2 = new ComponentWidget(scene, textField2); JToggleButton button2 = new JToggleButton("Click to remove/add ComponentWidget from/to the scene."); button2.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (textFieldWidget2.getParentWidget() != null) scene.removeChild(textFieldWidget2); else scene.addChild(textFieldWidget2); scene.validate(); } }); scene.addChild(new ComponentWidget(scene, button2)); scene.addChild(textFieldWidget2); SceneSupport.show(scene); // TODO - call detach method on all ComponentWidget to prevent memory leaks }
/** @author Santhosh Kumar T */ public class RuleScene extends GraphScene<Node, Edge> { private final WidgetAction moveAction = ActionFactory.createMoveAction(); private final WidgetAction nodePopupAction = ActionFactory.createPopupMenuAction(new NodePopupProvider(this)); private final WidgetAction edgePopupAction = ActionFactory.createPopupMenuAction(new EdgePopupProvider(this)); private final WidgetAction hoverAction; private final WidgetAction editAction; private LayerWidget interactionLayer = new LayerWidget(this); private WidgetAction connectAction = ActionFactory.createConnectAction(interactionLayer, new NodeConnectProvider()); private TwoStateHoverProvider hoverProvider; public File file; public RuleScene(TwoStateHoverProvider hoverProvider, EditProvider edgeEditProvider) { addChild(nodes); addChild(connections); addChild(interactionLayer); this.hoverProvider = hoverProvider; hoverAction = ActionFactory.createHoverAction(new Highlighter(hoverProvider)); editAction = ActionFactory.createEditAction(edgeEditProvider); getActions().addAction(hoverAction); getActions().addAction(ActionFactory.createPopupMenuAction(new ScenePopupProvider(this))); getActions().addAction(ActionFactory.createWheelPanAction()); } /*-------------------------------------------------[ Rule ]---------------------------------------------------*/ private Syntax syntax; private Rule rule; public final Observable ruleObservable = new Observable() { @Override public void notifyObservers(Object arg) { setChanged(); super.notifyObservers(arg); } }; public void setRule(Syntax syntax, Rule rule) { this.syntax = syntax; if (this.rule != null) { for (Node node : getNodes().toArray(new Node[getNodes().size()])) removeNodeWithEdges(node); } this.rule = rule; if (this.rule != null) populate(rule.node); layout(); hoverProvider.unsetHovering(this); ruleObservable.notifyObservers(rule); } public Syntax getSyntax() { return syntax; } public Rule getRule() { return rule; } private void populate(Node node) { if (findWidget(node) == null) { addNode(node); for (Edge edge : node.outgoing) { populate(edge.target); addEdge(edge); setEdgeSource(edge, edge.source); setEdgeTarget(edge, edge.target); } } } /*-------------------------------------------------[ Layout ]---------------------------------------------------*/ private final SceneLayout layout = LayoutFactory.createSceneGraphLayout(this, new RuleLayout(false)); public void layout() { validate(); layout.invokeLayoutImmediately(); } public void layout(Object model) { findWidget(model).revalidate(); layout(); } public void refresh() { Syntax syntax = this.syntax; Rule rule = this.rule; setRule(null, null); setRule(syntax, rule); } /*-------------------------------------------------[ Nodes ]---------------------------------------------------*/ private final LayerWidget nodes = new LayerWidget(this); @Override protected Widget attachNodeWidget(Node node) { NodeWidget widget = new NodeWidget(this); widget.setFont(Util.FIXED_WIDTH_FONT); widget.highLight(false); nodes.addChild(widget); // widget.getActions().addAction(moveAction); widget.getActions().addAction(hoverAction); widget.getActions().addAction(nodePopupAction); widget.getActions().addAction(connectAction); return widget; } /*-------------------------------------------------[ Edges ]---------------------------------------------------*/ private final LayerWidget connections = new LayerWidget(this); private final Router router = new RuleRouter(); private LabelWidget createEdgeLabel(Edge edge) { LabelWidget label = new LabelWidget(this, edge.toString()); label.setFont(Util.FIXED_WIDTH_FONT); label.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0)); label.getActions().addAction(hoverAction); label.getActions().addAction(editAction); label.getActions().addAction(moveAction); label.getActions().addAction(edgePopupAction); return label; } @Override protected Widget attachEdgeWidget(Edge edge) { EdgeWidget connection = new EdgeWidget(this); connection.setStroke(Util.STROKE_2); connection.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED); LabelWidget label = createEdgeLabel(edge); connection.setConstraint(label, LayoutFactory.ConnectionWidgetLayoutAlignment.TOP_CENTER, 0.5f); connection.addChild(label); connections.addChild(connection); connection.setRouter(router); connection.highLight(false); connection.getActions().addAction(hoverAction); connection.getActions().addAction(editAction); connection.getActions().addAction(edgePopupAction); return connection; } @Override protected void attachEdgeSourceAnchor(Edge edge, Node oldSource, Node newSource) { Widget w = newSource != null ? findWidget(newSource) : null; ((ConnectionWidget) findWidget(edge)).setSourceAnchor(AnchorFactory.createRectangularAnchor(w)); } @Override protected void attachEdgeTargetAnchor(Edge edge, Node oldTarget, Node newTarget) { Widget w = newTarget != null ? findWidget(newTarget) : null; ((ConnectionWidget) findWidget(edge)).setTargetAnchor(AnchorFactory.createRectangularAnchor(w)); } /*-------------------------------------------------[ Executing ]---------------------------------------------------*/ private Widget executionWidget; private void executing(Widget widget) { if (executionWidget != widget) { if (executionWidget != null) { ((NBLRWidget) executionWidget).executing(false); executionWidget = null; } if (widget != null) { executionWidget = widget; ((NBLRWidget) executionWidget).executing(true); final Rectangle bounds = Util.bounds(widget); SwingUtilities.invokeLater( new Runnable() { @Override public void run() { getView().scrollRectToVisible(bounds); } }); } validate(); } } public void executing(Node node) { executing(findWidget(node)); } public void executing(Edge edge) { executing(findWidget(edge)); } /*-------------------------------------------------[ Antialiasing ]---------------------------------------------------*/ public void paintChildren() { Graphics2D g = getGraphics(); Object anti = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING); Object textAnti = g.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); super.paintChildren(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, anti); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textAnti); } }
public ProxyAnchorExpandTest() { setBackground(Color.LIGHT_GRAY); // layer for widgets LayerWidget mainLayer = new LayerWidget(this); addChild(mainLayer); // layer for connections LayerWidget connLayer = new LayerWidget(this); addChild(connLayer); // outer widget Widget outerWidget = new Widget(this); outerWidget.setOpaque(true); outerWidget.setBackground(Color.WHITE); outerWidget.setBorder(BorderFactory.createLineBorder(10)); outerWidget.setPreferredLocation(new Point(100, 100)); outerWidget.setLayout( LayoutFactory.createVerticalFlowLayout(LayoutFactory.SerialAlignment.CENTER, 4)); outerWidget.addChild( new LabelWidget( this, "The anchor switches based on a state in StateModel used by ProxyAnchor.")); outerWidget.addChild( new LabelWidget(this, "ConnectionWidget has the same anchors assigned all the time.")); // inner widget LabelWidget innerWidget = new LabelWidget(this, "Internal frame"); innerWidget.setBorder(BorderFactory.createLineBorder()); outerWidget.addChild(innerWidget); mainLayer.addChild(outerWidget); // the target widget Widget targetWidget = new LabelWidget(this, "Click here to switch the state in StateModel/anchor."); targetWidget.setOpaque(true); targetWidget.setBackground(Color.WHITE); targetWidget.setBorder(BorderFactory.createLineBorder(10)); targetWidget.setPreferredLocation(new Point(450, 300)); mainLayer.addChild(targetWidget); // an action for switching a state of a StateModel used by the ProxyAnchor to determinate an // active anchor WidgetAction switchAction = ActionFactory.createSelectAction(new SwitchProvider()); targetWidget.getActions().addAction(switchAction); Anchor outerAnchor = AnchorFactory.createRectangularAnchor(outerWidget); Anchor innerAnchor = AnchorFactory.createRectangularAnchor(innerWidget); // a proxy anchor which acts like an one of the specified anchors. // The active anchor is defined by a state in the StateModel Anchor proxyAnchor = AnchorFactory.createProxyAnchor(model, outerAnchor, innerAnchor); // the connection widget ConnectionWidget conn = new ConnectionWidget(this); conn.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED); connLayer.addChild(conn); // the proxy anchor is used as a source conn.setSourceAnchor(proxyAnchor); // the target anchor is assigned to the targetWidget widget conn.setTargetAnchor(AnchorFactory.createRectangularAnchor(targetWidget)); }