Skip to content
Snippets Groups Projects
Commit 74c5e2d9 authored by Stephane Dallongeville's avatar Stephane Dallongeville
Browse files

fixed possible NPE + indentation

parent a99f427c
No related branches found
No related tags found
No related merge requests found
package plugins.adufour.protocols.gui.link; package plugins.adufour.protocols.gui.link;
import icy.resource.ResourceUtil;
import icy.sequence.Sequence;
import java.awt.Color; import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics2D; import java.awt.Graphics2D;
...@@ -16,6 +13,8 @@ import javax.swing.ImageIcon; ...@@ -16,6 +13,8 @@ import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.TransferHandler; import javax.swing.TransferHandler;
import icy.resource.ResourceUtil;
import icy.sequence.Sequence;
import plugins.adufour.blocks.lang.BlockDescriptor; import plugins.adufour.blocks.lang.BlockDescriptor;
import plugins.adufour.blocks.lang.WorkFlow; import plugins.adufour.blocks.lang.WorkFlow;
import plugins.adufour.vars.lang.Var; import plugins.adufour.vars.lang.Var;
...@@ -27,88 +26,93 @@ import plugins.adufour.vars.util.VarListener; ...@@ -27,88 +26,93 @@ import plugins.adufour.vars.util.VarListener;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public abstract class DragDropZone extends JButton implements VarListener<Boolean>, TypeChangeListener public abstract class DragDropZone extends JButton implements VarListener<Boolean>, TypeChangeListener
{ {
protected static final int DRAGnDROP_ACTION = TransferHandler.LINK; protected static final int DRAGnDROP_ACTION = TransferHandler.LINK;
public static final int DEFAULT_ICON_SIZE = 13; public static final int DEFAULT_ICON_SIZE = 13;
public static final Image LINK_RIGHT = ResourceUtil.getAlphaIconAsImage("round_arrow_right.png"); public static final Image LINK_RIGHT = ResourceUtil.getAlphaIconAsImage("round_arrow_right.png");
public static final Image LINK_LEFT = ResourceUtil.getAlphaIconAsImage("round_arrow_left.png"); public static final Image LINK_LEFT = ResourceUtil.getAlphaIconAsImage("round_arrow_left.png");
public static final Image PIN_LEFT = ResourceUtil.getAlphaIconAsImage("pin_map_left.png"); public static final Image PIN_LEFT = ResourceUtil.getAlphaIconAsImage("pin_map_left.png");
public static final Image PIN_RIGHT = ResourceUtil.getAlphaIconAsImage("pin_map_right.png"); public static final Image PIN_RIGHT = ResourceUtil.getAlphaIconAsImage("pin_map_right.png");
public static final Image BUTTON = ResourceUtil.getAlphaIconAsImage("round.png"); public static final Image BUTTON = ResourceUtil.getAlphaIconAsImage("round.png");
private final Var<?> variable; private final Var<?> variable;
private int iconSize = DEFAULT_ICON_SIZE; private int iconSize = DEFAULT_ICON_SIZE;
private Icon defaultIcon; private Icon defaultIcon;
private final Icon errorIcon; private final Icon errorIcon;
private final Icon emptyIcon = new ImageIcon(ResourceUtil.ICON_NULL); private final Icon emptyIcon = new ImageIcon(ResourceUtil.ICON_NULL);
/** /**
* Creates a new drop zone for the given variable * Creates a new drop zone for the given variable
* *
* @param flowPane * @param flowPane
* the flow pane where variable links will come from * the flow pane where variable links will come from
* @param block * @param block
* the box that owns the specified input * the box that owns the specified input
* @param input * @param input
* the input variable to link to * the input variable to link to
* @return * @return
*/ */
public static <T> DragDropZone createDropZone(Image image, WorkFlow workFlow, BlockDescriptor block, Var<T> input) public static <T> DragDropZone createDropZone(Image image, WorkFlow workFlow, BlockDescriptor block, Var<T> input)
{ {
return new DropZone<T>(image, workFlow, block, input); return new DropZone<T>(image, workFlow, block, input);
} }
/** /**
* Creates a new drag zone for the given variable * Creates a new drag zone for the given variable
* *
* @param workFlow * @param workFlow
* the work flow where variable links will go to * the work flow where variable links will go to
* @param block * @param block
* the box that owns the specified output * the box that owns the specified output
* @param variable * @param variable
* the input variable to link * the input variable to link
* @return * @return
*/ */
public static <T> DragDropZone createDragZone(Image image, WorkFlow workFlow, BlockDescriptor block, Var<T> variable) public static <T> DragDropZone createDragZone(Image image, WorkFlow workFlow, BlockDescriptor block,
Var<T> variable)
{ {
return new DragZone<T>(image, workFlow, block, variable); return new DragZone<T>(image, workFlow, block, variable);
} }
protected DragDropZone(String toolTipText, Image image, Var<?> variable) protected DragDropZone(String toolTipText, Image image, Var<?> variable)
{ {
super(""); super("");
this.variable = variable; this.variable = variable;
if (variable instanceof MutableType) ((MutableType) variable).addTypeChangeListener(this); if (variable instanceof MutableType)
((MutableType) variable).addTypeChangeListener(this);
setToolTipText("<html><h4>" + toolTipText.replace("\n", "<br/>") + "</h4></html>"); setToolTipText("<html><h4>" + toolTipText.replace("\n", "<br/>") + "</h4></html>");
setDefaultLinkIcon(); setDefaultLinkIcon();
setContentAreaFilled(false); setContentAreaFilled(false);
setFocusable(false); setFocusable(false);
setBorderPainted(false); setBorderPainted(false);
errorIcon = getLinkIcon(Color.red); errorIcon = getLinkIcon(Color.red);
updateDefaultIcon(); updateDefaultIcon();
setDefaultLinkIcon(); setDefaultLinkIcon();
setPreferredSize(new Dimension(iconSize, iconSize)); setPreferredSize(new Dimension(iconSize, iconSize));
setMinimumSize(getPreferredSize()); setMinimumSize(getPreferredSize());
} }
private void updateDefaultIcon() private void updateDefaultIcon()
{ {
Class<?> dataType = variable.getType(); Class<?> dataType = null;
if (variable != null)
dataType = variable.getType();
if (dataType != null) if (dataType != null)
{ {
if (variable instanceof VarGenericArray) if (variable instanceof VarGenericArray)
...@@ -121,21 +125,21 @@ public abstract class DragDropZone extends JButton implements VarListener<Boolea ...@@ -121,21 +125,21 @@ public abstract class DragDropZone extends JButton implements VarListener<Boolea
dataType = dataType.getComponentType(); dataType = dataType.getComponentType();
} }
} }
defaultIcon = getLinkIcon(getColor(dataType)); defaultIcon = getLinkIcon(getColor(dataType));
} }
public Icon getLinkIcon(Color color) public Icon getLinkIcon(Color color)
{ {
Image myImage = new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_INT_ARGB); Image myImage = new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) myImage.getGraphics(); Graphics2D g = (Graphics2D) myImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// draw the colored background // draw the colored background
g.setColor(color); g.setColor(color);
g.fillOval(0, 0, iconSize, iconSize); g.fillOval(0, 0, iconSize, iconSize);
// draw the white arrow // draw the white arrow
g.setColor(Color.white); g.setColor(Color.white);
Path2D.Float arrow = new Path2D.Float(); Path2D.Float arrow = new Path2D.Float();
...@@ -150,62 +154,68 @@ public abstract class DragDropZone extends JButton implements VarListener<Boolea ...@@ -150,62 +154,68 @@ public abstract class DragDropZone extends JButton implements VarListener<Boolea
g.fill(arrow); g.fill(arrow);
return new ImageIcon(myImage); return new ImageIcon(myImage);
} }
public void setEmptyIcon() public void setEmptyIcon()
{ {
setIcon(emptyIcon); setIcon(emptyIcon);
} }
public void setDefaultLinkIcon() public void setDefaultLinkIcon()
{ {
setIcon(defaultIcon); setIcon(defaultIcon);
} }
public void setErrorLinkIcon() public void setErrorLinkIcon()
{ {
setIcon(errorIcon); setIcon(errorIcon);
} }
public void setIconSize(int size) public void setIconSize(int size)
{ {
// this.iconSize = size; // this.iconSize = size;
setDefaultLinkIcon(); setDefaultLinkIcon();
} }
public void dispose() public void dispose()
{ {
if (variable instanceof MutableType) ((MutableType) variable).removeTypeChangeListener(this); if (variable instanceof MutableType)
((MutableType) variable).removeTypeChangeListener(this);
} }
@Override @Override
public void valueChanged(Var<Boolean> source, Boolean oldValue, Boolean newValue) public void valueChanged(Var<Boolean> source, Boolean oldValue, Boolean newValue)
{ {
setDefaultLinkIcon(); setDefaultLinkIcon();
} }
@Override @Override
public void referenceChanged(Var<Boolean> source, Var<? extends Boolean> oldReference, Var<? extends Boolean> newReference) public void referenceChanged(Var<Boolean> source, Var<? extends Boolean> oldReference,
Var<? extends Boolean> newReference)
{ {
setDefaultLinkIcon(); setDefaultLinkIcon();
} }
@Override @Override
public void typeChanged(Object source, Class<?> oldType, Class<?> newType) public void typeChanged(Object source, Class<?> oldType, Class<?> newType)
{ {
updateDefaultIcon(); updateDefaultIcon();
setDefaultLinkIcon(); setDefaultLinkIcon();
} }
public static Color getColor(Class<?> type) public static Color getColor(Class<?> type)
{ {
if (type == null) return Color.gray; if (type == null)
return Color.gray;
if (type == Sequence.class) return Color.blue;
if (type == Sequence.class)
if (type == Boolean.class || type == Boolean.TYPE) return Color.orange.darker().darker(); return Color.blue;
if (type.isPrimitive() || Number.class.isAssignableFrom(type)) return Color.magenta.darker().darker(); if (type == Boolean.class || type == Boolean.TYPE)
return Color.orange.darker().darker();
if (type.isPrimitive() || Number.class.isAssignableFrom(type))
return Color.magenta.darker().darker();
return Color.black; return Color.black;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment