package jetbrains.mps.ide.dataFlow.presentation; /*Generated by MPS */ import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import org.jetbrains.mps.openapi.model.SNodeReference; import java.util.Set; import java.util.HashSet; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import org.apache.log4j.Level; import java.awt.event.MouseEvent; import java.awt.Component; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; public abstract class AbstractBlock implements IBlock { private static final Logger LOG = LogManager.getLogger(AbstractBlock.class); protected int myX; protected int myY; protected int myWidth; protected int myHeight; protected SNodeReference mySourceNode; private SNodeReference myRuleNodeReference; private int myPaddingX = 0; private int myPaddingY = 0; private int myCharHeight = 0; private int myStringWidth = 0; private String myCaption; private Set<IBlockListener> myBlockListeners = new HashSet<IBlockListener>(); private Set<IBlock> mySucc; public AbstractBlock(int x, int y, int width, int height, SNodeReference sourceNode, String caption, String ruleNodeReference) { this.myX = x; this.myY = y; this.myWidth = width; this.myHeight = height; this.mySourceNode = sourceNode; this.myCaption = caption; if (ruleNodeReference != null) { try { this.myRuleNodeReference = PersistenceFacade.getInstance().createNodeReference(ruleNodeReference); } catch (IllegalArgumentException e) { if (LOG.isEnabledFor(Level.ERROR)) { LOG.error("Can't find node: " + ruleNodeReference, e); } this.myRuleNodeReference = null; } } } @Override public SNodeReference getSourceNode() { return this.mySourceNode; } public SNodeReference getRuleNodeReference() { return this.myRuleNodeReference; } @Override public int getX() { return this.myX; } @Override public int getY() { return this.myY; } @Override public int getWidth() { return this.myWidth; } @Override public int getHeight() { return this.myHeight; } @Override public void addBlockListener(IBlockListener listener) { this.myBlockListeners.add(listener); } @Override public void removeBlockListener(IBlockListener listener) { this.myBlockListeners.remove(listener); } @Override public void setSucc(Set<IBlock> succ) { this.mySucc = succ; } @Override public Set<IBlock> succ() { return this.mySucc; } @Override public boolean processMousePressed(MouseEvent mEvent) { int x = mEvent.getX(); int y = mEvent.getY(); if (x >= this.myX && x <= this.myX + this.myWidth && y > this.myY && y <= this.myY + this.myHeight) { for (IBlockListener listener : this.myBlockListeners) { listener.mousePressed(mEvent, AbstractBlock.this); } return true; } else { return false; } } @Override public void relayout(Component c) { Font font = c.getFont(); FontMetrics metrics = c.getFontMetrics(font); this.myStringWidth = metrics.stringWidth(this.myCaption); this.myCharHeight = metrics.getHeight(); this.myWidth = this.myStringWidth + 2 * AbstractBlock.this.getPaddingX(metrics); this.myHeight = this.myCharHeight + 2 * AbstractBlock.this.getPaddingY(metrics); } private int getPaddingX(FontMetrics metrics) { return metrics.getHeight() / 2; } private int getPaddingY(FontMetrics metrics) { return metrics.getHeight() / 3; } @Override public void setWidth(int width) { this.myWidth = width; this.myPaddingX = (this.myWidth - this.myStringWidth) / 2; } @Override public void setHeight(int height) { this.myHeight = height; this.myPaddingY = (this.myHeight - this.myCharHeight) / 2; } @Override public void setX(int x) { this.myX = x; } @Override public void setY(int y) { this.myY = y; } protected void paintCaption(Graphics g) { g.drawString(this.myCaption, this.myX + this.myPaddingX, this.myY + this.myPaddingY + this.myCharHeight); } protected abstract void paintBlock(Graphics g); @Override public void paint(Graphics g) { AbstractBlock.this.paintBlock(g); AbstractBlock.this.paintCaption(g); } }