Browse Source

Furthermore enhance jme3-core's Node Editor interfaces (changed protection levels and a few callbacks)

MeFisto94 6 years ago
parent
commit
bbeea57468

+ 30 - 15
jme3-core/src/com/jme3/gde/core/editor/nodes/ConnectionEndpoint.java

@@ -50,11 +50,12 @@ public abstract class ConnectionEndpoint extends JPanel implements MouseInputLis
     public static boolean pressed = false;
     public static boolean pressed = false;
     protected ImageIcon img;
     protected ImageIcon img;
     protected ImageIcon prevImg;
     protected ImageIcon prevImg;
-    private String type;
+    protected String type;
     protected ParamType paramType;
     protected ParamType paramType;
-    private String text = "";
+    protected String text = "";
     protected DraggablePanel node;
     protected DraggablePanel node;
-    private int index = 1;
+    protected int index = 1;
+    protected Connection connection;
 
 
     public String getText() {
     public String getText() {
         return text;
         return text;
@@ -156,6 +157,18 @@ public abstract class ConnectionEndpoint extends JPanel implements MouseInputLis
         e.consume();
         e.consume();
     }
     }
 
 
+    @Override
+    public void mouseEntered(MouseEvent e) {
+        Diagram diag = getDiagram();
+        if (diag.draggedFrom != null && diag.draggedFrom != this) {
+            prevImg = img;
+            canConnect(diag.draggedFrom);
+            diag.draggedTo = this;
+            diag.draggedFrom.canConnect(this);
+        }
+
+    }
+    
     /**
     /**
      * Changes the look of this connector. Implies repainting
      * Changes the look of this connector. Implies repainting
      * @param icon The Icon to use
      * @param icon The Icon to use
@@ -177,20 +190,21 @@ public abstract class ConnectionEndpoint extends JPanel implements MouseInputLis
      */
      */
     public void disconnect() {
     public void disconnect() {
         setIcon(Icons.imgGrey);
         setIcon(Icons.imgGrey);
-    }
-
-    @Override
-    public void mouseEntered(MouseEvent e) {
-        Diagram diag = getDiagram();
-        if (diag.draggedFrom != null && diag.draggedFrom != this) {
-            prevImg = img;
-            canConnect(diag.draggedFrom);
-            diag.draggedTo = this;
-            diag.draggedFrom.canConnect(this);
+        
+        if (connection != null) {
+            getNode().removeComponentListener(connection);
+            connection = null;
         }
         }
-
     }
     }
 
 
+    public Connection getConnection() {
+        return connection;
+    }
+    
+    public boolean isConnected() {
+        return connection != null;
+    }
+    
     /**
     /**
      * Determines whether this dot can form a {@link Connection} with the other
      * Determines whether this dot can form a {@link Connection} with the other
      * specified dot. Subclasses should only override this, when they want to
      * specified dot. Subclasses should only override this, when they want to
@@ -218,8 +232,9 @@ public abstract class ConnectionEndpoint extends JPanel implements MouseInputLis
     
     
     protected abstract boolean allowConnection(ConnectionEndpoint pair);
     protected abstract boolean allowConnection(ConnectionEndpoint pair);
 
 
-    protected void connect(Connection connection) {
+    public void connect(Connection connection) {
         getNode().addComponentListener(connection);
         getNode().addComponentListener(connection);
+        this.connection = connection;
         setIcon(Icons.imgGreen);
         setIcon(Icons.imgGreen);
     }
     }
 
 

+ 47 - 23
jme3-core/src/com/jme3/gde/core/editor/nodes/Diagram.java

@@ -198,6 +198,13 @@ public abstract class Diagram extends JPanel implements MouseListener,
     public void addConnection(Connection conn) {
     public void addConnection(Connection conn) {
         connections.add(conn);
         connections.add(conn);
         add(conn);
         add(conn);
+        
+        if (conn.getStart().getNode() instanceof NodePanel) {
+            ((NodePanel)conn.getStart().getNode()).onConnect(conn);
+        }
+        if (conn.getEnd().getNode() instanceof NodePanel) {
+            ((NodePanel)conn.getEnd().getNode()).onConnect(conn);
+        }
         repaint();
         repaint();
     }
     }
 
 
@@ -219,7 +226,7 @@ public abstract class Diagram extends JPanel implements MouseListener,
         setComponentZOrder(node, 0);
         setComponentZOrder(node, 0);
         node.addComponentListener(this);
         node.addComponentListener(this);
     }
     }
-
+    
     protected void removeSelectedConnection(Selectable selectedItem) {        
     protected void removeSelectedConnection(Selectable selectedItem) {        
         Connection selectedConnection = (Connection) selectedItem;
         Connection selectedConnection = (Connection) selectedItem;
         removeConnection(selectedConnection);
         removeConnection(selectedConnection);
@@ -235,7 +242,7 @@ public abstract class Diagram extends JPanel implements MouseListener,
         if (result == JOptionPane.OK_OPTION) {
         if (result == JOptionPane.OK_OPTION) {
             for (Selectable selectedItem : selectedItems) {
             for (Selectable selectedItem : selectedItems) {
                 if (selectedItem instanceof NodePanel) {
                 if (selectedItem instanceof NodePanel) {
-                    removeSelectedNode(selectedItem);
+                    removeNode((NodePanel)selectedItem);
                 }
                 }
                 if (selectedItem instanceof Connection) {
                 if (selectedItem instanceof Connection) {
                     removeSelectedConnection(selectedItem);
                     removeSelectedConnection(selectedItem);
@@ -246,28 +253,24 @@ public abstract class Diagram extends JPanel implements MouseListener,
     }
     }
 
 
     /**
     /**
-     * Called from {@link #removeSelected() } to also disconnect all the 
-     * connections made to the node in question
-     * 
-     * @param selectedItem The item to remove
+     * Remove a Node and all it's related connections from the diagram.
+     * @param node The node to remove
      */
      */
-    private void removeSelectedNode(Selectable selectedItem) {
-        NodePanel selectedNode = (NodePanel) selectedItem;
-        nodes.remove(selectedNode);
+    public void removeNode(NodePanel node) {
+        nodes.remove(node);
         for (Iterator<Connection> it = connections.iterator(); it.hasNext();) {
         for (Iterator<Connection> it = connections.iterator(); it.hasNext();) {
             Connection conn = it.next();
             Connection conn = it.next();
-            if (conn.start.getNode() == selectedNode || conn.end.getNode() == selectedNode) {
+            if (conn.start.getNode() == node || conn.end.getNode() == node) {
                 it.remove();
                 it.remove();
-                conn.end.disconnect();
-                conn.start.disconnect();
-                remove(conn);
+                removeConnection(conn);
+                parent.notifyRemoveConnection(conn);
             }
             }
         }
         }
 
 
-        selectedNode.cleanup();
-        remove(selectedNode);
+        node.cleanup();
+        remove(node);
         repaint();
         repaint();
-        parent.notifyRemoveNode(selectedNode);
+        parent.notifyRemoveNode(node);
     }
     }
 
 
     public List<Selectable> getSelectedItems() {
     public List<Selectable> getSelectedItems() {
@@ -276,6 +279,7 @@ public abstract class Diagram extends JPanel implements MouseListener,
 
 
     /**
     /**
      * Called by {@link ConnectionEndpoint} when a Curve has been dragged
      * Called by {@link ConnectionEndpoint} when a Curve has been dragged
+     * @param e
      */
      */
     protected void draggingDot(MouseEvent e) {}
     protected void draggingDot(MouseEvent e) {}
 
 
@@ -312,8 +316,9 @@ public abstract class Diagram extends JPanel implements MouseListener,
     /**
     /**
      * Selection from the editor. Select the item and notify the topComponent
      * Selection from the editor. Select the item and notify the topComponent
      * @param selectable the item to select
      * @param selectable the item to select
+     * @param multi Whether Multi Selection is allowed
      */
      */
-    public void select(Selectable selectable, boolean multi) {
+    public void selectAndNotify(Selectable selectable, boolean multi) {
         parent.selectionChanged(doSelect(selectable, multi));
         parent.selectionChanged(doSelect(selectable, multi));
     }
     }
     
     
@@ -388,28 +393,37 @@ public abstract class Diagram extends JPanel implements MouseListener,
      * have to implement {@link #trySelect(java.lang.String) }
      * have to implement {@link #trySelect(java.lang.String) }
      *
      *
      * @param key The unique key
      * @param key The unique key
+     * @param multi Support Multi Selection?
      * @return The selected item
      * @return The selected item
      */
      */
-    public Selectable select(String key) {
+    public Selectable select(String key, boolean multi) {
         for (NodePanel nodePanel: nodes) {
         for (NodePanel nodePanel: nodes) {
             if (nodePanel.getKey().equals(key)) {
             if (nodePanel.getKey().equals(key)) {
-                return doSelect(nodePanel, false);
+                return doSelect(nodePanel, multi);
             }
             }
         }
         }
 
 
         for (Connection connection: connections) {
         for (Connection connection: connections) {
             if (connection.getKey().equals(key)) {
             if (connection.getKey().equals(key)) {
-                return doSelect(connection, false);
+                return doSelect(connection, multi);
             }
             }
         }
         }
         
         
         Selectable s = trySelect(key);
         Selectable s = trySelect(key);
         if (s != null) {
         if (s != null) {
-            return doSelect(s, false);
+            return doSelect(s, multi);
         }
         }
 
 
         return null;
         return null;
     }
     }
+    
+    public Selectable select(String key) {
+        return select(key, false);
+    }
+    
+    public Selectable select(Selectable selectable, boolean multi) {
+        return doSelect(selectable, multi);
+    }
 
 
     public void clear() {
     public void clear() {
         removeAll();
         removeAll();
@@ -462,10 +476,20 @@ public abstract class Diagram extends JPanel implements MouseListener,
     }
     }
 
 
     private void removeConnection(Connection selectedConnection) {
     private void removeConnection(Connection selectedConnection) {
+        ConnectionEndpoint start = selectedConnection.getStart();
+        ConnectionEndpoint end = selectedConnection.getEnd();
+        
         connections.remove(selectedConnection);
         connections.remove(selectedConnection);
-        selectedConnection.end.disconnect();
-        selectedConnection.start.disconnect();
+        end.disconnect();
+        start.disconnect();
         remove(selectedConnection);
         remove(selectedConnection);
+        
+        if (start.getNode() instanceof NodePanel) {
+            ((NodePanel)start.getNode()).onDisconnect(selectedConnection);
+        }
+        if (end.getNode() instanceof NodePanel) {
+            ((NodePanel)end.getNode()).onDisconnect(selectedConnection);
+        }
     }
     }
 
 
     /**
     /**

+ 19 - 2
jme3-core/src/com/jme3/gde/core/editor/nodes/DraggablePanel.java

@@ -31,6 +31,7 @@
  */
  */
 package com.jme3.gde.core.editor.nodes;
 package com.jme3.gde.core.editor.nodes;
 
 
+import java.awt.Dimension;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.awt.event.MouseMotionListener;
@@ -47,7 +48,7 @@ import javax.swing.SwingUtilities;
  */
  */
 public class DraggablePanel extends JPanel implements MouseListener, MouseMotionListener {
 public class DraggablePanel extends JPanel implements MouseListener, MouseMotionListener {
     protected int svdx, svdy, svdex, svdey;
     protected int svdx, svdy, svdex, svdey;
-    private boolean vertical = false;
+    protected boolean vertical = false;
     protected Diagram diagram;
     protected Diagram diagram;
 
 
     public DraggablePanel(boolean vertical) {
     public DraggablePanel(boolean vertical) {
@@ -56,6 +57,7 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion
     }
     }
 
 
     public DraggablePanel() {
     public DraggablePanel() {
+        super();
         addMouseListener(this);
         addMouseListener(this);
         addMouseMotionListener(this);
         addMouseMotionListener(this);
     }
     }
@@ -107,13 +109,28 @@ public class DraggablePanel extends JPanel implements MouseListener, MouseMotion
             diagram.multiMove(this, xoffset, yoffset);
             diagram.multiMove(this, xoffset, yoffset);
             e.consume();
             e.consume();
         }
         }
+       
     }
     }
 
 
     protected void movePanel(int xoffset, int yoffset) {
     protected void movePanel(int xoffset, int yoffset) {
         if (vertical) {
         if (vertical) {
             xoffset = 0;
             xoffset = 0;
         }
         }
-        setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset));
+        if (getParent() != null) {
+            Dimension parentSize = getParent().getSize();
+            Dimension thisSize = getSize();
+            
+            // Support clamping to all dimensions
+            if (parentSize.height > 0 && parentSize.width > 0 && thisSize.height > 0 && thisSize.width > 0) {
+                setLocation(Math.max(0, Math.min(svdx + xoffset, parentSize.width - thisSize.width)), Math.max(0, Math.min(svdy + yoffset, parentSize.height - thisSize.height)));
+            } else {
+                // Only supports clamping to the left upper bound
+                setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset));
+            }
+        } else {
+            // Only supports clamping to the left upper bound
+            setLocation(Math.max(0, svdx + xoffset), Math.max(0, svdy + yoffset));
+        }
     }
     }
 
 
     /** 
     /** 

+ 44 - 9
jme3-core/src/com/jme3/gde/core/editor/nodes/NodePanel.java

@@ -46,6 +46,7 @@ import java.awt.event.MouseEvent;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import javax.swing.GroupLayout;
 import javax.swing.GroupLayout;
+import javax.swing.Icon;
 import javax.swing.JLabel;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JPanel;
 import javax.swing.LayoutStyle;
 import javax.swing.LayoutStyle;
@@ -61,9 +62,10 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
     protected List<JLabel> outputLabels = new ArrayList<JLabel>();
     protected List<JLabel> outputLabels = new ArrayList<JLabel>();
     protected List<ConnectionEndpoint> inputDots = new ArrayList<ConnectionEndpoint>();
     protected List<ConnectionEndpoint> inputDots = new ArrayList<ConnectionEndpoint>();
     protected List<ConnectionEndpoint> outputDots = new ArrayList<ConnectionEndpoint>();
     protected List<ConnectionEndpoint> outputDots = new ArrayList<ConnectionEndpoint>();
-    private JPanel content;
+    protected JPanel content;
     protected JLabel header;
     protected JLabel header;
     protected Color color;
     protected Color color;
+    protected Color backgroundColor = new Color(170, 170, 170, 120);
     protected String name;
     protected String name;
     protected NodeToolBar toolBar = null;
     protected NodeToolBar toolBar = null;
 
 
@@ -142,6 +144,24 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
         return label;
         return label;
     }
     }
     
     
+    /**
+     * This is called <b>after</b> a connection has been made in the editor.<br>
+     * This is because some nodes want to change their status when that happens
+     * (e.g. add another connection endpoint to use).
+     * @param conn The connection
+     * @see #onDisconnect(com.jme3.gde.core.editor.nodes.Connection) 
+     */
+    protected void onConnect(Connection conn) {}
+    
+    /**
+     * This is called <b>after</b> a connection has been disconnected in the editor.<br>
+     * This is because some nodes want to change their status when that happens
+     * (e.g. clean up connection endpoints).
+     * @param conn The connection
+     * @see #onConnect(com.jme3.gde.core.editor.nodes.Connection)
+     */
+    protected void onDisconnect(Connection conn) {}
+    
     @Override
     @Override
     public String getName() {
     public String getName() {
         return name;
         return name;
@@ -201,20 +221,32 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
     @SuppressWarnings("unchecked")
     @SuppressWarnings("unchecked")
     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
     protected void initComponents() {
     protected void initComponents() {
+        String oldHeaderText = null;
+        Icon oldHeaderIcon = null;
+        
+        if (getLayout() instanceof GroupLayout) {
+            removeAll();
+            oldHeaderText = header.getText();
+            oldHeaderIcon = header.getIcon();
+        }
+        
         header = new JLabel(Icons.vert);
         header = new JLabel(Icons.vert);
         header.setForeground(Color.BLACK);
         header.setForeground(Color.BLACK);
         header.addMouseListener(labelMouseMotionListener);
         header.addMouseListener(labelMouseMotionListener);
         header.addMouseMotionListener(labelMouseMotionListener);
         header.addMouseMotionListener(labelMouseMotionListener);
         header.setHorizontalAlignment(SwingConstants.LEFT);
         header.setHorizontalAlignment(SwingConstants.LEFT);
         header.setFont(new Font("Tahoma", Font.BOLD, 11));
         header.setFont(new Font("Tahoma", Font.BOLD, 11));
+        
+        if (getLayout() instanceof GroupLayout) {
+            header.setText(oldHeaderText);
+            header.setIcon(oldHeaderIcon);
+        }
 
 
         content = new JPanel();
         content = new JPanel();
         content.setOpaque(false);
         content.setOpaque(false);
         GroupLayout contentLayout = new GroupLayout(content);
         GroupLayout contentLayout = new GroupLayout(content);
         content.setLayout(contentLayout);
         content.setLayout(contentLayout);
-
         int txtLength = 100;
         int txtLength = 100;
-
         GroupLayout.ParallelGroup grpHoriz = contentLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
         GroupLayout.ParallelGroup grpHoriz = contentLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
 
 
         for (int i = 0; i < outputDots.size(); i++) {
         for (int i = 0; i < outputDots.size(); i++) {
@@ -257,7 +289,7 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
                 layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                 layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                 .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                 .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                         .addGap(6, 6, 6)
                         .addGap(6, 6, 6)
-                        .addComponent(header, 100, 100, 100))
+                        .addComponent(header, 100, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE))
                 .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                 .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                         .addGap(6, 6, 6))
                         .addGap(6, 6, 6))
                 .addComponent(content, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
                 .addComponent(content, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
@@ -296,7 +328,7 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
             }
             }
         }
         }
 
 
-        g.setColor(new Color(170, 170, 170, 120));
+        g.setColor(backgroundColor);
         g.fillRoundRect(5, 1, getWidth() - 9, getHeight() - 6, 15, 15);
         g.fillRoundRect(5, 1, getWidth() - 9, getHeight() - 6, 15, 15);
         g.setColor(borderColor);
         g.setColor(borderColor);
 
 
@@ -306,16 +338,19 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
         g.setColor(borderColor);
         g.setColor(borderColor);
         g.drawLine(4, 0, 14, 0);
         g.drawLine(4, 0, 14, 0);
         g.drawLine(4, 0, 4, 10);
         g.drawLine(4, 0, 4, 10);
+        paintTitleBar(g);
+    }
+    
+    protected void paintTitleBar(Graphics2D g) {
         g.setColor(Color.BLACK);
         g.setColor(Color.BLACK);
         g.drawLine(5, 15, getWidth() - 6, 15);
         g.drawLine(5, 15, getWidth() - 6, 15);
         g.setColor(new Color(190, 190, 190));
         g.setColor(new Color(190, 190, 190));
         g.drawLine(5, 16, getWidth() - 6, 16);
         g.drawLine(5, 16, getWidth() - 6, 16);
-
+        
         Color c1 = new Color(color.getRed(), color.getGreen(), color.getBlue(), 150);
         Color c1 = new Color(color.getRed(), color.getGreen(), color.getBlue(), 150);
         Color c2 = new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
         Color c2 = new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
         g.setPaint(new GradientPaint(0, 15, c1, getWidth(), 15, c2));
         g.setPaint(new GradientPaint(0, 15, c1, getWidth(), 15, c2));
         g.fillRect(5, 1, getWidth() - 10, 14);
         g.fillRect(5, 1, getWidth() - 10, 14);
-
     }
     }
 
 
     public abstract ConnectionEndpoint createConnectionEndpoint(String type, 
     public abstract ConnectionEndpoint createConnectionEndpoint(String type, 
@@ -341,9 +376,9 @@ public abstract class NodePanel extends DraggablePanel implements Selectable, Ke
     }
     }
     
     
 // used to pass press and drag events to the NodePanel when they occur on the label
 // used to pass press and drag events to the NodePanel when they occur on the label
-    private LabelMouseMotionListener labelMouseMotionListener = new LabelMouseMotionListener();
+    protected final LabelMouseMotionListener labelMouseMotionListener = new LabelMouseMotionListener();
 
 
-    private class LabelMouseMotionListener extends MouseAdapter {
+    protected class LabelMouseMotionListener extends MouseAdapter {
         @Override
         @Override
         public void mousePressed(MouseEvent e) {
         public void mousePressed(MouseEvent e) {
             MouseEvent me = SwingUtilities.convertMouseEvent(e.getComponent(), e, NodePanel.this);
             MouseEvent me = SwingUtilities.convertMouseEvent(e.getComponent(), e, NodePanel.this);