de.fhg.iais.asc.ui.parts.TopLabeledElementJPanelBuilder.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.ui.parts.TopLabeledElementJPanelBuilder.java

Source

package de.fhg.iais.asc.ui.parts;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.commons.lang.StringUtils;

import de.fhg.iais.asc.ui.i18n.Messages;

public final class TopLabeledElementJPanelBuilder {
    private static final double WEIGHTY_LABEL = 0.1;
    private static final double WEIGHTY_ELEMENT = 0.1;

    private final String prefix;

    private final JPanel panel = new JPanel(new GridBagLayout());

    private final GridBagConstraints cLabel = new GridBagConstraints();
    private final GridBagConstraints cElement = new GridBagConstraints();

    //    private JLabel lastLabel;

    public TopLabeledElementJPanelBuilder(String prefix) {
        this.prefix = (StringUtils.isEmpty(prefix)) ? "" : (prefix + ".");

        this.cLabel.gridx = 0;
        this.cLabel.gridy = 0;
        this.cLabel.weightx = 0.1;
        this.cLabel.weighty = WEIGHTY_LABEL;
        this.cLabel.fill = GridBagConstraints.HORIZONTAL;

        this.cElement.gridx = 0;
        this.cElement.gridy = 1;
        this.cLabel.weightx = 0.1;
        this.cElement.weighty = WEIGHTY_ELEMENT;
        this.cElement.fill = GridBagConstraints.HORIZONTAL;
    }

    public <E extends JComponent> E add(String labelName, E element) {
        String labelText = Messages.getString(this.prefix + "Label." + labelName);
        String tooltipText = Messages.getString(this.prefix + "Tooltip." + labelName, null);

        JLabel label = new JLabel(labelText + " ");
        label.setToolTipText(tooltipText);
        this.panel.add(label, this.cLabel);
        ++this.cLabel.gridx;

        element.setToolTipText(tooltipText);
        this.panel.add(element, this.cElement);
        ++this.cElement.gridx;

        //        this.lastLabel = label;

        return element;
    }

    public JPanel getResult() {
        return this.panel;
    }

}