aspose file tools*
The moose likes Beginning Java and the fly likes code that does what HOME button does Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "code that does what HOME button does" Watch "code that does what HOME button does" New topic
Author

code that does what HOME button does

Leo Max
Ranch Hand

Joined: Sep 27, 2009
Posts: 36
I have a string: hi there my name is Leo
I want to move the cursor to the beginning of the sentence. That line that flashes whenever you click on any text editable area. I want it to move to the beginning of the line so that I can write something. I don't want to use \r or newstring + oldstring. It HAS TO BE HOME The line I'm writing isn't necessarily in English.

Ex: English: hi there my name is Leo <-- the HOME button takes me to right before hi (look at it from left to right)
Ex: English + Arabic: مرحباً أنا name ليو <-- the HOME button takes me to right after the second word (look at it from left to right)
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
The InputMap for the component holding the text (eg JTextField) will hold a mapping for the action to take when the HOME key is pressed. If there is no mapping for this component the call will walk the component hierarchy until it finds one. The returned value can be used to get the Action to run from the component's (or one of it's ancestors) ActionMap.

For example:

If you want to run this action you can call it's actionPerformed method. An easier but dirtier approach is to simulate a HOME key press.
Leo Max
Ranch Hand

Joined: Sep 27, 2009
Posts: 36
Hi. I do wish to simulate the the HOME key because I'm only dealing with strings. There is no editable text area.




Okay I shall tell you exactly what I'm trying to do

I wrote two lines as an example in my first message. Take the first one and paste it in notepad. Click anywhere on the sentence and then press the HOME key. You'll see that the cursor moves to the end of the second arabic word. Right click on that spot (or just press the button in the keyboard that simulates a right mouse button) and choose INSERT UNICODE CONTROL CHARACTER then choose RLE RIGHT TO LEFT EMBEDDING.
You'll see that the order of the sentence has changed (look left to right) to arabic english arabic arabic.

Right click anywhere in notepad and choose SHOW UNICODE CONTROL CHARACTER. You'll see that this special character is put at the end of the line. I know the unicode rep of this character \u202B. I tried adding it but it didn't work. So I figured I'd do it like I usually do but couldn't find a way to simulate the HOME key.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Sorry, I'm rather confused now. Your first post talked of positioning the flashing line (Caret) which suggest you have a visual display with a caret. Now you are saying you only have a String and no display so are you saying you want to find the index in the string where the caret would be if it were displayed and the home key was pressed?
Leo Max
Ranch Hand

Joined: Sep 27, 2009
Posts: 36
Sorry I was trying to explain my situation. I brought up the editable text area in my first msg in order to show you what I want to happen when I press the home key.
so yes you're right. you want to find the index in the string where the caret would be if it were displayed and the home key was pressed?" <- EXACTLY


In my last message, I told you exactly what I was trying to accomplish. I'm trying to automate the process of "fixing" a line that has both languages in it such that the order of words will be correct.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

There is no cursor in a String, so stop thinking about Home keys and what not. What you need to do is embedd the correct directional control characters in your text. There are three codes to think about:
\u202D: Left to Right Override (use left to right order even when the system thinks otherwise)
\u202E: Right to Left Override (use right to left order even when the system thinks otherwise)
\u202C: Stop using the current Override (revert to what the system thinks)

So your string starts with arabic, has english, then more arabic, you would do:


The java.text.Bidi class may help in complex situations, but I have never used it.


Steve
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
Ok, I understand now. Yes you need to insert the \u202B char at the end of the second Arabic word and not at the end of the current sentence.

I don't know the rules to apply to simulate a home key press. You could always look at the source code for the action class that handles the GUI case and see what it does.
The class is: javax.swing.text.DefaultEditorKit$BeginLineAction
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code that does what HOME button does