View Javadoc
1   package net.logAnalyzer.utils.gui.quickbar;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Dimension;
6   import java.awt.FlowLayout;
7   import java.awt.GradientPaint;
8   import java.awt.Graphics;
9   import java.awt.Graphics2D;
10  import java.awt.Insets;
11  import java.awt.event.ActionListener;
12  
13  import javax.swing.Icon;
14  import javax.swing.JPanel;
15  import javax.swing.border.Border;
16  
17  import net.logAnalyzer.gui.LAFrame;
18  import net.logAnalyzer.utils.gui.GraphicsAttributes;
19  
20  public abstract class QuickPanel extends JPanel implements ActionListener {
21      private class QuickPanelBorder implements Border {
22  
23          public Insets getBorderInsets(Component c) {
24              return new Insets(0, 10, 10, 10);
25          }
26  
27          public boolean isBorderOpaque() {
28              return false;
29          }
30  
31          public void paintBorder(Component c, Graphics g, int x, int y,
32                  int width, int height) {
33              Graphics2D gfx = (Graphics2D) g;
34              // Actions background
35              gfx.setPaint(GraphicsAttributes.BACKGROUND_COLOR);
36              gfx.fillRect(x, y + 7, x + width, y + height);
37              // Action border
38              gfx.setPaint(GraphicsAttributes.QUICKPANEL_BORDER_COLOR);
39              g.fillRect(x, y + 7, x + 1, y + height);
40              g.fillRect(x + width - 1, y + 7, x + width, y + height);
41              g.fillRect(x, y + height - 1, x + width, y + height);
42              // Title background
43              gfx.setPaint(new GradientPaint(x + width / 2, y, GraphicsAttributes.QUICKTITLE_GRADIENTSTART_COLOR,
44                      width, y, GraphicsAttributes.QUICKTITLE_GRADIENTEND_COLOR));
45              gfx.fillRoundRect(x, y, x + width, y + 14, 7, 7);
46              gfx.fillRoundRect(x, y, x + width, y + 14, 7, 7);
47              if (quickTitle != null) {
48                  gfx.fillRect(x, y + 3, x + width, y
49                          + getInsets().top
50                          + (int) (quickTitle.getLocation().getY() + quickTitle
51                                  .getPreferredSize().getHeight()));
52              }
53          }
54  
55      }
56  
57      private QuickTitle quickTitle;
58  
59      private QuickAction[] quickActions;
60  
61      public QuickPanel() {
62          super();
63          setBorder(new QuickPanelBorder());
64          addComponents();
65      }
66  
67      /***
68       * Paint no background in the component.
69       */
70      public void paintComponent(Graphics g) {
71          // NOP
72      }
73  
74      private LAFrame frame;
75  
76      public void setFrame(LAFrame frame) {
77          this.frame = frame;
78      }
79  
80      public LAFrame getFrame() {
81          return frame;
82      }
83  
84      public abstract Icon getIcon();
85  
86      public abstract String getTitle();
87  
88      public abstract QuickAction[] getActions();
89  
90      /***
91       * Adds the components and computes the height of the panel.
92       * 
93       */
94      protected void addComponents() {
95          setLayout(new FlowLayout(FlowLayout.LEFT));
96          int newHeight = getInsets().top + getInsets().bottom;
97          // Add title
98          quickTitle = new QuickTitle(getTitle(), getIcon());
99          add(quickTitle);
100         newHeight += quickTitle.getPreferredSize().getHeight();
101         // Add actions
102         quickActions = getActions();
103         for (int i = 0; i < quickActions.length; i++) {
104             QuickAction lastAction = quickActions[i];
105             lastAction.addActionListener(this);
106             add((Component) lastAction);
107             newHeight += ((Component) lastAction).getPreferredSize()
108                     .getHeight()
109                     + ((FlowLayout) getLayout()).getVgap();
110         }
111         setPreferredSize(new Dimension((int) getPreferredSize().getWidth(),
112                 newHeight));
113     }
114 }