use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class SparkTransferManager method sendImage.
/**
* Send an image to a user.
*
* @param image the image to send.
* @param room the ChatRoom of the user you wish to send the image to.
*/
public void sendImage(final BufferedImage image, final ChatRoom room) {
File tmpDirectory = new File(Spark.getSparkUserHome(), "/tempImages");
tmpDirectory.mkdirs();
String imageName = "image_" + StringUtils.randomString(2) + ".png";
final File imageFile = new File(tmpDirectory, imageName);
// Write image to system.
room.setCursor(new Cursor(Cursor.WAIT_CURSOR));
SwingWorker writeImageThread = new SwingWorker() {
public Object construct() {
try {
// Write out file in separate thread.
ImageIO.write(image, "png", imageFile);
} catch (IOException e) {
Log.error(e);
}
return true;
}
public void finished() {
ChatRoomImpl roomImpl = (ChatRoomImpl) room;
sendFile(imageFile, roomImpl.getParticipantJID());
SparkManager.getChatManager().getChatContainer().activateChatRoom(room);
room.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
};
writeImageThread.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class BookmarksUI method addRegisteredServices.
private void addRegisteredServices() {
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
try {
if (SparkManager.getConnection().isConnected()) {
mucServices = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getServiceNames();
}
} catch (XMPPException | SmackException e) {
Log.error("Unable to load MUC Service Names.", e);
}
return mucServices;
}
@Override
public void finished() {
if (mucServices == null) {
return;
}
for (Object mucService : mucServices) {
String service = (String) mucService;
if (!hasService(service)) {
addServiceToList(service);
}
}
}
};
worker.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class ConferenceRoomBrowser method startLoadingImg.
private void startLoadingImg() {
SwingWorker startLoading = new SwingWorker() {
@Override
public Object construct() {
return null;
}
@Override
public void finished() {
refreshButton.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE));
refreshButton.validate();
refreshButton.repaint();
refreshItem.setIcon(SparkRes.getImageIcon(SparkRes.BUSY_IMAGE));
refreshItem.validate();
refreshItem.repaint();
}
};
startLoading.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class ConferenceRoomBrowser method addRoomToTable.
/**
* Adds a room to the room table.
*
* @param jid
* the jid of the conference room.
* @param roomName
* the name of the conference room.
* @param numberOfOccupants
* the number of occupants in the conference room. If -1 is
* specified, the the occupant count will show as n/a.
*/
private void addRoomToTable(final String jid, final String roomName, final int numberOfOccupants) {
SwingWorker addRoomThread = new SwingWorker() {
@Override
public Object construct() {
JLabel iconLabel = new JLabel();
iconLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
boolean isbookmark = false;
boolean ispassword = false;
ImageIcon bookmarkicon = SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON);
ImageIcon passwordicon = SparkRes.getImageIcon(SparkRes.LOCK_16x16);
if (isBookmarked(jid)) {
isbookmark = true;
iconLabel.setIcon(SparkRes.getImageIcon(SparkRes.BOOKMARK_ICON));
}
if (isPasswordProtected(jid)) {
ispassword = true;
}
if (isbookmark && ispassword) {
Image img = ImageCombiner.combine(bookmarkicon, passwordicon);
iconLabel.setIcon(new ImageIcon(img));
} else if (isbookmark) {
iconLabel.setIcon(bookmarkicon);
} else if (ispassword) {
Image img = ImageCombiner.returnTransparentImage(passwordicon.getIconWidth(), passwordicon.getIconHeight());
Image combined = ImageCombiner.combine(new ImageIcon(img), passwordicon);
iconLabel.setIcon(new ImageIcon(combined));
}
String occupants = Integer.toString(numberOfOccupants);
if (numberOfOccupants == -1) {
occupants = "n/a";
}
final Object[] insertRoom = new Object[] { iconLabel, roomName, XmppStringUtils.parseLocalpart(jid), occupants };
return insertRoom;
}
@Override
public void finished() {
Object[] insertRoom = (Object[]) get();
roomsTable.getTableModel().addRow(insertRoom);
}
};
addRoomThread.start();
}
use of org.jivesoftware.spark.util.SwingWorker in project Spark by igniterealtime.
the class SendFileTransfer method sendFile.
public void sendFile(final OutgoingFileTransfer transfer, FileTransferManager transferManager, final String jid, final String nickname) {
this.transferManager = transferManager;
// SPARK-1869
FileTransferNegotiator.getInstanceFor(SparkManager.getConnection());
FileTransferNegotiator.IBB_ONLY = SettingsManager.getLocalPreferences().isFileTransferIbbOnly();
cancelButton.setVisible(true);
retryButton.setVisible(false);
this.fullJID = jid;
this.nickname = nickname;
this.transfer = transfer;
String fileName = transfer.getFileName();
long fileSize = transfer.getFileSize();
ByteFormat format = new ByteFormat();
String text = format.format(fileSize);
fileToSend = new File(transfer.getFilePath());
imageLabel.setFile(fileToSend);
fileLabel.setText(fileName + " (" + text + ")");
ContactList contactList = SparkManager.getWorkspace().getContactList();
ContactItem contactItem = contactList.getContactItemByJID(jid);
titleLabel.setText(Res.getString("message.transfer.waiting.on.user", contactItem.getDisplayName()));
if (isImage(fileName)) {
try {
URL imageURL = new File(transfer.getFilePath()).toURI().toURL();
ImageIcon image = new ImageIcon(imageURL);
image = GraphicUtils.scaleImageIcon(image, 64, 64);
imageLabel.setIcon(image);
} catch (MalformedURLException e) {
Log.error("Could not locate image.", e);
imageLabel.setIcon(SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32));
}
} else {
File file = new File(transfer.getFilePath());
Icon icon = GraphicUtils.getIcon(file);
imageLabel.setIcon(icon);
}
cancelButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
transfer.cancel();
}
});
cancelButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
cancelButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
cancelButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});
progressBar.setMaximum(100);
progressBar.setVisible(false);
progressBar.setStringPainted(true);
add(progressBar, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
add(progressLabel, new GridBagConstraints(1, 3, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 150, 0));
SwingWorker worker = new SwingWorker() {
public Object construct() {
while (true) {
try {
if (transfer.getBytesSent() > 0 && _starttime == 0) {
_starttime = System.currentTimeMillis();
}
long starttime = System.currentTimeMillis();
long startbyte = transfer.getBytesSent();
Thread.sleep(500);
FileTransfer.Status status = transfer.getStatus();
if (status == Status.error || status == Status.complete || status == Status.cancelled || status == Status.refused) {
break;
}
long endtime = System.currentTimeMillis();
long endbyte = transfer.getBytesSent();
long timediff = endtime - starttime;
long bytediff = endbyte - startbyte;
updateBar(transfer, nickname, TransferUtils.calculateSpeed(bytediff, timediff));
} catch (InterruptedException e) {
Log.error("Unable to sleep thread.", e);
}
}
return "";
}
public void finished() {
updateBar(transfer, nickname, "??MB/s");
}
};
worker.start();
makeClickable(imageLabel);
makeClickable(titleLabel);
}
Aggregations