KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > motif > MotifInternalFrameTitlePane


1 /*
2  * @(#)MotifInternalFrameTitlePane.java 1.32 04/04/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.motif;
9
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.border.*;
14 import javax.swing.event.InternalFrameEvent JavaDoc;
15 import javax.swing.plaf.basic.*;
16 import java.util.EventListener JavaDoc;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.beans.PropertyChangeEvent JavaDoc;
19 import java.beans.VetoableChangeListener JavaDoc;
20 import java.beans.PropertyVetoException JavaDoc;
21
22 /**
23  * Class that manages a Motif title bar
24  * @version 1.32 04/15/04
25  *
26  * @since 1.3
27  */

28 public class MotifInternalFrameTitlePane
29     extends BasicInternalFrameTitlePane implements LayoutManager, ActionListener, PropertyChangeListener JavaDoc
30 {
31     SystemButton systemButton;
32     MinimizeButton minimizeButton;
33     MaximizeButton maximizeButton;
34     JPopupMenu systemMenu;
35     Title title;
36     Color color;
37     Color highlight;
38     Color shadow;
39
40     // The width and height of a title pane button
41
public final static int BUTTON_SIZE = 19; // 17 + 1 pixel border
42

43
44     public MotifInternalFrameTitlePane(JInternalFrame frame) {
45         super(frame);
46     }
47
48     protected void installDefaults() {
49         setFont(UIManager.getFont("InternalFrame.titleFont"));
50     setPreferredSize(new Dimension(100, BUTTON_SIZE));
51     }
52
53     protected void uninstallListeners() {
54         // Get around protected method in superclass
55
super.uninstallListeners();
56     }
57
58     protected PropertyChangeListener JavaDoc createPropertyChangeListener() {
59         return this;
60     }
61
62     protected LayoutManager createLayout() {
63         return this;
64     }
65
66     JPopupMenu getSystemMenu() {
67         return systemMenu;
68     }
69
70     protected void assembleSystemMenu() {
71     systemMenu = new JPopupMenu();
72     JMenuItem mi = (JMenuItem)systemMenu.add(new JMenuItem(restoreAction));
73         mi.setMnemonic('R');
74     mi = (JMenuItem) systemMenu.add(new JMenuItem(moveAction));
75         mi.setMnemonic('M');
76     mi = (JMenuItem) systemMenu.add(new JMenuItem(sizeAction));
77         mi.setMnemonic('S');
78     mi = (JMenuItem) systemMenu.add(new JMenuItem(iconifyAction));
79         mi.setMnemonic('n');
80     mi = (JMenuItem) systemMenu.add(new JMenuItem(maximizeAction));
81         mi.setMnemonic('x');
82     systemMenu.add(new JSeparator());
83     mi = (JMenuItem) systemMenu.add(new JMenuItem(closeAction));
84         mi.setMnemonic('C');
85     
86         systemButton = new SystemButton();
87     systemButton.addActionListener(new ActionListener() {
88             public void actionPerformed(ActionEvent e) {
89                 systemMenu.show(systemButton, 0, BUTTON_SIZE);
90             }
91         });
92
93         systemButton.addMouseListener(new MouseAdapter() {
94             public void mousePressed(MouseEvent evt) {
95                 if ((evt.getClickCount() == 2)) {
96                     closeAction.actionPerformed(new
97                         ActionEvent(evt.getSource(),
98                             ActionEvent.ACTION_PERFORMED,
99                             null, evt.getWhen(), 0));
100                     systemMenu.setVisible(false);
101                 }
102             }
103         });
104     }
105
106
107     protected void createButtons() {
108     minimizeButton = new MinimizeButton();
109     minimizeButton.addActionListener(iconifyAction);
110
111     maximizeButton = new MaximizeButton();
112     maximizeButton.addActionListener(maximizeAction);
113     }
114
115
116     protected void addSubComponents() {
117         title = new Title(frame.getTitle());
118         title.setFont(getFont());
119
120     add(systemButton);
121         add(title);
122     add(minimizeButton);
123     add(maximizeButton);
124     }
125
126     public void paintComponent(Graphics g) {
127     }
128
129     void setColors(Color c, Color h, Color s) {
130         color = c;
131         highlight = h;
132         shadow = s;
133     }
134
135     public void actionPerformed(ActionEvent e) {
136     }
137
138     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
139     String JavaDoc prop = (String JavaDoc)evt.getPropertyName();
140     JInternalFrame f = (JInternalFrame)evt.getSource();
141     boolean value = false;
142     if (JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
143         repaint();
144         } else if (prop.equals("maximizable")) {
145             if ((Boolean JavaDoc)evt.getNewValue() == Boolean.TRUE)
146                 add(maximizeButton);
147             else
148                 remove(maximizeButton);
149             revalidate();
150             repaint();
151         } else if (prop.equals("iconable")) {
152             if ((Boolean JavaDoc)evt.getNewValue() == Boolean.TRUE)
153                 add(minimizeButton);
154             else
155                 remove(minimizeButton);
156             revalidate();
157             repaint();
158         } else if (prop.equals(JInternalFrame.TITLE_PROPERTY)) {
159             repaint();
160         }
161         enableActions();
162     }
163
164     public void addLayoutComponent(String JavaDoc name, Component c) {}
165     public void removeLayoutComponent(Component c) {}
166     public Dimension preferredLayoutSize(Container c) {
167         return minimumLayoutSize(c);
168     }
169     
170     public Dimension minimumLayoutSize(Container c) {
171     return new Dimension(100, BUTTON_SIZE);
172     }
173     
174     public void layoutContainer(Container c) {
175     int w = getWidth();
176     systemButton.setBounds(0, 0, BUTTON_SIZE, BUTTON_SIZE);
177     int x = w - BUTTON_SIZE;
178
179     if(frame.isMaximizable()) {
180         maximizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
181         x -= BUTTON_SIZE;
182     } else if(maximizeButton.getParent() != null) {
183         maximizeButton.getParent().remove(maximizeButton);
184     }
185         
186     if(frame.isIconifiable()) {
187         minimizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
188         x -= BUTTON_SIZE;
189     } else if(minimizeButton.getParent() != null) {
190         minimizeButton.getParent().remove(minimizeButton);
191     }
192
193         title.setBounds(BUTTON_SIZE, 0, x, BUTTON_SIZE);
194     }
195
196     protected void showSystemMenu(){
197       systemMenu.show(systemButton, 0, BUTTON_SIZE);
198     }
199    
200     protected void hideSystemMenu(){
201       systemMenu.setVisible(false);
202     }
203     
204     static Dimension buttonDimension = new Dimension(BUTTON_SIZE, BUTTON_SIZE);
205
206     private abstract class FrameButton extends JButton {
207
208         FrameButton() {
209             super();
210             setFocusPainted(false);
211             setBorderPainted(false);
212         }
213
214         public boolean isFocusTraversable() {
215             return false;
216         }
217
218         public void requestFocus() {
219             // ignore request.
220
}
221
222         public Dimension getMinimumSize() {
223             return buttonDimension;
224         }
225
226         public Dimension getPreferredSize() {
227             return buttonDimension;
228         }
229
230         public void paintComponent(Graphics g) {
231             Dimension d = getSize();
232             int maxX = d.width - 1;
233             int maxY = d.height - 1;
234
235             // draw background
236
g.setColor(color);
237             g.fillRect(1, 1, d.width, d.height);
238
239             // draw border
240
boolean pressed = getModel().isPressed();
241             g.setColor(pressed ? shadow : highlight);
242             g.drawLine(0, 0, maxX, 0);
243             g.drawLine(0, 0, 0, maxY);
244             g.setColor(pressed ? highlight : shadow);
245             g.drawLine(1, maxY, maxX, maxY);
246             g.drawLine(maxX, 1, maxX, maxY);
247         }
248     }
249     
250     private class MinimizeButton extends FrameButton {
251         public void paintComponent(Graphics g) {
252             super.paintComponent(g);
253             g.setColor(highlight);
254             g.drawLine(7, 8, 7, 11);
255             g.drawLine(7, 8, 10, 8);
256             g.setColor(shadow);
257             g.drawLine(8, 11, 10, 11);
258             g.drawLine(11, 9, 11, 11);
259         }
260     }
261
262     private class MaximizeButton extends FrameButton {
263         public void paintComponent(Graphics g) {
264             super.paintComponent(g);
265             int max = BUTTON_SIZE - 5;
266             boolean isMaxed = frame.isMaximum();
267             g.setColor(isMaxed ? shadow : highlight);
268             g.drawLine(4, 4, 4, max);
269             g.drawLine(4, 4, max, 4);
270             g.setColor(isMaxed ? highlight : shadow);
271             g.drawLine(5, max, max, max);
272             g.drawLine(max, 5, max, max);
273         }
274     }
275
276     private class SystemButton extends FrameButton {
277         public boolean isFocusTraversable() { return false; }
278         public void requestFocus() {}
279
280         public void paintComponent(Graphics g) {
281             super.paintComponent(g);
282             g.setColor(highlight);
283             g.drawLine(4, 8, 4, 11);
284             g.drawLine(4, 8, BUTTON_SIZE - 5, 8);
285             g.setColor(shadow);
286             g.drawLine(5, 11, BUTTON_SIZE - 5, 11);
287             g.drawLine(BUTTON_SIZE - 5, 9, BUTTON_SIZE - 5, 11);
288         }
289     }
290
291     private class Title extends FrameButton {
292         Title(String JavaDoc title) {
293             super();
294             setText(title);
295             setHorizontalAlignment(SwingConstants.CENTER);
296             setBorder(BorderFactory.createBevelBorder(
297                 BevelBorder.RAISED,
298                 UIManager.getColor("activeCaptionBorder"),
299                 UIManager.getColor("inactiveCaptionBorder")));
300
301             // Forward mouse events to titlebar for moves.
302
addMouseMotionListener(new MouseMotionListener() {
303                 public void mouseDragged(MouseEvent e) {
304                     forwardEventToParent(e);
305                 }
306                 public void mouseMoved(MouseEvent e) {
307                     forwardEventToParent(e);
308                 }
309             });
310             addMouseListener(new MouseListener() {
311                 public void mouseClicked(MouseEvent e) {
312                     forwardEventToParent(e);
313                 }
314                 public void mousePressed(MouseEvent e) {
315                     forwardEventToParent(e);
316                 }
317                 public void mouseReleased(MouseEvent e) {
318                     forwardEventToParent(e);
319                 }
320                 public void mouseEntered(MouseEvent e) {
321                     forwardEventToParent(e);
322                 }
323                 public void mouseExited(MouseEvent e) {
324                     forwardEventToParent(e);
325                 }
326             });
327         }
328
329         void forwardEventToParent(MouseEvent e) {
330             getParent().dispatchEvent(new MouseEvent(
331                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
332                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
333         }
334
335         public void paintComponent(Graphics g) {
336             super.paintComponent(g);
337             if (frame.isSelected()) {
338                 g.setColor(UIManager.getColor("activeCaptionText"));
339             } else {
340                 g.setColor(UIManager.getColor("inactiveCaptionText"));
341             }
342             Dimension d = getSize();
343             String JavaDoc frameTitle = frame.getTitle();
344             if (frameTitle != null) {
345                 MotifGraphicsUtils.drawStringInRect(frame, g, frameTitle,
346                                                     0, 0, d.width, d.height,
347                                                     SwingConstants.CENTER);
348             }
349         }
350     }
351 }
352
Popular Tags