1 19 25 26 package org.netbeans.swing.tabcontrol.plaf; 27 28 import org.netbeans.swing.tabcontrol.TabDataModel; 29 import org.openide.awt.HtmlRenderer; 30 31 import javax.swing.*; 32 import java.awt.*; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 51 class BaseTabLayoutModel implements TabLayoutModel { 52 protected TabDataModel model; 53 protected int textHeight = -1; 54 protected int padX = 5; 55 protected int padY = 5; 56 protected JComponent renderTarget; 57 58 protected BaseTabLayoutModel(TabDataModel model, JComponent renderTarget) { 59 this.model = model; 60 this.renderTarget = renderTarget; 61 } 62 63 private Font getFont() { 64 return renderTarget.getFont(); 65 } 66 67 protected int iconWidth(int index) { 68 Icon ic = model.getTab(index).getIcon(); 69 int result; 70 if (ic != null) { 71 result = ic.getIconWidth(); 72 } else { 73 result = 0; 74 } 75 return result; 76 } 77 78 protected int iconHeight (int index) { 79 Icon ic = model.getTab(index).getIcon (); 80 int result; 81 if (ic != null) { 82 result = ic.getIconHeight(); 83 } else { 84 result = 0; 85 } 86 return result; 87 } 88 89 protected int textWidth(int index) { 90 try { 91 String text = model.getTab(index).getText(); 92 return textWidth(text, getFont()); 93 } catch (NullPointerException npe) { 94 IllegalArgumentException iae = new IllegalArgumentException ( 95 "Error fetching width for tab " + index 97 + " - model size is " 98 + model.size() 99 + " TabData is " + model.getTab(index) 101 + " model contents: " 102 + model); throw iae; 104 } 105 } 106 107 private static Map <String ,Integer > widthMap = new HashMap <String ,Integer >(31); 108 109 static int textWidth(String text, Font f) { 110 Integer result = widthMap.get(text); 114 if (result == null) { 115 double wid = HtmlRenderer.renderString(text, BasicScrollingTabDisplayerUI.getOffscreenGraphics(), 0, 0, 116 Integer.MAX_VALUE, 117 Integer.MAX_VALUE, f, 118 Color.BLACK, HtmlRenderer.STYLE_TRUNCATE, 119 false); 120 result = new Integer (Math.round(Math.round(wid))); 121 widthMap.put(text, result); 122 } 123 return result.intValue(); 124 } 125 126 protected int textHeight(int index) { 127 if (textHeight == -1) { 128 String testStr = "Zgj"; Font f = getFont(); 131 textHeight = new Double (f.getStringBounds(testStr, 132 BasicScrollingTabDisplayerUI.getOffscreenGraphics().getFontRenderContext()).getWidth()).intValue() + 2; 133 } 134 return textHeight; 135 } 136 137 public int getX(int index) { 138 int result = renderTarget.getInsets().left; 139 for (int i = 0; i < index; i++) { 140 result += getW(i); 141 } 142 return result; 143 } 144 145 public int getY(int index) { 146 return renderTarget.getInsets().top; 147 } 148 149 public int getH(int index) { 150 return Math.max (textHeight(index) + padY, model.getTab(index).getIcon().getIconHeight() + padY); 151 } 152 153 public int getW(int index) { 154 return textWidth(index) + iconWidth(index) + padX; 155 } 156 157 public int indexOfPoint(int x, int y) { 158 int max = model.size(); 159 int pos = renderTarget.getInsets().left; 160 for (int i = 0; i < max; i++) { 161 pos += getW(i); 162 if (pos > x) { 163 return i; 164 } 165 } 166 return -1; 167 } 168 169 public int dropIndexOfPoint(int x, int y) { 170 Insets insets = renderTarget.getInsets(); 171 int contentWidth = renderTarget.getWidth() 172 - (insets.left + insets.right); 173 int contentHeight = renderTarget.getHeight() 174 - (insets.bottom + insets.top); 175 if (y < insets.top || y > contentHeight || x < insets.left 176 || x > contentWidth) { 177 return -1; 178 } 179 int max = model.size(); 180 int pos = insets.left; 181 for (int i = 0; i < max; i++) { 182 int delta = getW(i); 183 pos += delta; 184 if (x <= (pos - delta / 2)) { 185 return i; 186 } else if (x < pos) { 187 return i + 1; 188 } 189 } 190 return max; 191 } 192 193 public void setPadding(Dimension d) { 194 padX = d.width; 195 padY = d.height; 196 } 197 } 198 | Popular Tags |