Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / javax / swing / plaf / basic / BasicDesktopIconUI.java
blob3f8730249daf1fc804c84858fb07e173284aa790
1 /* BasicDesktopIconUI.java --
2 Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 * This is package-private to avoid an accessor method. */
307 transient BoundButton button;
309 /** The PropertyChangeListener listening to the JDesktopIcon. */
310 private transient PropertyChangeListener propertyHandler;
312 /** The default icon used when no frame icon is given by the JInternalFrame. */
313 static Icon defaultIcon = new InternalFrameDefaultMenuIcon();
316 * This is a helper class that is used in JDesktopIcon and gives the Button a predetermined size.
318 private class BoundButton extends JButton
321 * Creates a new BoundButton object.
323 * @param title The title of the button.
325 public BoundButton(String title)
327 super(title);
331 * This method returns a standard size (based on the defaults of the JDesktopIcon) and the insets.
333 * @return The preferred size of the JDesktopIcon.
335 public Dimension getPreferredSize()
337 Insets insets = desktopIcon.getInsets();
338 return new Dimension(iconWidth - insets.left - insets.right,
339 iconHeight - insets.top - insets.bottom);
343 * This method returns the minimum size of the button.
345 * @return The minimum size of the button.
347 public Dimension getMinimumSize()
349 return getPreferredSize();
353 * This method returns the maximum size of the button.
355 * @return The maximum size of the button.
357 public Dimension getMaximumSize()
359 return getPreferredSize();
364 * Creates a new BasicDesktopIconUI object.
366 public BasicDesktopIconUI()
368 // Nothing to do here.
372 * This method creates a new BasicDesktopIconUI for the given JComponent.
374 * @param c The JComponent to create a UI for.
376 * @return A new BasicDesktopIconUI.
378 public static ComponentUI createUI(JComponent c)
380 return new BasicDesktopIconUI();
384 * This method installs the UI for the given JComponent.
386 * @param c The JComponent to install this UI for.
388 public void installUI(JComponent c)
390 if (c instanceof JDesktopIcon)
392 desktopIcon = (JDesktopIcon) c;
393 desktopIcon.setLayout(new BorderLayout());
394 frame = desktopIcon.getInternalFrame();
396 installDefaults();
397 installComponents();
398 installListeners();
400 desktopIcon.setOpaque(true);
405 * This method uninstalls the UI for the given JComponent.
407 * @param c The JComponent to uninstall this UI for.
409 public void uninstallUI(JComponent c)
411 desktopIcon.setOpaque(false);
413 uninstallListeners();
414 uninstallComponents();
415 uninstallDefaults();
417 frame = null;
418 desktopIcon.setLayout(null);
419 desktopIcon = null;
423 * This method installs the necessary sub components for the JDesktopIcon.
425 protected void installComponents()
427 // Try to create a button based on what the frame's
428 // state is currently
429 button = new BoundButton(frame.getTitle());
430 button.setHorizontalAlignment(SwingConstants.LEFT);
431 button.setHorizontalTextPosition(SwingConstants.TRAILING);
433 Icon use = frame.getFrameIcon();
434 if (use == null)
435 use = defaultIcon;
436 button.setIcon(use);
438 desktopIcon.add(button, SwingConstants.CENTER);
442 * This method uninstalls the sub components for the JDesktopIcon.
444 protected void uninstallComponents()
446 desktopIcon.remove(button);
448 button = null;
452 * This method installs the listeners needed by this UI.
454 protected void installListeners()
456 mouseHandler = createMouseInputListener();
458 desktopIcon.addMouseMotionListener(mouseHandler);
459 desktopIcon.addMouseListener(mouseHandler);
461 propertyHandler = new PropertyChangeListener()
463 public void propertyChange(PropertyChangeEvent e)
465 if (e.getPropertyName().equals(JInternalFrame.TITLE_PROPERTY))
466 button.setText(desktopIcon.getInternalFrame().getTitle());
467 else if (e.getPropertyName().equals(JInternalFrame.FRAME_ICON_PROPERTY))
469 Icon use = desktopIcon.getInternalFrame().getFrameIcon();
470 if (use == null)
471 use = defaultIcon;
472 button.setIcon(use);
474 desktopIcon.revalidate();
475 desktopIcon.repaint();
478 frame.addPropertyChangeListener(propertyHandler);
480 button.addActionListener(new ActionListener()
482 public void actionPerformed(ActionEvent e)
484 deiconize();
490 * This method uninstalls the listeners needed by the UI.
492 protected void uninstallListeners()
494 // button is nulled so no need to remove it.
496 frame.removePropertyChangeListener(propertyHandler);
497 propertyHandler = null;
499 desktopIcon.removeMouseMotionListener(mouseHandler);
500 desktopIcon.removeMouseListener(mouseHandler);
504 * This method installs the defaults for the JDesktopIcon.
506 protected void installDefaults()
508 // FIXME: Move border to defaults.
509 desktopIcon.setBorder(new DesktopIconBorder());
513 * This method uninstalls the defaults for the JDesktopIcon.
515 protected void uninstallDefaults()
517 desktopIcon.setBorder(null);
521 * This method creates a new MouseInputListener for the JDesktopIcon.
523 * @return A new MouseInputListener.
525 protected MouseInputListener createMouseInputListener()
527 return new MouseInputHandler();
531 * This method returns the preferred size for the given JComponent.
533 * @param c The JComponent to find a preferred size for.
535 * @return The preferred size.
537 public Dimension getPreferredSize(JComponent c)
539 return new Dimension(iconWidth, iconHeight);
543 * This method returns the minimum size for the given JComponent.
545 * @param c The JComponent to find a minimum size for.
547 * @return The minimum size.
549 public Dimension getMinimumSize(JComponent c)
551 return getPreferredSize(c);
555 * This method returns the maximum size for the given JComponent.
557 * @param c The JComponent to find a maximum size for.
559 * @return The maximum size.
561 public Dimension getMaximumSize(JComponent c)
563 return getPreferredSize(c);
567 * This method returns the insets of the given JComponent.
569 * @param c The JComponent to find insets for.
571 * @return The insets of the given JComponent.
573 public Insets getInsets(JComponent c)
575 return c.getInsets();
579 * This method deiconizes the JInternalFrame associated with the JDesktopIcon.
581 public void deiconize()
585 frame.setIcon(false);
587 catch (PropertyVetoException pve)
589 // We do nothing if the attempt has been vetoed.