Netbeans slow repaint over Remote Desktop [solution]

Lately I have been connecting sometimes to my computer at work, from home via Remote Desktop Connection (RDP). While everything is pretty much acceptable, I have noticed that Netbeans was so slow in repainting its windows, to the point of becoming unusable. With other IDEs and other software in general I never had a similar issue. If you are experiencing the same problem, here is how to fix it.

Go to the installation directory of your Netbeans (that would be on Windows probably under C:\Program Files\NetBeans 8.0), and then go to the etc folder and look for a file named netbeans.conf. Open this file in an editor and search for the text “noddraw” (with two ‘d’). You should probably see that this property has the value ‘true’ set to it:

Change the value of the property to ‘false’ and save the file. Then restart Netbeans. Repainting must be faster now. Good luck!

Posted in Uncategorized | Leave a comment

Unicode characters dropped in PDF files generated with iText and Flying Saucer

Flying Saucer is a very useful Java library that uses iText to convert HTML pages to PDF documents. Here is a nice tutorial on how to use Flying Saucer.

The last few days I was trying unsuccessfully  to generate some report that contained non-standard Unicode characters (in my case it was Greek, but I guess the same problem exists for other character sets as well, like Cyrillic, Armenian, etc). The problem was that the Greek characters seemed to be omitted; they didn’t show up in the document.

The code I was using was more or less something like that:

public class Html2Pdf {

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File("output.pdf");
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("input.html"));
        document.close();
        Desktop.getDesktop().open(file);
    }
}

And the input HTML file was something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <h1>Αρνάκι άσπρο και παχύ</h1>
    </body>
</html>

When I tried to convert this simple HTML to PDF, I got a blank page.

After lots of hours of troubleshooting, I finally discovered that, for some reason, if no specific font is used, the generated PDF uses some kind of  default (probably Helvetica) font, that contains a very limited character set, that obviously does not contain the Greek code page.

So I came up with this simple trick, that seems to solve the problem. I only had to make sure that all elements in my HTML file will use a font that contains Greek, like Arial:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

    <head>
        <style>
        * { font-family: Arial; }
        </style>
    </head>
    <body>
        <h1>Αρνάκι άσπρο και παχύ</h1>
    </body>
</html>

Arial is a pretty standard font, installed by default in most operating system, and implements a wide variety of alphabets (including Greek).

I hope this helped…

 

Posted in Uncategorized | Leave a comment

MySQL: select the n-th element from a delimiter separated value set

Lately I’ve been dealing with a database where a lot of values are stored as strings of concatenated values, separated by delimiters (e.g. “1,2,3,4″). I have to use these values in queries, for example, for joining with other tables. Selecting a specific value from a list of delimiter separated values can be a bit tricky.

In the beginning I was trying to do that using the SUBSTR and LOCATE functions of MySQL. This worked very well when I had only two values in the string. For example:

mysql> SET @values = 'Cat#Dog';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTR(@values,1,LOCATE('#',@values)-1) `1st value`;
+-----------+
| 1st value |
+-----------+
| Cat       |
+-----------+

mysql> SELECT SUBSTR(@values,LOCATE('#',@values)+1) `2nd value`;
+-----------+
| 2nd value |
+-----------+
| Dog       |
+-----------+

But if you have more than two values in your string, things start to get more complicated. For example, here’s how to get the 2nd value from a 3-value string:

mysql> SET @values = 'Square#Triangle#Hexagon';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTR(@values,LOCATE('#',@values)+1,LOCATE('#',
       SUBSTR(@values,LOCATE('#',@values)+1))-1) `2nd value`;
+-----------+
| 2nd value |
+-----------+
| Triangle  |
+-----------+

I thought I should find a way to generalize this so I can get the n-th value of N-value strings, but it was obvious that it was going to get unreadable and unmanageable very soon.

Then I found about an interesting MySQL function called SUBSTRING_INDEX(). According to MySQL’s documentation it “returns a substring from a string before the specified number of occurrences of the delimiter”, which at least to my ears sounds a bit obscure… After doing a couple of tests it was very clear to me to understand what it does:

mysql> SET @values = '1#2#3#4#5#6#7#8#9#10';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTRING_INDEX(@values,'#',4);
+--------------------------------+
| SUBSTRING_INDEX(@values,'#',4) |
+--------------------------------+
| 1#2#3#4                        |
+--------------------------------+

mysql> SELECT SUBSTRING_INDEX(@values,'#',-3);
+---------------------------------+
| SUBSTRING_INDEX(@values,'#',-3) |
+---------------------------------+
| 8#9#10                          |
+---------------------------------+

This function takes a string of values, a delimiter and a number n that specifies a position (or index). This index can be either positive or negative. If it is positive, the function returns the portion of the string that contains the first n values. If it is negative, it returns the portion of the string that contains the last ABS(n) values.

That was all I needed to write a generic piece of code that would return the n-th value. First one call SUBSTRING_INDEX to get the portion of the string that contain the first n values, and then a second call to get the last value from the subset!

In this example we extract the 4th value from a string containing animals:

mysql> SET @values = 'cat#dog#horse#parrot#gecko';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@values,'#',4),'#',-1);
+--------------------------------------------------------+
| SUBSTRING_INDEX(SUBSTRING_INDEX(@values,'#',4),'#',-1) |
+--------------------------------------------------------+
| parrot                                                 |
+--------------------------------------------------------+

In general, to select the N-th value from a string VALUES that contains values separated by delimiter DELIM, you have to use:

SUBSTRING_INDEX( SUBSTRING_INDEX( VALUES, DELIM, N ), DELIM, -1 )

I hope this helped! :-)

 

Posted in Uncategorized | 3 Comments

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 | 4 Comments