samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util.heavy; samer@0: import samer.core.*; samer@0: import samer.core.util.Tools; samer@0: import samer.core.util.MouseRetarget; samer@0: import samer.core.util.swing.MenuBuilder; samer@0: import samer.core.util.swing.DynamicPopupHandler; samer@0: import java.awt.*; samer@0: import java.awt.event.*; samer@0: samer@0: /** samer@0:

samer@0: This is a Canvas which maintains a valid Graphics samer@0: object. The Graphics associated with the Canvas samer@0: becomes available only at a certain time (not samer@0: during construction) so we have to setup the samer@0: Graphics in response to some event. It seems that the samer@0: only event a component reliably gets after being samer@0: realized is a size event. We use the first size samer@0: event to get a Graphics and notify any derived samer@0: classes through the realized() samer@0: method which is there to be overridden. samer@0: samer@0: */ samer@0: samer@0: public class VCanvas extends Canvas implements Viewer samer@0: { samer@0: public Graphics graphics=null; samer@0: public int width, height; samer@0: samer@0: /** void realized() samer@0: * This is called when the cached Graphics object samer@0: * (see GCanvas) is created. The component is probably still samer@0: * invisible at this point, but we can get off-screen samer@0: * Images and do other useful setting up things that samer@0: * don't seem to work in the constructor samer@0: * samer@0: */ samer@0: protected void realized() {} samer@0: protected void sized() {} samer@0: samer@0: public void clear() { clear(graphics); } samer@0: public void clear(Graphics g) { samer@0: g.setColor(getBackground()); samer@0: g.fillRect(0,0,width,height); samer@0: } samer@0: samer@0: private void cacheGraphics() samer@0: { samer@0: if (graphics!=null) graphics.dispose(); samer@0: graphics=Tools.initGraphics(getGraphics()); samer@0: } samer@0: samer@0: public void removeNotify() samer@0: { samer@0: detach(); samer@0: // graphics=null; // this might cause a problem! synchronization? samer@0: super.removeNotify(); samer@0: } samer@0: samer@0: public void addNotify() samer@0: { samer@0: super.addNotify(); samer@0: Dimension d = getSize(); samer@0: width=d.width; height=d.height; samer@0: cacheGraphics(); samer@0: attach(); realized(); // ?? samer@0: } samer@0: samer@0: public Dimension getMinimumSize() { samer@0: return new Dimension(0,0); samer@0: } samer@0: samer@0: { // initialisation code samer@0: addMouseListener(MouseRetarget.listener); samer@0: addComponentListener( new ComponentAdapter() { samer@0: public void componentResized(ComponentEvent e) { samer@0: Dimension d = getSize(); samer@0: width=d.width; height=d.height; samer@0: cacheGraphics(); samer@0: sized(); samer@0: } samer@0: } ); samer@0: samer@0: setBackground(Shell.getColor("background",null)); samer@0: setForeground(Shell.getColor("foreground",null)); samer@0: } samer@0: samer@0: /* this is v cunning (actually - not really) */ samer@0: public Image createPixel() { return createPixel(1,1); } samer@0: public Image createPixel(Color c) { return createPixel(c,1,1); } samer@0: public Image createPixel(int w, int h) { return createPixel(getForeground(),w,h); } samer@0: public Image createPixel(Color c, int w, int h) samer@0: { samer@0: Image img=createImage(w,h); samer@0: Graphics g=img.getGraphics(); samer@0: g.setColor(c); samer@0: g.fillRect(0,0,w,w); samer@0: g.dispose(); samer@0: return img; samer@0: } samer@0: samer@0: // ....... MenuAnchor bits ....................... samer@0: samer@0: private DynamicPopupHandler thing=null; samer@0: // private JPopupMenu thing=null; samer@0: samer@0: public Component getComponent() { return this; } samer@0: public void attach() {} samer@0: public void detach() {} samer@0: public void exposeCommands(Agent agent) { samer@0: // menu=MenuBuilder.showCommands(agent,getComponent(),menu); samer@0: thing=MenuBuilder.showCommands(agent,getComponent(),thing); samer@0: } samer@0: samer@0: protected void finalize() { samer@0: if (graphics!=null) graphics.dispose(); samer@0: } samer@0: samer@0: } samer@0: samer@0: