KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > utils > OverlayObject


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32
33 package org.antlr.works.utils;
34
35 import org.antlr.xjlib.appkit.frame.XJFrameInterface;
36
37 import javax.swing.*;
38 import java.awt.*;
39 import java.awt.event.*;
40
41 public abstract class OverlayObject {
42
43     public static final int DEFAULT_WIDTH = 300;
44     public static final int DEFAULT_HEIGHT = 40;
45
46     public static final int ALIGN_LEFT = 0;
47     public static final int ALIGN_CENTER = 1;
48     public static final int ALIGN_CUSTOM = 2;
49
50     protected XJFrameInterface parentFrame;
51     protected JComponent parentComponent;
52     protected JComponent content;
53
54     public OverlayObject(XJFrameInterface parentFrame, JComponent parentComponent) {
55
56         this.parentFrame = parentFrame;
57         this.parentComponent = parentComponent;
58
59         createKeyBindings();
60         content = overlayCreateInterface();
61         content.setVisible(false);
62
63         parentFrame.getLayeredPane().add(content, JLayeredPane.MODAL_LAYER);
64
65         createListeners();
66     }
67
68     private void createListeners() {
69         parentFrame.getJavaContainer().addComponentListener(new ComponentAdapter() {
70             public void componentHidden(ComponentEvent e) {
71                 if(content.isVisible())
72                     content.setVisible(false);
73             }
74         });
75
76         parentComponent.addComponentListener(new ComponentAdapter() {
77             public void componentMoved(ComponentEvent e) {
78                 if(!isOverlayVisibleInParentComponent()) {
79                     hide();
80                 }
81
82                 if(content.isVisible()) {
83                     resize();
84                 }
85             }
86
87             public void componentResized(ComponentEvent e) {
88                 if(!isOverlayVisibleInParentComponent()) {
89                     hide();
90                 }
91
92                 if(content.isVisible()) {
93                     resize();
94                 }
95             }
96         });
97
98         parentComponent.addMouseListener(new MouseAdapter() {
99             public void mouseClicked(MouseEvent e) {
100                 super.mouseClicked(e);
101                 if(content.isVisible()) {
102                     hide();
103                 }
104             }
105
106             public void mousePressed(MouseEvent e) {
107                 super.mousePressed(e);
108                 if(content.isVisible()) {
109                     hide();
110                 }
111             }
112         });
113     }
114
115     public void createKeyBindings() {
116         if(overlayDisplayKeyStroke() == null)
117             return;
118
119         parentComponent.getInputMap().put(overlayDisplayKeyStroke(), overlayDisplayKeyStrokeMappingName());
120         parentComponent.getActionMap().put(overlayDisplayKeyStrokeMappingName(), new AbstractAction() {
121             public void actionPerformed(ActionEvent e) {
122                 display();
123             }
124         });
125     }
126
127     public boolean isOverlayVisibleInParentComponent() {
128         Rectangle vr = SwingUtilities.convertRectangle(parentComponent, parentComponent.getVisibleRect(), parentFrame.getJavaContainer());
129         Rectangle cr = SwingUtilities.convertRectangle(parentFrame.getLayeredPane(), content.getBounds(), parentFrame.getJavaContainer());
130
131         return vr.intersects(cr);
132     }
133
134     public void hide() {
135         if(content.isVisible()) {
136             content.setVisible(false);
137             parentComponent.requestFocusInWindow();
138         }
139     }
140
141     public void resize() {
142         Rectangle r = parentComponent.getVisibleRect();
143         Point p = SwingUtilities.convertPoint(parentComponent, new Point(r.x, r.y), parentFrame.getRootPane());
144         int x = 0;
145         int y = 0;
146         switch(overlayDefaultAlignment()) {
147             case ALIGN_CENTER:
148                 x = p.x+r.width/2-overlayDefaultWidth()/2;
149                 y = p.y+r.height/2-overlayDefaultHeight()/2;
150                 break;
151             case ALIGN_LEFT:
152                 x = p.x+5;
153                 y = p.y+r.height/2-overlayDefaultHeight()/2;
154                 break;
155             case ALIGN_CUSTOM:
156                 Point cp = overlayCustomPosition();
157                 if(cp != null) {
158                     x = cp.x;
159                     y = cp.y;
160                 }
161                 break;
162         }
163         content.setBounds(x, y, overlayDefaultWidth(), overlayDefaultHeight());
164     }
165
166     public void display() {
167         if(overlayWillDisplay()) {
168             resize();
169             content.setVisible(true);
170         } else {
171             content.setVisible(false);
172         }
173     }
174
175     public boolean isVisible() {
176         return content.isVisible();
177     }
178
179     public abstract JComponent overlayCreateInterface();
180     public abstract boolean overlayWillDisplay();
181
182     public int overlayDefaultWidth() {
183         return DEFAULT_WIDTH;
184     }
185
186     public int overlayDefaultHeight() {
187         return DEFAULT_HEIGHT;
188     }
189
190     public int overlayDefaultAlignment() {
191         return ALIGN_CENTER;
192     }
193
194     public Point overlayCustomPosition() {
195         return null;
196     }
197
198     public KeyStroke overlayDisplayKeyStroke() {
199         return null;
200     }
201
202     public String JavaDoc overlayDisplayKeyStrokeMappingName() {
203         return null;
204     }
205
206 }
207
Popular Tags