All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.util.swing.ASBodyPanel Maven / Gradle / Ivy

/**
 * Copyright (C) 2010-2012 Agrosense [email protected]
 *
 * Licensed under the Eclipse Public License - v 1.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.eclipse.org/legal/epl-v10.html
 *
 * 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.
 */
package nl.cloudfarming.client.util.swing;

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;

/**
 * Panel which contains AgroSense header background. The constructor needs a
 * LayoutManger for the body area of this panel. there are two areas which can
 * be manipulated. The Title area: use setTitle to add a title The body area:
 * use the default add methods of component to add components to it
 *
 * @author Timon Veenstra 
 * @author Merijn Zengers
 */
public class ASBodyPanel extends JPanel {

    private AgroSenseHeader header;
    private JPanel bodyPanel;
    private static final int PANEL_HEIGHT = 400;

    /**
     * Adding components directly to the body panel is deprecated. Inject a
     * custom body panel instead
     *
     * @deprecated use {@link ASBodyPanel#ASBodyPanel(javax.swing.JPanel) }
     * @param bodyLayoutManager
     */
    public ASBodyPanel(LayoutManager bodyLayoutManager) {
        this.bodyPanel = new JPanel(bodyLayoutManager);
        initComponents();
    }

    /**
     * Adding components directly to the body panel is deprecated. Inject a
     * custom body panel instead
     *
     * @deprecated use {@link ASBodyPanel#ASBodyPanel(javax.swing.JPanel) }
     */
    public ASBodyPanel() {
        this.bodyPanel = new JPanel();
        initComponents();
    }

    /**
     * Create a new panel with given bodyPanel as body
     *
     * @param bodyPanel
     */
    public ASBodyPanel(JPanel bodyPanel) {
        this.bodyPanel = bodyPanel;
        initComponents();
    }

    private void initComponents() {
        setLayout(new MigLayout(
                "wrap 1, insets 0", // Layout Constraints
                "[grow]", // Column constraints with default align
                "[min!][grow]" // Row constraints with default align                
                ));
        setBackground(Color.white);

        bodyPanel.setBackground(Color.white);
        header = new AgroSenseHeader();
        Dimension size = new Dimension(header.getWidth(), PANEL_HEIGHT);
        setPreferredSize(size);
//        setMinimumSize(size);
//        setMaximumSize(size);
//        setSize(size);
        super.add(header);
        super.add(bodyPanel, "grow");
    }

    /**
     * Sets the title of this panel. This will be displayed in the title area of
     * the panel
     *
     * @param title the title to set.
     */
    public void setTitle(String title) {
        header.setTitle(title);
    }

    /**
     * Set the icon for this panel Will place this icon before the title
     *
     * @param icon the icon to set
     */
    public void setIcon(ImageIcon icon) {
        header.setIcon(icon);
    }

    /**
     *
     * @deprecated Do not add components to this panel directly, instead use the
     * constructor with a panel argument
     * @param component
     * @return
     */
    @Override
    public Component add(Component component) {
        return bodyPanel.add(component);
    }

    /**
     * @deprecated Do not add components to this panel directly, instead use the
     * constructor with a panel argument
     * @param component
     * @param constraints
     */
    @Override
    public void add(Component component, Object constraints) {
        bodyPanel.add(component, constraints);
    }

    private class AgroSenseHeader extends AgroSenseImage {

        private JLabel title;
        private JLabel iconLabel;

        public AgroSenseHeader() {
            init();
        }

        private void init() {
            this.setLayout(new MigLayout("wrap 2, insets 0 0 0 0"));
            iconLabel = new JLabel();
            iconLabel.setPreferredSize(new Dimension(0, 0));
            title = new JLabel();
            title.setFont(new java.awt.Font("DejaVu Sans", 0, 24));
            this.add(iconLabel, "gap top 40, gap left 12");
            this.add(title, "gap top 40, gap left 5");

        }

        void setIcon(ImageIcon icon) {
            iconLabel.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
            iconLabel.setIcon(icon);
            this.repaint();
        }

        void setTitle(String title) {
            this.title.setText(title);
            this.repaint();
        }
    }

    @NbBundle.Messages("asbodypanel.background.image=nl/cloudfarming/client/util/swing/background-base600.png")
    private class AgroSenseImage extends JPanel {

        private Image background = ImageUtilities.loadImage(Bundle.asbodypanel_background_image(), true);

        public AgroSenseImage() {
            Dimension size = new Dimension(background.getWidth(null), background.getHeight(null));
            setPreferredSize(size);
            setMinimumSize(size);
            setMaximumSize(size);
            setSize(size);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g.drawImage(background, 0, 0, getWidth(), background.getHeight(this), null);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy