Results 1 to 13 of 13
Thread: Selecting a text
- 02-27-2011, 03:59 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Selecting a text
Hello,
I am trying to look for a code which can select a specified text from a java file .
To be more precise, I have a java code :
public Demo() {
int abc = 1;
while (abc<5) {
System.out.println(abc);
abc++;
System.out.println("Done");
}
}
I want to select the lines "System.out.println(abc) & abc++. Is there a way to do that.
Any type of help will be appreciated.
Thanks in advance,
Nitin
- 02-27-2011, 05:24 AM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
That's easy :)
Just read the java file as if its an ordinary txt file. e.g u can use bufferedReader for this
Read the file line by line and search (using contains() api) each line for
1. System.out.println and
2. abc++
Now, if you want to show these lines as highlighted, then you can use a highlighter for this.
Hope that helps :-)
- 02-27-2011, 07:11 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Thanks for the help.
But I don't want to highlight it. I want to select it. So this is what I want to do exactly.
--> Select the line.
--> Than call the Extract Method Refactoring tool on the selected code.
I know how to call Extract Method using the Robot class, but need help with selection.
- 02-27-2011, 07:24 AM #4
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
Could you please elaborate more: what do you mean by select the line?
It would be better, if you explain the whole scenario as to what do you want to achieve?
- 02-27-2011, 07:36 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Ok,
So I am making a tool which looks into your code identifies code smells and refactors it for you. Eclipse has many refactoring tools like Extract Method. Extract Variable..... All of these have a short cut to call them, but you need something selected to call them.
So I am done with identifying the code smells and I know how to generate a key press event to call these methods, but I need something selected to call them. So I am trying to figure out something for it.
For Example: Make a java file in Eclipse of my precious code.
Select those 2 lines and select Refactor --> Extract Method from the tool bar and follow the steps
So if you don't select anything you can't call Extract Method. I am trying to figure out how I can select these 2 lines using code :(
- 02-27-2011, 08:00 AM #6
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
read the java file into a JTextpane.
Jtextpane allows you to select characters/words/sentences using mouse, from there you can just invoke the shortcuts you already know.
I hope this is what you want?
- 02-27-2011, 08:23 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
I think I am not able to explain it properly.
I don't want to use mouse to select it. I want to write a code to select it. Its complicated. But I want everything to be automated from the start to end. Just the user specifies the file and thats it. So I can't ask user to select anything. I want code to select it.
Thanks for the help though.
-
But select it based on what criteria? If you don't have this solidly defined yet, you can't write the method. No one can.
- 02-27-2011, 12:48 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
I have. I know the text which I want to select. There is a criteria to know what to select. Just don't know how to select.
-
- 02-27-2011, 01:35 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
The criteria varies from code smell to code smell. So let's say I'm talking about the switch case code smell. If the switch case code smell is there, I need to extract all the switch cases individually in separate method. So I need to select each case separately and than call Extract Method refactoring on each o them.
Now can you help me to select each swith case individually.
I don't think knowing this will make much of the difference, but I don't have any problem in sharing it.
-
It sounds as if you're looking to parse or perform syntactic analysis on your text, much as a compiler would parse it, which is no simple feat. I am going to bow out of this thread because I have little knowledge about this large and difficult subject (yet), but there are chapters and books written on the subject that might help you. Best of luck.
- 02-27-2011, 07:04 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Selecting every other array
By Rivy2112 in forum New To JavaReplies: 10Last Post: 10-11-2010, 10:39 PM -
Selecting Files in a folder
By ravjot28 in forum New To JavaReplies: 1Last Post: 02-26-2010, 04:25 PM -
JFileChooser example (selecting a directory)
By Java Tip in forum Java TipReplies: 1Last Post: 02-03-2009, 01:25 PM -
Selecting parts of an image
By shaungoater in forum Java 2DReplies: 1Last Post: 12-15-2007, 10:06 PM -
selecting a record in database
By ramachandran in forum New To JavaReplies: 0Last Post: 10-25-2007, 07:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks