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.jdesktop.layout;
043:
044: import java.awt.Container;
045: import java.awt.FontMetrics;
046: import java.awt.Toolkit;
047: import javax.swing.JComponent;
048: import javax.swing.SwingConstants;
049: import javax.swing.UIManager;
050:
051: /**
052: * An implementation of <code>LayoutStyle</code> for the Windows look and feel.
053: * This information comes from:
054: * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwue/html/ch14e.asp
055: *
056: * @version $Revision$
057: */
058: class WindowsLayoutStyle extends LayoutStyle {
059: /**
060: * Base dialog units along the horizontal axis.
061: */
062: private int baseUnitX;
063: /**
064: * Base dialog units along the vertical axis.
065: */
066: private int baseUnitY;
067:
068: public int getPreferredGap(JComponent source, JComponent target,
069: int type, int position, Container parent) {
070: // Invoke super to check arguments.
071: super .getPreferredGap(source, target, type, position, target);
072:
073: if (type == INDENT) {
074: if (position == SwingConstants.EAST
075: || position == SwingConstants.WEST) {
076: int gap = getButtonChildIndent(source, position);
077: if (gap != 0) {
078: return gap;
079: }
080: return 10;
081: }
082: // Treat vertical INDENT as RELATED
083: type = RELATED;
084: }
085: if (type == UNRELATED) {
086: // Between unrelated controls: 7
087: return getCBRBPadding(source, target, position,
088: dluToPixels(7, position));
089: } else { //type == RELATED
090: boolean sourceLabel = (source.getUIClassID() == "LabelUI");
091: boolean targetLabel = (target.getUIClassID() == "LabelUI");
092:
093: if (((sourceLabel && !targetLabel) || (targetLabel && !sourceLabel))
094: && (position == SwingConstants.EAST || position == SwingConstants.WEST)) {
095: // Between text labels and their associated controls (for
096: // example, text boxes and list boxes): 3
097: // NOTE: We're not honoring:
098: // 'Text label beside a button 3 down from the top of
099: // the button,' but I suspect that is an attempt to
100: // enforce a baseline layout which will be handled
101: // separately. In order to enforce this we would need
102: // this API to return a more complicated type (Insets,
103: // or something else).
104: return getCBRBPadding(source, target, position,
105: dluToPixels(3, position));
106: }
107: // Between related controls: 4
108: return getCBRBPadding(source, target, position,
109: dluToPixels(4, position));
110: }
111: }
112:
113: public int getContainerGap(JComponent component, int position,
114: Container parent) {
115: super .getContainerGap(component, position, parent);
116: return getCBRBPadding(component, position, dluToPixels(7,
117: position));
118: }
119:
120: private int dluToPixels(int dlu, int direction) {
121: if (baseUnitX == 0) {
122: calculateBaseUnits();
123: }
124: if (direction == SwingConstants.EAST
125: || direction == SwingConstants.WEST) {
126: return dlu * baseUnitX / 4;
127: }
128: //assert (direction == SwingConstants.NORTH ||
129: // direction == SwingConstants.SOUTH);
130: return dlu * baseUnitY / 8;
131: }
132:
133: private void calculateBaseUnits() {
134: // This calculation comes from:
135: // http://support.microsoft.com/default.aspx?scid=kb;EN-US;125681
136: FontMetrics metrics = Toolkit.getDefaultToolkit()
137: .getFontMetrics(UIManager.getFont("Button.font"));
138: baseUnitX = metrics
139: .stringWidth("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
140: baseUnitX = (baseUnitX / 26 + 1) / 2;
141: // The -1 comes from experimentation.
142: baseUnitY = metrics.getAscent() + metrics.getDescent() - 1;
143: }
144: }
|