Results 1 to 13 of 13
  1. #1
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    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 :-)

  3. #3
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Question

    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?

  5. #5
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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 :(

  6. #6
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    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?

  7. #7
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  9. #9
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mathnitin View Post
    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.
    Without passing the criteria to us, I'm not sure how we can help you here.

  11. #11
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  13. #13
    mathnitin is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    I know its the parser problem and the parser part is done. I am using AST tree for that. What I am looking for is a later part "How to select a specified text using code and not mouse."

Similar Threads

  1. Selecting every other array
    By Rivy2112 in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 10:39 PM
  2. Selecting Files in a folder
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 02-26-2010, 04:25 PM
  3. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-03-2009, 01:25 PM
  4. Selecting parts of an image
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 12-15-2007, 10:06 PM
  5. selecting a record in database
    By ramachandran in forum New To Java
    Replies: 0
    Last Post: 10-25-2007, 07:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •