1 19 package org.netbeans.swing.plaf.gtk; 20 21 import javax.swing.*; 22 import javax.swing.border.Border ; 23 import java.awt.*; 24 25 33 public class AdaptiveMatteBorder implements Border { 34 private Insets insets; 35 private int shadowDepth; 36 private boolean topLeftInsets; 37 38 39 public AdaptiveMatteBorder(boolean t, boolean l, boolean b, boolean r, int shadowDepth, boolean topLeftInsets) { 40 insets = new Insets (t ? topLeftInsets ? shadowDepth + 1 : 1 : 0, l ? topLeftInsets ? shadowDepth + 1: 1 : 0, b ? 1 + shadowDepth : shadowDepth, r ? 1 + shadowDepth : shadowDepth); 41 this.shadowDepth = shadowDepth; 42 this.topLeftInsets = topLeftInsets; 43 } 44 45 public AdaptiveMatteBorder(boolean t, boolean l, boolean b, boolean r, int shadowDepth) { 46 this (t, l, b, r, shadowDepth, false); 47 } 48 49 private Insets maybeOmitInsets (Insets ins, Component c) { 50 if (shadowDepth <= 0 || !topLeftInsets) { 51 return ins; 52 } 53 Insets result = new Insets(ins.top, ins.left, ins.right, ins.bottom); 54 if (topLeftInsets) { 55 Point p = c.getLocation(); 56 if (p.x > 10) { 57 result.left = 1; 58 } 59 if (p.y > 10) { 60 result.top = 1; 61 } 62 } 63 return result; 64 } 65 66 public Insets getBorderInsets(Component c) { 67 return maybeOmitInsets(insets, c); 68 } 69 70 public boolean isBorderOpaque() { 71 return false; 72 } 73 74 75 public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { 76 Color color = g.getColor(); 77 Insets ins = getBorderInsets(c); 78 Point p = c.getLocation(); 79 80 g.setColor (UIManager.getColor("controlShadow")); w -= shadowDepth; 83 h -= shadowDepth; 84 if (topLeftInsets) { 85 if (p.y <= 10) { 86 y += shadowDepth; 87 h -= shadowDepth; 88 } 89 if (p.x <= 10) { 90 x += shadowDepth; 91 w -= shadowDepth; 92 } 93 } 94 if (ins.top > 0) { 95 g.fillRect(x, y, w, 1); 96 } 97 if (ins.left > 0) { 98 g.fillRect(x, y, 1, h); 99 } 100 if (ins.right > 0) { 101 g.fillRect(x + w - 1, y, 1, h); 102 } 103 if (ins.bottom > 0) { 104 g.fillRect(x, y + h - 1, w, 1); 105 } 106 107 boolean isViewTab = isViewTab(c); 108 109 if (shadowDepth > 1) { 110 Rectangle clip = g.getClipBounds(); 111 boolean clipTouchesRight = ((clip.x + clip.width) >= (x + w)); 112 boolean clipTouchesBottom = ((clip.y + clip.height) >= (y + h)); 113 114 if (clipTouchesBottom || clipTouchesRight) { 115 Color ctrl = UIManager.getColor ("control"); Color base = UIManager.getColor("controlShadow"); 117 118 Color curr; 119 for (int i = 1; i < shadowDepth; i++) { 120 curr = colorTowards (base, ctrl, shadowDepth, i+1); 121 g.setColor (curr); 122 if (clipTouchesRight && ins.right > 0) { 123 g.fillRect(x + w - 1 + i, y + (isViewTab ? 0 : i), 1, h); 124 } 125 if (clipTouchesBottom && ins.bottom > 0) { 126 g.fillRect(x + i, y + h - 1 + i, w - 1, 1); 127 } 128 } 129 } 130 } 131 g.setColor (color); 132 } 133 134 137 static boolean isViewTab (Component c) { 138 if (c.getParent() instanceof JComponent) { 139 JComponent jc = (JComponent) c.getParent(); 140 Object o = jc.getClientProperty("viewType"); 141 if (o != null && o instanceof Integer ) { 142 return ((Integer ) o).intValue() == 0; 143 } 144 } 145 return false; 146 } 147 148 private static final float[] comps = new float[4]; 149 private static final float[] targs = new float[4]; 150 151 static final Color colorTowards (Color base, Color target, float steps, float step) { 152 base.getColorComponents(comps); 153 target.getColorComponents(targs); 154 155 comps[3] = 1.0f; 157 float factor = (step / steps); 158 159 for (int i=0; i < 3; i++) { 160 comps[i] = saturate(comps[i] - (factor * (comps[i] - targs[i]))); 161 } 162 163 164 Color result = new Color (comps[0], comps[1], comps[2], comps[3]); 166 return result; 167 } 168 169 private static final float saturate (float f) { 170 float orig = f; 171 if (f > 1) { 172 f = 1; 173 } 174 if (f < 0) { 175 f = 0; 176 } 177 return f; 178 } 179 180 } 181 182 | Popular Tags |