Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / plaf / basic / BasicDesktopIconUI.java
blob4867c27d8ca7755034eef7e8fde46ac098a242d0
1 /* BasicDesktopIconUI.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import java.awt.BorderLayout;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.Dimension;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 import java.awt.Rectangle;
48 import java.awt.event.ActionEvent;
49 import java.awt.event.ActionListener;
50 import java.awt.event.MouseEvent;
51 import java.beans.PropertyChangeEvent;
52 import java.beans.PropertyChangeListener;
53 import java.beans.PropertyVetoException;
55 import javax.swing.Icon;
56 import javax.swing.JButton;
57 import javax.swing.JComponent;
58 import javax.swing.JDesktopPane;
59 import javax.swing.JInternalFrame;
60 import javax.swing.JInternalFrame.JDesktopIcon;
61 import javax.swing.SwingConstants;
62 import javax.swing.border.Border;
63 import javax.swing.event.MouseInputAdapter;
64 import javax.swing.event.MouseInputListener;
65 import javax.swing.plaf.ComponentUI;
66 import javax.swing.plaf.DesktopIconUI;
68 /**
69 * This class acts as the UI delegate for JDesktopIcons for the Basic look and feel.
71 public class BasicDesktopIconUI extends DesktopIconUI
73 /**
74 * This helper class handles mouse events that occur on the JDesktopIcon.
76 public class MouseInputHandler extends MouseInputAdapter
78 /** The x offset from the MouseEvent coordinates to the top left corner. */
79 private transient int xOffset;
81 /** The y offset fromt he MouseEvent coordinates to the top left corner. */
82 private transient int yOffset;
84 /** A cached value of the JDesktopPane that parents this JDesktopIcon. */
85 private transient JDesktopPane pane;
87 /**
88 * This method is called when the mouse is dragged in the JDesktopIcon.
90 * @param e The MouseEvent.
92 public void mouseDragged(MouseEvent e)
94 Rectangle b = desktopIcon.getBounds();
96 moveAndRepaint(desktopIcon, b.x + e.getX() - xOffset,
97 b.y + e.getY() - yOffset, b.width, b.height);
101 * This method is called when the mouse is moved in the JDesktopIcon.
103 * @param e The MouseEvent.
105 public void mouseMoved(MouseEvent e)
107 // Nothing to do.
111 * This method is called when the mouse is pressed in the JDesktopIcon.
113 * @param e The MouseEvent.
115 public void mousePressed(MouseEvent e)
117 xOffset = e.getX();
118 yOffset = e.getY();
119 pane = frame.getDesktopPane();
120 if (pane != null)
121 pane.getDesktopManager().beginDraggingFrame(desktopIcon);
125 * This method is called when the mouse is released in the JDesktopIcon.
127 * @param e The MouseEvent.
129 public void mouseReleased(MouseEvent e)
131 if (pane != null)
132 pane.getDesktopManager().endDraggingFrame(desktopIcon);
133 xOffset = 0;
134 yOffset = 0;
138 * This method moves and repaints the JDesktopIcon to the given bounds.
140 * @param f The JComponent to move and repaint.
141 * @param newX The new x coordinate.
142 * @param newY The new y coordinate.
143 * @param newWidth The new width.
144 * @param newHeight The new height.
146 public void moveAndRepaint(JComponent f, int newX, int newY, int newWidth,
147 int newHeight)
149 if (pane != null)
150 pane.getDesktopManager().dragFrame(f, newX, newY);
151 else
152 desktopIcon.setBounds(newX, newY, newWidth, newHeight);
157 * This class acts as the border for the JDesktopIcon.
159 private class DesktopIconBorder implements Border
161 /** The left inset value. */
162 int left = 10;
164 /** The top inset value. */
165 int top = 4;
167 /** The right inset value. */
168 int right = top;
170 /** The bottom inset value. */
171 int bottom = top;
174 * This method returns the insets of the border.
176 * @param c The Component to find border insets for.
178 * @return The border insets.
180 public Insets getBorderInsets(Component c)
182 return new Insets(top, left, bottom, right);
186 * This method returns whether the border is opaque.
188 * @return Whether the border is opaque.
190 public boolean isBorderOpaque()
192 return true;
196 * This method paints the border.
198 * @param c The Component the border is in.
199 * @param g The Graphics object to paint with.
200 * @param x The x coordinate of the Component.
201 * @param y The y coordinate of the Component.
202 * @param width The width of the Component.
203 * @param height The height of the Component.
205 public void paintBorder(Component c, Graphics g, int x, int y, int width,
206 int height)
208 g.translate(x, y);
209 Color saved = g.getColor();
211 g.setColor(Color.LIGHT_GRAY);
213 g.fillRect(0, 0, left, height);
214 g.fillRect(0, 0, width, top);
215 g.fillRect(0, height - bottom, width, bottom);
216 g.fillRect(width - right, 0, right, height);
218 g.setColor(Color.BLACK);
219 g.drawRect(0, 0, width - 1, height - 1);
221 int fHeight = height / 4;
222 int hLeft = left / 2;
224 g.setColor(Color.BLACK);
225 g.fillRect(hLeft, fHeight, 2, 2);
226 g.fillRect(hLeft, fHeight * 2, 2, 2);
227 g.fillRect(hLeft, fHeight * 3, 2, 2);
229 g.setColor(saved);
230 g.translate(-x, -y);
234 /** The static width and height of the iconSize. */
235 private static final int iconSize = 16;
238 * This class represents the default frame icon when none
239 * is supplied by the JInternalFrame.
241 static class InternalFrameDefaultMenuIcon implements Icon
244 * This returns the icon height.
246 * @return The icon height.
248 public int getIconHeight()
250 return iconSize;
254 * This returns the icon width.
256 * @return The icon width.
258 public int getIconWidth()
260 return iconSize;
264 * This method paints the icon.
266 * @param c The Component this icon belongs to.
267 * @param g The Graphics object to paint with.
268 * @param x The x coordinate to paint at.
269 * @param y The y coordinate to paint at.
271 public void paintIcon(Component c, Graphics g, int x, int y)
273 g.translate(x, y);
274 Color saved = g.getColor();
276 g.setColor(Color.BLUE);
277 g.fillRect(0, 0, iconSize, (int) ((double) iconSize / 3) + 1);
279 g.setColor(Color.WHITE);
280 g.fillRect(0, (int) ((double) iconSize / 3), iconSize, iconSize * 5 / 6);
282 g.setColor(Color.GRAY);
283 g.drawRect(0, 0, iconSize, iconSize);
285 g.setColor(saved);
286 g.translate(-x, -y);
290 /** The default JDesktopIcon width. */
291 private static final int iconWidth = 160;
293 /** The default JDesktopIcon height */
294 private static final int iconHeight = 35;
296 /** The JDesktopIcon this UI delegate represents. */
297 protected JDesktopIcon desktopIcon;
299 /** The JInternalFrame associated with the JDesktopIcon. */
300 protected JInternalFrame frame;
302 /** The MouseListener responsible for reacting to MouseEvents on the JDesktopIcon. */
303 private transient MouseInputListener mouseHandler;
305 /** The Button in the JDesktopIcon responsible for deiconifying it. */
306 private transient BoundButton button;
308 /** The PropertyChangeListener listening to the JDesktopIcon. */
309 private transient PropertyChangeListener propertyHandler;
311 /** The default icon used when no frame icon is given by the JInternalFrame. */
312 static Icon defaultIcon = new InternalFrameDefaultMenuIcon();
315 * This is a helper class that is used in JDesktopIcon and gives the Button a predetermined size.
317 private class BoundButton extends JButton
320 * Creates a new BoundButton object.
322 * @param title The title of the button.
324 public BoundButton(String title)
326 super(title);
330 * This method returns a standard size (based on the defaults of the JDesktopIcon) and the insets.
332 * @return The preferred size of the JDesktopIcon.
334 public Dimension getPreferredSize()
336 Insets insets = desktopIcon.getInsets();
337 return new Dimension(iconWidth - insets.left - insets.right,
338 iconHeight - insets.top - insets.bottom);
342 * This method returns the minimum size of the button.
344 * @return The minimum size of the button.
346 public Dimension getMinimumSize()
348 return getPreferredSize();
352 * This method returns the maximum size of the button.
354 * @return The maximum size of the button.
356 public Dimension getMaximumSize()
358 return getPreferredSize();
363 * Creates a new BasicDesktopIconUI object.
365 public BasicDesktopIconUI()
370 * This method creates a new BasicDesktopIconUI for the given JComponent.
372 * @param c The JComponent to create a UI for.
374 * @return A new BasicDesktopIconUI.
376 public static ComponentUI createUI(JComponent c)
378 return new BasicDesktopIconUI();
382 * This method installs the UI for the given JComponent.
384 * @param c The JComponent to install this UI for.
386 public void installUI(JComponent c)
388 if (c instanceof JDesktopIcon)
390 desktopIcon = (JDesktopIcon) c;
391 desktopIcon.setLayout(new BorderLayout());
392 frame = desktopIcon.getInternalFrame();
394 installDefaults();
395 installComponents();
396 installListeners();
398 desktopIcon.setOpaque(true);
403 * This method uninstalls the UI for the given JComponent.
405 * @param c The JComponent to uninstall this UI for.
407 public void uninstallUI(JComponent c)
409 desktopIcon.setOpaque(false);
411 uninstallListeners();
412 uninstallComponents();
413 uninstallDefaults();
415 frame = null;
416 desktopIcon.setLayout(null);
417 desktopIcon = null;
421 * This method installs the necessary sub components for the JDesktopIcon.
423 protected void installComponents()
425 // Try to create a button based on what the frame's
426 // state is currently
427 button = new BoundButton(frame.getTitle());
428 button.setHorizontalAlignment(SwingConstants.LEFT);
429 button.setHorizontalTextPosition(SwingConstants.TRAILING);
431 Icon use = frame.getFrameIcon();
432 if (use == null)
433 use = defaultIcon;
434 button.setIcon(use);
436 desktopIcon.add(button, SwingConstants.CENTER);
440 * This method uninstalls the sub components for the JDesktopIcon.
442 protected void uninstallComponents()
444 desktopIcon.remove(button);
446 button = null;
450 * This method installs the listeners needed by this UI.
452 protected void installListeners()
454 mouseHandler = createMouseInputListener();
456 desktopIcon.addMouseMotionListener(mouseHandler);
457 desktopIcon.addMouseListener(mouseHandler);
459 propertyHandler = new PropertyChangeListener()
461 public void propertyChange(PropertyChangeEvent e)
463 if (e.getPropertyName().equals(JInternalFrame.TITLE_PROPERTY))
464 button.setText(desktopIcon.getInternalFrame().getTitle());
465 else if (e.getPropertyName().equals(JInternalFrame.FRAME_ICON_PROPERTY))
467 Icon use = desktopIcon.getInternalFrame().getFrameIcon();
468 if (use == null)
469 use = defaultIcon;
470 button.setIcon(use);
472 desktopIcon.revalidate();
473 desktopIcon.repaint();
476 frame.addPropertyChangeListener(propertyHandler);
478 button.addActionListener(new ActionListener()
480 public void actionPerformed(ActionEvent e)
482 deiconize();
488 * This method uninstalls the listeners needed by the UI.
490 protected void uninstallListeners()
492 // button is nulled so no need to remove it.
494 frame.removePropertyChangeListener(propertyHandler);
495 propertyHandler = null;
497 desktopIcon.removeMouseMotionListener(mouseHandler);
498 desktopIcon.removeMouseListener(mouseHandler);
502 * This method installs the defaults for the JDesktopIcon.
504 protected void installDefaults()
506 // FIXME: Move border to defaults.
507 desktopIcon.setBorder(new DesktopIconBorder());
511 * This method uninstalls the defaults for the JDesktopIcon.
513 protected void uninstallDefaults()
515 desktopIcon.setBorder(null);
519 * This method creates a new MouseInputListener for the JDesktopIcon.
521 * @return A new MouseInputListener.
523 protected MouseInputListener createMouseInputListener()
525 return new MouseInputHandler();
529 * This method returns the preferred size for the given JComponent.
531 * @param c The JComponent to find a preferred size for.
533 * @return The preferred size.
535 public Dimension getPreferredSize(JComponent c)
537 return new Dimension(iconWidth, iconHeight);
541 * This method returns the minimum size for the given JComponent.
543 * @param c The JComponent to find a minimum size for.
545 * @return The minimum size.
547 public Dimension getMinimumSize(JComponent c)
549 return getPreferredSize(c);
553 * This method returns the maximum size for the given JComponent.
555 * @param c The JComponent to find a maximum size for.
557 * @return The maximum size.
559 public Dimension getMaximumSize(JComponent c)
561 return getPreferredSize(c);
565 * This method returns the insets of the given JComponent.
567 * @param c The JComponent to find insets for.
569 * @return The insets of the given JComponent.
571 public Insets getInsets(JComponent c)
573 return c.getInsets();
577 * This method deiconizes the JInternalFrame associated with the JDesktopIcon.
579 public void deiconize()
583 frame.setIcon(false);
585 catch (PropertyVetoException pve)