Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/trapezium/chisel/gui/TitleBar.java


1   /*  TitleBar
2    *
3    */
4   
5   package com.trapezium.chisel.gui;
6   
7   import java.awt.*;
8   import java.awt.event.*;
9   
10  /** A title bar control for a movable window.  The title bar displays a title,
11   *  moves the window which owns it.  By default it assumes its parent is the
12   *  window to move, but any ancestor can be specified.  The title bar can also
13   *  contain up to three buttons (minimize, maximize and close) as specified
14   *  in the style parameter.  The default is a maximize and a close button.
15   */
16  public class TitleBar extends ChiselAWTPane implements ActionListener {
17  
18      public static final int CLOSEBUTTON = 1;
19      public static final int MINBUTTON = 2;
20      public static final int MAXBUTTON = 4;
21  
22      String title;
23      int style;
24  
25      GlyphButton minButton = null;
26      GlyphButton maxButton = null;
27      GlyphButton closeButton = null;
28  
29      boolean activated = false;
30  
31      public TitleBar(String title) {
32          this(title, CLOSEBUTTON | MAXBUTTON, 1);
33      }
34  
35      public TitleBar(String title, int style) {
36          this(title, style, 1);
37      }
38  
39      public TitleBar(String title, int style, int ancestor) {
40          super(NO_BORDER);
41          this.title = title;
42          this.style = style;
43  
44          setBackground(DEFAULT_INACTIVECOLOR);
45          setForeground(DEFAULT_TITLETEXTCOLOR);
46          setFont(new Font("Dialog", Font.BOLD, 12));
47          setLayout(null);
48          if ((style & MINBUTTON) != 0) {
49              minButton = new GlyphButton(GlyphButton.MINIMIZE);
50              minButton.setForeground(Color.blue);
51              minButton.addActionListener(this);
52              add(minButton);
53          }
54          if ((style & MAXBUTTON) != 0) {
55              maxButton = new GlyphButton(GlyphButton.MAXIMIZE);
56              maxButton.setForeground(Color.black);
57              maxButton.addActionListener(this);
58              add(maxButton);
59          }
60  
61          if ((style & CLOSEBUTTON) != 0) {
62              closeButton = new GlyphButton(GlyphButton.CLOSE);
63              closeButton.setForeground(Color.red);
64              closeButton.addActionListener(this);
65              add(closeButton);
66          }
67  
68          Mover mover = new Mover(this, ancestor);
69          addMouseListener(mover);
70          addMouseMotionListener(mover);
71      }
72  
73      /** one of the window buttons fired */
74    public void actionPerformed( ActionEvent e ) {
75        Object source = e.getSource();
76        ChiselAWTViewer viewer = (ChiselAWTViewer) getParent();
77        if (source == minButton) {
78            //System.out.println("minimize");
79            viewer.minimize();
80        } else if (source == maxButton) {
81            //System.out.println("maximize");
82            viewer.maximize();
83        } else if (source == closeButton) {
84            //System.out.println("close");
85            viewer.close();
86        }
87    }
88  
89  
90      public void paint(Graphics g) {
91          super.paint(g);
92          if (title != null && title.length() > 0) {
93              //g.clipRect(0, 0, getSize().width - minButton.getLocation().x, getSize().height);
94              g.setColor(getForeground());
95          Font font = getFont();
96          FontMetrics fm = getFontMetrics(font);
97              g.setFont(font);
98              g.drawString(title, 4, 2 + fm.getAscent());
99          }
100     }
101 
102 
103     public void doLayout() {
104         Dimension size = getSize();
105 
106         int hgap = 2;
107 
108         Dimension buttonsize;
109         int x = size.width;
110         int y;
111 
112         if (closeButton != null) {
113             buttonsize = closeButton.getPreferredSize();
114             x -= hgap + buttonsize.width;
115             y = (size.height - buttonsize.height) / 2;
116             closeButton.setBounds( x, y, buttonsize.width, buttonsize.height);
117         }
118         if (maxButton != null) {
119             buttonsize = maxButton.getPreferredSize();
120             x -= hgap + buttonsize.width;
121             y = (size.height - buttonsize.height) / 2;
122             maxButton.setBounds( x, y, buttonsize.width, buttonsize.height);
123         }
124         if (minButton != null) {
125             buttonsize = minButton.getPreferredSize();
126             x -= hgap + buttonsize.width;
127             y = (size.height - buttonsize.height) / 2;
128             minButton.setBounds( x, y, buttonsize.width, buttonsize.height);
129         }
130     }
131 
132     public Dimension getPreferredSize() {
133         Dimension size = (closeButton != null ? closeButton.getPreferredSize() : new Dimension(12, 12));
134         size.width *= 7;
135         size.height += 4;
136         return size;
137     }
138 
139     public void setText(String text) {
140         title = text;
141         repaint();
142     }
143 
144     public String getText() {
145         return( title );
146     }
147 
148     public void setActivated(boolean activated) {
149         this.activated = activated;
150         setBackground(activated ? DEFAULT_ACTIVECOLOR : DEFAULT_INACTIVECOLOR);
151         repaint();
152     }
153 }
154