Monthly Archives: December 2013

Web

Get cursor position in textarea or input with JQuery

cursor_pos

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) {
                el.focus();
                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;
        }
    })(jQuery);

And here usage example:



getCursorPosition

As the result we’ll see next:
cursor_pos_result

The post was created by originally
http://stackoverflow.com/questions/1891444/how-can-i-get-cursor-position-in-a-textarea/1909997#1909997

Create desktop application launcher in Linux

app-launcher
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]
Name=YourAppName
Exec=~/apps/YourAppName/bin/run.sh
Icon=~/apps/YourAppName/bin/icon.png
Type=Application
Terminal=false
StartupNotify=true

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

dvorak2

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,
http://linuxru.org/tips/41
http://ubuntologia.ru/forum/viewtopic.php?f=34&t=4245&start=10

Stream file download in a JSF managed bean

file_download

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();

  ec.responseReset();
  ec.setResponseContentType(contentType);
  ec.setResponseContentLength(content.length());
  ec.setResponseHeader("Content-Disposition", 
    "attachment; filename=\"" + fileName + "\"");

  OutputStream output = ec.getResponseOutputStream();
  output.write(content.getBytes());

  fc.responseComplete();
}

And example usage in action listener:

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

See also,
http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean

Generate random number in Java

catrandom1

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, http://docs.oracle.com/javase/7/docs/api/java/util/Random.html

Spring with multiple transaction managers

zt4PX
Here is repost of http://stackoverflow.com/questions/4423125/spring-is-it-possible-to-use-multiple-transaction-managers-in-the-same-applica 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>

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

and code using:

public class TransactionalService {

    @Transactional("account")
    public void setSomethingInAccount() { ... }

    @Transactional("businessData")
    public void doSomethingInBusinessData() { ... }
}

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

See also,
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-multiple-tx-mgrs-with-attransactional
Enjoy!