samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.core.util.heavy;
|
samer@0
|
11 import samer.core.*;
|
samer@0
|
12 import samer.core.util.Tools;
|
samer@0
|
13 import samer.core.util.MouseRetarget;
|
samer@0
|
14 import samer.core.util.swing.MenuBuilder;
|
samer@0
|
15 import samer.core.util.swing.DynamicPopupHandler;
|
samer@0
|
16 import java.awt.*;
|
samer@0
|
17 import java.awt.event.*;
|
samer@0
|
18
|
samer@0
|
19 /**
|
samer@0
|
20 <p>
|
samer@0
|
21 This is a Canvas which maintains a valid Graphics
|
samer@0
|
22 object. The Graphics associated with the Canvas
|
samer@0
|
23 becomes available only at a certain time (not
|
samer@0
|
24 during construction) so we have to setup the
|
samer@0
|
25 Graphics in response to some event. It seems that the
|
samer@0
|
26 only event a component reliably gets after being
|
samer@0
|
27 realized is a size event. We use the first size
|
samer@0
|
28 event to get a Graphics and notify any derived
|
samer@0
|
29 classes through the <code>realized()</code>
|
samer@0
|
30 method which is there to be overridden.
|
samer@0
|
31
|
samer@0
|
32 */
|
samer@0
|
33
|
samer@0
|
34 public class VCanvas extends Canvas implements Viewer
|
samer@0
|
35 {
|
samer@0
|
36 public Graphics graphics=null;
|
samer@0
|
37 public int width, height;
|
samer@0
|
38
|
samer@0
|
39 /** <b><code>void realized()</code></b>
|
samer@0
|
40 * This is called when the cached Graphics object
|
samer@0
|
41 * (see GCanvas) is created. The component is probably still
|
samer@0
|
42 * invisible at this point, but we can get off-screen
|
samer@0
|
43 * Images and do other useful setting up things that
|
samer@0
|
44 * don't seem to work in the constructor
|
samer@0
|
45 *
|
samer@0
|
46 */
|
samer@0
|
47 protected void realized() {}
|
samer@0
|
48 protected void sized() {}
|
samer@0
|
49
|
samer@0
|
50 public void clear() { clear(graphics); }
|
samer@0
|
51 public void clear(Graphics g) {
|
samer@0
|
52 g.setColor(getBackground());
|
samer@0
|
53 g.fillRect(0,0,width,height);
|
samer@0
|
54 }
|
samer@0
|
55
|
samer@0
|
56 private void cacheGraphics()
|
samer@0
|
57 {
|
samer@0
|
58 if (graphics!=null) graphics.dispose();
|
samer@0
|
59 graphics=Tools.initGraphics(getGraphics());
|
samer@0
|
60 }
|
samer@0
|
61
|
samer@0
|
62 public void removeNotify()
|
samer@0
|
63 {
|
samer@0
|
64 detach();
|
samer@0
|
65 // graphics=null; // this might cause a problem! synchronization?
|
samer@0
|
66 super.removeNotify();
|
samer@0
|
67 }
|
samer@0
|
68
|
samer@0
|
69 public void addNotify()
|
samer@0
|
70 {
|
samer@0
|
71 super.addNotify();
|
samer@0
|
72 Dimension d = getSize();
|
samer@0
|
73 width=d.width; height=d.height;
|
samer@0
|
74 cacheGraphics();
|
samer@0
|
75 attach(); realized(); // ??
|
samer@0
|
76 }
|
samer@0
|
77
|
samer@0
|
78 public Dimension getMinimumSize() {
|
samer@0
|
79 return new Dimension(0,0);
|
samer@0
|
80 }
|
samer@0
|
81
|
samer@0
|
82 { // initialisation code
|
samer@0
|
83 addMouseListener(MouseRetarget.listener);
|
samer@0
|
84 addComponentListener( new ComponentAdapter() {
|
samer@0
|
85 public void componentResized(ComponentEvent e) {
|
samer@0
|
86 Dimension d = getSize();
|
samer@0
|
87 width=d.width; height=d.height;
|
samer@0
|
88 cacheGraphics();
|
samer@0
|
89 sized();
|
samer@0
|
90 }
|
samer@0
|
91 } );
|
samer@0
|
92
|
samer@0
|
93 setBackground(Shell.getColor("background",null));
|
samer@0
|
94 setForeground(Shell.getColor("foreground",null));
|
samer@0
|
95 }
|
samer@0
|
96
|
samer@0
|
97 /* this is v cunning (actually - not really) */
|
samer@0
|
98 public Image createPixel() { return createPixel(1,1); }
|
samer@0
|
99 public Image createPixel(Color c) { return createPixel(c,1,1); }
|
samer@0
|
100 public Image createPixel(int w, int h) { return createPixel(getForeground(),w,h); }
|
samer@0
|
101 public Image createPixel(Color c, int w, int h)
|
samer@0
|
102 {
|
samer@0
|
103 Image img=createImage(w,h);
|
samer@0
|
104 Graphics g=img.getGraphics();
|
samer@0
|
105 g.setColor(c);
|
samer@0
|
106 g.fillRect(0,0,w,w);
|
samer@0
|
107 g.dispose();
|
samer@0
|
108 return img;
|
samer@0
|
109 }
|
samer@0
|
110
|
samer@0
|
111 // ....... MenuAnchor bits .......................
|
samer@0
|
112
|
samer@0
|
113 private DynamicPopupHandler thing=null;
|
samer@0
|
114 // private JPopupMenu thing=null;
|
samer@0
|
115
|
samer@0
|
116 public Component getComponent() { return this; }
|
samer@0
|
117 public void attach() {}
|
samer@0
|
118 public void detach() {}
|
samer@0
|
119 public void exposeCommands(Agent agent) {
|
samer@0
|
120 // menu=MenuBuilder.showCommands(agent,getComponent(),menu);
|
samer@0
|
121 thing=MenuBuilder.showCommands(agent,getComponent(),thing);
|
samer@0
|
122 }
|
samer@0
|
123
|
samer@0
|
124 protected void finalize() {
|
samer@0
|
125 if (graphics!=null) graphics.dispose();
|
samer@0
|
126 }
|
samer@0
|
127
|
samer@0
|
128 }
|
samer@0
|
129
|
samer@0
|
130
|