Skip to content

Commit

Permalink
UI: highlight edges based on current selection
Browse files Browse the repository at this point in the history
  • Loading branch information
frothga committed Dec 21, 2023
1 parent 9b804c5 commit 82653c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
25 changes: 20 additions & 5 deletions N2A/src/gov/sandia/n2a/ui/eq/GraphEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
Expand Down Expand Up @@ -74,11 +73,24 @@ public class GraphEdge
protected static float strokeThicknessScaled;
protected static int padNameBetweenScaled; // px

// Color
protected static Color colorForeground; // Normal color of lines and text
protected static Color colorActive; // Color of edges touching the lead selection
protected static Color colorDrag; // Color of edge that is either currently being dragged or would be if drag started now.

static
{
rescale (1);
}

public static void updateUI ()
{
// TODO: base these on canvas background color
colorForeground = Color.black;
colorActive = Color.blue;
colorDrag = new Color (0, 0xC0, 0);
}

public GraphEdge (GraphNode nodeFrom, NodePart partTo, String alias)
{
this.nodeFrom = nodeFrom;
Expand Down Expand Up @@ -701,22 +713,25 @@ Not a true paintComponent(), but rather a subroutine of PanelEquationGraph.paint
g gets disposed by the caller immediately after all GraphEdges are done drawing, so any
changes we make to g are safe.
**/
public void paintComponent (Graphics g)
public void paintComponent (Graphics2D g2)
{
Graphics2D g2 = (Graphics2D) g;
Color color = colorForeground;
if (nodeFrom.parent.focus == nodeFrom || nodeFrom.parent.focus == nodeTo) color = colorActive;
if (nodeFrom.parent.likelyTip == this) color = colorDrag;

g2.setColor (Color.black);
g2.setColor (color);
if (line != null) g2.draw (line);
if (head != null)
{
if (! headFill) g2.setColor (PanelEquationGraph.background);
g2.fill (head);
g2.setColor (Color.black);
g2.setColor (color);
g2.draw (head);
}

if (! text.isEmpty () && label != null)
{
// TODO: base matte and text foreground color on canvas background
g2.setColor (new Color (0xD0FFFFFF, true));
g2.fill (textBox);
g2.setColor (Color.black);
Expand Down
21 changes: 21 additions & 0 deletions N2A/src/gov/sandia/n2a/ui/eq/GraphNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,14 @@ public void updateEdge (String alias, NodePart partTo, boolean kill)
parent.repaint (paintRegion);
}

public void repaintEdges ()
{
Rectangle paintRegion = new Rectangle (0, 0, -1, -1);
for (GraphEdge ge : edgesIn ) paintRegion = paintRegion.union (ge.bounds);
for (GraphEdge ge : edgesOut) paintRegion = paintRegion.union (ge.bounds);
parent.repaint (paintRegion);
}

/**
Sets bounds to current preferred location & size. Updates everything affected by the change.
**/
Expand Down Expand Up @@ -1265,6 +1273,13 @@ public void focusGained (FocusEvent e)
getTreeCellRendererComponent (getEquationTree ().tree, node, true, open, false, -2, true);
restoreFocus (); // does repaint
container.updateHighlights (node);

if (parent.focus != GraphNode.this)
{
repaintEdges ();
if (parent.focus != null) parent.focus.repaintEdges ();
parent.focus = GraphNode.this;
}
}

public void focusLost (FocusEvent e)
Expand All @@ -1278,6 +1293,12 @@ public void focusLost (FocusEvent e)

getTreeCellRendererComponent (getEquationTree ().tree, node, GraphNode.this.selected, open, false, -2, false);
GraphNode.this.repaint ();

if (parent.focus == GraphNode.this)
{
parent.focus = null;
repaintEdges ();
}
}
});
}
Expand Down
13 changes: 12 additions & 1 deletion N2A/src/gov/sandia/n2a/ui/eq/PanelEquationGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class PanelEquationGraph extends JScrollPane
protected ColoredBorder border;
protected NodeBase lastHighlightTarget;

protected static Color background = new Color (0xF0F0F0); // light gray
protected static Color background;

public PanelEquationGraph (PanelEquations container)
{
Expand Down Expand Up @@ -367,6 +367,8 @@ public class GraphPanel extends JPanel
protected GraphMouseListener mouseListener;
protected List<GraphEdge> edges = new ArrayList<GraphEdge> (); // Note that GraphNodes are stored directly as Swing components.
public Point offset = new Point (); // Offset from persistent coordinates to viewport coordinates. Add this to a stored (x,y) value to get non-negative coordinates that can be painted.
protected GraphNode focus; // Set by paint() for use by GraphNode.paintComponent()
protected GraphEdge likelyTip; // ditto
protected JPopupMenu arrowMenu;
protected GraphEdge arrowEdge; // Most recent edge when arrowMenu was activated.
protected Point popupLocation;
Expand Down Expand Up @@ -451,6 +453,7 @@ public void updateUI ()
if (zoom == 0) zoom = 1; // Workaround. Superclass calls updateUI() before our constructor runs.
scaleFonts ();
if (layout != null) layout.UIupdated = true;
GraphEdge.updateUI ();
}

public void scaleFonts ()
Expand Down Expand Up @@ -1754,9 +1757,17 @@ public void mouseWheelMoved (MouseWheelEvent me)
public void mouseMoved (MouseEvent me)
{
if (container.locked) return;

GraphEdge e = graphPanel.findTipAt (me.getPoint ());
if (e == null) setCursor (Cursor.getDefaultCursor ());
else setCursor (Cursor.getPredefinedCursor (Cursor.MOVE_CURSOR));

GraphEdge f = graphPanel.likelyTip;
if (f == e) return;
graphPanel.likelyTip = e;

if (e != null) graphPanel.repaint (e.bounds);
if (f != null) graphPanel.repaint (f.bounds);
}

public void mousePressed (MouseEvent me)
Expand Down

0 comments on commit 82653c3

Please sign in to comment.