use of org.jivesoftware.spark.ui.ContactGroup in project Spark by igniterealtime.
the class ContactListAssistantPlugin method moveItems.
/**
* Moves a collection of <code>ContactItem</code>s to the specified group.
*
* @param contactItems the contact items to move.
* @param groupName the name of the group to move to.
*/
private void moveItems(Collection<ContactItem> contactItems, String groupName) {
final ContactGroup contactGroup = getContactGroup(groupName);
ContactGroup oldGroup;
for (ContactItem contactItem : contactItems) {
oldGroup = getContactGroup(contactItem.getGroupName());
if (oldGroup.isSharedGroup()) {
continue;
}
addContactItem(contactGroup, contactItem, true);
}
}
use of org.jivesoftware.spark.ui.ContactGroup in project Spark by igniterealtime.
the class ContactListAssistantPlugin method addContactItem.
/**
* Copies or moves a new <code>ContactItem</code> into the <code>ContactGroup</code>.
*
* @param contactGroup the ContactGroup.
* @param item the ContactItem to move.
* @param move true if the ContactItem should be moved, otherwise false.
*/
private void addContactItem(final ContactGroup contactGroup, final ContactItem item, final boolean move) {
ContactItem newContact = UIComponentRegistry.createContactItem(item.getAlias(), item.getNickname(), item.getJID());
newContact.setPresence(item.getPresence());
newContact.setIcon(item.getIcon());
newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
boolean groupHadAvailableContacts = false;
// Do not copy/move a contact item only if it is not already in the Group.
if (contactGroup.getContactItemByJID(item.getJID(), true) != null) {
return;
}
if (!PresenceManager.isOnline(item.getJID())) {
contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJID(), null);
} else {
groupHadAvailableContacts = contactGroup.hasAvailableContacts();
contactGroup.addContactItem(newContact);
}
contactGroup.clearSelection();
// Updating group title
contactGroup.fireContactGroupUpdated();
final ContactGroup oldGroup = getContactGroup(item.getGroupName());
final boolean groupAvailableContacts = groupHadAvailableContacts;
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
RosterGroup groupFound = null;
for (RosterGroup group : roster.getGroups()) {
if (group.getName().equals(contactGroup.getGroupName())) {
try {
groupFound = group;
if (!groupAvailableContacts) {
SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
}
group.addEntry(entry);
} catch (XMPPException | SmackException e1) {
Log.error(e1);
return false;
}
}
}
// This is a new group
if (groupFound == null) {
groupFound = roster.createGroup(contactGroup.getGroupName());
try {
groupFound.addEntry(entry);
if (!groupAvailableContacts) {
SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
}
} catch (XMPPException | SmackException e) {
Log.error(e);
}
}
return true;
}
@Override
public void finished() {
if ((Boolean) get()) {
// Now try and remove the group from the old one.
if (move) {
removeContactItem(oldGroup, item);
if (!localPreferences.isEmptyGroupsShown() && !oldGroup.hasAvailableContacts()) {
SparkManager.getContactList().toggleGroupVisibility(oldGroup.getGroupName(), false);
}
}
}
}
};
worker.start();
}
use of org.jivesoftware.spark.ui.ContactGroup in project Spark by igniterealtime.
the class UserManager method searchContacts.
public void searchContacts(String contact, final JFrame parent) {
if (parents.get(parent) == null && parent.getGlassPane() != null) {
parents.put(parent, parent.getGlassPane());
}
// Make sure we are using the default glass pane
final Component glassPane = parents.get(parent);
parent.setGlassPane(glassPane);
final Map<String, ContactItem> contactMap = new HashMap<>();
final List<ContactItem> contacts = new ArrayList<>();
final ContactList contactList = SparkManager.getWorkspace().getContactList();
for (ContactGroup contactGroup : contactList.getContactGroups()) {
contactGroup.clearSelection();
for (ContactItem contactItem : contactGroup.getContactItems()) {
if (!contactMap.containsKey(contactItem.getJID())) {
contacts.add(contactItem);
contactMap.put(contactItem.getJID(), contactItem);
}
}
}
// Sort
Collections.sort(contacts, itemComparator);
final JContactItemField contactField = new JContactItemField(new ArrayList<>(contacts));
JPanel layoutPanel = new JPanel();
layoutPanel.setLayout(new GridBagLayout());
JLabel enterLabel = new JLabel(Res.getString("label.contact.to.find"));
enterLabel.setFont(new Font("dialog", Font.BOLD, 10));
layoutPanel.add(enterLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
layoutPanel.add(contactField, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 50, 0));
layoutPanel.setBorder(BorderFactory.createBevelBorder(0));
contactField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent keyEvent) {
if (keyEvent.getKeyChar() == KeyEvent.VK_ENTER) {
if (ModelUtil.hasLength(contactField.getText())) {
ContactItem item = contactMap.get(contactField.getText());
if (item == null) {
item = contactField.getSelectedContactItem();
}
if (item != null) {
parent.setGlassPane(glassPane);
parent.getGlassPane().setVisible(false);
contactField.dispose();
SparkManager.getChatManager().activateChat(item.getJID(), item.getDisplayName());
}
}
} else if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) {
parent.setGlassPane(glassPane);
parent.getGlassPane().setVisible(false);
contactField.dispose();
}
}
});
contactField.getList().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
contactField.setSelectetIndex(e);
ContactItem item = contactField.getSelectedContactItem();
MouseEvent exx = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiers(), e.getX() + 20, e.getY(), e.getClickCount(), false);
SparkManager.getContactList().setSelectedUser(item.getJID());
SparkManager.getContactList().showPopup(contactField.getPopup(), exx, item);
}
if (e.getClickCount() == 2) {
if (ModelUtil.hasLength(contactField.getText())) {
ContactItem item = contactMap.get(contactField.getText());
if (item == null) {
item = contactField.getSelectedContactItem();
}
if (item != null) {
parent.setGlassPane(glassPane);
parent.getGlassPane().setVisible(false);
contactField.dispose();
SparkManager.getChatManager().activateChat(item.getJID(), item.getDisplayName());
}
}
}
}
});
final JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setLayout(new GridBagLayout());
mainPanel.add(layoutPanel, new GridBagConstraints(0, 0, 1, 1, 0.5, 0.5, GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(SparkManager.getMainWindow().getTopToolBar().getHeight() + SparkManager.getWorkspace().getStatusBar().getHeight() + 47, 1, 5, 1), 200, 0));
mainPanel.setOpaque(false);
contactField.setText(contact);
parent.setGlassPane(mainPanel);
parent.getGlassPane().setVisible(true);
contactField.focus();
mainPanel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
parent.setGlassPane(glassPane);
parent.getGlassPane().setVisible(false);
contactField.dispose();
}
});
parent.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
parent.setGlassPane(glassPane);
parent.getGlassPane().setVisible(false);
contactField.dispose();
parent.removeWindowListener(this);
}
public void windowDeactivated(final WindowEvent windowEvent) {
TimerTask task = new SwingTimerTask() {
public void doRun() {
if (contactField.canClose()) {
windowClosing(windowEvent);
}
}
};
TaskEngine.getInstance().schedule(task, 250);
}
});
}
use of org.jivesoftware.spark.ui.ContactGroup in project Spark by igniterealtime.
the class ContactListAssistantPlugin method initialize.
@Override
public void initialize() {
moveToMenu = new JMenu(Res.getString("menuitem.move.to"));
copyToMenu = new JMenu(Res.getString("menuitem.copy.to"));
localPreferences = new LocalPreferences();
final ContactList contactList = SparkManager.getContactList();
contactList.addContextMenuListener(new ContextMenuListener() {
@Override
public void poppingUp(Object object, final JPopupMenu popup) {
final Collection<ContactItem> contactItems = Collections.unmodifiableCollection(contactList.getSelectedUsers());
if (!contactItems.isEmpty()) {
final List<ContactGroup> contactGroups = contactList.getContactGroups();
Collections.sort(contactGroups, ContactList.GROUP_COMPARATOR);
for (final ContactGroup group : contactGroups) {
if (group.isUnfiledGroup() || group.isOfflineGroup()) {
continue;
}
if (isContactItemInGroup(contactItems, group)) {
continue;
}
final Action moveAction = new AbstractAction() {
private static final long serialVersionUID = 6542011870221162331L;
@Override
public void actionPerformed(ActionEvent actionEvent) {
moveItems(contactItems, group.getGroupName());
}
};
final Action copyAction = new AbstractAction() {
private static final long serialVersionUID = 2232885525630977329L;
@Override
public void actionPerformed(ActionEvent actionEvent) {
copyItems(contactItems, group.getGroupName());
}
};
moveAction.putValue(Action.NAME, group.getGroupName());
moveToMenu.add(moveAction);
copyAction.putValue(Action.NAME, group.getGroupName());
copyToMenu.add(copyAction);
}
popup.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent) {
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent) {
moveToMenu.removeAll();
copyToMenu.removeAll();
popup.removePopupMenuListener(this);
}
@Override
public void popupMenuCanceled(PopupMenuEvent popupMenuEvent) {
moveToMenu.removeAll();
copyToMenu.removeAll();
popup.removePopupMenuListener(this);
}
});
int index = -1;
if (!Default.getBoolean("DISABLE_RENAMES") && Enterprise.containsFeature(Enterprise.RENAMES_FEATURE)) {
for (int i = 0; i < popup.getComponentCount(); i++) {
Object o = popup.getComponent(i);
if (o instanceof JMenuItem && ((JMenuItem) o).getText().equals(Res.getString("menuitem.rename"))) {
index = i;
break;
}
}
} else
index = 3;
if (contactItems.size() == 1) {
// Add MOVE/COPY options right after the RENAME option or in it's place if it doesn't exist.
if (index != -1) {
// See if we should disable the "Move to" and "Copy to" menu options
if (!Default.getBoolean("DISABLE_MOVE_AND_COPY") && Enterprise.containsFeature(Enterprise.MOVE_COPY_FEATURE)) {
popup.add(moveToMenu, index + 1);
popup.add(copyToMenu, index + 2);
}
}
} else if (contactItems.size() > 1) {
// See if we should disable the "Move to" and "Copy to" menu options
if (!Default.getBoolean("DISABLE_MOVE_AND_COPY") && Enterprise.containsFeature(Enterprise.MOVE_COPY_FEATURE)) {
popup.addSeparator();
popup.add(moveToMenu);
popup.add(copyToMenu);
}
// Clean up the extra separator if "Broadcast" menu items are disabled
if (!Default.getBoolean("DISABLE_BROADCAST_MENU_ITEM") && Enterprise.containsFeature(Enterprise.BROADCAST_FEATURE))
popup.addSeparator();
}
}
}
@Override
public void poppingDown(JPopupMenu popup) {
}
@Override
public boolean handleDefaultAction(MouseEvent e) {
return false;
}
});
updateAvatarsInContactList();
SettingsManager.addPreferenceListener(preference -> updateAvatarsInContactList());
}
use of org.jivesoftware.spark.ui.ContactGroup 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