Monthly Archives: December 2013


Get cursor position in textarea or input with JQuery


The next snippet is a JQuery function helps to obtain the posiion of cursor in any HTML input element:

    (function($, undefined) {
        $.fn.getCursorPosition = function() {
            var el = $(this).get(0);
            var pos = 0;
            if ('selectionStart' in el) {
                pos = el.selectionStart;
            } else if ('selection' in document) {
                var Sel = document.selection.createRange();
                var SelLength = document.selection.createRange().text.length;
                Sel.moveStart('character', -el.value.length);
                pos = Sel.text.length - SelLength;
            return pos;

And here usage example:


As the result we’ll see next:

The post was created by originally

Create desktop application launcher in Linux

To create a launcher for your downloaded new software you can do following:
1. Create file “YourAppName.desktop” in ~/Desktop folder.
2. Fill it with params:

[Desktop Entry]

This is workaround for “Do you want to run or display its contents?” dialog message!

For more examples see /usr/share/applications folder.

Keyboard layout configuration with setxkbmap in Linux


To get worked keyboard layout well follow next steps:

1. Install x11-xkb-utils package:

# apt-get install x11-xkb-utils

2. Add the line to your ~/.xinitrc file:

setxkbmap -layout 'us,ru' -option 'grp:ctrl_shift_toggle'

Help with grp:

  • grp: toggle – switch right Alt;
  • grp: shift_toggle – two keys shift;
  • grp: ctrl_shift_toggle – ctrl shift;
  • grp: alt_shift_toggle – alt shift;
  • grp: ctrl_alt_toggle – ctrl alt;
  • grp: caps_toggle – switching on CapsLock;
  • grp: lwin_toggle – switch on the left “Win” key on a Windows keyboard ( for the X Window older than 4.2) ;
  • grp: rwin_toggle – switching from the right “Win” key on a Windows keyboard ( for the X Window older than 4.2) ;
  • grp: menu_toggle – switching on the ” Context Menu ” on the Windows keyboard ( for the X Window older than 4.2) ;
  • grp: lctrl_toggle – switch on the left Control key
  • grp: rctrl_toggle – switching from the right Control key
  • grp: switch – switch on the right Alt only at the time of pressing
  • grp: lwin_switch – switch on the left Windows key only at the moment of pressing
  • grp: rwin_switch – switch on the right key Windows only at the moment of pressing
  • grp: win_switch – switching on any key Windows only at the moment of pressing

So simple, good luck!

See also,

Stream file download in a JSF managed bean


To output contents directly to browser in JSF 2.x use next method:

public static void downloadFile(String fileName, String content, String contentType) throws IOException {
  FacesContext fc = FacesContext.getCurrentInstance();
  ExternalContext ec = fc.getExternalContext();

    "attachment; filename=\"" + fileName + "\"");

  OutputStream output = ec.getResponseOutputStream();


And example usage in action listener:

public void onSomeEvent(ActionEvent event) {
  try {
    StringBuilder result = new StringBuilder();
    downloadFile("file.csv", result.toString(), "text/csv");
  } catch (Exception e) {
    /* handle errors... */

See also,

Generate random number in Java


To get random number for Integer use next snippet:

  Random random = new Random();
  int min = 100;
  int max = 900;
  int randomInt = random.nextInt(max - min + 1) + min;

Method Random.nextInt(n) supports argument with MAX value.

For other methods which is not support the argument use this pattern:

  Random random = new Random();
  double min = 10;
  double max = 90;
  double randomDouble = random.nextDouble() * (max - min) + min;

See also,

Spring with multiple transaction managers

Here is repost of question.

Here is applicationContext.xml config part:

<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager"> 
  <property name="entityManagerFactory" ref="entityManagerFactory1" /> 
  <qualifier value="account"/> 

<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory2" /> 
  <qualifier value="businessData"/> 

and code using:

public class TransactionalService {

    public void setSomethingInAccount() { ... }

    public void doSomethingInBusinessData() { ... }

So, as you see we can simply use several connections with transaction management in Spring application.

See also,