Commit 5e227297 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Added dnd into launcher frame.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1092 b35dd754-fafc-0310-a699-88a17e54d16e
parent a653ff41
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 1999-2005 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is
subject to license terms.
*/
package org.jivesoftware.messenger.launcher;
import javax.swing.JFrame;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
/**
* Creates a DroppableFrame. A droppable frame allows for DnD of file objects from the OS onto the actual frame
* via <code>File</code>
*/
public class DroppableFrame extends JFrame implements DropTargetListener, DragSourceListener, DragGestureListener {
private DropTarget dropTarget = new DropTarget(this, this);
private DragSource dragSource = DragSource.getDefaultDragSource();
/**
* Creates an Instance of the Droppable Frame.
*/
public DroppableFrame() {
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
}
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
}
public void dragExit(DragSourceEvent DragSourceEvent) {
}
public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
}
public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
}
public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit(DropTargetEvent dropTargetEvent) {
}
public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
}
public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
}
public void drop(DropTargetDropEvent dropTargetDropEvent) {
try {
Transferable transferable = dropTargetDropEvent.getTransferable();
if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext()) {
File file = (File) iterator.next();
if (file.isFile()) {
fileDropped(file);
}
if (file.isDirectory()) {
directoryDropped(file);
}
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
}
else {
dropTargetDropEvent.rejectDrop();
}
}
catch (IOException io) {
io.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
}
/**
* Notified when a file has been dropped onto the frame.
*
* @param file the file that has been dropped.
*/
public void fileDropped(File file){
}
/**
* Notified when a directory has been dropped onto the frame.
*
* @param file the directory that has been dropped.
*/
public void directoryDropped(File file){
}
}
\ No newline at end of file
......@@ -11,18 +11,36 @@
package org.jivesoftware.messenger.launcher;
import java.awt.*;
import com.jivesoftware.xmpp.workgroup.utils.URLFileSystem;
import org.jdesktop.jdic.tray.SystemTray;
import org.jdesktop.jdic.tray.TrayIcon;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.util.XMLProperties;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.*;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.util.XMLProperties;
import org.jdesktop.jdic.tray.TrayIcon;
import org.jdesktop.jdic.tray.SystemTray;
import java.io.IOException;
/**
* Graphical launcher for Jive Messenger.
......@@ -31,13 +49,14 @@ import org.jdesktop.jdic.tray.SystemTray;
*/
public class Launcher {
private Process messengerd = null;
private Process messengerd;
private String configFile = JiveGlobals.getMessengerHome() + File.separator + "conf" + File.separator + "jive-messenger.xml";
private JPanel toolbar = new JPanel();
private ImageIcon offIcon;
private ImageIcon onIcon;
private TrayIcon trayIcon;
private JFrame frame;
/**
* Creates a new Launcher object.
......@@ -54,7 +73,16 @@ public class Launcher {
}
String title = "Jive Messenger";
final JFrame frame = new JFrame(title);
frame = new DroppableFrame() {
public void fileDropped(File file) {
String fileName = file.getName();
if (fileName.endsWith("*.jar")) {
installPlugin(file);
}
}
};
frame.setTitle(title);
ImageIcon splash = null;
JLabel splashLabel = null;
......@@ -131,7 +159,7 @@ public class Launcher {
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Start")) {
if ("Start".equals(e.getActionCommand())) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// Adjust button and menu items.
startApplication();
......@@ -145,8 +173,11 @@ public class Launcher {
// Start a thread to enable the admin button after 8 seconds.
Thread thread = new Thread() {
public void run() {
try { sleep(8000); }
catch (Exception e) { }
try {
sleep(8000);
}
catch (Exception e) {
}
// Enable the Launch Admin button/menu item only if the
// server has started.
if (stopButton.isEnabled()) {
......@@ -159,7 +190,7 @@ public class Launcher {
thread.start();
}
else if (e.getActionCommand().equals("Stop")) {
else if ("Stop".equals(e.getActionCommand())) {
stopApplication();
// Change to the "off" button.
frame.setIconImage(offIcon.getImage());
......@@ -173,14 +204,14 @@ public class Launcher {
startMenuItem.setEnabled(true);
stopMenuItem.setEnabled(false);
}
else if (e.getActionCommand().equals("Launch Admin")) {
else if ("Launch Admin".equals(e.getActionCommand())) {
launchBrowser();
}
else if (e.getActionCommand().equals("Quit")) {
else if ("Quit".equals(e.getActionCommand())) {
stopApplication();
System.exit(0);
}
else if (e.getActionCommand().equals("Hide/Show") || e.getActionCommand().equals("PressAction")) {
else if ("Hide/Show".equals(e.getActionCommand()) || "PressAction".equals(e.getActionCommand())) {
// Hide/Unhide the window if the user clicked in the system tray icon or
// selected the menu option
if (frame.isVisible()) {
......@@ -256,10 +287,10 @@ public class Launcher {
File windowsExe = new File(new File("").getAbsoluteFile(), "messengerd.exe");
File unixExe = new File(new File("").getAbsoluteFile(), "messengerd");
if (windowsExe.exists()) {
messengerd = Runtime.getRuntime().exec(new String[] { windowsExe.toString() });
messengerd = Runtime.getRuntime().exec(new String[]{windowsExe.toString()});
}
else if (unixExe.exists()) {
messengerd = Runtime.getRuntime().exec(new String[] { unixExe.toString() });
messengerd = Runtime.getRuntime().exec(new String[]{unixExe.toString()});
}
else {
throw new FileNotFoundException();
......@@ -307,5 +338,47 @@ public class Launcher {
JOptionPane.showMessageDialog(new JFrame(), configFile + " " + e.getMessage());
}
}
private void installPlugin(final File plugin) {
final JDialog dialog = new JDialog(frame, "Installing Plugin", true);
dialog.getContentPane().setLayout(new BorderLayout());
JProgressBar bar = new JProgressBar();
bar.setIndeterminate(true);
bar.setString("Installing Plugin. Please wait...");
bar.setStringPainted(true);
dialog.getContentPane().add(bar, BorderLayout.CENTER);
dialog.pack();
dialog.setSize(225, 55);
final SwingWorker installerThread = new SwingWorker() {
public Object construct() {
File pluginsDir = new File(JiveGlobals.getMessengerHome(), "plugins");
String tempName = plugin.getName() + ".part";
File tempPluginsFile = new File(pluginsDir, tempName);
File realPluginsFile = new File(pluginsDir, plugin.getName());
// Copy Plugin into Dir.
try {
URLFileSystem.copy(plugin.toURL(), tempPluginsFile);
// If successfull, rename to real plugin name.
tempPluginsFile.renameTo(realPluginsFile);
}
catch (IOException e) {
e.printStackTrace();
}
return realPluginsFile;
}
public void finished() {
dialog.setVisible(false);
}
};
// Start installation
installerThread.start();
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 1999-2005 Jive Software. All rights reserved.
*
* This software is the proprietary information of Jive Software. Use is
subject to license terms.
*/
package org.jivesoftware.messenger.launcher;
import javax.swing.SwingUtilities;
/**
* Modified SwingWorker that actually works.
* @author Derek DeMoro
*/
public abstract class SwingWorker {
private Object value; // see getValue(), setValue()
private Thread thread;
/**
* Class to maintain reference to current worker thread
* under separate synchronization control.
*/
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) {
thread = t;
}
synchronized Thread get() {
return thread;
}
synchronized void clear() {
thread = null;
}
}
private ThreadVar threadVar;
/**
* Get the value produced by the worker thread, or null if it
* hasn't been constructed yet.
*/
protected synchronized Object getValue() {
return value;
}
/**
* Set the value produced by worker thread
*/
private synchronized void setValue(Object x) {
value = x;
}
/**
* Compute the value to be returned by the <code>get</code> method.
*/
public abstract Object construct();
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished() {
}
/**
* A new method that interrupts the worker thread. Call this method
* to force the worker to stop what it's doing.
*/
public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
/**
* Return the value created by the <code>construct</code> method.
* Returns null if either the constructing thread or the current
* thread was interrupted before a value was produced.
*
* @return the value created by the <code>construct</code> method
*/
public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
/**
* Start a thread that will call the <code>construct</code> method
* and then exit.
*/
public SwingWorker() {
final Runnable doFinished = new Runnable() {
public void run() {
finished();
}
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
finished();
}
});
}
};
Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = threadVar.get();
if (t != null) {
t.start();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment