View | Details | Raw Unified | Return to bug 465495 | Differences between
and this patch

Collapse All | Expand All

(-)a/bundles/org.eclipse.jface/src/org/eclipse/jface/util/BidiUtils.java (-2 / +102 lines)
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
	 *
577
	 * @param string
578
	 *            the string
579
	 * @param handlingType
580
	 *            the type of handling
581
	 * @return the bidi processed string
582
	 * @throws IllegalArgumentException
583
	 *             if <code>handlingType</code> is not a known type identifier
584
	 */
585
	public static String applyBidiProcessing(String string, String handlingType) {
586
		if (LEFT_TO_RIGHT.equals(handlingType)) {
587
			return addUCC(string, false);
588
		} else if (RIGHT_TO_LEFT.equals(handlingType)) {
589
			return addUCC(string, true);
590
		} else if (AUTO.equals(handlingType)) {
591
			return addUCC(string, isRTLValue(string));
592
593
		} else if (getBidiSupport()) {
594
			if (BTD_DEFAULT.equals(handlingType)) {
595
				if (LEFT_TO_RIGHT.equals(getTextDirection())) {
596
					return addUCC(string, false);
597
				} else if (RIGHT_TO_LEFT.equals(getTextDirection())) {
598
					return addUCC(string, true);
599
				} else if (AUTO.equals(getTextDirection())) {
600
					return addUCC(string, isRTLValue(string));
601
				}
602
			} else {
603
				return StructuredTextProcessor.processTyped(string, handlingType);
604
			}
605
		}
606
		return string;
607
	}
608
609
	private static String addUCC(String string, boolean isRTL) {
610
		StringBuffer sb = new StringBuffer();
611
		if (isRTL) {
612
			sb.append(RLM).append(RLE);
613
		} else {
614
			sb.append(LRM).append(LRE);
615
		}
616
		sb.append(string);
617
		if (isRTL) {
618
			sb.append(RLM);
619
		} else {
620
			sb.append(LRM);
621
		}
622
		sb.append(PDF);
623
		return sb.toString();
624
	}
625
626
	private static boolean isRTLValue(String stringValue) {
627
		for (int i = 0; i < stringValue.length(); i++) {
628
			if (Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT
629
					|| Character
630
							.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC
631
					|| Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_ARABIC_NUMBER)
632
				return true;
633
			else if (Character.getDirectionality(stringValue.charAt(i)) == Character.DIRECTIONALITY_LEFT_TO_RIGHT) {
634
				return false;
635
			}
636
		}
637
		return false;
638
	}
639
640
}

Return to bug 465495