package com.onaro.sanscreen.client.view.topology; import ilog.views.IlvManagerView; import ilog.views.IlvManagerViewDecoration; import ilog.views.swing.IlvJScrollManagerView; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.concurrent.atomic.AtomicBoolean; import com.onaro.sanscreen.client.prefs.GeneralPreferenceManager; /** * Implements a "watermark" in each of the corners of the topology view (formation of 4).
* * It's possible to set a number of options at run-time that affect the visualization of this watermark. * Because there is no requirement yet only the watermark's text has been implemented to take changes * dynamically by using SANscreen's settings. * *

* The following static parameters could in the "future" be modified at run-time. *

* * @author cohenj * @since 8/14/2009 - Everest * */ public class TopologyWatermark implements IlvManagerViewDecoration, PropertyChangeListener { /** Margin value used to provide a space around. * (Future improvement, make this dynamic by the User.) */ private static final int MARGIN = 7; /** Watermak's font size. * (Future improvement, make this dynamic by the User.) */ private static final int FONT_SIZE = 40; /** Opaqueness of the text displayed in the watermark. * (Future improvement, make this dynamic by the User.) */ private static final AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f); /** Watermark's font type. * (Future improvement, make this dynamic by the User.) */ private static final Font font = new Font(Font.SANS_SERIF, Font.BOLD, FONT_SIZE); /** Holds the matermark's text going to be displayed. * (Future improvement, make this dynamic by the User.) */ private volatile String watermark; /** A panel containing an two scrollbars.*/ private IlvJScrollManagerView netGraphScrollManager; /** Thread safe, If the watermark is enabled then this var holds TRUE, otherwise FALSE */ private AtomicBoolean isWatermarkEnabled = new AtomicBoolean(false); /** Encapsulates information about the rendering of a particular font. */ private FontMetrics fm; /** * Constructor. * * @param netGraphScrollManager ILog scroll manager. */ public TopologyWatermark(IlvJScrollManagerView netGraphScrollManager) { this.netGraphScrollManager = netGraphScrollManager; GeneralPreferenceManager.getInstance().addPropertyChangeListener(this); watermark = GeneralPreferenceManager.getInstance().getTopologyWatermarkText(); isWatermarkEnabled.set(GeneralPreferenceManager.getInstance().isTopologyWatermarkEnabled()); } /** * Logic that take place during the rendering of the netGraphScrollManager */ public void paint(final Graphics g, final IlvManagerView ilvManagerView) { if (!isWatermarkEnabled.get()) return; Graphics2D g2 = (Graphics2D) g; // Setting for the: geometry, coordinate transformations, color management, and text layout. g2.setComposite(alpha); g2.setColor(Color.BLACK); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setFont(font); fm = g.getFontMetrics(); // useful measurements. int height = netGraphScrollManager.getHeight(); int width = netGraphScrollManager.getWidth(); int fontHeight = fm.getHeight(); int stringWidth = fm.stringWidth(watermark); // top-left corner g.drawString(watermark, MARGIN, fontHeight - MARGIN); // bottom-left corner g.drawString(watermark, MARGIN, height - fontHeight + MARGIN); // top-right corner g.drawString(watermark, width - stringWidth - 4 * MARGIN, fontHeight - MARGIN); // bottom-left corner g.drawString(watermark, width - stringWidth - 4 * MARGIN, height - fontHeight + MARGIN); } /** * Receive notifications after the "publisher" fires the change events. * @see {@link GeneralPreferenceManager#setTopologyWatermarkEnabled} */ public void propertyChange(PropertyChangeEvent evt) { final String propName = evt.getPropertyName(); if (GeneralPreferenceManager.SERVER_PREF_TOPOLOGY_WATERMARK_ENABLED.equals(propName)) { Boolean newValue = (Boolean) evt.getNewValue(); if (newValue != null) { isWatermarkEnabled.set(newValue); netGraphScrollManager.repaint(); } } if (GeneralPreferenceManager.SERVER_PREF_TOPOLOGY_WATERMARK_TEXT.equals(propName)) { String newValue = (String) evt.getNewValue(); if (newValue != null) { watermark = newValue; netGraphScrollManager.repaint(); } } } }