use of org.jivesoftware.spark.ui.ContactItem in project Spark by igniterealtime.
the class ReceiveFileTransfer method acceptRequest.
private void acceptRequest(final FileTransferRequest request) {
String requestor = request.getRequestor();
String bareJID = XmppStringUtils.parseBareJid(requestor);
ContactList contactList = SparkManager.getWorkspace().getContactList();
final ContactItem contactItem = contactList.getContactItemByJID(bareJID);
setBackground(new Color(239, 245, 250));
acceptButton.setVisible(false);
declineButton.setVisible(false);
titleLabel.setText(Res.getString("message.negotiate.file.transfer"));
titleLabel.setForeground(new Color(65, 139, 179));
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));
add(cancelButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
cancelButton.setVisible(true);
transfer = request.accept();
final File downloadedFile = new File(Downloads.getDownloadDirectory(), request.getFileName());
try {
_starttime = System.currentTimeMillis();
transfer.recieveFile(downloadedFile);
} catch (SmackException | IOException e) {
Log.error(e);
}
// setting it to percent
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
final Timer timer = new Timer();
TimerTask updateProgessBar = new TimerTask() {
@Override
public void run() {
if (transfer.getAmountWritten() >= request.getFileSize() || transfer.getStatus() == FileTransfer.Status.error || transfer.getStatus() == FileTransfer.Status.refused || transfer.getStatus() == FileTransfer.Status.cancelled || transfer.getStatus() == FileTransfer.Status.complete) {
this.cancel();
timer.cancel();
_endtime = System.currentTimeMillis();
updateonFinished(request, downloadedFile);
} else {
// 100 % = Filesize
// x % = Currentsize
long p = (transfer.getAmountWritten() * 100 / transfer.getFileSize());
progressBar.setValue(Math.round(p));
}
}
};
final Timer timer2 = new Timer();
TimerTask updatePrograssBarText = new TimerTask() {
long timenow;
long timeearlier;
long bytesnow;
long bytesearlier;
@Override
public void run() {
if (transfer.getAmountWritten() >= request.getFileSize() || transfer.getStatus() == FileTransfer.Status.error || transfer.getStatus() == FileTransfer.Status.refused || transfer.getStatus() == FileTransfer.Status.cancelled || transfer.getStatus() == FileTransfer.Status.complete) {
this.cancel();
timer2.cancel();
} else {
timenow = System.currentTimeMillis();
bytesnow = transfer.getAmountWritten();
bytesRead = transfer.getAmountWritten();
if (bytesRead == -1) {
bytesRead = 0;
}
ByteFormat format = new ByteFormat();
String text = format.format(bytesRead);
FileTransfer.Status status = transfer.getStatus();
if (status == FileTransfer.Status.in_progress) {
titleLabel.setText(Res.getString("message.receiving.file", contactItem.getDisplayName()));
String speed = TransferUtils.calculateSpeed(bytesnow - bytesearlier, timenow - timeearlier);
String est = TransferUtils.calculateEstimate(bytesnow, transfer.getFileSize(), _starttime, System.currentTimeMillis());
progressLabel.setText(Res.getString("message.transfer.progressbar.text.received", text, speed, est));
} else if (status == FileTransfer.Status.negotiating_stream) {
titleLabel.setText(Res.getString("message.negotiate.stream"));
}
bytesearlier = bytesnow;
timeearlier = timenow;
}
}
};
timer.scheduleAtFixedRate(updateProgessBar, 10, 10);
timer2.scheduleAtFixedRate(updatePrograssBarText, 10, 500);
}
use of org.jivesoftware.spark.ui.ContactItem in project Spark by igniterealtime.
the class ReceiveFileTransfer method transferDone.
private void transferDone(final FileTransferRequest request, FileTransfer transfer) {
cancelButton.setVisible(false);
showAlert(true);
String bareJID = XmppStringUtils.parseBareJid(request.getRequestor());
ContactList contactList = SparkManager.getWorkspace().getContactList();
ContactItem contactItem = contactList.getContactItemByJID(bareJID);
titleLabel.setText(Res.getString("message.received.file", contactItem.getDisplayName()));
fileLabel.setText(request.getFileName());
remove(acceptButton);
remove(declineButton);
remove(progressBar);
remove(pathButton);
final TransferButton openFileButton = new TransferButton();
final TransferButton openFolderButton = new TransferButton();
add(openFileButton, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
add(openFolderButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
final File downloadedFile = new File(Downloads.getDownloadDirectory(), request.getFileName());
openFileButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
openFileButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
openFileButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(MouseEvent e) {
launchFile(Downloads.getDownloadDirectory() + File.separator + request.getFileName());
}
});
openFolderButton.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
openFolderButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e) {
openFolderButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(MouseEvent event) {
launchFile(Downloads.getDownloadDirectory());
}
});
add(fileLabel, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 5, 5), 0, 0));
ResourceUtils.resButton(openFileButton, Res.getString("open"));
ResourceUtils.resButton(openFolderButton, Res.getString("open.folder"));
openFileButton.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
openFileButton.setForeground(new Color(73, 113, 196));
openFileButton.setFont(new Font("Dialog", Font.BOLD, 11));
openFolderButton.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(73, 113, 196)));
openFolderButton.setForeground(new Color(73, 113, 196));
openFolderButton.setFont(new Font("Dialog", Font.BOLD, 11));
imageLabel.setIcon(GraphicUtils.getIcon(downloadedFile));
imageLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
showPopup(e, downloadedFile);
}
public void mouseReleased(MouseEvent e) {
showPopup(e, downloadedFile);
}
});
if (isImage(downloadedFile.getName())) {
try {
URL imageURL = downloadedFile.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));
}
}
invalidate();
validate();
repaint();
}
use of org.jivesoftware.spark.ui.ContactItem 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);
}
use of org.jivesoftware.spark.ui.ContactItem in project Spark by igniterealtime.
the class PresenceChangePlugin method initialize.
public void initialize() {
// Listen for right-clicks on ContactItem
final ContactList contactList = SparkManager.getWorkspace().getContactList();
final Action listenAction = new AbstractAction() {
private static final long serialVersionUID = 7705539667621148816L;
public void actionPerformed(ActionEvent e) {
for (ContactItem item : contactList.getSelectedUsers()) {
String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
sparkContacts.add(bareAddress);
}
}
};
listenAction.putValue(Action.NAME, Res.getString("menuitem.alert.when.online"));
listenAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_ALARM_CLOCK));
final Action removeAction = new AbstractAction() {
private static final long serialVersionUID = -8726129089417116105L;
public void actionPerformed(ActionEvent e) {
for (ContactItem item : contactList.getSelectedUsers()) {
String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
sparkContacts.remove(bareAddress);
}
}
};
removeAction.putValue(Action.NAME, Res.getString("menuitem.remove.alert.when.online"));
removeAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.SMALL_DELETE));
contactList.addContextMenuListener(new ContextMenuListener() {
public void poppingUp(Object object, JPopupMenu popup) {
if (object instanceof ContactItem) {
ContactItem item = (ContactItem) object;
String bareAddress = XmppStringUtils.parseBareJid(item.getJID());
if (!item.getPresence().isAvailable() || item.getPresence().isAway()) {
if (sparkContacts.contains(bareAddress)) {
popup.add(removeAction);
} else {
popup.add(listenAction);
}
}
}
}
public void poppingDown(JPopupMenu popup) {
}
public boolean handleDefaultAction(MouseEvent e) {
return false;
}
});
// Check presence changes
SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
try {
Presence presence = (Presence) stanza;
if (!presence.isAvailable() || presence.isAway()) {
return;
}
String from = presence.getFrom();
ArrayList<String> removelater = new ArrayList<>();
for (final String jid : sparkContacts) {
if (jid.equals(XmppStringUtils.parseBareJid(from))) {
removelater.add(jid);
// sparkContacts.remove(jid);
String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
String time = SparkManager.DATE_SECOND_FORMATTER.format(new Date());
String infoText = Res.getString("message.user.now.available.to.chat", nickname, time);
if (localPref.getShowToasterPopup()) {
EventQueue.invokeLater(() -> {
SparkToaster toaster = new SparkToaster();
toaster.setDisplayTime(5000);
toaster.setBorder(BorderFactory.createBevelBorder(0));
toaster.setToasterHeight(150);
toaster.setToasterWidth(200);
toaster.setTitle(nickname);
toaster.showToaster(null, infoText);
toaster.setCustomAction(new AbstractAction() {
private static final long serialVersionUID = 4827542713848133369L;
@Override
public void actionPerformed(ActionEvent e) {
SparkManager.getChatManager().getChatRoom(jid);
}
});
});
}
ChatRoom room = SparkManager.getChatManager().getChatRoom(jid);
if (localPref.getWindowTakesFocus()) {
EventQueue.invokeLater(() -> SparkManager.getChatManager().activateChat(jid, nickname));
}
EventQueue.invokeLater(() -> room.getTranscriptWindow().insertNotificationMessage(infoText, ChatManager.NOTIFICATION_COLOR));
}
}
for (String s : removelater) {
sparkContacts.remove(s);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}, new StanzaTypeFilter(Presence.class));
}
use of org.jivesoftware.spark.ui.ContactItem in project Spark by igniterealtime.
the class BroadcastPlugin method initialize.
public void initialize() {
// See if we should disable all "Broadcast" menu items
if (Default.getBoolean("DISABLE_BROADCAST_MENU_ITEM") || !Enterprise.containsFeature(Enterprise.BROADCAST_FEATURE))
return;
// Add as ContainerDecoratr
SparkManager.getChatManager().addSparkTabHandler(this);
StanzaFilter serverFilter = new StanzaTypeFilter(Message.class);
SparkManager.getConnection().addAsyncStanzaListener(this, serverFilter);
// Register with action menu
final JMenu actionsMenu = SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));
JMenuItem broadcastHistoryMenu = new JMenuItem(Res.getString("title.broadcast.history"), SparkRes.getImageIcon(SparkRes.HISTORY_16x16));
JMenuItem broadcastMenu = new JMenuItem(Res.getString("title.broadcast.message"), SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
ResourceUtils.resButton(broadcastMenu, Res.getString("title.broadcast.message"));
actionsMenu.add(broadcastHistoryMenu);
actionsMenu.add(broadcastMenu);
broadcastMenu.addActionListener(e -> broadcastToRoster());
broadcastHistoryMenu.addActionListener(e -> {
new BroadcastHistoryFrame().run();
});
// Register with action menu
JMenuItem startConversationtMenu = new JMenuItem("", SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_IMAGE));
ResourceUtils.resButton(startConversationtMenu, Res.getString("menuitem.start.a.chat"));
if (!Default.getBoolean("HIDE_START_A_CHAT") && Enterprise.containsFeature(Enterprise.START_A_CHAT_FEATURE)) {
actionsMenu.add(startConversationtMenu, 0);
}
startConversationtMenu.addActionListener(e -> {
ContactList contactList = SparkManager.getWorkspace().getContactList();
Collection<ContactItem> selectedUsers = contactList.getSelectedUsers();
String selectedUser = "";
Iterator<ContactItem> selectedUsersIterator = selectedUsers.iterator();
if (selectedUsersIterator.hasNext()) {
ContactItem contactItem = selectedUsersIterator.next();
selectedUser = contactItem.getJID();
}
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
UIManager.put("OptionPane.cancelButtonText", Res.getString("cancel"));
String jid = (String) JOptionPane.showInputDialog(SparkManager.getMainWindow(), Res.getString("label.enter.address"), Res.getString("title.start.chat"), JOptionPane.QUESTION_MESSAGE, null, null, selectedUser);
if (ModelUtil.hasLength(jid) && ModelUtil.hasLength(XmppStringUtils.parseDomain(jid))) {
if (ModelUtil.hasLength(jid) && jid.indexOf('@') == -1) {
// Append server address
jid = jid + "@" + SparkManager.getConnection().getServiceName();
}
String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
jid = UserManager.escapeJID(jid);
ChatRoom chatRoom = SparkManager.getChatManager().createChatRoom(jid, nickname, nickname);
SparkManager.getChatManager().getChatContainer().activateChatRoom(chatRoom);
}
});
// Add send to selected users.
final ContactList contactList = SparkManager.getWorkspace().getContactList();
contactList.addContextMenuListener(new ContextMenuListener() {
public void poppingUp(Object component, JPopupMenu popup) {
if (component instanceof ContactGroup) {
final ContactGroup group = (ContactGroup) component;
Action broadcastMessageAction = new AbstractAction() {
private static final long serialVersionUID = -6411248110270296726L;
public void actionPerformed(ActionEvent e) {
broadcastToGroup(group);
}
};
broadcastMessageAction.putValue(Action.NAME, Res.getString("menuitem.broadcast.to.group"));
broadcastMessageAction.putValue(Action.SMALL_ICON, SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
popup.add(broadcastMessageAction);
}
}
public void poppingDown(JPopupMenu popup) {
}
public boolean handleDefaultAction(MouseEvent e) {
return false;
}
});
// Add Broadcast to roster
StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();
RolloverButton broadcastToRosterButton = new RolloverButton(SparkRes.getImageIcon(SparkRes.MEGAPHONE_16x16));
broadcastToRosterButton.setToolTipText(Res.getString("message.send.a.broadcast"));
statusBar.invalidate();
statusBar.validate();
statusBar.repaint();
broadcastToRosterButton.addActionListener(e -> broadcastToRoster());
}
Aggregations