4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A
4632N/Apackage sun.lwawt;
4632N/A
4685N/Aimport java.awt.Dimension;
4685N/Aimport java.awt.FontMetrics;
4632N/Aimport java.awt.Label;
4632N/Aimport java.awt.peer.LabelPeer;
4632N/A
4632N/Aimport javax.swing.JLabel;
4632N/Aimport javax.swing.SwingConstants;
4632N/A
4632N/A/**
4632N/A * Lightweight implementation of {@link LabelPeer}. Delegates most of the work
4632N/A * to the {@link JLabel}.
4632N/A */
4632N/Afinal class LWLabelPeer extends LWComponentPeer<Label, JLabel>
4632N/A implements LabelPeer {
4632N/A
4685N/A private static final int TEXT_XPAD = 5;
4685N/A private static final int TEXT_YPAD = 1;
4685N/A
4632N/A LWLabelPeer(final Label target, final PlatformComponent platformComponent) {
4632N/A super(target, platformComponent);
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected JLabel createDelegate() {
4632N/A final JLabel label = new JLabel();
4632N/A label.setVerticalAlignment(SwingConstants.TOP);
4632N/A return label;
4632N/A }
4632N/A
4632N/A @Override
5166N/A void initializeImpl() {
5166N/A super.initializeImpl();
4632N/A setText(getTarget().getText());
4632N/A setAlignment(getTarget().getAlignment());
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setText(final String label) {
4632N/A synchronized (getDelegateLock()) {
4632N/A getDelegate().setText(label);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void setAlignment(final int alignment) {
4632N/A synchronized (getDelegateLock()) {
4632N/A getDelegate().setHorizontalAlignment(convertAlignment(alignment));
4632N/A }
4632N/A }
4632N/A
4685N/A @Override
4685N/A public Dimension getMinimumSize() {
4685N/A int w = TEXT_XPAD;
4685N/A int h = TEXT_YPAD;
4685N/A final FontMetrics fm = getFontMetrics(getFont());
4685N/A if (fm != null) {
4685N/A final String text;
4685N/A synchronized (getDelegateLock()) {
4685N/A text = getDelegate().getText();
4685N/A }
4685N/A if (text != null) {
4685N/A w += fm.stringWidth(text);
4685N/A }
4685N/A h += fm.getHeight();
4685N/A }
4685N/A return new Dimension(w, h);
4685N/A }
4685N/A
4632N/A /**
4632N/A * Converts {@code Label} alignment constant to the {@code JLabel} constant.
4632N/A * If wrong Label alignment provided returns default alignment.
4632N/A *
4632N/A * @param alignment {@code Label} constant.
4632N/A *
4632N/A * @return {@code JLabel} constant.
4632N/A */
4632N/A private static int convertAlignment(final int alignment) {
4632N/A switch (alignment) {
4632N/A case Label.CENTER:
4632N/A return SwingConstants.CENTER;
4632N/A case Label.RIGHT:
4632N/A return SwingConstants.RIGHT;
4632N/A default:
4632N/A return SwingConstants.LEFT;
4632N/A }
4632N/A }
4632N/A}