Package ui.tabs

Source Code of ui.tabs.SearchTab$ConnectionListener

package ui.tabs;


import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import search.SearchRequest;
import ui.misc.MessageBox;
import ui.misc.feeder.FeederField;
import ui.misc.feeder.FeederFieldListener;
import ui.tabs.search.SearchSubTab;

import connection.server.ConnectionStateAdapter;
import connection.server.ServerConnection;

import static main.ClientMain.getSearchManager;


public class SearchTab extends AbstractTab implements FeederFieldListener
{
  private static final long serialVersionUID = -7679185291914353898L;

  private FeederField searchField;
  private JTabbedPane subPane;

  public SearchTab()
  {
    super();

    BorderLayout layout = new BorderLayout();
    layout.setHgap(4);
    layout.setVgap(4);
    setLayout(layout);

    add(new SearchBar(), BorderLayout.NORTH);

    SearchResults searchResults = new SearchResults();
    add(searchResults, BorderLayout.CENTER);

    new ConnectionListener();
  }

  private class ConnectionListener extends ConnectionStateAdapter
  {
    public void onConnected(ServerConnection newConnection)
    {
      super.onConnected(newConnection);
      searchField.setEnabled(true);
    }

    public void onDisconnected()
    {
      super.onDisconnected();
      searchField.setEnabled(false);

      getSearchManager().clearRequests();
    }

  }

  private class SearchResults extends JPanel
  {
    private static final long serialVersionUID = -5888825315145026203L;

    public SearchResults()
    {
      setLayout(new BorderLayout());

      subPane = new JTabbedPane();
      add(subPane, BorderLayout.CENTER);
    }

  }

  private class SearchBar extends JPanel
  {
    private static final long serialVersionUID = 6329727688382675354L;

    public SearchBar()
    {
      JLabel searchLabel = new JLabel("Search: ");
      searchLabel.setLabelFor(searchField);

      JButton feedButton = new JButton("Search");
      searchField = new FeederField(feedButton, SearchTab.this);
      searchField.setEnabled(false);

      BorderLayout layout = new BorderLayout();
      setLayout(layout);
      layout.setHgap(4);
      layout.setVgap(4);

      add(searchLabel, BorderLayout.WEST);
      add(searchField, BorderLayout.CENTER);
      add(feedButton, BorderLayout.EAST);
    }

  }

  public void feed(String text)
  {
    SearchSubTab subTab = new SearchSubTab(text);

    SearchRequest request;
    try
    {
      request = new SearchRequest(text, subTab);
    }
    catch (IllegalArgumentException e)
    {
      return;
    }
    subTab.setRequest(request);

    try
    {
      getSearchManager().addRequest(request);
    }
    catch (IllegalStateException e)
    {
      MessageBox.showInformation("You are not connected to a server.",
          "Not connected");
      return;
    }

    subTab.addToPane(subPane);
    subPane.setSelectedComponent(subTab);
  }

  public void removeSubTab(SearchSubTab subTab)
  {
    subPane.remove(subTab);
  }

}
TOP

Related Classes of ui.tabs.SearchTab$ConnectionListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.