KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > MarginViewportUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.explorer.propertysheet;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Rectangle JavaDoc;
29 import java.awt.event.ComponentEvent JavaDoc;
30 import java.awt.event.ComponentListener JavaDoc;
31 import java.awt.event.ContainerEvent JavaDoc;
32 import java.awt.event.ContainerListener JavaDoc;
33
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JViewport JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.plaf.ComponentUI JavaDoc;
38 import javax.swing.plaf.ViewportUI JavaDoc;
39 import javax.swing.plaf.basic.BasicViewportUI JavaDoc;
40
41
42 /** Viewport UI which will paint a margin if the contained
43  * SheetTable is showing one, so the margin appears to continue
44  * down to the bottom of the component.
45  *
46  * @author Tim Boudreau
47  */

48 class MarginViewportUI extends ViewportUI JavaDoc implements ComponentListener JavaDoc, ContainerListener JavaDoc {
49     private JViewport JavaDoc viewport;
50     private int lastHeight = -1;
51     private int stringWidth = -1;
52     private int stringHeight = -1;
53     private int ascent = -1;
54     Rectangle JavaDoc scratch = new Rectangle JavaDoc();
55     private String JavaDoc emptyString = "THIS IS A BUG"; //NOI18N
56
Color JavaDoc marginColor = UIManager.getColor("controlShadow"); //NOI18N
57
private int marginWidth = PropUtils.getMarginWidth();
58     private boolean marginPainted = false;
59     Dimension JavaDoc lastKnownSize = new Dimension JavaDoc();
60
61     /** Creates a new instance of MarginViewportUI */
62     private MarginViewportUI(JViewport JavaDoc jv) {
63         this.viewport = jv;
64     }
65
66     /** Uses a single shared instance, as does BasicViewportUI */
67     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
68         return new MarginViewportUI((JViewport JavaDoc) c);
69     }
70
71     public void installUI(JComponent JavaDoc c) {
72         super.installUI(c);
73
74         //Fetch the "no properties" string - it's not going to change
75
//for the life of the session
76
// noPropsString = NbBundle.getMessage(MarginViewportUI.class,
77
// "CTL_NoProperties"); //NOI18N
78
//Set an appropriate font and color. Only really relevant on OS-X to
79
//keep the font consistent with other NB fonts
80
Color JavaDoc fg = UIManager.getColor("controlShadow"); //NOI18N
81

82         if (fg == null) {
83             fg = Color.LIGHT_GRAY;
84         }
85
86         c.setForeground(fg);
87
88         Color JavaDoc bg = UIManager.getColor("window"); //NOI18N
89

90         if (bg == null) {
91             bg = Color.WHITE;
92         }
93
94         c.setBackground(bg);
95
96         Font JavaDoc f = UIManager.getFont("Tree.font"); //NOI18N
97

98         if (f == null) {
99             f = UIManager.getFont("controlFont"); //NOI18N
100
}
101
102         if (f != null) {
103             c.setFont(f);
104         }
105
106         c.addContainerListener(this);
107
108         Component JavaDoc[] kids = c.getComponents();
109
110         for (int i = 0; i < kids.length; i++) {
111             //Should almost always be empty anyway, if not only one component,
112
//but for completeness...
113
kids[i].addComponentListener(this);
114         }
115     }
116
117     public void uninstallUI(JComponent JavaDoc vp) {
118         JViewport JavaDoc jv = (JViewport JavaDoc) vp;
119         Component JavaDoc[] c = jv.getComponents();
120
121         for (int i = 0; i < c.length; i++) {
122             c[i].removeComponentListener(this);
123         }
124
125         jv.removeContainerListener(this);
126     }
127
128     public void setEmptyString(String JavaDoc s) {
129         emptyString = s;
130         stringWidth = -1;
131         stringHeight = -1;
132     }
133
134     public void setMarginColor(Color JavaDoc c) {
135         marginColor = c;
136     }
137
138     public void setMarginWidth(int margin) {
139         this.marginWidth = margin;
140     }
141
142     public void setMarginPainted(boolean val) {
143         if (marginPainted != val) {
144             marginPainted = val;
145             viewport.repaint();
146         }
147     }
148
149     /** Overridden to draw "no properties" if necessary */
150     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
151         Component JavaDoc view = ((JViewport JavaDoc) c).getView();
152
153         if (view != null) {
154             lastKnownSize = view.getSize();
155         }
156
157         if (stringWidth == -1) {
158             calcStringSizes(c.getFont(), g);
159         }
160
161         //Update will have set paintNoProps to the correct value
162
if (shouldPaintEmptyMessage()) {
163             //We need to paint centered "<No Properties>" text
164
g.setFont(c.getFont());
165             g.setColor(c.getForeground());
166
167             Rectangle JavaDoc r = getEmptyMessageBounds();
168
169             //See if we really need to do any painting
170
if (g.hitClip(r.x, r.y, r.width, r.height)) {
171                 //Paint the string
172
g.drawString(emptyString, r.x, r.y + ascent);
173             }
174         }
175     }
176
177     private void calcStringSizes(Font JavaDoc f, Graphics JavaDoc g) {
178         FontMetrics JavaDoc fm = g.getFontMetrics(f);
179         stringWidth = fm.stringWidth(emptyString);
180         stringHeight = fm.getHeight();
181         ascent = fm.getMaxAscent();
182     }
183
184     private Rectangle JavaDoc getEmptyMessageBounds() {
185         Insets JavaDoc ins = viewport.getInsets();
186
187         scratch.x = ins.left + (((viewport.getWidth() - (ins.left + ins.right)) / 2) - (stringWidth / 2));
188
189         scratch.y = ins.top + (((viewport.getHeight() - (ins.top + ins.bottom)) / 2) - (stringHeight / 2));
190
191         scratch.width = stringWidth;
192         scratch.height = stringHeight;
193
194         return scratch;
195     }
196
197     public void update(Graphics JavaDoc g, JComponent JavaDoc c) {
198         g.setColor(c.getBackground());
199
200         boolean margin = shouldPaintMargin();
201
202         int leftEdge = margin ? marginWidth : 0;
203         g.fillRect(leftEdge, 0, c.getWidth() - leftEdge, c.getHeight());
204
205         if (margin) {
206             g.setColor(marginColor);
207             g.fillRect(0, 0, marginWidth, c.getHeight());
208         }
209
210         paint(g, c);
211     }
212
213     private void scheduleRepaint(Dimension JavaDoc nuSize) {
214         if (!marginPainted && ((nuSize.height > 10) == (lastKnownSize.height > 10))) {
215             // return;
216
}
217
218         int heightDif = Math.abs(nuSize.height - lastKnownSize.height);
219
220         if (heightDif == 0) {
221             // return;
222
}
223
224         // if (heightDif != 0) {
225
Insets JavaDoc ins = viewport.getInsets();
226
227         /*
228                     int left = ins.left;
229                     int top = nuSize.height + ins.top;
230                     int width = marginWidth;
231                     int height = lastKnownSize.height - nuSize.height;
232
233                     viewport.repaint (left, top, width, height);
234                     */

235         viewport.repaint(ins.left, ins.top, marginWidth, viewport.getHeight() - (ins.top + ins.bottom));
236
237         // }
238
// if (nuSize.height < 10) {
239
Rectangle JavaDoc r = getEmptyMessageBounds();
240         viewport.repaint(r.x, r.y, r.width, r.height);
241
242         // }
243
}
244
245     private boolean shouldPaintEmptyMessage() {
246         Dimension JavaDoc d = viewport.getView().getSize();
247
248         return d.height < 10;
249     }
250
251     private boolean shouldPaintMargin() {
252         return marginPainted & !shouldPaintEmptyMessage();
253     }
254
255     public void componentAdded(ContainerEvent JavaDoc e) {
256         e.getChild().addComponentListener(this);
257     }
258
259     public void componentHidden(ComponentEvent JavaDoc e) {
260     }
261
262     public void componentMoved(ComponentEvent JavaDoc e) {
263     }
264
265     public void componentRemoved(ContainerEvent JavaDoc e) {
266         e.getChild().removeComponentListener(this);
267     }
268
269     public void componentResized(ComponentEvent JavaDoc e) {
270         scheduleRepaint(((Component JavaDoc) e.getSource()).getSize());
271     }
272
273     public void componentShown(ComponentEvent JavaDoc e) {
274         scheduleRepaint(((Component JavaDoc) e.getSource()).getSize());
275     }
276 }
277
Popular Tags