use of org.jivesoftware.spark.component.JContactItemField 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);
}
});
}
Aggregations