Tuesday, December 30, 2008

JavaScript Tab in TextBox - for IE and Firefox

While seeking for a way to make tabs work in Firefox I found many code posts specific to Internet Explorer - this code below works in Mozilla Firefox as well for cross-browser tab functionality in a textarea.

The other code I found on the web was missing some functions I wanted like inserting the tab at the cursor location if in the middle of text and returning the cursor to the appropriate position instead of just sticking it at the end of the text after the tab has been inserted. Additionally I needed to scroll back to the cursor location in a textarea.

Additionally a lot of the examples seemed overly complicated. I wanted something simple so put common code in functions so reworked some of the examples I found to end up with the following:


<html>
<head>
<script type="text/javascript">
function doTab(arg1,arg2){
t=arg2
var k;

//save cursor position
var top= arg2.scrollTop;
var left= arg2.scrollLeft;

if ("which" in arg1){k=arg1.which}
else if("keyCode" in arg1){k=arg1.keyCode}
else if("keyCode" in window.event){k=window.event.keyCode }
else if ("which" in window.event){k=arg1.which}
else{return true}

//insert tab
if (k == 9) {
insertatcursor(t, "\t");
arg2.scrollTop = top;
arg2.scrollLeft = left;
return false;
}
return true
}

function insertatcursor(myField, myValue) {

//IE
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}

//Mozilla Firefox
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
setSelRange(myField, endPos + myValue.length, endPos + myValue.length);
} else {
myField.value += myValue;
}
}

function setSelRange(inputEl, selStart, selEnd) {
if (inputEl.setSelectionRange) {
inputEl.focus();
inputEl.setSelectionRange(selStart, selEnd);
} else if (inputEl.createtextRange) {
var range = inputEl.createtextRange();
range.collapse(true);
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);
range.select();
}
}

</script>
</head>

<body>
<textarea onkeydown="return doTab(event,this);"></textarea>
</body>
</html>

Tuesday, November 11, 2008

How to Use SLF4J with Jetty 6

In order to use Log4J with Jetty 6 (to get Jetty 6 and presumably subsequent versions of jetty to log output using Log4J), you use SLF4J which is integrated with Jetty and is a bridge (actually they call it a facade) to whatever particular logger you want to use. In order to use Log4J...

In {jetty.home}/lib put these jars (versions may vary):

slf4j-api-1.5.5.jar
slf4j-log4j12-1.5.5.jar
log4j-1.2.15.jar

In {jetty.home}/resources put your log4j.xml file

For instance if the place where you installed jetty is C:/jetty6 your director tree will look something like this:

C:/jetty6/bin...
C:/jetty6/lib/slf4j-api-1.5.5.jar
C:/jetty6/lib/slf4j-log4j12-1.5.5.jar
C:/jetty6/lib/log4j-1.2.15.jar
C:/jetty6/resources/log4j.xml
etc.

Your log4j.xml file may look something like this:

<?xml version="1.0" encoding="UTF-8" ?>

<?xml:namespace prefix = log4j /><log4j:configuration>
<appender class="org.apache.log4j.DailyRollingFileAppender" name="FILE">
<param name="file" value="logs/jetty.log">
<param name="datePattern" value="'.'yyyy-MM">
<param name="append" value="true">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n">
</layout>
</appender>

<root>
<priority value="debug">
<appender-ref ref="FILE">
</root>
</log4j:configuration>

You can download the above technologies at these web sites:

http://www.mortbay.org
http://www.slf4j.org/
http://logging.apache.org/log4j/1.2/index.html