Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2012, 2014 IBM Corporation and others. |
2 |
* Copyright (c) 2012, 2015 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 14-19
Link Here
|
14 |
import java.util.HashMap; |
14 |
import java.util.HashMap; |
15 |
import java.util.Map; |
15 |
import java.util.Map; |
16 |
|
16 |
|
|
|
17 |
import org.eclipse.equinox.bidi.StructuredTextProcessor; |
17 |
import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory; |
18 |
import org.eclipse.equinox.bidi.StructuredTextTypeHandlerFactory; |
18 |
import org.eclipse.jface.internal.InternalPolicy; |
19 |
import org.eclipse.jface.internal.InternalPolicy; |
19 |
import org.eclipse.swt.SWT; |
20 |
import org.eclipse.swt.SWT; |
Lines 142-147
Link Here
|
142 |
* The RLE char |
143 |
* The RLE char |
143 |
*/ |
144 |
*/ |
144 |
static final char RLE = 0x202B; |
145 |
static final char RLE = 0x202B; |
|
|
146 |
|
147 |
/** |
148 |
* The RLM char |
149 |
*/ |
150 |
static final char RLM = 0x200F; |
145 |
|
151 |
|
146 |
/** |
152 |
/** |
147 |
* The LRO char |
153 |
* The LRO char |
Lines 537-540
Link Here
|
537 |
control.setTextDirection(textDir); |
543 |
control.setTextDirection(textDir); |
538 |
} |
544 |
} |
539 |
} |
545 |
} |
540 |
} |
546 |
|
|
|
547 |
/** |
548 |
* Applies bidi processing to the given string. |
549 |
* |
550 |
* <p> |
551 |
* Possible values for <code>handlingType</code> are: |
552 |
* <ul> |
553 |
* <li>{@link BidiUtils#LEFT_TO_RIGHT}</li> |
554 |
* <li>{@link BidiUtils#RIGHT_TO_LEFT}</li> |
555 |
* <li>{@link BidiUtils#AUTO}</li> |
556 |
* <li>{@link BidiUtils#BTD_DEFAULT}</li> |
557 |
* <li>the <code>String</code> constants in |
558 |
* {@link StructuredTextTypeHandlerFactory}</li> |
559 |
* <li>if OSGi is running, the types that have been contributed to the |
560 |
* <code>org.eclipse.equinox.bidi.bidiTypes</code> extension point.</li> |
561 |
* </ul> |
562 |
* <p> |
563 |
* The 3 values {@link #LEFT_TO_RIGHT}, {@link #RIGHT_TO_LEFT}, and |
564 |
* {@link #AUTO} are usable whether {@link #getBidiSupport() bidi support} |
565 |
* is enabled or disabled. |
566 |
* <p> |
567 |
* The remaining values only have an effect if bidi support is enabled. |
568 |
* <p> |
569 |
* The 4 first values {@link #LEFT_TO_RIGHT}, {@link #RIGHT_TO_LEFT}, |
570 |
* {@link #AUTO}, and {@link #BTD_DEFAULT} are for Base Text Direction (BTD) |
571 |
* handling. The remaining values are for Structured Text handling. |
572 |
* <p> |
573 |
* <strong>Note:</strong> The Structured Text handling only works if the |
574 |
* <code>org.eclipse.equinox.bidi</code> bundle is on the classpath! |
575 |
* <p> |
576 |
* <strong>Note:</strong> To deprocess the string you must use |
577 |
* {@link BidiUtils#deprocessBidi(String)} for the 4 first values or |
578 |
* {@link StructuredTextProcessor#deprocessTyped(String, String)} for the |
579 |
* remaining values. |
580 |
* </p> |
581 |
* |
582 |
* @param string |
583 |
* the string |
584 |
* @param handlingType |
585 |
* the type of handling |
586 |
* @return the bidi processed string |
587 |
* @throws IllegalArgumentException |
588 |
* if <code>handlingType</code> is not a known type identifier |
589 |
*/ |
590 |
public static String applyBidiProcessing(String string, String handlingType) { |
591 |
if (LEFT_TO_RIGHT.equals(handlingType)) { |
592 |
return addUCC(string, false); |
593 |
} else if (RIGHT_TO_LEFT.equals(handlingType)) { |
594 |
return addUCC(string, true); |
595 |
} else if (AUTO.equals(handlingType)) { |
596 |
return addUCC(string, isRTLValue(string)); |
597 |
|
598 |
} else if (getBidiSupport()) { |
599 |
if (BTD_DEFAULT.equals(handlingType)) { |
600 |
if (LEFT_TO_RIGHT.equals(getTextDirection())) { |
601 |
return addUCC(string, false); |
602 |
} else if (RIGHT_TO_LEFT.equals(getTextDirection())) { |
603 |
return addUCC(string, true); |
604 |
} else if (AUTO.equals(getTextDirection())) { |
605 |
return addUCC(string, isRTLValue(string)); |
606 |
} |
607 |
} else { |
608 |
return StructuredTextProcessor.processTyped(string, handlingType); |
609 |
} |
610 |
} |
611 |
return string; |
612 |
} |
613 |
|
614 |
/** |
615 |
* Strips off Unicode control characters from the given string. |
616 |
* |
617 |
* |
618 |
* @param string |
619 |
* the string |
620 |
* @return the bidi deprocessed string |
621 |
*/ |
622 |
public static String deprocessBidi(String string) { |
623 |
StringBuffer header1 = new StringBuffer().append(LRM).append(LRE), |
624 |
header2 = new StringBuffer().append(RLM).append(RLE); |
625 |
StringBuffer tailer1 = new StringBuffer().append(LRM).append(PDF), |
626 |
tailer2 = new StringBuffer().append(RLM).append(PDF); |
627 |
|
628 |
if ((string.startsWith(header1.toString()) && string.endsWith(tailer1.toString())) |
629 |
|| (string.startsWith(header2.toString()) && string.endsWith(tailer2.toString()))) { |
630 |
return string.substring(2, string.length() - 2); |
631 |
} |
632 |
return string; |
633 |
} |
634 |
|
635 |
private static String addUCC(String string, boolean isRTL) { |
636 |
StringBuffer sb = new StringBuffer(); |
637 |
if (isRTL) { |
638 |
sb.append(RLM).append(RLE); |
639 |
} else { |
640 |
sb.append(LRM).append(LRE); |
641 |
} |
642 |
sb.append(string); |
643 |
if (isRTL) { |
644 |
sb.append(RLM); |
645 |
} else { |
646 |
sb.append(LRM); |
647 |
} |
648 |
sb.append(PDF); |
649 |
return sb.toString(); |
650 |
} |
651 |
|
652 |
private static boolean isRTLValue(String stringValue) { |
653 |
for (int i = 0; i < stringValue.length(); i++) { |
654 |
if (Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT |
655 |
|| Character |
656 |
.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC |
657 |
|| Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_ARABIC_NUMBER) |
658 |
return true; |
659 |
else if (Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_LEFT_TO_RIGHT) { |
660 |
return false; |
661 |
} |
662 |
} |
663 |
return false; |
664 |
} |
665 |
|
666 |
} |