KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.swing.tabcontrol.plaf;
21
22 import org.netbeans.swing.tabcontrol.TabDataModel;
23
24 import javax.swing.*;
25 import java.awt.*;
26
27 /**
28  * Implementation of layout model for View-type tabs, which are not scrollable
29  * and are shrinking and extending their size to always cover whole tabs area.
30  *
31  * @author Dafe Simonek
32  */

33 final class ViewTabLayoutModel implements TabLayoutModel {
34
35     private TabDataModel model;
36
37     private JComponent renderTarget;
38
39     /**
40      * Creates a new instance of ViewTabLayoutModel
41      */

42     public ViewTabLayoutModel(TabDataModel model,
43                               JComponent renderTarget) {
44         this.model = model;
45         this.renderTarget = renderTarget;
46     }
47
48     public int getH(int index) {
49         checkIndex(index);
50         Insets insets = renderTarget.getInsets();
51         return renderTarget.getHeight() - (insets.bottom + insets.top);
52     }
53
54     public int getW(int index) {
55         checkIndex(index);
56         int x = computeX(index);
57         int nextX;
58         if (index < model.size() - 1) {
59             nextX = computeX(index + 1);
60         } else {
61             // last tab, special case
62
Insets insets = renderTarget.getInsets();
63             nextX = renderTarget.getWidth() - insets.right;
64         }
65         // substract from next tab to get width
66
return nextX - x;
67     }
68
69     public int getX(int index) {
70         checkIndex(index);
71         return computeX(index);
72     }
73
74     public int getY(int index) {
75         checkIndex(index);
76         return renderTarget.getInsets().top;
77     }
78
79     public int indexOfPoint(int x, int y) {
80         Insets insets = renderTarget.getInsets();
81         int contentWidth = renderTarget.getWidth()
82                 - (insets.left + insets.right);
83         int contentHeight = renderTarget.getHeight()
84                 - (insets.bottom + insets.top);
85         if (y < insets.top || y > contentHeight || x < insets.left
86                 || x > contentWidth) {
87             return -1;
88         }
89         int size = model.size();
90         int diff;
91         for (int i = 0; i < size; i++) {
92             diff = x - computeX(i);
93             if ((diff >= 0) && (diff < getW(i))) {
94                 return i;
95             }
96         }
97         return -1;
98     }
99
100     public int dropIndexOfPoint(int x, int y) {
101         Insets insets = renderTarget.getInsets();
102         int contentWidth = renderTarget.getWidth()
103                 - (insets.left + insets.right);
104         int contentHeight = renderTarget.getHeight()
105                 - (insets.bottom + insets.top);
106         if (y < insets.top || y > contentHeight || x < insets.left
107                 || x > contentWidth) {
108             return -1;
109         }
110         // can have rounding errors, not important here
111
int size = model.size();
112         float tabWidth = (float) contentWidth / (float) size;
113         // move in between tabs
114
x = x - insets.left + (int) tabWidth / 2;
115         int result = (int) (x / tabWidth);
116         return Math.min(result, model.size());
117     }
118
119     public void setPadding(Dimension d) {
120         //do nothing
121
}
122
123     /**
124      * Checks validity of given index
125      */

126     private void checkIndex(int index) {
127         int size = model.size();
128         if ((index < 0) || (index >= size)) {
129             throw new IllegalArgumentException JavaDoc("Index out of valid scope 0.."
130                                                + (size - 1)
131                                                + ": "
132                                                + index);
133         }
134     }
135
136     /**
137      * Computes and returns x coordination of left side of the tab with given
138      * index
139      */

140     private int computeX(int index) {
141         Insets insets = renderTarget.getInsets();
142         int contentWidth = renderTarget.getWidth()
143                 - (insets.left + insets.right);
144         return (contentWidth * index / model.size()) + insets.left;
145     }
146
147
148 }
149
Popular Tags