diff src/samer/core_/util/heavy/VCanvas.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/core_/util/heavy/VCanvas.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,130 @@
+/*
+ *	Copyright (c) 2000, Samer Abdallah, King's College London.
+ *	All rights reserved.
+ *
+ *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
+ *	without even the implied warranty of MERCHANTABILITY or 
+ *	FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+package samer.core.util.heavy;
+import  samer.core.*;
+import  samer.core.util.Tools;
+import  samer.core.util.MouseRetarget;
+import  samer.core.util.swing.MenuBuilder;
+import  samer.core.util.swing.DynamicPopupHandler;
+import  java.awt.*;
+import  java.awt.event.*;
+
+/**
+		<p>
+		This is a Canvas which maintains a valid Graphics
+		object. The Graphics associated with the Canvas
+		becomes available only at a certain time (not
+		during construction) so we have to setup the
+		Graphics in response to some event. It seems that the 
+		only event a component reliably gets after being
+		realized is a size event. We use the first size
+		event to get a Graphics and notify any derived
+		classes through the <code>realized()</code>
+		method which is there to be overridden.
+	
+ */
+
+public class VCanvas extends Canvas implements Viewer
+{
+	public Graphics graphics=null;
+	public int		 width, height;
+
+	/**	<b><code>void realized()</code></b>
+	  *	This is called when the cached Graphics object
+	  *	(see GCanvas) is created. The component is probably still
+	  *	invisible at this point, but we can get off-screen
+	  *	Images and do other useful setting up things that
+	  *	don't seem to work in the constructor
+	  *
+	  */
+	protected void realized() {}
+	protected void sized() {}
+
+	public void clear() { clear(graphics); }
+	public void clear(Graphics g) {
+		g.setColor(getBackground());
+		g.fillRect(0,0,width,height);
+	}
+
+	private void cacheGraphics()
+	{
+		if (graphics!=null) graphics.dispose();
+		graphics=Tools.initGraphics(getGraphics());
+	}
+	
+	public void removeNotify()
+	{
+		detach();
+		// graphics=null; // this might cause a problem! synchronization?
+		super.removeNotify();
+	}
+
+	public void addNotify()
+	{
+		super.addNotify();
+		Dimension d = getSize();
+		width=d.width; height=d.height;
+		cacheGraphics();
+		attach(); realized(); // ??
+	}
+
+	public Dimension getMinimumSize() {
+		return new Dimension(0,0);
+	}
+
+	{	// initialisation code
+		addMouseListener(MouseRetarget.listener);
+		addComponentListener( new ComponentAdapter() {
+			public void componentResized(ComponentEvent e) {
+				Dimension d = getSize();
+				width=d.width; height=d.height;
+				cacheGraphics();
+				sized();
+			}
+		} );
+
+		setBackground(Shell.getColor("background",null));
+		setForeground(Shell.getColor("foreground",null));
+	}
+
+	/* this is v cunning (actually - not really) */
+	public Image createPixel() { return createPixel(1,1); }
+	public Image createPixel(Color c) { return createPixel(c,1,1); }
+	public Image createPixel(int w, int h) { return createPixel(getForeground(),w,h); }
+	public Image createPixel(Color c, int w, int h)
+	{
+		Image img=createImage(w,h);
+		Graphics g=img.getGraphics();
+		g.setColor(c);
+		g.fillRect(0,0,w,w);
+		g.dispose();
+		return img;
+	}
+
+	// ....... MenuAnchor bits .......................
+
+	private DynamicPopupHandler thing=null;
+	// private JPopupMenu thing=null;
+
+	public Component	getComponent() { return this; }
+	public void			attach() {}
+	public void			detach() {}
+	public void	exposeCommands(Agent agent) {
+		// menu=MenuBuilder.showCommands(agent,getComponent(),menu);
+		thing=MenuBuilder.showCommands(agent,getComponent(),thing);
+	}
+
+	protected void finalize() {
+		if (graphics!=null) graphics.dispose();
+	}
+
+}
+
+