📄 servicediscoverydialog.java
字号:
/* * Copyright (C) 2003 Adam Olsen * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 1, or (at your option) * any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.Locale;import java.util.Properties;import java.util.ResourceBundle;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.SwingUtilities;import javax.swing.event.TableModelEvent;import javax.swing.table.AbstractTableModel;import javax.swing.table.TableColumn;import org.jivesoftware.smack.XMPPException;import org.jivesoftware.smackx.ServiceDiscoveryManager;import org.jivesoftware.smackx.packet.DiscoverInfo;import org.jivesoftware.smackx.packet.DiscoverItems;import com.valhalla.gui.*;import com.valhalla.jbother.groupchat.GroupChatBookmarks;/** * For browsing Jabber services Displays a dialog for browsing services on * Jabber entities such as servers according to JEP-0030 * *@author Adam olsen *@created May 25, 2005 *@version 1.0 */public class ServiceDiscoveryDialog extends JDialog { private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault()); private String host; private JButton closeButton = new JButton(resources.getString("closeButton")); private JButton searchButton = new JButton(resources.getString("search")); private ServiceTableModel tableModel = new ServiceTableModel(this); private JTable table = new JTable(tableModel); private JLabel title = new JLabel(resources.getString("serviceDiscoveryManager")); private MJTextField serverField = new MJTextField(); private JLabel status = new JLabel(resources.getString("status") + ": "); private JPanel bottomPanel = new JPanel(); private JPanel middlePanel = new JPanel(new BorderLayout(5, 5)); private JPanel topPanel = new JPanel(); private JLabel hostLabel = new JLabel(resources.getString("host") + ": "); private ServiceDiscoveryThread currentThread = null; private ServiceDiscoveryDialog thisPointer = this; private TablePopupMenu popupMenu = new TablePopupMenu(this, table); private Vector history = new Vector(); private int current = -1; private JButton forward = new JButton(Standard.getIcon("images/buttons/Forward24.gif")); private JButton back = new JButton(Standard.getIcon("images/buttons/Back24.gif")); private Properties cache = JBotherLoader.getDiscoveryCache(); private boolean writing = false; /** * Creates a ServiceDiscoveryDialog with parent as it's parent * *@param parent the JFrame that owns this dialog */ public ServiceDiscoveryDialog(JFrame parent) { super(parent); setTitle(resources.getString("serviceDiscovery")); initComponents(); } /** * Sets up the Dialog layout */ private void initComponents() { tableModel.setTable(table); JPanel panel = (JPanel) getContentPane(); panel.setLayout(new BorderLayout(5, 5)); panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); JScrollPane scrollPane = new JScrollPane(table); scrollPane.getViewport().setBackground(Color.WHITE); topPanel.setLayout(new BorderLayout(5, 5)); topPanel.add(hostLabel, BorderLayout.WEST); topPanel.add(serverField, BorderLayout.CENTER); JPanel navPanel = new JPanel(); navPanel.setLayout(new BoxLayout(navPanel, BoxLayout.X_AXIS)); forward.setPreferredSize(new Dimension(26, 26)); back.setPreferredSize(new Dimension(26, 26)); navPanel.add(back); navPanel.add(forward); topPanel.add(navPanel, BorderLayout.EAST); middlePanel.add(topPanel, BorderLayout.NORTH); middlePanel.add(scrollPane, BorderLayout.CENTER); panel.add(title, BorderLayout.NORTH); table.setBorder(BorderFactory.createEtchedBorder()); panel.add(middlePanel, BorderLayout.CENTER); bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS)); bottomPanel.add(status); bottomPanel.add(Box.createHorizontalGlue()); bottomPanel.add(searchButton); bottomPanel.add(closeButton); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); panel.add(bottomPanel, BorderLayout.SOUTH); pack(); setSize(new Dimension(600, 300)); if (BuddyList.getInstance().checkConnection()) { serverField.setText(BuddyList.getInstance().getConnection() .getHost()); } ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { runServiceDiscovery(null); addHistoryItem(serverField.getText()); } }; addHistoryItem(serverField.getText()); serverField.addActionListener(listener); searchButton.addActionListener(listener); Standard.cascadePlacement(this); closeButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { writeServiceDiscoveryCache(); dispose(); } }); table.addMouseListener(new PopupMouseListener()); forward.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { forwardHandler(); } }); back.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { backHandler(); } }); back.setEnabled(false); forward.setEnabled(false); runServiceDiscovery(null); } /** * Description of the Method */ private void writeServiceDiscoveryCache() { if (writing) { return; } writing = true; File file = new File(JBother.settingsDir + File.separatorChar + "discocache.properties"); try { FileOutputStream stream = new FileOutputStream(file); cache.store(stream, ""); stream.close(); } catch (Exception ex) { } writing = false; } /** * Description of the Method */ private void backHandler() { current--; String id = (String) history.get(current); if (id == null) { current++; return; } forward.setEnabled(true); if (current == 0) { back.setEnabled(false); } runServiceDiscovery(id); } /** * Description of the Method */ private void forwardHandler() { current++; String id = (String) history.get(current); if (id == null) { current--; return; } back.setEnabled(true); if (current == history.size() - 1) { forward.setEnabled(false); } runServiceDiscovery(id); } /** * Adds a feature to the HistoryItem attribute of the * ServiceDiscoveryDialog object * *@param id The feature to be added to the HistoryItem attribute */ protected void addHistoryItem(String id) { for (int i = 0; i > current + 1; i--) { history.remove(i); } history.add(id); current++; forward.setEnabled(false); back.setEnabled(true); } /** * Listens for mouse events * *@author Adam Olsen *@created May 25, 2005 *@version 1.0 */ class PopupMouseListener extends MouseAdapter { /** * Description of the Method * *@param e Description of the Parameter */ public void mousePressed(MouseEvent e) { checkPop(e); } /** * Description of the Method * *@param e Description of the Parameter */ public void mouseReleased(MouseEvent e) { checkPop(e); } /** * Description of the Method * *@param e Description of the Parameter */ public void mouseClicked(MouseEvent e) { if (e.getClickCount() >= 2) { int row = table.getSelectedRow(); if (row < 0) { return; } String id = (String) tableModel.getValueAt(row, 1); runServiceDiscovery(id); addHistoryItem(id); } else { checkPop(e); } } /** * Description of the Method * *@param e Description of the Parameter */ public void checkPop(MouseEvent e) { if (e.isPopupTrigger()) { popupMenu.popup(e); } } } /** * Starts the service discovery thread on the specified server * *@param server the server to run discovery on */ protected void runServiceDiscovery(String server) { if (!BuddyList.getInstance().checkConnection()) { BuddyList.getInstance().connectionError(); return; } if (server != null) { serverField.setText(server); } if (serverField.getText().equals("")) { Toolkit.getDefaultToolkit().beep(); return; } status.setText(resources.getString("status") + ": " + resources.getString("collecting") + " ..."); tableModel.clear(); if (currentThread != null) { currentThread.abortDiscovery(); } currentThread = new ServiceDiscoveryThread(); new Thread(currentThread).start(); } /** * The thread that actually collects disco information about the server * *@author Adam Olsen *@created May 25, 2005 *@version 1.0 */ class ServiceDiscoveryThread implements Runnable { private boolean stopped = false; private ArrayList discoItems = new ArrayList(); /** * Main processing method for the ServiceDiscoveryThread object */ public void run() { if (!BuddyList.getInstance().checkConnection()) { BuddyList.getInstance().connectionError(); return; } ServiceDiscoveryManager manager = new ServiceDiscoveryManager( BuddyList.getInstance().getConnection()); // get the discover items for the server try { DiscoverItems items = manager.discoverItems(serverField.getText()); Iterator i = items.getItems(); String top[] = new String[]{serverField.getText(), serverField.getText(), "", "", ""}; discoItems.add(top); tableModel.addItem(top); while (i.hasNext()) { DiscoverItems.Item item = (DiscoverItems.Item) i.next(); if (stopped) { return; } final String[] entry = new String[]{item.getName(), item.getEntityID(), "", ""}; discoItems.add(entry); SwingUtilities.invokeLater( new Runnable() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -