KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > BaseTabLayoutModel


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 /*
20  *
21  * BaseTabLayoutModel.java
22  *
23  * Created on May 16, 2003, 4:22 PM
24  */

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 JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Default implementation of TabLayoutModel. Simply provides a series of
38  * rectangles for each tab starting at 0 and ending at the last element, with
39  * the width set to the calculated width for the string plus a padding value
40  * assigned in <code>setPadding</code>.
41  * <p>
42  * To implement TabLayoutModel, it is often useful to create an implementation which
43  * wraps an instance of <code>DefaultTabLayoutModel</code>, and uses it to calculate
44  * tab sizes.
45  *
46  * <strong>Do not use this class directly, use DefaultTabLayoutModel - this class
47  * exists to enable unit tests to provide a subclass</strong>
48  *
49  * @author Tim Boudreau
50  */

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 JavaDoc text = model.getTab(index).getText();
92             return textWidth(text, getFont());
93         } catch (NullPointerException JavaDoc npe) {
94             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(
95                     "Error fetching width for tab " + //NOI18N
96
index
97                     + " - model size is "
98                     + model.size()
99                     + " TabData is " + //NOI18N
100
model.getTab(index)
101                     + " model contents: "
102                     + model); //NOI18N
103
throw iae;
104         }
105     }
106
107     private static Map JavaDoc<String JavaDoc,Integer JavaDoc> widthMap = new HashMap JavaDoc<String JavaDoc,Integer JavaDoc>(31);
108
109     static int textWidth(String JavaDoc text, Font f) {
110         //Note: If we choose to support multiple fonts in different
111
//tab controls in the system, make the cache non-static and
112
//dump it if the font changes.
113
Integer JavaDoc 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 JavaDoc(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             //No need to calculate for every string
129
String JavaDoc testStr = "Zgj"; //NOI18N
130
Font f = getFont();
131             textHeight = new Double JavaDoc(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