1 19 package org.openide.explorer.propertysheet; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.awt.event.ComponentEvent ; 30 import java.awt.event.ComponentListener ; 31 import java.awt.event.ContainerEvent ; 32 import java.awt.event.ContainerListener ; 33 34 import javax.swing.JComponent ; 35 import javax.swing.JViewport ; 36 import javax.swing.UIManager ; 37 import javax.swing.plaf.ComponentUI ; 38 import javax.swing.plaf.ViewportUI ; 39 import javax.swing.plaf.basic.BasicViewportUI ; 40 41 42 48 class MarginViewportUI extends ViewportUI implements ComponentListener , ContainerListener { 49 private JViewport viewport; 50 private int lastHeight = -1; 51 private int stringWidth = -1; 52 private int stringHeight = -1; 53 private int ascent = -1; 54 Rectangle scratch = new Rectangle (); 55 private String emptyString = "THIS IS A BUG"; Color marginColor = UIManager.getColor("controlShadow"); private int marginWidth = PropUtils.getMarginWidth(); 58 private boolean marginPainted = false; 59 Dimension lastKnownSize = new Dimension (); 60 61 62 private MarginViewportUI(JViewport jv) { 63 this.viewport = jv; 64 } 65 66 67 public static ComponentUI createUI(JComponent c) { 68 return new MarginViewportUI((JViewport ) c); 69 } 70 71 public void installUI(JComponent c) { 72 super.installUI(c); 73 74 Color fg = UIManager.getColor("controlShadow"); 82 if (fg == null) { 83 fg = Color.LIGHT_GRAY; 84 } 85 86 c.setForeground(fg); 87 88 Color bg = UIManager.getColor("window"); 90 if (bg == null) { 91 bg = Color.WHITE; 92 } 93 94 c.setBackground(bg); 95 96 Font f = UIManager.getFont("Tree.font"); 98 if (f == null) { 99 f = UIManager.getFont("controlFont"); } 101 102 if (f != null) { 103 c.setFont(f); 104 } 105 106 c.addContainerListener(this); 107 108 Component [] kids = c.getComponents(); 109 110 for (int i = 0; i < kids.length; i++) { 111 kids[i].addComponentListener(this); 114 } 115 } 116 117 public void uninstallUI(JComponent vp) { 118 JViewport jv = (JViewport ) vp; 119 Component [] 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 s) { 129 emptyString = s; 130 stringWidth = -1; 131 stringHeight = -1; 132 } 133 134 public void setMarginColor(Color 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 150 public void paint(Graphics g, JComponent c) { 151 Component view = ((JViewport ) 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 if (shouldPaintEmptyMessage()) { 163 g.setFont(c.getFont()); 165 g.setColor(c.getForeground()); 166 167 Rectangle r = getEmptyMessageBounds(); 168 169 if (g.hitClip(r.x, r.y, r.width, r.height)) { 171 g.drawString(emptyString, r.x, r.y + ascent); 173 } 174 } 175 } 176 177 private void calcStringSizes(Font f, Graphics g) { 178 FontMetrics fm = g.getFontMetrics(f); 179 stringWidth = fm.stringWidth(emptyString); 180 stringHeight = fm.getHeight(); 181 ascent = fm.getMaxAscent(); 182 } 183 184 private Rectangle getEmptyMessageBounds() { 185 Insets 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 g, JComponent 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 nuSize) { 214 if (!marginPainted && ((nuSize.height > 10) == (lastKnownSize.height > 10))) { 215 } 217 218 int heightDif = Math.abs(nuSize.height - lastKnownSize.height); 219 220 if (heightDif == 0) { 221 } 223 224 Insets ins = viewport.getInsets(); 226 227 235 viewport.repaint(ins.left, ins.top, marginWidth, viewport.getHeight() - (ins.top + ins.bottom)); 236 237 Rectangle r = getEmptyMessageBounds(); 240 viewport.repaint(r.x, r.y, r.width, r.height); 241 242 } 244 245 private boolean shouldPaintEmptyMessage() { 246 Dimension 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 e) { 256 e.getChild().addComponentListener(this); 257 } 258 259 public void componentHidden(ComponentEvent e) { 260 } 261 262 public void componentMoved(ComponentEvent e) { 263 } 264 265 public void componentRemoved(ContainerEvent e) { 266 e.getChild().removeComponentListener(this); 267 } 268 269 public void componentResized(ComponentEvent e) { 270 scheduleRepaint(((Component ) e.getSource()).getSize()); 271 } 272 273 public void componentShown(ComponentEvent e) { 274 scheduleRepaint(((Component ) e.getSource()).getSize()); 275 } 276 } 277 | Popular Tags |