📄 servicediscoverydialog.java
字号:
public void run() { tableModel.addItem(entry); } }); } for (int icount = 0; icount < discoItems.size(); icount++) { String[] entry = (String[]) discoItems.get(icount); final String id = entry[1]; status.setText(resources.getString("status") + ": " + resources.getString("gettingFeatures") + " (" + id + ") ..."); // get the discover info about each item DiscoverInfo info = null; try { info = manager.discoverInfo(id); } catch (XMPPException e) { } // if the service discovery has been aborted, bail out if (stopped) { return; } tableModel.setDisco(icount, info); if (info != null) { final DiscoverInfo tempInfo = info; final int index = icount; SwingUtilities.invokeLater( new Runnable() { public void run() { Iterator identities = tempInfo.getIdentities(); while (identities.hasNext()) { DiscoverInfo.Identity identity = (DiscoverInfo.Identity) identities.next(); if (stopped) { return; } // set the table information tableModel.setItemInfo(index, identity.getName(), identity.getCategory(), identity.getType()); if (identity.getCategory() .equals("gateway")) { cache.setProperty(id, identity.getCategory() + " " + identity.getType()); } } } }); } } } catch (XMPPException e) { String message = e.getMessage(); if (e.getXMPPError() != null) { message = resources.getString("xmppError" + e.getXMPPError().getCode()); } status.setText(resources.getString("error") + ": " + message); return; } writeServiceDiscoveryCache(); SwingUtilities.invokeLater( new Runnable() { public void run() { status.setText(resources.getString("status") + ": " + resources.getString("completed") + "."); } }); } /** * Aborts the service discovery */ public void abortDiscovery() { this.stopped = true; } }}/** * The popup menu for each of the disco items * *@author Adam Olsen *@created May 25, 2005 *@version 1.0 */class TablePopupMenu extends JPopupMenu { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private JTable table; private ServiceTableModel model; private ServiceDiscoveryDialog dialog; private JMenuItem browseItem = new JMenuItem(resources.getString("browse")); private JMenuItem registerItem = new JMenuItem(resources.getString("register")); private JMenuItem joinItem = new JMenuItem(resources.getString("join")); private JMenuItem addItem = new JMenuItem(resources.getString("addToRoster")); private JMenuItem searchItem = new JMenuItem(resources.getString("search")); /** * Default constructor * *@param dialog The ServiceDiscoveryDialog to connect this popup menu to *@param table the table to connect this menu to */ public TablePopupMenu(ServiceDiscoveryDialog dialog, JTable table) { this.dialog = dialog; this.table = table; model = (ServiceTableModel) table.getModel(); final TablePopupMenu thisPointer = this; add(addItem); add(browseItem); add(registerItem); add(joinItem); add(searchItem); // show the add buddy dialog addItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { AddBuddyDialog dialog = new AddBuddyDialog(); String id = getId(); dialog.setBuddyId(id); dialog.setVisible(true); } }); // the browse item runs service discovery on that item browseItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String id = getId(); thisPointer.dialog.runServiceDiscovery(id); thisPointer.dialog.addHistoryItem(id); } }); joinItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String id = getId(); GroupChatBookmarks.showDialog(id, BuddyList.getInstance() .getConnection().getUser(), ""); } }); // the register item forms a RegistrationForm for that item registerItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String id = getId(); new RegistrationForm(BuddyList.getInstance().getContainerFrame(), id).getRegistrationInfo(); } }); // search dialog searchItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String id = getId(); SearchDialog searchDialog = new SearchDialog(id); searchDialog.setSize(700, 500); } }); } /** * Returns the ID of the selected row * *@return the id of the row */ private String getId() { int row = this.table.getSelectedRow(); if (row < 0) { return ""; } String id = (String) model.getValueAt(row, 1); return id; } /** * Shows the popup menu * *@param e the mouse event */ public void popup(MouseEvent e) { int selectedRow = table.rowAtPoint(e.getPoint()); if (selectedRow < 0) { return; } table.setRowSelectionInterval(selectedRow, selectedRow); String features = model.getFeatures(selectedRow); if (features.indexOf("r") > -1) { registerItem.setEnabled(true); } else { registerItem.setEnabled(false); } if (features.indexOf("b") > -1) { browseItem.setEnabled(true); } else { browseItem.setEnabled(false); } if (features.indexOf("j") > -1) { joinItem.setEnabled(true); } else { joinItem.setEnabled(false); } if (features.indexOf("s") > -1) { searchItem.setEnabled(true); } else { searchItem.setEnabled(false); } validate(); show(table, e.getX(), e.getY()); }}/** * The table model for the ServiceDiscoveryDialog table * *@author Adam Olsen *@created May 25, 2005 *@version 1.0 */class ServiceTableModel extends AbstractTableModel { private ServiceDiscoveryDialog dialog; private JTable table; private ArrayList items = new ArrayList(); private String[] columns = new String[]{"Name", "JID", "Category", "Type"}; private ArrayList infos = new ArrayList(); /** * Sets the table value for this model * *@param table the table that this model represents */ public void setTable(JTable table) { this.table = table; TableColumn column = null; // set the default column widths for (int i = 0; i < getColumnCount(); i++) { column = table.getColumnModel().getColumn(i); if (i < 2) { column.setPreferredWidth(150); } // sport column is bigger else { column.setPreferredWidth(50); } } } /** * Default Constructor * *@param dialog the ServiceDiscoveryDialog that contains this table */ public ServiceTableModel(ServiceDiscoveryDialog dialog) { this.dialog = dialog; } /** * gets the number of columns in this table * *@return the number of columns */ public int getColumnCount() { return columns.length; } /** * Gets the number of rows in the table * *@return the number of rows in the table */ public int getRowCount() { return items.size(); } /** * Returns the name of a specific column * *@param column the column who's name is wanted *@return the name of the column */ public String getColumnName(int column) { return columns[column]; } /** * Get the Object for a specific coordinate in the table * *@param row the row of the item *@param column the column of the item *@return the Object at the specified coordinates */ public Object getValueAt(int row, int column) { synchronized (items) { String[] item = (String[]) items.get(row); return item[column]; } } /** * gets the features of a specific item (row) * *@param row the index of the row you want information for *@return a string containing all of the features the row supports */ public String getFeatures(int row) { synchronized (infos) { String info = ""; try { info = (String) infos.get(row); } catch (Exception e) { } return info; } } /** * Sets the disco information once it's found * *@param index the index of the row you want to set the information about *@param disco the information about the row */ public void setDisco(int index, DiscoverInfo disco) { synchronized (infos) { String info = ""; if (disco != null) { if (disco.containsFeature("jabber:iq:register")) { info += "r"; } if (disco.containsFeature("jabber:iq:browse") || disco.containsFeature("http://jabber.org/protocol/disco")) { info += "b"; } if (disco.containsFeature("muc_public")) { info += "j"; } if (disco.containsFeature("jabber:iq:search")) { info += "s"; } } infos.add(index, info); } } /** * Adds a row to the table * *@param item the array containing the item to add */ public void addItem(String[] item) { synchronized (items) { items.add(item); fireTableRowsInserted(items.size(), items.size()); } } /** * Sets information about a specific row * *@param row the row to set *@param name the new name *@param category the category of the row *@param type The new itemInfo value */ public void setItemInfo(int row, String name, String category, String type) { synchronized (items) { String[] item = (String[]) items.get(row); item[0] = name; item[2] = category; item[3] = type; fireTableRowsUpdated(row, row); } } /** * Clears the table */ public void clear() { items.clear(); infos.clear(); table.tableChanged(new TableModelEvent(this)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -