/* * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.net.*; import javax.swing.*; import netscape.javascript.JSObject; import netscape.javascript.JSException; /** Simple example of dragging applets to the desktop illustrating: *
isAppletDragStart
* setAppletCloseListener
* showDocument and showStatus
* <shortcut> tag
* plugin.jar on the compilation classpath.
*/
public class DragExample extends JApplet {
private URL documentURL;
private int statusCount;
private ContentPanel panel;
private ActionListener closeListener;
private static final int DRAG_BAR_HEIGHT = 12;
private static final Font DRAG_BAR_FONT = new Font("SansSerif", Font.PLAIN, 10);
private static final String TEXT = "Click And Drag Here";
class ContentPanel extends JPanel {
private boolean gotTextWidth;
private int textWidth;
public ContentPanel() {
super();
setBackground(Color.GRAY);
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (closeListener != null) {
if (e.getButton() == MouseEvent.BUTTON1 &&
e.getY() <= DRAG_BAR_HEIGHT &&
e.getX() >= (getWidth() - DRAG_BAR_HEIGHT)) {
// Shut ourselves down
closeListener.actionPerformed(null);
}
}
}
});
}
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics;
int width = getWidth();
int height = getHeight();
// Draw the red bar and text
g.setColor(Color.RED);
g.fillRect(0, 0, width, DRAG_BAR_HEIGHT);
g.setColor(Color.WHITE);
g.setFont(DRAG_BAR_FONT);
if (!gotTextWidth) {
FontMetrics fm = g.getFontMetrics();
textWidth = fm.charsWidth(TEXT.toCharArray(), 0, TEXT.length());
gotTextWidth = true;
}
g.drawString(TEXT, (width - textWidth) / 2, DRAG_BAR_HEIGHT - 1);
// Draw the close region
int xLeft = getWidth() - DRAG_BAR_HEIGHT + 1;
int xRight = getWidth() - 2;
int xTop = 1;
int xBottom = DRAG_BAR_HEIGHT - 2;
g.drawLine(xLeft, xTop, xRight, xBottom);
g.drawLine(xLeft, xBottom, xRight, xTop);
}
public boolean isAppletDragStart(MouseEvent e) {
// Change the drag gesture to be left-click and drag with
// no modifier keys
return ((e.getSource() == this ||
((e.getSource() instanceof Container) &&
(((Container) e.getSource()).isAncestorOf(this)))) &&
e.getY() <= DRAG_BAR_HEIGHT &&
e.getX() < (getWidth() - DRAG_BAR_HEIGHT) &&
!e.isAltDown() &&
!e.isAltGraphDown() &&
!e.isControlDown() &&
!e.isMetaDown() &&
!e.isShiftDown());
}
}
public void init() {
try {
try {
JSObject win = JSObject.getWindow(this);
documentURL =
new URL((String) win.eval("document.URL"));
} catch (JSException e) {
// When running in disconnected mode, we can't talk
// back to the browser, so provide a reasonable
// default
documentURL = new URL("https://jdk6.dev.java.net/plugin2/");
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Note that if the applet has been dragged to
// the desktop, but the original web page of
// the applet is still visible, then clicking
// this button will replace the current web
// page (effectively forcing a reload),
// causing the applet to be disconnected. If
// the applet has already been disconnected,
// clicking this button will cause the page to
// be opened in a new browser window via an
// invocation of the system web browser. This
// illustrates that the
// AppletContext.showDocument() functionality
// gracefully falls back to an implementation
// standalone from the web browser as the
// applet is disconnected from the browser.
JButton button = new JButton("Reopen Web Page");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getAppletContext().showDocument(documentURL);
}
});
panel = new ContentPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Box.createVerticalGlue());
panel.add(createCenteredButton(button));
panel.add(Box.createVerticalGlue());
// Note that as long as the web page
// originally containing the applet is still
// visible, clicking this button will display
// the status message in the browser's status
// bar. Once the applet has been disconnected
// from the browser, the status message will
// be displayed in the Java Console.
button = new JButton("Show Applet Status");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getAppletContext().showStatus("Showng Applet Status: " +
(++statusCount));
}
});
panel.add(createCenteredButton(button));
panel.add(Box.createVerticalGlue());
add(panel);
}
});
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
private static Component createCenteredButton(JButton button) {
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(button);
box.add(Box.createHorizontalGlue());
return box;
}
//----------------------------------------------------------------------
// Methods for interacting with the drag-and-drop support
//
public boolean isAppletDragStart(MouseEvent e) {
if (panel == null) {
return false;
}
return panel.isAppletDragStart(e);
}
public void setAppletCloseListener(ActionListener l) {
closeListener = l;
}
public void appletRestored() {
closeListener = null;
}
}