PageRenderTime 530ms CodeModel.GetById 306ms app.highlight 19ms RepoModel.GetById 203ms app.codeStats 0ms

/src/mpv5/ui/beans/ExpandablePanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 161 lines | 101 code | 27 blank | 33 comment | 2 complexity | 43be60e5e05e57615a92789563363a5c MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * ExpandablePanel.java
  8 *
  9 * Created on Aug 17, 2010, 4:10:12 PM
 10 */
 11package mpv5.ui.beans;
 12
 13import java.awt.Color;
 14import java.awt.Dimension;
 15import java.awt.Font;
 16import java.awt.Graphics;
 17import java.awt.Graphics2D;
 18import java.awt.GridBagConstraints;
 19import java.awt.GridBagLayout;
 20import java.awt.Insets;
 21import java.awt.RenderingHints;
 22import java.awt.event.MouseEvent;
 23import java.awt.event.MouseListener;
 24import java.awt.font.FontRenderContext;
 25import java.awt.font.LineMetrics;
 26import java.awt.image.BufferedImage;
 27import javax.swing.JLabel;
 28import javax.swing.JPanel;
 29
 30/**
 31 *
 32 * @author andreas.weber
 33 */
 34public class ExpandablePanel extends javax.swing.JPanel {
 35
 36    public ExpandablePanel() {
 37        this("+", new JPanel());
 38    }
 39
 40    public JPanel getContentPane() {
 41        return contentPanel_;
 42    }
 43
 44    /** This method is called from within the constructor to
 45     * initialize the form.
 46     * WARNING: Do NOT modify this code. The content of this method is
 47     * always regenerated by the Form Editor.
 48     */
 49    @SuppressWarnings("unchecked")
 50    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 51    private void initComponents() {
 52
 53        setName("Form"); // NOI18N
 54        setLayout(new java.awt.BorderLayout());
 55    }// </editor-fold>//GEN-END:initComponents
 56    // Variables declaration - do not modify//GEN-BEGIN:variables
 57    // End of variables declaration//GEN-END:variables
 58    private boolean selected;
 59    private JPanel contentPanel_;
 60    private HeaderPanel headerPanel_;
 61
 62    private class HeaderPanel extends JPanel implements MouseListener {
 63
 64        private String text_;
 65        private Font font;
 66        private BufferedImage open, closed;
 67        final int OFFSET = 30, PAD = 5;
 68
 69        public HeaderPanel(String text) {
 70            addMouseListener(this);
 71            text_ = text;
 72            font = new Font("sans-serif", Font.PLAIN, 12);
 73            // setRequestFocusEnabled(true);
 74            setPreferredSize(new Dimension(200, 20));
 75            int w = getWidth();
 76            int h = getHeight();
 77
 78            /*try {
 79            open = ImageIO.read(new File("images/arrow_down_mini.png"));
 80            closed = ImageIO.read(new File("images/arrow_right_mini.png"));
 81            } catch (IOException e) {
 82            e.printStackTrace();
 83            }*/
 84
 85        }
 86
 87        @Override
 88        protected void paintComponent(Graphics g) {
 89            super.paintComponent(g);
 90            Graphics2D g2 = (Graphics2D) g;
 91            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 92                    RenderingHints.VALUE_ANTIALIAS_ON);
 93            int h = getHeight();
 94            /*if (selected)
 95            g2.drawImage(open, PAD, 0, h, h, this);
 96            else
 97            g2.drawImage(closed, PAD, 0, h, h, this);
 98             */ // Uncomment once you have your own images
 99            g2.setFont(font);
100            FontRenderContext frc = g2.getFontRenderContext();
101            LineMetrics lm = font.getLineMetrics(text_, frc);
102            float height = lm.getAscent() + lm.getDescent();
103            float x = OFFSET;
104            float y = (h + height) / 2 - lm.getDescent();
105            g2.drawString(text_, x, y);
106        }
107
108        public void mouseClicked(MouseEvent e) {
109            toggleSelection();
110        }
111
112        public void mouseEntered(MouseEvent e) {
113        }
114
115        public void mouseExited(MouseEvent e) {
116        }
117
118        public void mousePressed(MouseEvent e) {
119        }
120
121        public void mouseReleased(MouseEvent e) {
122        }
123    }
124
125    public ExpandablePanel(String text, JPanel content) {
126        super(new GridBagLayout());
127        GridBagConstraints gbc = new GridBagConstraints();
128        gbc.insets = new Insets(1, 3, 0, 3);
129        gbc.weightx = 1.0;
130        gbc.fill = GridBagConstraints.HORIZONTAL;
131        gbc.gridwidth = GridBagConstraints.REMAINDER;
132
133        selected = false;
134        headerPanel_ = new HeaderPanel(text);
135
136        setBackground(new Color(200, 200, 220));
137        contentPanel_ = content;
138
139        add(headerPanel_, gbc);
140        add(contentPanel_, gbc);
141        contentPanel_.setVisible(false);
142
143        JLabel padding = new JLabel();
144        gbc.weighty = 1.0;
145        add(padding, gbc);
146
147    }
148
149    public void toggleSelection() {
150        selected = !selected;
151
152        if (contentPanel_.isShowing()) {
153            contentPanel_.setVisible(false);
154        } else {
155            contentPanel_.setVisible(true);
156        }
157
158        validate();
159        headerPanel_.repaint();
160    }
161}