The best place to *find* answers to programming/development questions, imo, however it's the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but again, a great reference for *finding* answers.

My Music (Nickleus)

20130925

html javascript - set focus to first page input text textfield when page loads

here's how you set the focus in the first input text textfield of an html page when the page loads.

put this in your page template header (inside the <head> tag, or just inside an individual page's <head> tag):
<script type="text/javascript" src="/scripts/keyboard-controls.js"></script>

create a file here:
scripts/keyboard-controls.js

scripts/keyboard-controls.js

function setFocusOnFirstTextfield() {
    var nodeList = document.getElementsByTagName("input");
    for(item in nodeList) {
        if(nodeList[item].getAttribute("type") == "text" || nodeList[item].getAttribute("type") == "password") {
            nodeList[item].focus();
            break;
        }
    };
}


put this in your page template footer (or just at the bottom of an individual html page):

<script type="text/javascript">
    setFocusOnFirstTextfield();
</script>


(right before the closing body tag, </body>)

so, if you're using a template for your header and footer, then this code will work for all pages in your project :)

richfaces how to set focus on an h:inputText input text textfield (HtmlInputText) in a rich:modalPanel when clicking an open/show button

if you have a button that opens a modal panel and you want to set the focus to the first text input field, then use the rich:modalPanel's onshow attribute:
<rich:modalPanel onshow="document.getElementById('trq_det_form:hit_marking').focus()" ...

trq_det_form is the modaldPanel form id and hit_marking is the textfield input id.

20130924

richfaces how to close rich:modalPanel with escape key rich:hotKey, including modalpanels that open other modalpanels (multi-tier/multi-level)

the following code works both for single modal panels AND for multi-level modal panels (modal panels that open new modal panels, i.e. more than one modal panel open at the same time/on top of each other).

note: all rich:modalPanel tags must have a unique id attribute.

note: for some reason (1, 2), at least in our company's app, you have click the modal panel and then press escape in order for the "hide" functionality to work, so if anyone has a solution to this, i'd love to hear it. i tried generating a javascript click() event on the modalpanel to set focus to it, but it didn't help/work. but, despite that, it's still better and faster than having to mouse up to the "x" (close) icon or down to the cancel button at the bottom :)



scripts/keyboard-controls.js

...
var ActiveModal = new function() {
    this.visiblePanelIDs = new Array();
    this.activeModalPanel = null;
    this.removeModal = function(a) {
        if(a && (a != "wait")) {
            this.visiblePanelIDs.splice(0,1);
            this.activeModalPanel = this.visiblePanelIDs[0];
        }
    };
    this.addModal = function(a) {
        if(a && (a != "wait")) {

            a = a.id.replace(/Container/,'');
            this.visiblePanelIDs.splice(0,0,a);
            this.activeModalPanel = this.visiblePanelIDs[0];
        }
    };
};

...


explanation of a = a.id.replace(/Container/,''); :
gets the actual modalPanel id by stripping away the richfaces-appended text "Container" on the rendered id

the JSON code above is based on Justin Skinner's Multiple RichFaces Modal with Same Shortcut Key to Close. my improvement is that the code now works with multi-level modalpanels--it works with an array of modalpanel ids.

every time a new modalpanel is opened, its id is placed at the beginning of the array and "older" panels (that are still open) get pushed further back in the array so that when you close the modalpanel on top, the code simply removes the id at the front of the array--index 0 (zero).

e.g.:
open modalpanel1
visiblePanelIDs: {"modalpanel1"}
activeModalPanel: modalpanel1

open modalpanel2
visiblePanelIDs: {"modalpanel2","modalpanel1"}
activeModalPanel: modalpanel2

close modalpanel2
visiblePanelIDs: {"modalpanel1"}
activeModalPanel: modalpanel1

activeModalPanel is the modalpanel id that will get closed when you press the escape key

explanation of if(a && (a != "wait")):

during my testing of the code, i found out that the functions removeModal and addModal get run, no matter what, the first time ActiveModal gets initialized, and the "a" parameter is undefined (nothing). also, sometimes during processing, the parameter in to the functions has the value "wait". so, i don't want to do anything in the functions unless the parameter exists AND it isn't equal to "wait".



template code (or at the top of your xhtml/jsf page)

...
<script type="text/javascript" src="/scripts/keyboard-controls.js"></script>
</head>

<body>
...

<rich:hotKey key="esc" handler="if(ActiveModal.activeModalPanel){Richfaces.hideModalPanel(ActiveModal.activeModalPanel);}"/>
...



rich:modalPanel attributes onshow and onhide

<rich:modalPanel onshow="ActiveModal.addModal(this)" onhide="ActiveModal.removeModal('x')" id="transportAgreementDetails">


explanation of ActiveModal.removeModal('x'):

the parameter ('x') can be anything you want because the removeModal function simply removes the modal panel id at the beginning of the array (index 0), but we need some kind of text string sent in so we know that the function was called by the user and that it wasn't called during initialization of the ActiveModal object AND that it isn't a "wait" (see my explanation above).

20130920

how to generically find "this" richfaces rich:modalPanel's jsf non-html-rendered id, i.e. the id in the jsf/xhtml code (-"Container")

when this code is run in a browser:
<rich:modalPanel id="changePwdPanel" onshow="alert(this.id)">
...
</rich:modalPanel>

it outputs the following:
changePwdPanelContainer

and the resulting html code looks like this:
<div id="changePwdPanel" style="display: none;"><input autocomplete="off" id="changePwdPanelOpenedState" name="changePwdPanelOpenedState" type="hidden" /><div class="rich-modalpanel " id="changePwdPanelContainer" ...


if you want to get the actual modalPanel coded id, i.e. changePwdPanel, you can e.g. do this:

<rich:modalPanel id="changePwdPanel" onshow="alert((this.id).replace(/Container/,''))">
...
</rich:modalPanel>


20130918

how to download/save videos from NRK in ubuntu 13.04

title in norwegian: hvordan laste ned og lagre videoer fra NRK Nett TV
---

here's a video of my friend Yassin on NRK:
http://tv.nrk.no/serie/distriktsnyheter-oestlandssendingen/dkoa99091613/16-09-2013#t=4m22s

here's what i had to do to save a copy of that video:
go here:
tv.nrk.no/innstillinger

choose "Link til avspilling med HLS", then click "Lagre"

go to the video link again:
http://tv.nrk.no/serie/distriktsnyheter-oestlandssendingen/dkoa99091613/16-09-2013#t=4m22s

click the Play icon

the page will redirect to this URL:
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/master.m3u8

save that page/file (ctrl+s), then open the downloaded file  (master.m3u8) in a text editor (e.g. geany)

the file contents will look like this:

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=212000,RESOLUTION=320x180,CODECS="avc1.66.30, mp4a.40.2"
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_0_av.m3u8?null=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=387000,RESOLUTION=480x270,CODECS="avc1.66.30, mp4a.40.2"
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_1_av.m3u8?null=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=712000,RESOLUTION=640x360,CODECS="avc1.77.30, mp4a.40.2"
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_2_av.m3u8?null=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1410000,RESOLUTION=960x540,CODECS="avc1.77.30, mp4a.40.2"
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_3_av.m3u8?null=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2384000,RESOLUTION=1280x720
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_4_av.m3u8?null=
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=77000,CODECS="mp4a.40.2"
http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_0_a.m3u8?null=



the URL for the highest quality version of the video is the one that ends in "index_4_av.m3u8" (you can also see that in the comment above it, it says 1280x720).

now make sure you have avconv installed from ubuntu's repos:
sudo apt-get install avconv

the portion of the news about Yassin begins at 04 minutes and 11 seconds and lasts for 5 minutes. I don't want the whole news report which is like 15 minutes long, so run the following command in a terminal window:
cd

cd Downloads

avconv -i "http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_4_av.m3u8" -ss 00:04:11 -t 00:05:00 -codec copy yassin.mp4

when the downloading and converting is done, the resulting video file will now be in your Downloads folder:
/home/me/Downloads/yassin.mp4

if you want to download the whole video just cut out the time parameter parts, e.g.:
avconv -i "http://nordond27a-f.akamaihd.net/i/wo/open/b5/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc/b59297a8dd562f58eb7a5f77d101b0a7eb4500dc_,141,316,563,1266,2250,.mp4.csmil/index_4_av.m3u8" -codec copy yassin.mp4


special thanks to this thread:
http://freak.no/forum/showthread.php?t=249006

note: avconv is the "new" replacement for the old program ffmpeg


UPDATE 20131031

i was getting some errors when trying to download a specific video encoded with h264:
[NULL @ 0x123d480] non-existing PPS referenced
[h264 @ 0x1241300] non-existing PPS 0 referenced
[h264 @ 0x1241300] decode_slice_header error
[h264 @ 0x1241300] no frame!



so i installed some extra packages:
sudo apt-get install libx264-dev libfaac-dev librtmp-dev libass-dev

then specified the video and audio codecs to use, like this:
avconv -i http://nrkclip3b-f.akamaihd.net/i/wo/open/84/84af95d37d4b5c59574b6d45589612e022d52762/84af95d37d4b5c59574b6d45589612e022d52762_,141,316,563,1266,2250,.mp4.csmil/index_4_av.m3u8 -c:v libx264 -c:a libvo_aacenc knaus2.mp4

then it worked :)

20130913

sahi - how to access a specific table cell using regex when the cell doesn't have an id






here's some relevant, rendered HTML from a richfaces rich:dataTable for the image above:

...
<tbody id="invFormremoveTRQPanel:removeTrqPnlTbl:tb">
    <tr class="rich-table-row rich-table-firstrow row1">
        <td id="invFormremoveTRQPanel:removeTrqPnlTbl:0:j_id159" class="rich-table-cell wsS ">
            <input type="checkbox" name="invFormremoveTRQPanel:removeTrqPnlTbl:0:j_id161"></input>
        </td>
        <td id="invFormremoveTRQPanel:removeTrqPnlTbl:0:j_id162" class="rich-table-cell wsS ">
            X_TRQ_13144907
        </td>

...


the green code represents the first row's second column. the red code is a random id string generated by richfaces because in the xhtml/jsf file, there was no id set for the cell:

...
<rich:column>
    <f:facet name="header">#{msg['label.rcvrRefId']}</f:facet>
    <h:outputText value="#{trq.getRcvrRefId()}" />
</rich:column>

...

here's one way to access the element using regexp:
_cell(/invFormremoveTRQPanel:removeTrqPnlTbl:0:*/+"[1]")

invFormremoveTRQPanel:removeTrqPnlTbl:0 == the row identifier
/.../ == regular expression code
* == match any auto-generated id string, e.g. j_id162, this will match every column in the first row
so to specify which column (0-based) we want we write [1]

sahi - how to get tomorrow's date in the format dd.MM.yyyy

i needed a simple way to generate a date string so i could set a rich:calendar textbox input field using a sahi script, so here's sample code for one solution to generating tomorrow's date as a text string:
var $formattedDate = java.util.Calendar.getInstance();
$formattedDate.add(java.util.Calendar.DAY_OF_YEAR, 1);
$formattedDate = new java.text.SimpleDateFormat("dd.MM.yyyy").format($formattedDate.getTime());
_alert($formattedDate);


which will give output like:
14.09.2013

it's cool how you can call java directly in the sahi code :)

thanks to the post how to add the specific number of days to a date which was obtained

20130910

sahi - regex how to specify an indeterminate row number for a textfield element in a table row column

i have a datatable that expands with a new row every time i click an "add new row" button, and only the new row will have an editable textfield input, so to generalize the sahi script code you can use a regex, so it will match whichever single row has a visible textfield input:
_setValue(_textbox(/detailForm:agrSCFactorsTable:\d{1}:SCF_nameIn/), $name);

\d{1} means a digit (number) with 1 in length, i.e. 0-9.

so that regex will match elements like:
_setValue(_textbox(/detailForm:agrSCFactorsTable:0:SCF_nameIn/), $name);
...
_setValue(_textbox(/detailForm:agrSCFactorsTable:3:SCF_nameIn/), $name);
...
_setValue(_textbox(/detailForm:agrSCFactorsTable:7:SCF_nameIn/), $name);
...


20130904

[SOLVED] sahi "for loop" not working

i noticed on the sahi "for loops" documentation page that the for loop variable "i" wasn't being declared with a dollar sign ( $ ):

for (var i=0; i<links.length; i++){
...

so when i wrote a for loop in one of my tests i decided to insert a dollar sign for consistency purposes (so all variables used the dollar sign):
 function setTimeFutureHours($hrs) {
    ...
    for(var $i=0; i<$hrs; $i++) {
        ...
    }
}


but when i ran the code, the for loop never ran. i quickly realized that i hadn't put a dollar sign next to the "i" in the middle, so i changed it and it ran perfectly:

function setTimeFutureHours($hrs) {
    ...
    for(var $i=0; $i<$hrs; $i++) {
        ...
    }
}


just out of curiosity, i tried the code without dollar signs next to the "i", like the documentation said and that also worked; you just have to be consistent and either use dollar signs on all of the variables in the for loop phrase or don't use dollar signs ;)

sahi - how to set a richfaces rich:inputNumberSpinner value and verify it got set correctly

i have the following form composed of a rich:calendar and 2 rich:inputNumberSpinner elements:

<a4j:form id="dateAndTimeSetterForm">
    <h:panelGrid id="dateAndTimeSetter" style="border:1px" columns="1">
        <h:panelGrid style="border:0px;width:100%;align:center" columns="1">
            <rich:calendar
                requiredMessage="#{msg['label.timeRequired']}"
                value="#{listTRQsDataModel.requestHandler.dateInEditPosition}"
                required="#{listTRQsDataModel.requestHandler.dateTimeMandatory}"
                locale="#{userMenuBean.userLocale}" direction="auto"
                id="dateSetterCal" popup="false" showInput="true"
                showApplyButton="false" datePattern="#{msg['calendar.datePattern']}" cellWidth="24px"
                cellHeight="22px" style="width:200px;align:center;">
                <a4j:support event="onchanged" ajaxSingle="true"
                    reRender="hourSpinner, minuteSpinner" />
            </rich:calendar>
        </h:panelGrid>
        <h:panelGrid style="border:0px;width:100%;align:center;"
            columns="2">
            <h:outputLabel style="align:right"
                value="#{msg['label.hour']}" />
            <h:outputLabel
                value="#{msg['label.minute']}" />
            <rich:inputNumberSpinner id="hourSpinner" minValue="0"
                style="align:right" maxValue="23" inputSize="2"
                value="#{listTRQsDataModel.requestHandler.hourForTimeInEditPosition}" />
            <rich:inputNumberSpinner id="minuteSpinner" minValue="0"
                maxValue="59" inputSize="2"
                value="#{listTRQsDataModel.requestHandler.minuteForTimeInEditPosition}" />

        </h:panelGrid>
        <h:panelGrid style="border:0px;align:center;" columns="1">
            <a4j:commandButton id="setTimeBtn" style="align:center"
                styleClass="regularbutton"
                action="#{listTRQsDataModel.requestHandler.saveDateInPosition}"
                oncomplete="if (#{facesContext.maximumSeverity==null}) #{rich:component('time_panel_trq')}.hide()"
                reRender="#{listTRQsDataModel.requestHandler.dateIdentInEditPosition}"
                value="#{msg['label.setTime']}" />
        </h:panelGrid>
    </h:panelGrid>
</a4j:form>

which looks like this in a browser:



the following sahi code ...
* opens this date+time setting modal panel
* gets the current hour value (originally "12")
* clicks the "up arrow" icon for hour ("Time" in norwegian) to make the hour "13"
* verifies that the hour got set correctly
* then clicks the "set time" submit button ("Sett tid" in norwegian)
* then verifies that the modal panel is closed--the submit button is no longer visible

function set_trq_pickup_time_future_1_hr() {
    _click(_link("trq_det_form:setTimefirstpickupdate"));
 

    _wait(10000, _isVisible(_button("dateAndTimeSetterForm:setTimeBtn")));
 

    var $currentHour = _fetch(_textbox("dateAndTimeSetterForm:hourSpinner").value);
 

// clicks the first hour spinner based on class--there are two and "up 1 hour" is on top, i.e. first, so "down 1 hour" would be "rich-spinner-btn rich-spinner-button[1]"
    _click(_imageSubmitButton("rich-spinner-btn rich-spinner-button", _in(_table("dateAndTimeSetterForm:hourSpinnerButtons"))));
 

    var $newHour = ($currentHour+1);
 

    _wait(_textbox("dateAndTimeSetterForm:hourSpinner").value == $newHour);
 

    _click(_button("dateAndTimeSetterForm:setTimeBtn"));
 

    _wait(10000, _isVisible(_button("dateAndTimeSetterForm:setTimeBtn")) == false);
}

20130902

[SOLVED] jboss 4.2.2.GA - character encoding problem when getting URL/URI parameter value

we have some code that gets a URL parameter value:
String savedSearchName = context.getExternalContext().getRequestParameterMap().get("name");

the URL looks like this:
http://localhost:9001/pages/listTRs.jsf?a=li&name=testsøk1

so even though the parameter looked like it had the right norwegian encoding (testsøk1), when we got the parameter value for "name", in java, the String savedSearchName had the following value:
testsøk1

to fix this we had to change the following file:
/path/to/jboss/server/mydomain/deploy/jboss-web.deployer/server.xml

and add a URIEncoding attribute to the <Connector> tag for the HTTP/1.1 protocol:

<Service name="jboss.web">
    <Connector port="9001"
        address="${jboss.bind.address}"
        URIEncoding="UTF-8"
        maxThreads="250"
        maxHttpHeaderSize="8192"
        emptySessionPath="true"
        protocol="HTTP/1.1"
        enableLookups="false"
        redirectPort="8443"
        acceptCount="100"
        connectionTimeout="20000"
        disableUploadTimeout="true" />
...
</service>


you have to restart your jboss sever for the change to take effect. after restart, the norwegian characters were correct in java:
testsøk1