001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing.plaf.basic;
022:
023: import java.awt.BorderLayout;
024: import java.awt.Component;
025: import java.awt.Dimension;
026: import java.awt.Insets;
027: import java.awt.Rectangle;
028: import java.awt.event.MouseEvent;
029: import java.beans.PropertyVetoException;
030:
031: import javax.swing.DesktopManager;
032: import javax.swing.JComponent;
033: import javax.swing.JInternalFrame;
034: import javax.swing.JLayeredPane;
035: import javax.swing.LookAndFeel;
036:
037: import javax.swing.event.MouseInputAdapter;
038: import javax.swing.event.MouseInputListener;
039:
040: import javax.swing.plaf.ComponentUI;
041: import javax.swing.plaf.DesktopIconUI;
042:
043: import org.apache.harmony.x.swing.ComponentDragImplHelper;
044: import org.apache.harmony.x.swing.Utilities;
045:
046: import org.apache.harmony.x.swing.internal.nls.Messages;
047:
048: public class BasicDesktopIconUI extends DesktopIconUI {
049: private static final int INSETS_WIDTH = 5;
050:
051: public class MouseInputHandler extends MouseInputAdapter {
052: private ComponentDragImplHelper helper;
053: private DesktopManager desktopManager;
054:
055: public MouseInputHandler() {
056: helper = new ComponentDragImplHelper();
057: }
058:
059: public void mouseDragged(final MouseEvent e) {
060: Rectangle bounds = helper.mouseDragged(e);
061: moveAndRepaint(desktopIcon, bounds.x, bounds.y,
062: bounds.width, bounds.height);
063: }
064:
065: public void mouseMoved(final MouseEvent e) {
066: return;
067: }
068:
069: public void mousePressed(final MouseEvent e) {
070: if (e.getClickCount() > 1 && frame.isIconifiable()) {
071: deiconize();
072: return;
073: }
074:
075: desktopManager = ((BasicInternalFrameUI) frame.getUI())
076: .getDesktopManager();
077: try {
078: frame.setSelected(true);
079: Component desktop = desktopIcon.getParent();
080: if (desktop instanceof JLayeredPane) {
081: ((JLayeredPane) desktop).moveToFront(desktopIcon);
082: }
083: } catch (final PropertyVetoException e1) {
084: }
085: helper.beginDragging(e, desktopIcon, desktopIcon
086: .getDesktopPane());
087: desktopManager.beginDraggingFrame(desktopIcon);
088: }
089:
090: public void mouseReleased(final MouseEvent e) {
091: helper.endDraggingOrResizing(e);
092: desktopManager.endDraggingFrame(desktopIcon);
093: }
094:
095: public void moveAndRepaint(final JComponent f, final int newX,
096: final int newY, final int newWidth, final int newHeight) {
097: desktopManager.dragFrame(desktopIcon, newX, newY);
098: }
099: }
100:
101: public static ComponentUI createUI(final JComponent c) {
102: return new BasicDesktopIconUI();
103: }
104:
105: protected JInternalFrame.JDesktopIcon desktopIcon;
106: protected JInternalFrame frame;
107: protected JComponent iconPane;
108: private MouseInputListener mouseHandler;
109:
110: public BasicDesktopIconUI() {
111: }
112:
113: protected MouseInputListener createMouseInputListener() {
114: if (mouseHandler == null) {
115: mouseHandler = new MouseInputHandler();
116: }
117:
118: return mouseHandler;
119: }
120:
121: public void deiconize() {
122: try {
123: frame.setIcon(false);
124: } catch (final PropertyVetoException e) {
125: }
126: }
127:
128: public Insets getInsets(final JComponent c) {
129: if (c == null) {
130: throw new NullPointerException(Messages.getString(
131: "swing.03", "component")); //$NON-NLS-1$ //$NON-NLS-2$
132: }
133: //return desktopIcon.getInsets();
134: return new Insets(INSETS_WIDTH, INSETS_WIDTH, INSETS_WIDTH,
135: INSETS_WIDTH);
136: }
137:
138: public Dimension getMaximumSize(final JComponent c) {
139: return getPreferredSize(c);
140: }
141:
142: public Dimension getMinimumSize(final JComponent c) {
143: //return iconPane.getLayout().minimumLayoutSize(c);
144: return getPreferredSize(c);
145: }
146:
147: public Dimension getPreferredSize(final JComponent c) {
148: return Utilities.addInsets(iconPane.getPreferredSize(),
149: desktopIcon.getInsets());
150: }
151:
152: protected void installComponents() {
153: iconPane = new BasicInternalFrameTitlePane(frame);
154: desktopIcon.add(iconPane, BorderLayout.CENTER);
155: }
156:
157: protected void uninstallComponents() {
158: desktopIcon.remove(iconPane);
159: }
160:
161: protected void installDefaults() {
162: LookAndFeel.installBorder(desktopIcon, "DesktopIcon.border");
163: }
164:
165: protected void uninstallDefaults() {
166: LookAndFeel.uninstallBorder(desktopIcon);
167: }
168:
169: protected void installListeners() {
170: mouseHandler = createMouseInputListener();
171:
172: desktopIcon.addMouseListener(mouseHandler);
173: desktopIcon.addMouseMotionListener(mouseHandler);
174: }
175:
176: protected void uninstallListeners() {
177: desktopIcon.removeMouseListener(mouseHandler);
178: desktopIcon.removeMouseMotionListener(mouseHandler);
179: }
180:
181: public void installUI(final JComponent c) {
182: desktopIcon = (JInternalFrame.JDesktopIcon) c;
183: frame = desktopIcon.getInternalFrame();
184:
185: installDefaults();
186: installListeners();
187: installComponents();
188:
189: desktopIcon.setSize(desktopIcon.getPreferredSize());
190: }
191:
192: public void uninstallUI(final JComponent c) {
193: uninstallDefaults();
194: uninstallListeners();
195: uninstallComponents();
196: }
197: }
|