Source Code Cross Referenced for SubstanceBaseline.java in  » Swing-Library » substance-look-feel » org » jvnet » substance » baseline » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Swing Library » substance look feel » org.jvnet.substance.baseline 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2005-2008 Substance Kirill Grouchnikov, based on work by
003:         * Sun Microsystems, Inc. All rights reserved.
004:         *
005:         * This library is free software; you can redistribute it and/or
006:         * modify it under the terms of the GNU Lesser General Public
007:         * License as published by the Free Software Foundation; either
008:         * version 2.1 of the License, or (at your option) any later version.
009:         *
010:         * This library is distributed in the hope that it will be useful,
011:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         * Lesser General Public License for more details.
014:         *
015:         * You should have received a copy of the GNU Lesser General Public
016:         * License along with this library; if not, write to the Free Software
017:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018:         */
019:        package org.jvnet.substance.baseline;
020:
021:        import java.awt.*;
022:        import java.lang.reflect.InvocationTargetException;
023:        import java.lang.reflect.Method;
024:        import java.util.*;
025:
026:        import javax.swing.*;
027:        import javax.swing.border.*;
028:        import javax.swing.plaf.metal.MetalLookAndFeel;
029:        import javax.swing.text.JTextComponent;
030:        import javax.swing.text.View;
031:
032:        import org.jdesktop.layout.Baseline;
033:        import org.jvnet.substance.utils.MemoryAnalyzer;
034:        import org.jvnet.substance.utils.SubstanceSizeUtils;
035:
036:        /**
037:         * Convenience class that can be used to determine the baseline of a particular
038:         * component under Substance.
039:         */
040:        public class SubstanceBaseline extends Baseline {
041:            //
042:            // Used by button and label baseline code, cached to avoid excessive
043:            // garbage.
044:            //
045:            private static final Rectangle viewRect = new Rectangle();
046:            private static final Rectangle textRect = new Rectangle();
047:            private static final Rectangle iconRect = new Rectangle();
048:
049:            //
050:            // These come from TitleBorder. NOTE that these are NOT final in
051:            // TitledBorder
052:            //
053:            private static final int EDGE_SPACING = 2;
054:            private static final int TEXT_SPACING = 2;
055:
056:            private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
057:
058:            // Prototype label for calculating baseline of tables.
059:            private static JLabel TABLE_LABEL;
060:
061:            // Prototype label for calculating baseline of lists.
062:            private static JLabel LIST_LABEL;
063:
064:            // Prototype label for calculating baseline of trees.
065:            private static JLabel TREE_LABEL;
066:
067:            // Corresponds to com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
068:            private static Class CLASSIC_WINDOWS;
069:            // Whether or not we've tried to load WindowsClassicLookAndFeel.
070:            private static boolean checkedForClassic;
071:
072:            // Map<Class,Method>
073:            private static final Map BASELINE_MAP = Collections
074:                    .synchronizedMap(new HashMap());
075:            private static Method COMPONENT_BASELINE_METHOD;
076:
077:            static {
078:                COMPONENT_BASELINE_METHOD = null;
079:                try {
080:                    COMPONENT_BASELINE_METHOD = Component.class
081:                            .getMethod("getBaseline", new Class[] { int.class,
082:                                    int.class });
083:                } catch (NoSuchMethodException nsme) {
084:                }
085:            }
086:
087:            private static int invokeBaseline(Method method, JComponent c,
088:                    int width, int height) {
089:                int baseline = -1;
090:                try {
091:                    baseline = ((Integer) method.invoke(c, new Object[] {
092:                            new Integer(width), new Integer(height) }))
093:                            .intValue();
094:                } catch (IllegalAccessException iae) {
095:                } catch (IllegalArgumentException iae2) {
096:                } catch (InvocationTargetException ite2) {
097:                }
098:                return baseline;
099:            }
100:
101:            private static Insets rotateInsets(Insets topInsets,
102:                    int targetPlacement) {
103:                switch (targetPlacement) {
104:                case JTabbedPane.LEFT:
105:                    return new Insets(topInsets.left, topInsets.top,
106:                            topInsets.right, topInsets.bottom);
107:                case JTabbedPane.BOTTOM:
108:                    return new Insets(topInsets.bottom, topInsets.left,
109:                            topInsets.top, topInsets.right);
110:                case JTabbedPane.RIGHT:
111:                    return new Insets(topInsets.left, topInsets.bottom,
112:                            topInsets.right, topInsets.top);
113:                default:
114:                    return new Insets(topInsets.top, topInsets.left,
115:                            topInsets.bottom, topInsets.right);
116:                }
117:            }
118:
119:            private static int getMaxTabHeight(JTabbedPane tp) {
120:                int fontHeight = tp.getFontMetrics(tp.getFont()).getHeight();
121:                int height = fontHeight;
122:                boolean tallerIcons = false;
123:                for (int counter = tp.getTabCount() - 1; counter >= 0; counter--) {
124:                    Icon icon = tp.getIconAt(counter);
125:                    if (icon != null) {
126:                        int iconHeight = icon.getIconHeight();
127:                        height = Math.max(height, iconHeight);
128:                        if (iconHeight > fontHeight) {
129:                            tallerIcons = true;
130:                        }
131:                    }
132:                }
133:                Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
134:                height += 2;
135:                if (!tallerIcons) {
136:                    height += tabInsets.top + tabInsets.bottom;
137:                }
138:                return height;
139:            }
140:
141:            private static int getTabbedPaneBaseline(JTabbedPane tp, int height) {
142:                if (tp.getTabCount() > 0) {
143:                    Insets insets = tp.getInsets();
144:                    Insets contentBorderInsets = UIManager
145:                            .getInsets("TabbedPane.contentBorderInsets");
146:                    Insets tabAreaInsets = rotateInsets(UIManager
147:                            .getInsets("TabbedPane.tabAreaInsets"), tp
148:                            .getTabPlacement());
149:                    FontMetrics metrics = tp.getFontMetrics(tp.getFont());
150:                    int maxHeight = getMaxTabHeight(tp);
151:                    iconRect.setBounds(0, 0, 0, 0);
152:                    textRect.setBounds(0, 0, 0, 0);
153:                    viewRect.setBounds(0, 0, Short.MAX_VALUE, maxHeight);
154:                    SwingUtilities.layoutCompoundLabel(tp, metrics, "A", null,
155:                            SwingUtilities.CENTER, SwingUtilities.CENTER,
156:                            SwingUtilities.CENTER, SwingUtilities.TRAILING,
157:                            viewRect, iconRect, textRect, 0);
158:                    int baseline = textRect.y + metrics.getAscent();
159:                    switch (tp.getTabPlacement()) {
160:                    case JTabbedPane.TOP:
161:                        baseline += insets.top + tabAreaInsets.top;
162:                        return baseline;
163:                    case JTabbedPane.BOTTOM:
164:                        baseline = tp.getHeight() - insets.bottom
165:                                - tabAreaInsets.bottom - maxHeight + baseline;
166:                        return baseline;
167:                    case JTabbedPane.LEFT:
168:                    case JTabbedPane.RIGHT:
169:                        baseline += insets.top + tabAreaInsets.top;
170:                        return baseline;
171:                    }
172:                }
173:                return -1;
174:            }
175:
176:            private static int getAquaTabbedPaneBaseline(JTabbedPane tp,
177:                    int height) {
178:                Font font = tp.getFont();
179:                FontMetrics metrics = tp.getFontMetrics(font);
180:                int ascent = metrics.getAscent();
181:                int offset;
182:                switch (tp.getTabPlacement()) {
183:                case JTabbedPane.TOP:
184:                    offset = 5;
185:                    if (tp.getFont().getSize() > 12) {
186:                        offset = 6;
187:                    }
188:                    int yOffset = 20 - metrics.getHeight();
189:                    yOffset /= 2;
190:                    return offset + yOffset + ascent - 1;
191:                case JTabbedPane.BOTTOM:
192:                    if (tp.getFont().getSize() > 12) {
193:                        offset = 6;
194:                    } else {
195:                        offset = 4;
196:                    }
197:                    return height
198:                            - (20 - ((20 - metrics.getHeight()) / 2 + ascent))
199:                            - offset;
200:                case JTabbedPane.LEFT:
201:                case JTabbedPane.RIGHT:
202:                    // Aqua rotates left/right text, so that there isn't a good
203:                    // baseline.
204:                    return -1;
205:                }
206:                return -1;
207:            }
208:
209:            private static int getSliderBaseline(JSlider slider, int height) {
210:                if (slider.getPaintLabels()) {
211:                    FontMetrics metrics = slider.getFontMetrics(slider
212:                            .getFont());
213:                    Insets insets = slider.getInsets();
214:                    Insets focusInsets = (Insets) UIManager
215:                            .get("Slider.focusInsets");
216:                    if (slider.getOrientation() == JSlider.HORIZONTAL) {
217:                        int contentHeight = height - insets.top - insets.bottom
218:                                - focusInsets.top - focusInsets.bottom;
219:                        int tickLength = SubstanceSizeUtils
220:                                .getSliderTickSize(slider.getFont().getSize());
221:                        int thumbHeight = SubstanceSizeUtils
222:                                .getSliderIconSize(slider.getFont().getSize());
223:                        int centerSpacing = thumbHeight;
224:                        if (slider.getPaintTicks()) {
225:                            // centerSpacing += getTickLength();
226:                            centerSpacing += tickLength;
227:                        }
228:                        // Assume uniform labels.
229:                        centerSpacing += metrics.getAscent()
230:                                + metrics.getDescent();
231:                        int trackY = insets.top + focusInsets.top
232:                                + (contentHeight - centerSpacing - 1) / 2;
233:
234:                        int trackHeight = thumbHeight;
235:                        int tickY = trackY + trackHeight;
236:                        int tickHeight = tickLength;
237:                        if (!slider.getPaintTicks()) {
238:                            tickHeight = 0;
239:                        }
240:                        int labelY = tickY + tickHeight;
241:                        return labelY + metrics.getAscent();
242:                    } else { // vertical
243:                        boolean inverted = slider.getInverted();
244:                        Integer value = inverted ? getMinSliderValue(slider)
245:                                : getMaxSliderValue(slider);
246:                        if (value != null) {
247:                            int thumbHeight = 11;
248:                            thumbHeight = SubstanceSizeUtils
249:                                    .getSliderIconSize(slider.getFont()
250:                                            .getSize());
251:                            int trackBuffer = Math.max(metrics.getHeight() / 2,
252:                                    thumbHeight / 2);
253:                            int contentY = focusInsets.top + insets.top;
254:                            int trackY = contentY + trackBuffer;
255:                            int trackHeight = height - focusInsets.top
256:                                    - focusInsets.bottom - insets.top
257:                                    - insets.bottom - trackBuffer - trackBuffer;
258:                            int maxValue = getMaxSliderValue(slider).intValue();
259:                            int min = slider.getMinimum();
260:                            int max = slider.getMaximum();
261:                            double valueRange = (double) max - (double) min;
262:                            double pixelsPerValue = trackHeight / valueRange;
263:                            int trackBottom = trackY + (trackHeight - 1);
264:                            int yPosition = trackY;
265:                            double offset;
266:
267:                            if (!inverted) {
268:                                offset = pixelsPerValue
269:                                        * ((double) max - value.intValue());
270:                            } else {
271:                                offset = pixelsPerValue
272:                                        * ((double) value.intValue() - min);
273:                            }
274:                            yPosition = Math.max(trackY, yPosition);
275:                            yPosition = Math.min(trackBottom, yPosition);
276:                            return yPosition - metrics.getHeight() / 2
277:                                    + metrics.getAscent();
278:                        }
279:                    }
280:                }
281:                return -1;
282:            }
283:
284:            private static Integer getMaxSliderValue(JSlider slider) {
285:                Dictionary dictionary = slider.getLabelTable();
286:                if (dictionary != null) {
287:                    Enumeration keys = dictionary.keys();
288:                    int max = slider.getMinimum() - 1;
289:                    while (keys.hasMoreElements()) {
290:                        max = Math.max(max, ((Integer) keys.nextElement())
291:                                .intValue());
292:                    }
293:                    if (max == slider.getMinimum() - 1) {
294:                        return null;
295:                    }
296:                    return new Integer(max);
297:                }
298:                return null;
299:            }
300:
301:            private static Integer getMinSliderValue(JSlider slider) {
302:                Dictionary dictionary = slider.getLabelTable();
303:                if (dictionary != null) {
304:                    Enumeration keys = dictionary.keys();
305:                    int min = slider.getMaximum() + 1;
306:                    while (keys.hasMoreElements()) {
307:                        min = Math.min(min, ((Integer) keys.nextElement())
308:                                .intValue());
309:                    }
310:                    if (min == slider.getMaximum() + 1) {
311:                        return null;
312:                    }
313:                    return new Integer(min);
314:                }
315:                return null;
316:            }
317:
318:            private static int getProgressBarBaseline(JProgressBar pb,
319:                    int height) {
320:                if (pb.isStringPainted()
321:                        && pb.getOrientation() == JProgressBar.HORIZONTAL) {
322:                    FontMetrics metrics = pb.getFontMetrics(pb.getFont());
323:                    Insets insets = pb.getInsets();
324:                    int y = insets.top;
325:                    height -= insets.top + insets.bottom;
326:                    return y
327:                            + (height + metrics.getAscent()
328:                                    - metrics.getLeading() - metrics
329:                                    .getDescent()) / 2;
330:                }
331:                return -1;
332:            }
333:
334:            private static int getTreeBaseline(JTree tree, int height) {
335:                int rowHeight = tree.getRowHeight();
336:                if (TREE_LABEL == null) {
337:                    TREE_LABEL = new JLabel("X");
338:                    TREE_LABEL.setIcon(UIManager.getIcon("Tree.closedIcon"));
339:                }
340:                JLabel label = TREE_LABEL;
341:                label.setFont(tree.getFont());
342:                if (rowHeight <= 0) {
343:                    rowHeight = label.getPreferredSize().height;
344:                }
345:                return getLabelBaseline(label, rowHeight)
346:                        + tree.getInsets().top;
347:            }
348:
349:            private static int getTableBaseline(JTable table, int height) {
350:                if (TABLE_LABEL == null) {
351:                    TABLE_LABEL = new JLabel("");
352:                    TABLE_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1));
353:                }
354:                JLabel label = TABLE_LABEL;
355:                label.setFont(table.getFont());
356:                int rowMargin = table.getRowMargin();
357:                int baseline = getLabelBaseline(label, table.getRowHeight()
358:                        - rowMargin);
359:                return baseline += rowMargin / 2;
360:            }
361:
362:            private static int getTextAreaBaseline(JTextArea text, int height) {
363:                Insets insets = text.getInsets();
364:                FontMetrics fm = text.getFontMetrics(text.getFont());
365:                return insets.top + fm.getAscent();
366:            }
367:
368:            private static int getListBaseline(JList list, int height) {
369:                int rowHeight = list.getFixedCellHeight();
370:                if (LIST_LABEL == null) {
371:                    LIST_LABEL = new JLabel("X");
372:                    LIST_LABEL.setBorder(new EmptyBorder(1, 1, 1, 1));
373:                }
374:                JLabel label = LIST_LABEL;
375:                label.setFont(list.getFont());
376:                // JList actually has much more complex behavior here.
377:                // If rowHeight != -1 the rowHeight is either the max of all cell
378:                // heights (layout orientation != VERTICAL), or is variable depending
379:                // upon the cell. We assume a default size.
380:                // We could theoretically query the real renderer, but that would
381:                // not work for an empty model and the results may vary with
382:                // the content.
383:                if (rowHeight == -1) {
384:                    rowHeight = label.getPreferredSize().height;
385:                }
386:                return getLabelBaseline(label, rowHeight)
387:                        + list.getInsets().top;
388:            }
389:
390:            private static int getScrollPaneBaseline(JScrollPane sp, int height) {
391:                Component view = sp.getViewport().getView();
392:                if (view instanceof  JComponent) {
393:                    int baseline = getBaseline((JComponent) view);
394:                    if (baseline > 0) {
395:                        return baseline + sp.getViewport().getY();
396:                    }
397:                }
398:                return -1;
399:            }
400:
401:            private static int getPanelBaseline(JPanel panel, int height) {
402:                Border border = panel.getBorder();
403:                if (border instanceof  TitledBorder) {
404:                    TitledBorder titledBorder = (TitledBorder) border;
405:                    if (titledBorder.getTitle() != null
406:                            && !"".equals(titledBorder.getTitle())) {
407:                        Font font = titledBorder.getTitleFont();
408:                        if (font == null) {
409:                            font = panel.getFont();
410:                            if (font == null) {
411:                                font = new Font("Dialog", Font.PLAIN, 12);
412:                            }
413:                        }
414:                        Border border2 = titledBorder.getBorder();
415:                        Insets borderInsets;
416:                        if (border2 != null) {
417:                            borderInsets = border2.getBorderInsets(panel);
418:                        } else {
419:                            borderInsets = EMPTY_INSETS;
420:                        }
421:                        FontMetrics fm = panel.getFontMetrics(font);
422:                        int fontHeight = fm.getHeight();
423:                        int descent = fm.getDescent();
424:                        int ascent = fm.getAscent();
425:                        int y = EDGE_SPACING;
426:                        int h = height - EDGE_SPACING * 2;
427:                        int diff;
428:                        switch (((TitledBorder) border).getTitlePosition()) {
429:                        case TitledBorder.ABOVE_TOP:
430:                            diff = ascent
431:                                    + descent
432:                                    + (Math.max(EDGE_SPACING, TEXT_SPACING * 2) - EDGE_SPACING);
433:                            return y + diff - (descent + TEXT_SPACING);
434:                        case TitledBorder.TOP:
435:                        case TitledBorder.DEFAULT_POSITION:
436:                            diff = Math.max(0, ((ascent / 2) + TEXT_SPACING)
437:                                    - EDGE_SPACING);
438:                            return (y + diff - descent)
439:                                    + (borderInsets.top + ascent + descent) / 2;
440:                        case TitledBorder.BELOW_TOP:
441:                            return y + borderInsets.top + ascent + TEXT_SPACING;
442:                        case TitledBorder.ABOVE_BOTTOM:
443:                            return (y + h)
444:                                    - (borderInsets.bottom + descent + TEXT_SPACING);
445:                        case TitledBorder.BOTTOM:
446:                            h -= fontHeight / 2;
447:                            return ((y + h) - descent)
448:                                    + ((ascent + descent) - borderInsets.bottom)
449:                                    / 2;
450:                        case TitledBorder.BELOW_BOTTOM:
451:                            h -= fontHeight;
452:                            return y + h + ascent + TEXT_SPACING;
453:                        }
454:                    }
455:                }
456:                return -1;
457:            }
458:
459:            private static int getSpinnerBaseline(JSpinner spinner, int height) {
460:                JComponent editor = spinner.getEditor();
461:                if (editor instanceof  JSpinner.DefaultEditor) {
462:                    JSpinner.DefaultEditor defaultEditor = (JSpinner.DefaultEditor) editor;
463:                    JTextField tf = defaultEditor.getTextField();
464:                    Insets spinnerInsets = spinner.getInsets();
465:                    Insets editorInsets = defaultEditor.getInsets();
466:                    int offset = spinnerInsets.top + editorInsets.top;
467:                    height -= (offset + spinnerInsets.bottom + editorInsets.bottom);
468:                    if (height <= 0) {
469:                        return -1;
470:                    }
471:                    return offset + getSingleLineTextBaseline(tf, height);
472:                }
473:                Insets insets = spinner.getInsets();
474:                FontMetrics fm = spinner.getFontMetrics(spinner.getFont());
475:                return insets.top + fm.getAscent();
476:            }
477:
478:            private static int getLabelBaseline(JLabel label, int height) {
479:                Icon icon = (label.isEnabled()) ? label.getIcon() : label
480:                        .getDisabledIcon();
481:                FontMetrics fm = label.getFontMetrics(label.getFont());
482:
483:                resetRects(label, height);
484:
485:                SwingUtilities.layoutCompoundLabel(label, fm, "a", icon, label
486:                        .getVerticalAlignment(),
487:                        label.getHorizontalAlignment(), label
488:                                .getVerticalTextPosition(), label
489:                                .getHorizontalTextPosition(), viewRect,
490:                        iconRect, textRect, label.getIconTextGap());
491:
492:                return textRect.y + fm.getAscent();
493:            }
494:
495:            private static int getComboBoxBaseline(JComboBox combobox,
496:                    int height) {
497:                Insets insets = combobox.getInsets();
498:                int y = insets.top;
499:                height -= (insets.top + insets.bottom);
500:                if (combobox.isEditable()) {
501:                    ComboBoxEditor editor = combobox.getEditor();
502:                    if (editor != null
503:                            && (editor.getEditorComponent() instanceof  JTextField)) {
504:                        JTextField tf = (JTextField) editor
505:                                .getEditorComponent();
506:                        return y + getSingleLineTextBaseline(tf, height);
507:                    }
508:                }
509:                // Use the renderer to calculate baseline
510:                y += 2;
511:                height -= 4;
512:                ListCellRenderer renderer = combobox.getRenderer();
513:                if (renderer instanceof  JLabel) {
514:                    int baseline = y
515:                            + getLabelBaseline((JLabel) renderer, height);
516:                    return baseline;
517:                }
518:                // Renderer isn't a label, use metrics directly.
519:                FontMetrics fm = combobox.getFontMetrics(combobox.getFont());
520:                return y + fm.getAscent();
521:            }
522:
523:            /**
524:             * Returns the baseline for single line text components, like
525:             * <code>JTextField</code>.
526:             */
527:            private static int getSingleLineTextBaseline(
528:                    JTextComponent textComponent, int h) {
529:                View rootView = textComponent.getUI()
530:                        .getRootView(textComponent);
531:                if (rootView.getViewCount() > 0) {
532:                    Insets insets = textComponent.getInsets();
533:                    int height = h - insets.top - insets.bottom;
534:                    int y = insets.top;
535:                    View fieldView = rootView.getView(0);
536:                    int vspan = (int) fieldView.getPreferredSpan(View.Y_AXIS);
537:                    if (height != vspan) {
538:                        int slop = height - vspan;
539:                        y += slop / 2;
540:                    }
541:                    FontMetrics fm = textComponent.getFontMetrics(textComponent
542:                            .getFont());
543:                    y += fm.getAscent();
544:                    return y;
545:                }
546:                return -1;
547:            }
548:
549:            /**
550:             * Returns the baseline for buttons.
551:             */
552:            private static int getButtonBaseline(AbstractButton button,
553:                    int height) {
554:                FontMetrics fm = button.getFontMetrics(button.getFont());
555:
556:                resetRects(button, height);
557:
558:                String text = button.getText();
559:                if (text != null && text.startsWith("<html>")) {
560:                    return -1;
561:                }
562:                // NOTE: that we use "a" here to make sure we get a valid value, if
563:                // we were to pass in an empty string or null we would not get
564:                // back the right thing.
565:                SwingUtilities.layoutCompoundLabel(button, fm, "a", button
566:                        .getIcon(), button.getVerticalAlignment(), button
567:                        .getHorizontalAlignment(), button
568:                        .getVerticalTextPosition(), button
569:                        .getHorizontalTextPosition(), viewRect, iconRect,
570:                        textRect, text == null ? 0 : button.getIconTextGap());
571:
572:                return textRect.y + fm.getAscent();
573:            }
574:
575:            private static void resetRects(JComponent c, int height) {
576:                Insets insets = c.getInsets();
577:                viewRect.x = insets.left;
578:                viewRect.y = insets.top;
579:                viewRect.width = c.getWidth() - (insets.right + viewRect.x);
580:                viewRect.height = height - (insets.bottom + viewRect.y);
581:                textRect.x = textRect.y = textRect.width = textRect.height = 0;
582:                iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
583:            }
584:
585:            private static boolean isOceanTheme() {
586:                try {
587:                    java.lang.reflect.Field field = MetalLookAndFeel.class
588:                            .getDeclaredField("currentTheme");
589:                    field.setAccessible(true);
590:                    Object theme = field.get(null);
591:                    return "javax.swing.plaf.metal.OceanTheme".equals(theme
592:                            .getClass().getName());
593:                } catch (Exception ex) {
594:                    ex.printStackTrace();
595:                }
596:                return false;
597:            }
598:
599:            /**
600:             * Creates an instance of Baseline. You typically don't create a Baseline.
601:             * The constructor is provided by look and feels that wish to provide
602:             * baseline support.
603:             * <p>
604:             * A custom look and feel that wants to provide <code>Baseline</code>
605:             * support should put the instance in the defaults returned from
606:             * <code>getDefaults</code>. If you want to override the baseline suport
607:             * for a look and feel place the instance in the defaults returned from
608:             * UIManager.getLookAndFeelDefaults(). Tthis will ensure that if the look
609:             * and feel changes the appropriate baseline can be used.
610:             */
611:            public SubstanceBaseline() {
612:                if (MemoryAnalyzer.isRunning())
613:                    MemoryAnalyzer
614:                            .enqueueUsage("SubstanceBaseline instantiated");
615:            }
616:
617:            /**
618:             * Returns the baseline for the specified component, or -1 if the baseline
619:             * can not be determined. The baseline is measured from the top of the
620:             * component.
621:             * 
622:             * @param component
623:             *            JComponent to calculate baseline for
624:             * @param width
625:             *            Width of the component to determine baseline for.
626:             * @param height
627:             *            Height of the component to determine baseline for.
628:             * @return baseline for the specified component
629:             */
630:            @Override
631:            public int getComponentBaseline(JComponent component, int width,
632:                    int height) {
633:                String uid = component.getUIClassID();
634:                if (MemoryAnalyzer.isRunning()) {
635:                    MemoryAnalyzer
636:                            .enqueueUsage("Computing baseline for " + uid);
637:                }
638:                int baseline = -1;
639:                if (uid == "ButtonUI" || uid == "CheckBoxUI"
640:                        || uid == "RadioButtonUI" || uid == "ToggleButtonUI") {
641:                    baseline = getButtonBaseline((AbstractButton) component,
642:                            height);
643:                } else if (uid == "ComboBoxUI") {
644:                    return getComboBoxBaseline((JComboBox) component, height);
645:                } else if (uid == "TextAreaUI") {
646:                    return getTextAreaBaseline((JTextArea) component, height);
647:                } else if (uid == "FormattedTextFieldUI"
648:                        || uid == "PasswordFieldUI" || uid == "TextFieldUI") {
649:                    baseline = getSingleLineTextBaseline(
650:                            (JTextComponent) component, height);
651:                } else if (uid == "LabelUI") {
652:                    baseline = getLabelBaseline((JLabel) component, height);
653:                } else if (uid == "ListUI") {
654:                    baseline = getListBaseline((JList) component, height);
655:                } else if (uid == "PanelUI") {
656:                    baseline = getPanelBaseline((JPanel) component, height);
657:                } else if (uid == "ProgressBarUI") {
658:                    baseline = getProgressBarBaseline((JProgressBar) component,
659:                            height);
660:                } else if (uid == "SliderUI") {
661:                    baseline = getSliderBaseline((JSlider) component, height);
662:                } else if (uid == "SpinnerUI") {
663:                    baseline = getSpinnerBaseline((JSpinner) component, height);
664:                } else if (uid == "ScrollPaneUI") {
665:                    baseline = getScrollPaneBaseline((JScrollPane) component,
666:                            height);
667:                } else if (uid == "TabbedPaneUI") {
668:                    baseline = getTabbedPaneBaseline((JTabbedPane) component,
669:                            height);
670:                } else if (uid == "TableUI") {
671:                    baseline = getTableBaseline((JTable) component, height);
672:                } else if (uid == "TreeUI") {
673:                    baseline = getTreeBaseline((JTree) component, height);
674:                }
675:                return Math.max(baseline, -1);
676:            }
677:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.