/src/mpv5/ui/beans/ExpandablePanel.java
Java | 161 lines | 101 code | 27 blank | 33 comment | 2 complexity | 43be60e5e05e57615a92789563363a5c MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * ExpandablePanel.java
- *
- * Created on Aug 17, 2010, 4:10:12 PM
- */
- package mpv5.ui.beans;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.RenderingHints;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.font.FontRenderContext;
- import java.awt.font.LineMetrics;
- import java.awt.image.BufferedImage;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- /**
- *
- * @author andreas.weber
- */
- public class ExpandablePanel extends javax.swing.JPanel {
- public ExpandablePanel() {
- this("+", new JPanel());
- }
- public JPanel getContentPane() {
- return contentPanel_;
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- setName("Form"); // NOI18N
- setLayout(new java.awt.BorderLayout());
- }// </editor-fold>//GEN-END:initComponents
- // Variables declaration - do not modify//GEN-BEGIN:variables
- // End of variables declaration//GEN-END:variables
- private boolean selected;
- private JPanel contentPanel_;
- private HeaderPanel headerPanel_;
- private class HeaderPanel extends JPanel implements MouseListener {
- private String text_;
- private Font font;
- private BufferedImage open, closed;
- final int OFFSET = 30, PAD = 5;
- public HeaderPanel(String text) {
- addMouseListener(this);
- text_ = text;
- font = new Font("sans-serif", Font.PLAIN, 12);
- // setRequestFocusEnabled(true);
- setPreferredSize(new Dimension(200, 20));
- int w = getWidth();
- int h = getHeight();
- /*try {
- open = ImageIO.read(new File("images/arrow_down_mini.png"));
- closed = ImageIO.read(new File("images/arrow_right_mini.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }*/
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- int h = getHeight();
- /*if (selected)
- g2.drawImage(open, PAD, 0, h, h, this);
- else
- g2.drawImage(closed, PAD, 0, h, h, this);
- */ // Uncomment once you have your own images
- g2.setFont(font);
- FontRenderContext frc = g2.getFontRenderContext();
- LineMetrics lm = font.getLineMetrics(text_, frc);
- float height = lm.getAscent() + lm.getDescent();
- float x = OFFSET;
- float y = (h + height) / 2 - lm.getDescent();
- g2.drawString(text_, x, y);
- }
- public void mouseClicked(MouseEvent e) {
- toggleSelection();
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- }
- public ExpandablePanel(String text, JPanel content) {
- super(new GridBagLayout());
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.insets = new Insets(1, 3, 0, 3);
- gbc.weightx = 1.0;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- selected = false;
- headerPanel_ = new HeaderPanel(text);
- setBackground(new Color(200, 200, 220));
- contentPanel_ = content;
- add(headerPanel_, gbc);
- add(contentPanel_, gbc);
- contentPanel_.setVisible(false);
- JLabel padding = new JLabel();
- gbc.weighty = 1.0;
- add(padding, gbc);
- }
- public void toggleSelection() {
- selected = !selected;
- if (contentPanel_.isShowing()) {
- contentPanel_.setVisible(false);
- } else {
- contentPanel_.setVisible(true);
- }
- validate();
- headerPanel_.repaint();
- }
- }