plz help me for this difficult project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tamara omar
    New Member
    • Sep 2006
    • 23

    #31
    everything is more than great but im still not able to find by regular expressions,i will run this program on the university computer so if it worked it will be good but if not i will tell the instructer that i couldnt handle it
    so for now im very sorry if i was a big trouble for u and thaaaaaaaaanks alot for everything u have done and maybe i will need u soon in another project:)
    thanks again

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #32
      Originally posted by tamara omar
      everything is more than great but im still not able to find by regular expressions,i will run this program on the university computer so if it worked it will be good but if not i will tell the instructer that i couldnt handle it
      so for now im very sorry if i was a big trouble for u and thaaaaaaaaanks alot for everything u have done and maybe i will need u soon in another project:)
      thanks again
      Here is how you were supposed to do the find All and the replace.
      Personally I think the program needs to be improved before it is submitted (Starting with the interface).

      Code:
      import javax.swing.*;
      import javax.swing.text.*;
      import java.awt.event.*;
      import java.awt.Container;
      import java.io.*;
      import java.awt.*;
      import java.util.ArrayList;
      import java.util.regex.*;
      public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions
      	private Container pane;
      	JTextArea area;
      	JFileChooser fc;
      	JPanel southPanel;
      	JPanel northPanel;
      	JPanel fieldsPanel;
      	JPanel buttonsPanel;
      	JScrollPane scrollPane;
      	JTextField findText;
      	JTextField replaceText;
      	Matcher matcher;
      	Pattern pattern;
      	private String previous = "";
      	static private final String newline = "\n";
      	private int lastPosition = 0;
      	public Tamara2() {
      		pane = getContentPane(); //pane will now replace getContentPane() from now onwards
      		setTitle("Tamara Omar");
      		setSize(400, 400);
      //
      		fc = new JFileChooser();
      		area = new JTextArea(10,20);
      		area.setDocument(new DefaultStyledDocument());
      		scrollPane = new JScrollPane(area);
      
      		area.setMargin(new Insets(5,5,5,5));
      		area.setEditable(true);
      
      		//make buttons
      		JButton clear = makeButton("Clear");
      		JButton open = makeButton("Open");
      		JButton save = makeButton("Save");
      		JButton edit = makeButton("Find Next");
      		JButton findAll = makeButton("Find All");
      		JButton replaceAll = makeButton("Replace All");
      		JButton replace = makeButton("Replace");
      		findText = new JTextField(10);
      		replaceText = new JTextField(10);
      
      
      		southPanel = new JPanel();
      		northPanel = new JPanel();
      		fieldsPanel = new JPanel();
      		buttonsPanel = new JPanel();
      		southPanel.setLayout(new GridLayout(2, 1));
      		//add the buttons to the panel
      		buttonsPanel.add(findAll);
      		buttonsPanel.add(replaceAll);
      		buttonsPanel.add(edit);
      		buttonsPanel.add(replace);
      
      		//add the fields to the panel
      		fieldsPanel.add(new JLabel("Find:"));
      		fieldsPanel.add(findText);
      		fieldsPanel.add(new JLabel("Replace with:"));
      		fieldsPanel.add(replaceText);
      
      
      		//add the panel to the frame at the bottom
      		southPanel.add(fieldsPanel);
      		southPanel.add(buttonsPanel);
      
      		northPanel.add(open);
      		northPanel.add(save);
      		northPanel.add(clear);
      
      		pane.add(northPanel, "North");
      		pane.add(scrollPane, "Center");
      		pane.add(southPanel, "South");
      
      		setVisible(true);
      }
      public JButton makeButton(String label) { //creates a button
      	JButton jb = new JButton(label);
      	jb.setActionCommand(label); //This message will be set in the action for the button
      	jb.addActionListener(this); //this (JFrame) will respond to this button's press
      	return jb;
      }
      
      public void actionPerformed(ActionEvent action) {
      	String command = action.getActionCommand();
      	if(command.equals("Find Next")) {
      		int pos = 0;
      		try {
      			String s = findText.getText();
      			if(previous.equalsIgnoreCase(s)) {
      				pos = findNext(area, s);
      			}
      			else {
      				pos = find(area, s);
      			}
      			if(pos == -1) {
      				JOptionPane.showMessageDialog(this,s+" was not found");
      			}
      			else {
      				int z = pos + s.length();
      				area.getHighlighter().removeAllHighlights();
      				area.getHighlighter().addHighlight(pos, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
      			    area.requestFocus();
      			}
      
      		}
      		catch(Exception e) {
      			e.printStackTrace();
      		}
      	}
      	else if(command.equals("Replace")) {
      		if(!replace(area, findText.getText(), replaceText.getText()) ) {
      			JOptionPane.showMessageDialog(this, "Text to replace was not found");
      		}
      	}
      	else if(command.equals("Replace All")) {
      		String find = findText.getText();
      		String replace = replaceText.getText();
      		replaceAll(area, find, replace);
      		area.requestFocus();
      		//JOptionPane.showMessageDialog(this,"Replace All");
      	}
      	else if(command.equals("Find All"))	{
      		area.setSelectedTextColor(Color.blue);
      		area.getHighlighter().removeAllHighlights();
      		String searchString = findText.getText();
      		ArrayList matches = findAll(area, searchString);
      		if(matches.size() == 0) {
      			JOptionPane.showMessageDialog(this, searchString+" Was not found");
      		}
      		else {
      			try {
      				for(int i = 0; i < matches.size(); i++) {
      					int x = ((Integer)matches.get(i)).intValue();
      					//area.setCaretPosition(x);
      					//area.moveCaretPosition(x + searchString.length());
      					int z = x + searchString.length();
      					area.getHighlighter().addHighlight(x, z, new DefaultHighlighter.DefaultHighlightPainter(Color.blue));
      
      					area.requestFocus();
      				}
      				JOptionPane.showMessageDialog(this, "'"+searchString+"' was found " + matches.size() + " times");
      			}
      			catch(Exception e) {
      				e.printStackTrace();
      			}
      
      		}
      
      	}
      	else if(command.equals("Open")) {
      		int returnVal = fc.showOpenDialog(Tamara2.this);
      		if (returnVal == JFileChooser.APPROVE_OPTION) {
      			 File file = fc.getSelectedFile();
      			 BufferedReader inFile = null;
      			 try {
      				 inFile = new BufferedReader(new FileReader(file));
      				 String line = "";
      				 while ((line = inFile.readLine()) != null) {
      					area.append(line + newline);
      				 }
      				 inFile.close();
      		 	 }
      		 	 catch(IOException iOE) {
      			 			 	iOE.printStackTrace();
      			 }
      	 	 }
      	 	 else {
      			 area.append("Open command cancelled by user." + newline);
      		 }
      
      	}
      	else if(command.equals("Save")) {
      		try {
      			int returnVal = fc.showSaveDialog(Tamara2.this);
      			if(returnVal == JFileChooser.APPROVE_OPTION) {
      				File file = fc.getSelectedFile();
      				BufferedWriter out = new BufferedWriter(new FileWriter(file));
      				int rows = area.getLineCount();
      				for(int i = 0; i < rows; i++) {
      					int end = area.getLineEndOffset(i);
      					int start = area.getLineStartOffset(i);
      					int length = end - start;
      					if(length <= 0) {
      						length = 1;
      					}
      					String s = area.getText(start, (length-1));
      					out.write(s);
      					out.newLine();
      					out.flush();
      
      				}
      				out.close();
      
      
      			}
      			else {
      				area.append("Save command cancelled by user." + newline);
      			}
      		}
      		catch(Exception e) {
      			e.printStackTrace();
      		}
      		area.setCaretPosition(area.getDocument().getLength());
      	}
      	if(command.equals("Clear")) {
      		area.setText("");
      	}
      
      
      }
      
      //********To change to Match using regular expressions*(return vector instead of ArrayList?)*********************
      public ArrayList findAll(JTextArea area, String text) {
      	ArrayList startPositions = new ArrayList();
      	int pos = 0;
      	boolean found = false;
      	pattern = Pattern.compile(text);
      	matcher = pattern.matcher(area.getText());
      	while(pos != -1) {
      		found = matcher.find(lastPosition);
      	    if(!found) {
      			pos = -1;
      		}
      		else {
      			pos = matcher.start();
      			startPositions.add(new Integer(pos));
      			lastPosition = pos + text.length();
      		}
      	}
      	return startPositions;
      
      }
      //************Does a replace all based on regular expressions*******************
      public void replaceAll(JTextArea area, String find, String replace) {
      	Pattern p = Pattern.compile(find);
      	matcher = p.matcher(area.getText());
      	StringBuffer sb = new StringBuffer();
      	sb.append(matcher.replaceAll(replace));
      	area.setText(sb.toString());
      	matcher = null;
      }
      public int find(JTextArea area, String find) {
      	boolean found = false;
      	int pos = 0;
      	pattern = Pattern.compile(find);
      	previous = find;
      	matcher = pattern.matcher(area.getText());
      	found = matcher.find(0);
      	if(!found) {
      		pos = -1;
      		lastPosition = 0;
      	}
      	else {
      		pos = matcher.start();
      		lastPosition = pos + find.length();
      	}
      	return pos;
      }
      
      public int findNext(JTextArea area, String find) {
      	boolean found = false;
      	int pos = 0;
      	found = matcher.find(lastPosition);
      	if(!found) {
      		pos = -1;
      		lastPosition = 0;
      	}
      	else {
      		pos = matcher.start();
      		lastPosition = pos + find.length();
      	}
      	return pos;
      }
      public boolean replace (JTextArea area, String find, String replace) {
      	boolean found = false;
      	int pos = 0;
      	pattern = Pattern.compile(find);
      	previous = find;
      	matcher = pattern.matcher(area.getText());
      	found = matcher.find(0);
      	if(!found) {
      		pos = -1;
      		lastPosition = 0;
      	}
      	else {
      		pos = matcher.start();
      		lastPosition = pos + find.length();
      		area.replaceRange(replace, pos, (pos + find.length()));
      	}
      	return found;
      }
      
      
      
      public static void main(String[] args) {
      	new Tamara2();
      	}
      }

      Comment

      Working...