Package search

Source Code of search.SearchRequest

package search;


import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.Collection;
import java.util.LinkedList;

import common.ClientServerProtocol;
import common.connection.ConnectionWriterInterface;
import common.files.MultiNamedFile;

import connection.server.ServerConnection;

import ui.tabs.search.SearchSubTab;

import static main.ClientMain.*;


public class SearchRequest
{
  private LinkedList<String> keywords;

  private SearchSubTab resultsTab;

  public SearchRequest(String searchText, SearchSubTab resultsTab)
  {
    keywords = new LinkedList<String>();

    StreamTokenizer tokenizer = createTokenizer(searchText);
    parseKeywords(tokenizer);

    if (keywords.size() == 0)
    {
      throw new IllegalArgumentException();
    }

    this.resultsTab = resultsTab;
  }

  private static StreamTokenizer createTokenizer(String input)
  {
    StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(input));

    tokenizer.eolIsSignificant(false);
    tokenizer.lowerCaseMode(false);
    tokenizer.quoteChar('\"');
    tokenizer.slashSlashComments(false);
    tokenizer.slashStarComments(false);

    return tokenizer;
  }

  private void parseKeywords(StreamTokenizer tokenizer)
  {
    while (thereIsAToken(tokenizer))
    {
      if ((tokenizer.ttype == '\"') || (tokenizer.ttype == StreamTokenizer.TT_WORD))
      {
        keywords.add(tokenizer.sval);
      }
    }
  }

  private static boolean thereIsAToken(StreamTokenizer tokenizer)
  {
    try
    {
      return tokenizer.nextToken() != StreamTokenizer.TT_EOF;
    }
    catch (IOException ignored)
    {
      return false;
    }
  }

  public int getId()
  {
    return hashCode();
  }

  public void send()
  {
    ServerConnection serverConnection = getConnectionManager().getConnection();

    int protocolId = ClientServerProtocol.PROTOCOL_FILES;
    int opcode = ClientServerProtocol.Files.OP_KEYWORDS;
    serverConnection.send(protocolId, opcode, new ConnectionWriterInterface()
    {
      public void run(ObjectOutputStream os) throws IOException
      {
        os.writeInt(getId());
        os.writeObject(keywords);
      }

    });
  }

  public void addResults(Collection<MultiNamedFile> results)
  {
    resultsTab.addResults(results);
  }

  public void removeTab()
  {
    getGuiManager().getSearchTab().removeSubTab(resultsTab);
  }

}
TOP

Related Classes of search.SearchRequest

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.