aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how to create a search engine? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to create a search engine?" Watch "how to create a search engine?" New topic
Author

how to create a search engine?

Rakesh vijayakrishnan
Greenhorn

Joined: Feb 07, 2009
Posts: 3
sir,
i want to know how we can create a search engine using plugins in swing... on eclipse IDE... java..
and also how to use it... please help me by giving the codes..
thanks..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Welcome to JavaRanch
Please don't ask for lots of code like that (unless you are paying for it): note this FAQ.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
What do you mean by "plugins in Swing"? What difference does it make which IDE you're using? What do you have so far?
Suraj Chandran
Ranch Hand

Joined: May 20, 2008
Posts: 53
If you want to plugin your SearchEngine as a contribution into EClipse, then you can use the org.eclipse.help.ui.searchEngine extn point. Its pretty simple to contribute to this point. Also try to google out the following classes which are involved in the above extn point.
ISearchEngine
ISearchScopefactory
RootScopePage

If pluging search in to Eclispe is what you want then the above info should definitely get you some help


I guess there are couple of design issues in this classic software the GOD coded.
Rakesh vijayakrishnan
Greenhorn

Joined: Feb 07, 2009
Posts: 3
Ulf Dittmer wrote:What do you mean by "plugins in Swing"? What difference does it make which IDE you're using? What do you have so far?

package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.MessageFormat;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoManager;

import com.sun.java_cup.internal.runtime.Scanner;
import com.sun.xml.internal.fastinfoset.sax.Properties;



public class ListSelectionDemo extends JPanel
{
JTextArea output;
String temp;
String newline = "\n";
private static final long serialVersionUID = 1L;
static JFrame frame;
JList list;
ListSelectionModel listSelectionModel;
JButton b;
JLabel l;
JTextField f;
File file;
File[] listData;
JSplitPane splitPane;
JSplitPane splitPanel;
JTree tree;
String ss;
static UndoManager undoManager = new UndoManager();

static JTextField jl;
static JButton savef;
static JFrame fr;
Font font;
final static JMenuItem undo=new JMenuItem("Undo");
private static final int PAGE_EXISTS = 0;
private static final int NO_SUCH_PAGE = 0;
final JMenuItem open=new JMenuItem("Open");
final JMenuItem save=new JMenuItem("save");
final JMenuItem exit=new JMenuItem("Exit");
final JMenuItem New=new JMenuItem("New");
final JMenuItem saveas=new JMenuItem("SaveAs");

final JMenuItem copy=new JMenuItem("Copy");
final JMenuItem paste=new JMenuItem("Paste");
final JMenuItem cut=new JMenuItem("Cut");
final JMenuItem print=new JMenuItem("Print");
public ListSelectionDemo()
{
super(new BorderLayout());

JMenu file1 = new JMenu("File");

open.addActionListener(new ItemListener());
saveas.addActionListener(new ItemListener());
New.addActionListener(new ItemListener());
save.addActionListener(new ItemListener());
exit.addActionListener(new ItemListener());
print.addActionListener(new ItemListener());
file1.setMnemonic(KeyEvent.VK_F);
file1.add(New);
file1.add(open);
file1.addSeparator( );
file1.add(save);
file1.add(saveas);
JMenu sentTo=new JMenu("Sent To");
sentTo.add(new JMenuItem("Desktop"));
file1.add(sentTo);
file1.add(print);
file1.add(exit);


JMenu edit= new JMenu("Edit");
edit.setMnemonic(KeyEvent.VK_E);
edit.add(undo);
undo.addActionListener(new ItemListener());
edit.addSeparator();
edit.add(cut);
cut.addActionListener(new ItemListener());
edit.add(copy);
copy.addActionListener(new ItemListener());
edit.add(paste);
paste.addActionListener(new ItemListener());

JMenuBar menuBar = new JMenuBar( );
menuBar.add(file1);
menuBar.add(edit);
frame.setJMenuBar(menuBar);
final JPanel controlPane = new JPanel();
l=new JLabel("SEARCH");
f=new JTextField(7);
b=new JButton("GO");
controlPane.add(l);
controlPane.add(f);
controlPane.add(b);

output = new JTextArea(1, 10);
output.setEditable(true);
JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
output.getDocument().addUndoableEditListener(
new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
undoManager.addEdit(e.getEdit());
updateButtons();
}
});
font = new Font("Verdana", Font.BOLD, 12);
output.setFont(font);
output.setBackground(Color.LIGHT_GRAY);
output.setForeground(Color.BLACK);

//Bottom Panel
JScrollPane listPane = new JScrollPane();
JPanel listContainer = new JPanel(new GridLayout(1,1));
listContainer.setBorder(BorderFactory.createTitledBorder("List"));

listContainer.add(listPane);

//Left Panel

JScrollPane leftPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
java.util.Properties p = System.getProperties();
tree = new JTree(p);
tree.setRootVisible(true);
leftPane = new JScrollPane(tree);

//Top panel
final JPanel bottomHalf = new JPanel(new BorderLayout());
bottomHalf.add(controlPane, BorderLayout.PAGE_START);
bottomHalf.add(outputPane, BorderLayout.CENTER);
bottomHalf.setPreferredSize(new Dimension(1020, 600));
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(bottomHalf);
splitPane.setBottomComponent(listContainer);
splitPane.setOneTouchExpandable(true);
splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPanel.setLeftComponent(leftPane);
splitPanel.setRightComponent(splitPane);
add(splitPanel, BorderLayout.WEST);
splitPanel.setOneTouchExpandable(true);


b.setActionCommand("search");

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String s=b.getActionCommand();
if(s.equals("search"))
{
String tf=f.getText();
System.out.println(tf);
if(tf.equals(""))
{
output.setText("");
output.setEditable(false);

JPanel listContainer = new JPanel(new GridLayout(1,1));
listContainer.setBorder(BorderFactory.createTitledBorder("RESULT"));
listContainer.add(new JLabel("PATH NOT FOUND"));

splitPane.setBottomComponent(listContainer);
}
else
{
file = new File(tf);
listData = file.listFiles();
list = new JList(listData);

listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
JScrollPane listPane1 = new JScrollPane(list);

JPanel listContainer1 = new JPanel(new GridLayout(1,1));
listContainer1.setBackground(Color.LIGHT_GRAY);
listContainer1.setBorder(BorderFactory.createTitledBorder("List"));
listContainer1.add(listPane1);
output.setEditable(true);
splitPane.setBottomComponent(listContainer1);

}

}
}
}
);

String[] modes = { "Option-1",
"Option-2",
"Option-3" };

final JComboBox comboBox = new JComboBox(modes);
comboBox.setSelectedIndex(2);
comboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String newMode = (String)comboBox.getSelectedItem();
if (newMode.equals("Option-1"))
{

}
else if (newMode.equals("Option-2"))
{

}
else
{

}
output.append("----------"+ "Mode: " + newMode + "----------" + newline);
}
}
);

controlPane.add(new JLabel("Selection mode:"));
controlPane.add(comboBox);
}


private static void createAndShowGUI()
{
frame =new JFrame("ListSelectionDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ListSelectionDemo demo = new ListSelectionDemo();
demo.setOpaque(true);
frame.setContentPane(demo);
frame.pack();
frame.setVisible(true);
}


public static void updateButtons() {
undo.setText(undoManager.getUndoPresentationName());
// redoButton.setText(undoManager.getRedoPresentationName());
undo.setEnabled(undoManager.canUndo());
// redoButton.setEnabled(undoManager.canRedo());
}

public static void func()
{
fr=new JFrame("SAVE FILE");
JPanel p=new JPanel();
JPanel save=new JPanel();

p.add(new JLabel("Enter Path:"));
jl=new JTextField(9);
p.add(jl);
savef=new JButton("Save File");
save.add(savef);
fr.add(p,BorderLayout.CENTER);
fr.add(save,BorderLayout.SOUTH);
fr.setBounds(500, 250,200,150);
//fr.pack();
fr.setVisible(true);
}


public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
}
);
}


public class ItemListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JMenuItem s = (JMenuItem) e.getSource();

if(s.equals(open))
{
JFileChooser fc= new JFileChooser();
if(fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
File file= fc.getSelectedFile();
ss=file.toString();
BufferedReader inputStream = null;
try
{

output.setEditable(true);
output.setText("");
inputStream =new BufferedReader(new FileReader(file));

String l;
while ((l = inputStream.readLine()) != null)
{
output.append(l);
output.append(newline);
}

int pos=output.getCaretPosition();
System.out.println(pos);
}
catch(Exception e1)
{
System.out.println(e1);
}

}
}

else if(s.equals(save))
{

try
{
FileWriter first = new FileWriter(new File(ss));
first.write(output.getText());
first.close();

}
catch(Exception e1)
{
System.out.println(e1);
}
}

else if(s.equals(saveas))
{
func();

savef.setActionCommand("sfile");
savef.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{

String s=savef.getActionCommand();

if(s.equals("sfile"))
{
String field=jl.getText();

try
{

System.out.println(field);
File fil=new File(field);

if(!fil.exists())
{

FileWriter first = new FileWriter(new File(field));
first.write(output.getText());
first.close();
fr.dispose();
}
else
{

jl.selectAll();
jl.requestFocus();
JOptionPane.showMessageDialog(fr, "File Exist", "ALERT", 0);

}

}
catch(Exception e1)
{
System.out.println(e1);
}

}

}
}
);

}

else if(s.equals(New))
{

output.setText("");
}

else if(s.equals(exit))
{

System.exit(0);
}

else if(s.equals(undo))
{
try {
undoManager.undo();
} catch (CannotRedoException cre) {
cre.printStackTrace();
}
updateButtons();
}

else if(s.equals(cut))
{
temp= output.getSelectedText();
output.replaceSelection("");
}
else if(s.equals(copy))
{
temp= output.getSelectedText();
}
else if(s.equals(paste))
{
output.append(temp);
}

else if(s.equals(print))
{
try{
output.print();
} catch(Exception excp){}

}
}

}

public int print(Graphics g, PageFormat pf, int page) throws PrinterException{
if (page > 0)
{
return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
frame.printAll(g);
return PAGE_EXISTS;
}



class SharedListSelectionHandler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent e)
{
ss=list.getSelectedValue().toString();

BufferedReader inputStream = null;
try
{

output.setText("");
inputStream =new BufferedReader(new FileReader(ss));

String l;
while ((l = inputStream.readLine()) != null)
{
output.append(l);
output.append(newline);
}

}
catch(Exception e1)
{
System.out.println(e1);
}
}
}
}



i have done the codings to search when we give d path of that file... but i have to change it in such a way that it should search even if we give d file name or any content of dat file.. Please help me out..
thanks...

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
Please edit your post to UseCodeTags. This is way too much code for anyone to read in such an unstructured way.
Rakesh vijayakrishnan
Greenhorn

Joined: Feb 07, 2009
Posts: 3
Ulf Dittmer wrote:Please edit your post to UseCodeTags. This is way too much code for anyone to read in such an unstructured way.


This is the code to search, that i have used.. i have to use pattern matching technique to search the files by giving only the file name or to get the file which is having the contents when we give the content to search.
Please help me.. Thanks...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  69
That's just GUI code - how does it relate to search? It seems to be doing something with files - is that what you intend to search? If so, what formats do these files have - TXT, RTF, DOC, PDF, XLS, XML, ...?

Except for TXT, none of these can be tackled purely with string search, because they're either binary, or include text that needs to be excluded from the search.

Either way, I'd use Lucene to create an index, and use that for the search.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create a search engine?