home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
MotifDesktopIconUI.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
13KB
|
389 lines
/*
* @(#)MotifDesktopIconUI.java 1.7 98/02/05
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.plaf.motif;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;
import com.sun.java.swing.plaf.*;
import java.beans.*;
import java.util.EventListener;
import java.io.Serializable;
/**
* Motif rendition of the component.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @version 1.7 02/05/98
* @author Thomas Ball
*/
public class MotifDesktopIconUI extends DesktopIconUI implements Serializable
{
JInternalFrame.JDesktopIcon desktopIcon;
JInternalFrame frame;
Icon defaultIcon;
IconButton iconButton;
IconLabel iconLabel;
JPopupMenu systemMenu;
EventListener mml;
final static Font defaultTitleFont = new Font("SansSerif", Font.PLAIN, 12);
public static ComponentUI createUI(JComponent c) {
return new MotifDesktopIconUI();
}
public MotifDesktopIconUI() {
}
String title;
public void installUI(JComponent c) {
desktopIcon = (JInternalFrame.JDesktopIcon)c;
frame = desktopIcon.getInternalFrame();
setDefaultIcon(UIManager.getIcon("DesktopIcon.icon"));
iconButton = new IconButton(defaultIcon);
// An unhanded way of creating a system popup menu.
MotifInternalFrameTitlePane titlePane =
new MotifInternalFrameTitlePane(frame);
systemMenu = titlePane.getSystemMenu();
iconButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
systemMenu.show(iconButton, 0, desktopIcon.getHeight());
}
});
iconButton.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
try {
frame.setIcon(false);
} catch (PropertyVetoException e2) { }
systemMenu.setVisible(false);
}
}
});
MotifFrameBorder border = new MotifFrameBorder(desktopIcon);
desktopIcon.setLayout(new BorderLayout());
iconButton.setBorder(border);
desktopIcon.add(iconButton, BorderLayout.CENTER);
iconLabel = new IconLabel();
iconLabel.setBorder(border);
desktopIcon.add(iconLabel, BorderLayout.SOUTH);
desktopIcon.setSize(desktopIcon.getPreferredSize());
desktopIcon.validate();
mml = new MotionListener();
desktopIcon.addMouseMotionListener((MouseMotionListener)mml);
desktopIcon.addMouseListener((MouseListener)mml);
JLayeredPane.putLayer(desktopIcon, JLayeredPane.getLayer(frame));
}
public void uninstallUI(JComponent c) {
desktopIcon.setLayout(null);
desktopIcon.remove(iconButton);
desktopIcon.remove(iconLabel);
desktopIcon.removeMouseMotionListener((MouseMotionListener)mml);
desktopIcon.removeMouseListener((MouseListener)mml);
desktopIcon = null;
frame = null;
}
final static int LABEL_HEIGHT = 18;
final static int LABEL_DIVIDER = 4; // padding between icon and label
public Dimension getMinimumSize(JComponent c) {
JInternalFrame iframe = desktopIcon.getInternalFrame();
int w = defaultIcon.getIconWidth();
int h = defaultIcon.getIconHeight() + LABEL_HEIGHT + LABEL_DIVIDER;
Border border = iframe.getBorder();
if(border != null) {
w += border.getBorderInsets(iframe).left +
border.getBorderInsets(iframe).right;
h += border.getBorderInsets(iframe).bottom +
border.getBorderInsets(iframe).top;
}
return new Dimension(w, h);
}
public Dimension getPreferredSize(JComponent c) {
return getMinimumSize(c);
}
public Dimension getMaximumSize(JComponent c){
return getMinimumSize(c);
}
public Insets getInsets(JComponent c) {
JInternalFrame iframe = desktopIcon.getInternalFrame();
Border border = iframe.getBorder();
if(border != null)
return border.getBorderInsets(iframe);
return new Insets(0,0,0,0);
}
private class MotionListener
extends MouseAdapter implements MouseMotionListener, Serializable
{
// _x & _y are the mousePressed location in absolute coordinate system
int _x, _y;
// __x & __y are the mousePressed location in source view's coordinate system
int __x, __y;
Rectangle startingBounds;
public void mouseReleased(MouseEvent e) {
_x = 0;
_y = 0;
__x = 0;
__y = 0;
startingBounds = null;
}
public void mousePressed(MouseEvent e) {
Point p = SwingUtilities.convertPoint((Component)e.getSource(),
e.getX(), e.getY(), null);
__x = e.getX();
__y = e.getY();
_x = p.x;
_y = p.y;
startingBounds = desktopIcon.getBounds();
try { frame.setSelected(true); } catch (PropertyVetoException e1) { }
if(desktopIcon.getParent() instanceof JLayeredPane) {
((JLayeredPane)desktopIcon.getParent()).moveToFront(desktopIcon);
}
if(e.getClickCount() > 1) {
if(frame.isIconifiable() && frame.isIcon()) {
try { frame.setIcon(false); } catch (PropertyVetoException e2) { }
}
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
Point p;
int newX, newY, newW, newH;
int deltaX;
int deltaY;
Dimension min;
Dimension max;
p = SwingUtilities.convertPoint((Component)e.getSource(),
e.getX(), e.getY(), null);
Insets i = desktopIcon.getInsets();
int pWidth, pHeight;
pWidth = ((JComponent)desktopIcon.getParent()).getWidth();
pHeight = ((JComponent)desktopIcon.getParent()).getHeight();
newX = startingBounds.x - (_x - p.x);
newY = startingBounds.y - (_y - p.y);
// Make sure we stay in-bounds
if(newX + i.left <= -__x)
newX = -__x - i.left;
if(newY + i.top <= -__y)
newY = -__y - i.top;
if(newX + __x + i.right > pWidth)
newX = pWidth - __x - i.right;
if(newY + __y + i.bottom > pHeight)
newY = pHeight - __y - i.bottom;
JDesktopPane d;
if((d = desktopIcon.getDesktopPane()) != null) {
DesktopManager dm;
dm = d.getDesktopManager();
dm.setBoundsForFrame(desktopIcon, newX, newY,
desktopIcon.getWidth(),
desktopIcon.getHeight());
} else {
moveAndRepaint(desktopIcon, newX, newY,
desktopIcon.getWidth(), desktopIcon.getHeight());
}
return;
}
public void moveAndRepaint(JComponent f, int newX, int newY,
int newWidth, int newHeight) {
Rectangle r = f.getBounds();
f.setBounds(newX, newY, newWidth, newHeight);
SwingUtilities.computeUnion(newX, newY, newWidth, newHeight, r);
f.getParent().repaint(r.x, r.y, r.width, r.height);
}
}; /// End MotionListener
/**
* Returns the default desktop icon.
*/
public Icon getDefaultIcon() {
return defaultIcon;
}
/**
* Sets the icon used as the default desktop icon.
*/
public void setDefaultIcon(Icon newIcon) {
defaultIcon = newIcon;
}
class IconLabel extends JPanel {
IconLabel() {
super();
setFont(defaultTitleFont);
// Forward mouse events to titlebar for moves.
addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseMoved(MouseEvent e) {
forwardEventToParent(e);
}
});
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
forwardEventToParent(e);
}
public void mousePressed(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseReleased(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseEntered(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseExited(MouseEvent e) {
forwardEventToParent(e);
}
});
}
void forwardEventToParent(MouseEvent e) {
getParent().dispatchEvent(new MouseEvent(
getParent(), e.getID(), e.getWhen(), e.getModifiers(),
e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
}
public boolean isFocusTraversable() {
return false;
}
public Dimension getMinimumSize() {
return new Dimension(defaultIcon.getIconWidth() + 1,
LABEL_HEIGHT + LABEL_DIVIDER);
}
public Dimension getPreferredSize() {
String title = frame.getTitle();
FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(defaultTitleFont);
int w = fm.stringWidth(title) + 4;
return new Dimension(w, LABEL_HEIGHT + LABEL_DIVIDER);
}
public void paint(Graphics g) {
super.paint(g);
// touch-up frame
int maxX = getWidth() - 1;
Color shadow =
UIManager.getColor("inactiveCaptionBorder").darker().darker();
g.setColor(shadow);
g.setClip(0, 0, getWidth(), getHeight());
g.drawLine(maxX - 1, 1, maxX - 1, 1);
g.drawLine(maxX, 0, maxX, 0);
// fill background
g.setColor(UIManager.getColor("inactiveCaption"));
g.fillRect(2, 1, maxX - 3, LABEL_HEIGHT + 1);
// draw text -- clipping to truncate text like CDE/Motif
g.setClip(2, 1, maxX - 4, LABEL_HEIGHT);
int y = LABEL_HEIGHT - g.getFontMetrics().getDescent();
g.setColor(UIManager.getColor("inactiveCaptionText"));
g.drawString(frame.getTitle(), 4, y);
}
}
class IconButton extends JButton {
Icon icon;
IconButton(Icon icon) {
super(icon);
this.icon = icon;
// Forward mouse events to titlebar for moves.
addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseMoved(MouseEvent e) {
forwardEventToParent(e);
}
});
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
forwardEventToParent(e);
}
public void mousePressed(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseReleased(MouseEvent e) {
if (!systemMenu.isShowing()) {
forwardEventToParent(e);
}
}
public void mouseEntered(MouseEvent e) {
forwardEventToParent(e);
}
public void mouseExited(MouseEvent e) {
forwardEventToParent(e);
}
});
}
void forwardEventToParent(MouseEvent e) {
getParent().dispatchEvent(new MouseEvent(
getParent(), e.getID(), e.getWhen(), e.getModifiers(),
e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
}
public boolean isFocusTraversable() {
return false;
}
}
}