NetBeans: how to bring GUI Builder Assistant back

NetBeans IDE comes with a very hand visual editor for designing Forms and Dialogs, called the “GUI Builder“. One of the features I like the most is the ability to snap components relative to another component’s position or relative to the container component’s edges, etc. However, if you work on a complex form with many different components around it is possible that things may get confusing regarding what is snapping where.

To work this around the GUI Builder provides the GUI Builder Assistant. This is the white box with the bulb icon on the left. During snapping this box gives enlightening information about how the moving component will be positioned, by displaying messages like: “Snap at the preferred distant from the container’s left border”, “Snap at the small preferred distance from a component above”, etc.

This GUI Builder Assistant tool comes with a close (‘X’) icon which allows you to easily hide it if you don’t need it. But if you accidentally clicked the close icon, it may be confusing to restore the assistant back. To do so, you have to go to the Tools menu and open Options dialog. From there go to the Java section, the select the GUI Builder tab, and make sure that you check the Show Assistant checkbox.

(In a pinch, you have to go to: Tools -> Options -> Java -> GUI Builder -> Show Assistant).

I hope this helps!

 

Posted in Uncategorized | Leave a comment

jQuery: How to check whether a div contains hidden “overflown” content?

The “overflow: hidden” CSS property instructs the browser to clip the excessive content that appears inside an element. One way to detect programmatically (in Javascript) whether an element actually contains any “clipped” content or not, is to compare the value of the “scrollWidth” (the total width of the content, including any clipped parts), against the value of the net width of the element (that is, the width of the element freed from any eventual border widths).

The first value can be directly obtained using the Javascript’s scrollWidth property. The latter can be calculated using jQuery’s innerWidth() method. Therefore, the code should more or less look like this:

        if (element.scrollWidth > $(element).innerWidth()) {
            // Contains hidden content!
        } else {
            // All content visible!
        }

I hope this helps!

Posted in Uncategorized | Leave a comment

How to constrain an image's dimensions while maintaining its ratio

I know, this is not nuclear physics, but it tricks my mind every once in a while when this occurs to me.

Let’s assume the general case of an image with dimensions w×h, and that you need to scale it down so that it fits within a certain rectangle sizing w0×h0, while its sides maintain the same ratio R=w/h.

In other words, we need to calculate a new pair of dimensions w’×h’ for our image, for which the following conditions must be satisfied:

(1) w’/h’ = w/h = R

(2) w’ ≤ w0

(3) h’ ≤ h0

Note that that the ratio of the constraining box w0/h0 is irrelevant to the ratio of the image R.

From the first of the above conditions (1) we get:

w’/h’ = w/h ⇒ w’·h = w·h’   w’/w = h’/h = φ

which means that both the width (w) and the height (h) of the image must be scaled by the same factor φ; thus the problem is pretty much reduced to simply calculating the scaling factor φ.

Let’s take it a bit further by combining the last finding with the inequations (2) and (3):

w’/w = h’/h = φ ⇒ w’ = w/φ , h’ = h/φ  ⇒ w/φ ≤ w0 , h/φ ≤ h0  ⇒ w/w0 ≤ φ , h/h0 ≤ φ .

From the last statement it is clear that all we need to do is to choose φ = max[ w/w0h/h0].

To summarize, these are the steps we need to take:

1)  φ = max[ w/w0h/h0]

2)  w’ = w/φ

3) h’ = h/φ

Or, if you please, the conditional equivalent:

Is w/wh/h0 ? 

then:  w’ = w0 , h’ = w0·h/w

or else:  w’ = h0·w/h , h’ = h0

Posted in Uncategorized | Leave a comment

How to arithmetically sort an array in Javascript

Something interesting that I discovered tonight is that JavaScript’s sort() function by default sorts values lexicographically (that means, for example, that “10″ comes before “2″), even when the array contains numerical data.

Let’s consider the following code:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort();
console.log(values);

The above code yields:

[1, 10, 100, 20, 3, 4]

However, in case we need to sort those values as numbers, it is possible to pass a “comparator” function as an argument to sort(), and this function will be used by sort() to decide the order of the elements. To sort arithmetically the array in the above example, we will have to do it like this:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort( function(a,b) {
    return a-b;
});
console.log(values);

Now the code above yields:

[1, 3, 4, 10, 20, 100]

For reverse sorting we just have to swap the two operands in the sorting function:

var values = [ 1, 10, 100, 4, 20, 3 ];
values.sort( function(a,b) {
    return b-a;
});
console.log(values);

Result:

[100, 20, 10, 4, 3, 1]

One thing to notice is that above code even works if the values are stored as strings; that’s because the values of the operands are implicitly converted to integers by the subtraction operator (‘-’):

var values = [ '1', '10', '100', '4', '20', '3' ];
values.sort( function(a,b) {
    return a-b;
});
console.log(values); 

Result:

["1", "3", "4", "10", "20", "100"]

If we would prefer to be more “bullet-proof” when dealing with numbers stored as strings, we could use the parseInt() function to make sure we are really dealing with integer values.

var values = [ '1', '10', '100', '4', '20', '3' ];
values.sort( function(a,b) {
    return parseInt(a)-parseInt(b);
});
console.log(values);

Result:

["1", "3", "4", "10", "20", "100"]

Notice that the sorted values remained stored as strings.

So, that was pretty much all of it, I hope this helped! :-)

 

Posted in Uncategorized | Leave a comment

PHP: Something to watch out when using global scope variables, or "why globals doesn't (seem to) work!?"

Okay, I guess this is probably a childish mistake for an experienced PHP-er, but I myself got busted tonight, and maybe I’m not the only one :-) So, here it is.

Consider the following small PHP program:

<?php
print_global_a();

$a = 10;

function print_global_a() {
        global $a;
        echo "$an";
}

What would be the output of this code? What?? 10?? Wrooong! Let’s save this as global.php and run it, see what happens.

giannis@ubuntu:~$ php global.php

giannis@ubuntu:~$

It outputs an empty new line… Okay, then maybe something wrong with the ‘globals’ keyword. Let’s try the $GLOBALS array instead:

<?php
print_global_a();

$a = 10;

function print_global_a() {
        echo $GLOBALS['a']."n";
}

Now let’s run it again:

giannis@ubuntu:~$ php global.php
PHP Notice:  Undefined index: a in /home/giannis/global.php on line 7

giannis@ubuntu:~$

It just got even worse; not only we still get an empty new line, we also get a big fat warning.

So, what is really wrong here? It is so simple, you probably noticed on the first glance, but if you didn’t, here it is: At the moment that the function is called the $a global variable is not yet set. Therefore, when we try to access it from within a function, we simply cannot, not because ‘globals’ is not working, but because it is not set in the first place. The fact that the definition of the function comes after the assignment may delude us to think that the variable will be set when the code in the function is executed.

So, in other words, the next time you may face a “globals” that doesn’t seem  to work, make sure that the variable is actually set at the point where the function is called.

Unfortunately, PHP is a tool that does not protect the programmer from doing nasty mistakes… I hope you found this interesting and helpful :-)

Posted in Uncategorized | Leave a comment

How to "un-commit" a revision from the top of a Subversion repository

Sometimes it may occur that a commit was performed in a subversion repository, that was not exactly what the original intention was. In such cases we would like to have an “undo” operation and bring the repository back to its original state, as it was exactly before the inadvertent commit.

The title of this post might be misleading, as technically there is no way to just un-commit or undo an executed commit from a subversion repository; after all, such feature would automatically cancel the very definition of version control.

Below I will describe the second best solution (or work around, if you prefer) on removing the last commit(s) from a subversion repository, which is to create a new repository and replay all the commits from the beginning and up to the revision just before the accidental commit. When done this, simply replacing the new repository with the old one will give the illusion that the unwanted commit(s) have been undone.

In this guide I assume that you have local command-line access and sufficient permission to the repository folder. I also assume that you wish to uncommit the last commit from the top of the repository. Once other commits have been performed, things get complicated and the procedure described below is not applicable. If the repository is used by multiple users make sure to ask from your peers to cease any commits until you are done with reverting the repository state.

First, you need to create a new empty repository:

giannis@ubuntu:~$ svnadmin create new-repository

Then, you need to dump all revision history from the original repository, starting from revision 1 and go up to the revision number that was exactly before the unwanted commit (typically this will be the current revision number minus 1).

giannis@ubuntu:~$ svnadmin dump old-repository -r 1:XXX > old-repository.dump
. . . . .
. . . . .
. . . . .
* Dumped revision XXX.

In the above command make sure to replace XXX with the current revision number decreased by 1.

Then you just need to load the dump file into the new repository:

giannis@ubuntu:~$ svnadmin load new-repository < old-repository.dump

Alternatively, it is possible to perform both dump and load in one single step, without having to create the intermediate dump file:

giannis@ubuntu:~$ svnadmin dump old-repository -r 1:XXX | svnadmin load new-repository

Finally, you just have to replace the new repository with the old one. Before doing so, it would be wise to create a backup of the original repository, just in a case something goes wrong.

giannis@ubuntu:~$ tar cvzf old-repository-backup.tar.gz old-repository/
. . . . .
. . . . .
. . . . .
giannis@ubuntu:~$ rm -rf old-repository
giannis@ubuntu:~$ mv new-repository old-repository

And you’re done! The advantage of this method is that you completely remove the accidental commit from the history of the repository, unlike other methods which simply merge the repository back to a previous revision.

I have done this many times and it helped. I hope you will also find this helpful. :-)

Posted in Uncategorized | Leave a comment

C++: calling a member function pointer

This article is about calling a member function (method) through a “function pointer”. I decided to write about it because it tricked me for a while, and I think this may be useful to others.

I will start with a small introduction and gradually get to the point. If you’re really in a hurry scroll down to the end of the article to see how it’s done!

Calling a “global” function pointer

In good old C, the following code is probably familiar to most seasoned programmers:

#include <stdio.h>

void func1( void (*func)() ) {
func();
}

void func2() {
printf("Goodbye world!n");
}

int main() {
func1(func2);
}

Now if we compile this and execute it we’ll get of course the following output:

giannis@goofy:~$ gcc fpointer.c
giannis@goofy:~$ ./a.out
Goodbye world!

The slightly strange syntax void (*func)() in func1()’s declaration, is what we call a “pointer to a function”. This one specifically declares that “func is pointer to a function with no arguments and no return value”.

Similarly, void (*func)(int) declares a pointer to a function that takes one int argument and returns no value, and int (*func)() declares a pointer to a function that takes no arguments and returns int.

Function pointers are like data pointers, except the point to executable code instead of data. It is pretty much a standard way in C to encapsulate and delegate behaviors or logic.

Now, let’s take the same scenario only this time within the scope of a C++ class. Consider this:

#include <stdio.h>

class MyClass {

private:

void func1( void (*func)() ) {
func();
}

void func2() {
printf("Goodbye world!n");
}

public:

void method() {
func1(func2);
}
};

int main() {
MyClass myObject;
myObject.method();
}

At least for me, this made sense… But what happens if we try to compile?

giannis@goofy:~$ g++ fpointer.cpp
fpointer.cpp: In member function void MyClass::method():
fpointer.cpp:19: error: no matching function for call to MyClass::func1()
fpointer.cpp:8: note: candidates are: void MyClass::func1(void (*)())

What the error message says, more or less, is that that func1() expected to be called with an argument of type void (*)(), however we tried to pass an “unresolved overloaded function type”.

Okay, that’s weird. func1() expects an argument of type void (*)(), and func2 was exactly of that type, right?

Well, not exactly. func2 is actually a MyClass::void (*)(), that is, it belongs to the MyClass namespace. On the other hand, func1() was expecting a void (*)(), which is a pointer to a function in the global scope/namespace (a “stray” function).

To fix this, we need to need to slightly modify the code in several few places.

First of all, the declaration of func1(). As we said the function will be called with an argument of type MyClass::void (*)(). Therefore it has to be altered accordingly:

void func1( void (MyClass::*func)() ) {
func();
}

Furthermore we need to change the way the function pointer is dereferenced inside func1(). It is not enough to just call func(). Remember that all the compiler knows is the address of a method, which remains the same for all instances of a class. When dereferencing a member function pointer, we also need to provide the object for whom the method is being called, in that case this holds a reference to this object, which is the one we aimed for. Thus, the func1()’s code becomes now:

void func1( void (MyClass::*func)() ) {
(*this.*func)();
}

The (*this.*func)(); statement just instructs the compiler to call the code of MyClass pointed by func, for this object.

And we’re almost done, we need to make one final small change, in the code that passes the member function pointer, so it makes clear that func2 is a method of MyClass and not a “stray” global function:

void method() {
func1(&MyClass::func2);
}

Now it should compile and work as expected:

giannis@goofy:~$ g++ fpointer.cpp
giannis@goofy:~$ ./a.out
Goodbye world!

Let’s put it all together:

#include <stdio.h>

class MyClass {

private:

void func1( void (MyClass::*func)() ) {
(*this.*func)();
}

void func2() {
printf("Goodbye world!n");
}

public:

void method() {
func1(&MyClass::func2);
}
};

int main() {
MyClass myObject;
myObject.method();
}

I hope this helped :-)

Posted in Uncategorized | 3 Comments

How to convert FLAC to MP3 on command line

First install ffmpeg (WinFF on Windows) and LAME. Then to convert a .flac to .mp3 type this on the command line:

ffmpeg -i "xxx.flac" -acodec libmp3lame -ab 128000 "xxx.mp3"

Posted in Uncategorized | Leave a comment

How to go off the record in Gmail when "Go off the record" is grayed

It is well known that due to a “bug in Gmail” [sic], it is possible to detect whether a user (that is apparently offline) is really offline (or has blocked you) or he/she is just hiding in invisible mode. To do so all you have to is to go in the Actions menu (in your chat box) and select “Go off the record”, and then send a simple message to the user. Absence of error message implies that the user is online, and hiding.

This is probably yesterday’s news for most of you, but a small detail is being often overlooked is that this option is clickable only if the other party is online. This complicates things a bit, as those people that love to practice the “invisible mode”, almost never show up online giving you almost zero chance to reveal them.

image-1.png

This article explains how to overcome this obstacle, by presenting one way to go off the record with a user, even when he is (or appears to be) offline, causing the “Go off the record” option to be disabled (“grayed”). The procedure that is described below is not trivial, but also not prohibitive, even for the average user. You may also need to install some additional software. So, let’s get started!

We need a Web Browser that is capable of Inspecting Elements (that is, when you right-click somewhere in the page, you get a “Inspect element” item in the context menu). I suggest either using Chrome, or Firefox with the Firebug plugin installed. For this example, I’m going with Chrome, because the desired functionality is built-in, and I don’t have to install any additional plugins.

Whenever you’re ready with the browser of your choice, enter your Gmail account and open a chat window with the person that you want to go off the record. Remember that if the person is offline (which is possibly our case), you must not just click on the person’s name, but rather you must click on the “Chat” button in the pop-up box.

image-2.png

In the chat window, click in the Actions menu. The menu shows up and the “Go off the record” is disabled (grayed):

image-3.png

Now be careful not to close the menu and right-click on exactly that menu item. You will see the context menu:

image-4.png

Now click on the “Inspect element” item of the context menu. The “Developer Tools” console will open at the bottom of your browser (or in a pop-up window), and a TD html element will be highlighted:

image-5.png

Then you must locate the nearest parent DIV element of the TD element. It must be 3-4 lines above:

image-6.png

You should be able to see the attribute class=”jM jK”. What you must do now is to change the class “jM” to “jL”. Double click between the double quotes to start the attribute value editor. Then simply change “jM” to “jL”. And press Enter to save the modification.

image-7.png

And almost magically the “Go off the record” menu item is now enabled for you to click it!

image-8.png

I hope this helped! :-)

See also: How to Detect invisible user in Gmail and gtalk Chat

Posted in Uncategorized | 8 Comments

Replace "n" with new line characters, using Notepad++

In case you have a file which contains “escaped” new lines (n), like this:

notepad-1.PNG

and you want to replace the escaped new line sequences with actual new line characters, you can easily accomplish this task using Notepad++.

First load your file (or just copy paste the text) in Notepad++.

notepad-2.PNG

Then select Search → Replace from the menu, or hit Ctrl-H. The Replace dialog will show up. Therein make sure that you fill out the following values; Find what: n (yes, two back-slashes), Replace with: n (yes, just one back-slash), and the most important, make sure you select the Extended (n, r, t, , x, …) radio button. Your Replace dialog must look like this:

replace-1.PNG

Click the Replace All button, and all escaped new line sequences will be replaced with new line characters:

notepad-3.PNG

I hope this helped!

Posted in Uncategorized | 16 Comments