View Javadoc
1   package org.argeo.cms.e4.addons;
2   
3   import java.security.AccessController;
4   import java.util.List;
5   import java.util.Locale;
6   import java.util.Set;
7   
8   import javax.annotation.PostConstruct;
9   import javax.security.auth.Subject;
10  
11  import org.argeo.eclipse.ui.specific.UiContext;
12  import org.eclipse.e4.core.services.nls.ILocaleChangeService;
13  import org.eclipse.e4.ui.model.application.MApplication;
14  import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
15  import org.eclipse.e4.ui.workbench.modeling.EModelService;
16  import org.eclipse.e4.ui.workbench.modeling.ElementMatcher;
17  import org.eclipse.swt.SWT;
18  
19  /** Integrate workbench with the locale provided at log in. */
20  public class LocaleAddon {
21  	private final static String STYLE_OVERRIDE = "styleOverride";
22  
23  	// Right to left languages
24  	private final static String ARABIC = "ar";
25  	private final static String HEBREW = "he";
26  
27  	@PostConstruct
28  	public void init(ILocaleChangeService localeChangeService, EModelService modelService, MApplication application) {
29  		Subject subject = Subject.getSubject(AccessController.getContext());
30  		Set<Locale> locales = subject.getPublicCredentials(Locale.class);
31  		if (!locales.isEmpty()) {
32  			Locale locale = locales.iterator().next();
33  			localeChangeService.changeApplicationLocale(locale);
34  			UiContext.setLocale(locale);
35  
36  			if (locale.getLanguage().equals(ARABIC) || locale.getLanguage().equals(HEBREW)) {
37  				List<MWindow> windows = modelService.findElements(application, MWindow.class, EModelService.ANYWHERE,
38  						new ElementMatcher(null, null, (String) null));
39  				for (MWindow window : windows) {
40  					String currentStyle = window.getPersistedState().get(STYLE_OVERRIDE);
41  					int style = 0;
42  					if (currentStyle != null) {
43  						style = Integer.parseInt(currentStyle);
44  					}
45  					style = style | SWT.RIGHT_TO_LEFT;
46  					window.getPersistedState().put(STYLE_OVERRIDE, Integer.toString(style));
47  				}
48  			}
49  		}
50  	}
51  }