📄 searchdialog.java
字号:
stopSearchButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { } }); addContactButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { AddBuddyDialog addBuddyDialog = new AddBuddyDialog(); addBuddyDialog.setBuddyId( ((Search.Item) resultsTableModel.getItems().get(selectedRow)).getJid() ); addBuddyDialog.setVisible(true); } }); userInfoButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { new InformationViewerDialog( ((Search.Item) resultsTableModel.getItems().get(selectedRow)).getJid()); } }); } /** * Gets the search attribute of the SearchDialog object * * @return The search value */ public Search getSearch() { return search; } /** * Sets the search attribute of the SearchDialog object * * @param search The new search value */ public void setSearch(Search search) { this.search = search; } /** * return string with first letter in uppercase * * @param aStr string to capitalize * @return capitalized string */ private String capitalizeString(String aStr) { return aStr.substring(0, 1).toUpperCase() + aStr.substring(1); } /** * send the empty "jabber:iq:search" query request to the service in order to receive * the list of search field and instructions * also the results table can be initialized, as now we know the columns * * @param service to query * @return true if service provides Jabber search service */ private boolean querySearchService(String service) { boolean returnVal = false; Search searchQuery = new Search(); searchQuery.setTo(service); searchQuery.setType(IQ.Type.GET); sendPacket(searchQuery); PacketFilter filter = new AndFilter(new PacketIDFilter(searchQuery.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector packetCollector = BuddyList.getInstance().getConnection().createPacketCollector(filter); IQ reply = (IQ) packetCollector.nextResult(SmackConfiguration.getPacketReplyTimeout()); if(reply == null) { SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog(thisPointer, resources.getString("searchRequestTimeout"), resources.getString("searchError"), JOptionPane.OK_OPTION); } } ); returnVal = false; } else { if(reply.getType() == IQ.Type.ERROR) { SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog(thisPointer, resources.getString("serviceDoesNotSupportJaberSearch"), resources.getString("searchError"), JOptionPane.OK_OPTION ); } }); returnVal = false; } else if(reply instanceof Search) { search = (Search) reply; returnVal = true; } } packetCollector.cancel(); return returnVal; } /** * "Search" button handler * todo: maybe it would be wise to put his code in separate thread? */ private void doSearch() { Search srch = new Search(); srch.setTo(service); srch.setType(IQ.Type.SET); HashMap searchCriteria = new HashMap(); // get the input from all the fields, build Search message and send it Iterator iSearchFields = searchTextFields.keySet().iterator(); while(iSearchFields.hasNext()) { String searchFieldName = (String) iSearchFields.next(); String searchFieldValue = ((JTextField) searchTextFields.get(searchFieldName)).getText(); searchCriteria.put(searchFieldName, searchFieldValue); } srch.setSearchFields(searchCriteria); sendPacket(srch); PacketFilter filter = new AndFilter(new PacketIDFilter(srch.getPacketID()), new PacketTypeFilter(IQ.class)); PacketCollector packetCollector = BuddyList.getInstance().getConnection().createPacketCollector(filter); final IQ reply = (IQ) packetCollector.nextResult(SmackConfiguration.getPacketReplyTimeout()); if(reply == null) { SwingUtilities.invokeLater( new Runnable() { public void run() { wait.setVisible( false ); JOptionPane.showMessageDialog(thisPointer, resources.getString("searchRequestTimeout"), resources.getString("searchError"), JOptionPane.OK_OPTION); } }); } else { SwingUtilities.invokeLater(new Runnable() { public void run() { wait.setVisible( false ); // update results table with new data resultsTableModel.setItems(((Search) reply).getItems()); resultsTableModel.fireTableStructureChanged(); resultsTableModel.fireTableDataChanged(); } }); } packetCollector.cancel(); } /** * Description of the Method * * @param packet Description of the Parameter */ private void sendPacket(Packet packet) { if(BuddyList.getInstance().getConnection().isConnected()) { BuddyList.getInstance().getConnection().sendPacket(packet); } } /** * table model for the results of the search * the number of columns will be know *after* querying the JUD. This * * @author synic * @created April 14, 2005 */ class SearchResultsTableModel extends AbstractTableModel { // list of Search.Items private ArrayList items = new ArrayList(); /** * Sets the items attribute of the SearchResultsTableModel object * * @param aItems The new items value */ private void setItems(ArrayList aItems) { items = aItems; } /** * Gets the items attribute of the SearchResultsTableModel object * * @return The items value */ public ArrayList getItems() { return items; } /** * Gets the rowCount attribute of the SearchResultsTableModel object * * @return The rowCount value */ public int getRowCount() { if(items == null) { return 0; } return items.size(); } /** * Gets the columnCount attribute of the SearchResultsTableModel object * * @return The columnCount value */ public int getColumnCount() { if(items == null || items.size() == 0) { return 0; } else { return ((Search.Item) items.get(0)).getAttributes().size() + 1; } } /** * Gets the valueAt attribute of the SearchResultsTableModel object * * @param rowIndex Description of the Parameter * @param columnIndex Description of the Parameter * @return The valueAt value */ public Object getValueAt(int rowIndex, int columnIndex) { String out = ""; Search.Item item = (Search.Item) items.get(rowIndex); if(columnIndex == item.getAttributes().size()) { out = item.getJid(); } else { Object[] keys = item.getAttributes().keySet().toArray(); out = (String) item.getAttributes().get(keys[columnIndex]); } return out; } /** * Gets the columnName attribute of the SearchResultsTableModel object * * @param col Description of the Parameter * @return The columnName value */ public String getColumnName(int col) { if(col == search.getSearchFields().keySet().size()) { return "JID"; } else { Object[] searchFields = search.getSearchFields().keySet().toArray(); return capitalizeString((String) searchFields[col]); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -