001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.tabcontrol.plaf;
043:
044: import org.netbeans.swing.tabcontrol.TabDataModel;
045:
046: import javax.swing.*;
047: import java.awt.*;
048:
049: /**
050: * Implementation of layout model for View-type tabs, which are not scrollable
051: * and are shrinking and extending their size to always cover whole tabs area.
052: *
053: * @author Dafe Simonek
054: */
055: final class ViewTabLayoutModel implements TabLayoutModel {
056:
057: private TabDataModel model;
058:
059: private JComponent renderTarget;
060:
061: /**
062: * Creates a new instance of ViewTabLayoutModel
063: */
064: public ViewTabLayoutModel(TabDataModel model,
065: JComponent renderTarget) {
066: this .model = model;
067: this .renderTarget = renderTarget;
068: }
069:
070: public int getH(int index) {
071: checkIndex(index);
072: Insets insets = renderTarget.getInsets();
073: return renderTarget.getHeight() - (insets.bottom + insets.top);
074: }
075:
076: public int getW(int index) {
077: checkIndex(index);
078: int x = computeX(index);
079: int nextX;
080: if (index < model.size() - 1) {
081: nextX = computeX(index + 1);
082: } else {
083: // last tab, special case
084: Insets insets = renderTarget.getInsets();
085: nextX = renderTarget.getWidth() - insets.right;
086: }
087: // substract from next tab to get width
088: return nextX - x;
089: }
090:
091: public int getX(int index) {
092: checkIndex(index);
093: return computeX(index);
094: }
095:
096: public int getY(int index) {
097: checkIndex(index);
098: return renderTarget.getInsets().top;
099: }
100:
101: public int indexOfPoint(int x, int y) {
102: Insets insets = renderTarget.getInsets();
103: int contentWidth = renderTarget.getWidth()
104: - (insets.left + insets.right);
105: int contentHeight = renderTarget.getHeight()
106: - (insets.bottom + insets.top);
107: if (y < insets.top || y > contentHeight || x < insets.left
108: || x > contentWidth) {
109: return -1;
110: }
111: int size = model.size();
112: int diff;
113: for (int i = 0; i < size; i++) {
114: diff = x - computeX(i);
115: if ((diff >= 0) && (diff < getW(i))) {
116: return i;
117: }
118: }
119: return -1;
120: }
121:
122: public int dropIndexOfPoint(int x, int y) {
123: Insets insets = renderTarget.getInsets();
124: int contentWidth = renderTarget.getWidth()
125: - (insets.left + insets.right);
126: int contentHeight = renderTarget.getHeight()
127: - (insets.bottom + insets.top);
128: if (y < insets.top || y > contentHeight || x < insets.left
129: || x > contentWidth) {
130: return -1;
131: }
132: // can have rounding errors, not important here
133: int size = model.size();
134: float tabWidth = (float) contentWidth / (float) size;
135: // move in between tabs
136: x = x - insets.left + (int) tabWidth / 2;
137: int result = (int) (x / tabWidth);
138: return Math.min(result, model.size());
139: }
140:
141: public void setPadding(Dimension d) {
142: //do nothing
143: }
144:
145: /**
146: * Checks validity of given index
147: */
148: private void checkIndex(int index) {
149: int size = model.size();
150: if ((index < 0) || (index >= size)) {
151: throw new IllegalArgumentException(
152: "Index out of valid scope 0.." + (size - 1) + ": "
153: + index);
154: }
155: }
156:
157: /**
158: * Computes and returns x coordination of left side of the tab with given
159: * index
160: */
161: private int computeX(int index) {
162: Insets insets = renderTarget.getInsets();
163: int contentWidth = renderTarget.getWidth()
164: - (insets.left + insets.right);
165: return (contentWidth * index / model.size()) + insets.left;
166: }
167:
168: }
|