Source code: org/gtk/java/swing/plaf/gtk/GtkInternalFrameTitlePane.java
1 /*
2 * @(#)GtkInternalFrameTitlePane.java 1.2 00/01/12
3 *
4 * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * This software is the proprietary information of Sun Microsystems, Inc.
7 * Use is subject to license terms.
8 *
9 */
10
11 package org.gtk.java.swing.plaf.gtk;
12
13 import java.awt.*;
14 import java.awt.event.*;
15 import javax.swing.*;
16 import javax.swing.border.*;
17 import javax.swing.event.InternalFrameEvent;
18 import java.util.EventListener;
19 import java.beans.PropertyChangeListener;
20 import java.beans.PropertyChangeEvent;
21 import java.beans.VetoableChangeListener;
22 import java.beans.PropertyVetoException;
23
24 /**
25 * Package private class that manages a Gtk title bar
26 * @version 1.12 10/15/98
27 */
28 class GtkInternalFrameTitlePane
29 extends JComponent implements LayoutManager, ActionListener, PropertyChangeListener
30 {
31 SystemButton systemButton;
32 MinimizeButton minimizeButton;
33 MaximizeButton maximizeButton;
34 JPopupMenu systemMenu;
35 Title title;
36 JInternalFrame iFrame;
37 Color color;
38 Color highlight;
39 Color shadow;
40
41 static final Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
42
43 // The width and height of a title pane button
44 public final static int BUTTON_SIZE = 19; // 17 + 1 pixel border
45
46 final int RESTORE_MENU_ITEM = 0;
47 final int MOVE_MENU_ITEM = 1;
48 final int SIZE_MENU_ITEM = 2;
49 final int MINIMIZE_MENU_ITEM = 3;
50 final int MAXIMIZE_MENU_ITEM = 4;
51 final int SEPARATOR_MENU_ITEM = 5;
52 final int CLOSE_MENU_ITEM = 6;
53
54 public GtkInternalFrameTitlePane(JInternalFrame f) {
55 iFrame = f;
56
57 setPreferredSize(new Dimension(100, BUTTON_SIZE));
58
59 systemMenu = new JPopupMenu() {
60 public void show(Component invoker, int x, int y) {
61 if(!iFrame.isIconifiable())
62 systemMenu.getComponentAtIndex(MINIMIZE_MENU_ITEM).setEnabled(false);
63 if(!iFrame.isMaximizable())
64 systemMenu.getComponentAtIndex(MAXIMIZE_MENU_ITEM).setEnabled(false);
65 if(!iFrame.isMaximizable() && !iFrame.isIconifiable())
66 systemMenu.getComponentAtIndex(RESTORE_MENU_ITEM).setEnabled(false);
67 if(!iFrame.isClosable())
68 systemMenu.getComponentAtIndex(CLOSE_MENU_ITEM).setEnabled(false);
69 super.show(invoker, x, y);
70 }
71 };
72
73 JMenuItem mi = (JMenuItem) systemMenu.add(new JMenuItem("Restore"));
74 mi.setEnabled(false);
75 mi.addActionListener(this);
76 /// PENDING(klobad) Move/Size actions on InternalFrame need to be determined
77 mi = (JMenuItem) systemMenu.add(new JMenuItem("Move"));
78 mi.setEnabled(false);
79 mi.addActionListener(this);
80 mi = (JMenuItem) systemMenu.add(new JMenuItem("Size"));
81 mi.setEnabled(false);
82 mi.addActionListener(this);
83 mi = (JMenuItem) systemMenu.add(new JMenuItem("Minimize"));
84 mi.addActionListener(this);
85 mi = (JMenuItem) systemMenu.add(new JMenuItem("Maximize"));
86 mi.addActionListener(this);
87 systemMenu.add(new JSeparator());
88 mi = (JMenuItem) systemMenu.add(new JMenuItem("Close"));
89 mi.addActionListener(this);
90
91 systemButton = new SystemButton();
92 systemButton.addActionListener(new ActionListener() {
93 public void actionPerformed(ActionEvent e) {
94 systemMenu.show(systemButton, 0, BUTTON_SIZE);
95 }
96 });
97 systemButton.addMouseListener(new MouseAdapter() {
98 public void mousePressed(MouseEvent e) {
99 if ((e.getClickCount() == 2)){
100 if (iFrame.isClosable()) {
101 try{
102 iFrame.setClosed(true);
103 } catch (PropertyVetoException e0) { }
104 }
105 systemMenu.setVisible(false);
106 }
107 }
108 });
109
110 minimizeButton = new MinimizeButton();
111 minimizeButton.addActionListener(this);
112 minimizeButton.setActionCommand("Iconify");
113
114 maximizeButton = new MaximizeButton();
115 maximizeButton.addActionListener(this);
116 maximizeButton.setActionCommand("Maximize");
117
118 title = new Title(iFrame.getTitle());
119 title.setFont(defaultTitleFont);
120
121 setLayout(this);
122
123 add(systemButton);
124 add(title);
125 add(minimizeButton);
126 add(maximizeButton);
127
128 // Make sure these are ok to leave on?
129 iFrame.addPropertyChangeListener(this);
130 }
131
132
133 void setColors(Color c, Color h, Color s) {
134 color = c;
135 highlight = h;
136 shadow = s;
137 }
138
139 JPopupMenu getSystemMenu() {
140 return systemMenu;
141 }
142
143 public void actionPerformed(ActionEvent e) {
144 try {
145 if ("Close".equals(e.getActionCommand()) && iFrame.isClosable()) {
146 iFrame.setClosed(true);
147 // 4118140
148 }
149 else if ((("Iconify".equals(e.getActionCommand())) ||
150 ("Minimize".equals(e.getActionCommand())))
151 &&
152 iFrame.isIconifiable()) {
153 if (!iFrame.isIcon()) {
154 iFrame.setIcon(true);
155 } else {
156 iFrame.setIcon(false);
157 }
158 }
159 else if ("Maximize".equals(e.getActionCommand()) &&
160 iFrame.isMaximizable()) {
161 if(!iFrame.isMaximum()) {
162 iFrame.setMaximum(true);
163 } else {
164 iFrame.setMaximum(false);
165 }
166 } else if ("Restore".equals(e.getActionCommand()) &&
167 iFrame.isMaximizable() && iFrame.isMaximum()) {
168 iFrame.setMaximum(false);
169 } else if ("Restore".equals(e.getActionCommand()) &&
170 iFrame.isIconifiable() && iFrame.isIcon()) {
171 iFrame.setIcon(false);
172 }
173 } catch (PropertyVetoException e0) { }
174
175 // Dismiss popup menu if it's displayed
176 if (systemMenu.isVisible()) {
177 systemMenu.setVisible(false);
178 }
179 }
180
181 public void propertyChange(PropertyChangeEvent evt) {
182 String prop = (String)evt.getPropertyName();
183 JInternalFrame f = (JInternalFrame)evt.getSource();
184 boolean value = false;
185 if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
186 repaint();
187 } else if(JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
188 value = ((Boolean)evt.getNewValue()).booleanValue();
189 systemMenu.getComponentAtIndex(RESTORE_MENU_ITEM).setEnabled(value);
190 systemMenu.getComponentAtIndex(MAXIMIZE_MENU_ITEM).setEnabled(!value);
191 } else if(JInternalFrame.IS_ICON_PROPERTY.equals(prop)) {
192 value = ((Boolean)evt.getNewValue()).booleanValue();
193 systemMenu.getComponentAtIndex(RESTORE_MENU_ITEM).setEnabled(value);
194 systemMenu.getComponentAtIndex(MAXIMIZE_MENU_ITEM).setEnabled(!value);
195 systemMenu.getComponentAtIndex(MINIMIZE_MENU_ITEM).setEnabled(!value);
196 }
197 }
198
199 public void addLayoutComponent(String name, Component c) {}
200 public void removeLayoutComponent(Component c) {}
201 public Dimension preferredLayoutSize(Container c) {
202 return new Dimension(c.getSize().width, BUTTON_SIZE);
203 }
204
205 public Dimension minimumLayoutSize(Container c) {
206 return new Dimension(c.getSize().width, BUTTON_SIZE);
207 }
208
209 public void layoutContainer(Container c) {
210 int w = getWidth();
211 systemButton.setBounds(0, 0, BUTTON_SIZE, BUTTON_SIZE);
212 int x = w - BUTTON_SIZE;
213
214 if(iFrame.isMaximizable()) {
215 maximizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
216 x -= BUTTON_SIZE;
217 } else if(maximizeButton.getParent() != null) {
218 maximizeButton.getParent().remove(maximizeButton);
219 }
220
221 if(iFrame.isIconifiable()) {
222 minimizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
223 x -= BUTTON_SIZE;
224 } else if(minimizeButton.getParent() != null) {
225 minimizeButton.getParent().remove(minimizeButton);
226 }
227
228 title.setBounds(BUTTON_SIZE, 0, x, BUTTON_SIZE);
229 }
230
231 protected void showSystemMenu(){
232 systemMenu.show(systemButton, 0, BUTTON_SIZE);
233 }
234
235 protected void hideSystemMenu(){
236 systemMenu.setVisible(false);
237 }
238
239 static Dimension buttonDimension = new Dimension(BUTTON_SIZE, BUTTON_SIZE);
240
241 private abstract class FrameButton extends JButton {
242
243 FrameButton() {
244 super();
245 setFocusPainted(false);
246 setBorderPainted(false);
247 }
248
249 public boolean isFocusTraversable() {
250 return false;
251 }
252
253 public void requestFocus() {
254 // ignore request.
255 }
256
257 public Dimension getMinimumSize() {
258 return buttonDimension;
259 }
260
261 public Dimension getPreferredSize() {
262 return buttonDimension;
263 }
264
265 public void paint(Graphics g) {
266 Dimension d = getSize();
267 int maxX = d.width - 1;
268 int maxY = d.height - 1;
269
270 // draw background
271 g.setColor(color);
272 g.fillRect(1, 1, d.width, d.height);
273
274 // draw border
275 boolean pressed = getModel().isPressed();
276 g.setColor(pressed ? shadow : highlight);
277 g.drawLine(0, 0, maxX, 0);
278 g.drawLine(0, 0, 0, maxY);
279 g.setColor(pressed ? highlight : shadow);
280 g.drawLine(1, maxY, maxX, maxY);
281 g.drawLine(maxX, 1, maxX, maxY);
282 }
283 }
284
285 private class MinimizeButton extends FrameButton {
286 public void paint(Graphics g) {
287 super.paint(g);
288 g.setColor(highlight);
289 g.drawLine(7, 8, 7, 11);
290 g.drawLine(7, 8, 10, 8);
291 g.setColor(shadow);
292 g.drawLine(8, 11, 10, 11);
293 g.drawLine(11, 9, 11, 11);
294 }
295 }
296
297 private class MaximizeButton extends FrameButton {
298 public void paint(Graphics g) {
299 super.paint(g);
300 int max = BUTTON_SIZE - 5;
301 boolean isMaxed = iFrame.isMaximum();
302 g.setColor(isMaxed ? shadow : highlight);
303 g.drawLine(4, 4, 4, max);
304 g.drawLine(4, 4, max, 4);
305 g.setColor(isMaxed ? highlight : shadow);
306 g.drawLine(5, max, max, max);
307 g.drawLine(max, 5, max, max);
308 }
309 }
310
311 private class SystemButton extends FrameButton {
312 public boolean isFocusTraversable() { return false; }
313 public void requestFocus() {}
314
315 public void paint(Graphics g) {
316 super.paint(g);
317 g.setColor(highlight);
318 g.drawLine(4, 8, 4, 11);
319 g.drawLine(4, 8, BUTTON_SIZE - 5, 8);
320 g.setColor(shadow);
321 g.drawLine(5, 11, BUTTON_SIZE - 5, 11);
322 g.drawLine(BUTTON_SIZE - 5, 9, BUTTON_SIZE - 5, 11);
323 }
324 }
325
326 private class Title extends FrameButton {
327 Title(String title) {
328 super();
329 setText(title);
330 setHorizontalAlignment(SwingConstants.CENTER);
331 setBorder(BorderFactory.createBevelBorder(
332 BevelBorder.RAISED,
333 UIManager.getColor("activeCaptionBorder"),
334 UIManager.getColor("inactiveCaptionBorder")));
335
336 // Forward mouse events to titlebar for moves.
337 addMouseMotionListener(new MouseMotionListener() {
338 public void mouseDragged(MouseEvent e) {
339 forwardEventToParent(e);
340 }
341 public void mouseMoved(MouseEvent e) {
342 forwardEventToParent(e);
343 }
344 });
345 addMouseListener(new MouseListener() {
346 public void mouseClicked(MouseEvent e) {
347 forwardEventToParent(e);
348 }
349 public void mousePressed(MouseEvent e) {
350 forwardEventToParent(e);
351 }
352 public void mouseReleased(MouseEvent e) {
353 forwardEventToParent(e);
354 }
355 public void mouseEntered(MouseEvent e) {
356 forwardEventToParent(e);
357 }
358 public void mouseExited(MouseEvent e) {
359 forwardEventToParent(e);
360 }
361 });
362 }
363
364 void forwardEventToParent(MouseEvent e) {
365 getParent().dispatchEvent(new MouseEvent(
366 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
367 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
368 }
369
370 public void paint(Graphics g) {
371 super.paint(g);
372 if (iFrame.isSelected()) {
373 g.setColor(UIManager.getColor("activeCaptionText"));
374 } else {
375 g.setColor(UIManager.getColor("inactiveCaptionText"));
376 }
377 Dimension d = getSize();
378 GtkGraphicsUtils.drawStringInRect(g, iFrame.getTitle(),
379 0, 0, d.width, d.height,
380 SwingConstants.CENTER);
381 }
382 }
383
384 } /// End Title Pane Class