2009-05-27

Problem Silverlight: Micro$oft owns your computer


Intro
I am using official version of M$ Windows XP at work, and all the day yesterday I fought with it's updates. Yes, I am talking about Silverlight. I do not care about other possible updates as long as they have no impact on my work. But this one... "Why not?" you will ask... Well, I have few reasons.

1. This computer is MINE!
It installed without my approve. Just downloaded and made ready to install. And more, when I am checking box "Don't notify me about this updates again" it waits about 5 minutes, than downloads it again and installs it again. I can't cancel or forbid installation of this package. Just can't. I have no control over own operation system! I can put TABOO on any package on my SUSE Linux box, but I can't make Windows ignore this peace of "optional" crap.


2. User-agent of browsers
When silverlight is installed - it changes user-agent string of web browser to include itself. M$ already changed user-agent of my Firefox to this one:
Mozilla/5.0 (Windows; U; Windows NT 5.1; uk; rv:1.9) Gecko/2008052906 Firefox/3.0 (.NET CLR 3.5.30729)

What for? Someone is using those dirty techniques to determine silverlight or .NET installed on my machine? And why M$ hacks my browsers without my permission to do it? I can't agree with such politics.

3. Politics
Development of Silverlight looks like politics solution from my point of view. If you can remember history of Netscape - you will see that M$ always releases own products of same kind after they're losing market. Market is totally occupied by Adobe with it's products and experience with graphical applications. M$ will easily take market back with such "optional" features as Silverlight.

4. Religion
I am Java developer. And being Java developer I can see that no one believes in desktop java apps because of JRE on client's computer. Do you really think that JRE is harder to install than Flash Player? No it's not. But "everyone has a flash player, but no one has JRE installed" is already mature religion in most countries. So that will be my own religion - if something is written in silverlight - I don't need it with it's crappy silverlights.

2009-05-07

Java and music player


Today I found post on elliotth's blog about Java and MP3. Being developer of Xine engine for aTunes I have own thoughts about it.

Step 1: Target users
At first you should decide who will be your users. Depending on this choice you have different amount of possibilities. For example, Qt Jambi with Phonon will fit for all systems, while Xine is not available for M$Windows users.

Step 2: Engine
Second part of music player is engine. I am long time SUSE user and I prefer Xine over other because it ships with SUSE and all best KDE apps are using it as engine. Also popular MPlayer, VLC and Gstreamer. Of course I hate Gstreamer because of strange format support politics and trashy sound, but it's your own choice.

Step 3: How to use engine
After you decided what engine you want to use - there is a question how to use it.

a) JNI
You can use some wrapper generator like SWIG to generate wrappers and use it from your application. While this is the fastest way to invoke engine - there are few possible problems:
1. You have to build your application for all platforms yourself. It can be hard enough if you have no compilers for target platform and you have never used some of target platforms, or you have only 32bit CPU.
2. Static vs dynamic linking. I am not C/C++ guru, but I noticed that some apps build on other systems than mine can't find libs from system folders like /usr/lib. It's easy to understand that there are some linking problems for target platform depending on libs path or libs versions.

b) JNA
It's my favorite. JNA guys did a great job. They solved all possible problems in JNI approach, building jna.jar for all platforms and supporting dynamic linking. The only problem - mostly you have to read *.h files and write classes to wrap them. It is simple if you know a bit of C/C++.

c) cmd line
You always can invoke your engine from command line. MPlayer have it's own command line interface, so you can just send commands and read responses from Process stream. Also, Amarok supports commands like "amarok -s" to stop. May be your favorite player also does? Of course you should give user to choose path to your supported engine, while Linux platforms can be found in $PATH it will not work on M$Windows.

d) Use Qt Jambi
I already wrote about Qt Jambi. It's great solution for any desktop application, not just music player. And it have it's own media API, that can be found in examples. I suggest you to download and take a look yourself.

e) choose from existing wrappers
Many engines have it's own wrappers already. Here are some of them:

2009-03-06

Chaos Obliterators handmade

There are only two metal models of Obliterators on Games-Workshop. I really hate metal models. That's why I took Chaos Terminators box and pack of greenstuff to make my own obliterators.

Obliterator-virus is technovirus by fluff, and I can't understand why original obliterators are covered with skin like some Nurglish followers. Skin should be visible in places of technology attachments as for me, but not to cover all the huge terminator armour.

This guy got additional inputs into his brain, in addition to all his weapons in right arm and "power fist" made from terminator "possessed arm" and arm of possessed marine:


Head of this guy is covered with small datacables, and he got servo arm somewhere. His "power fist" is a combination of terminator's shoulder and possessed arm to:


Once this guy was terminator, and got obliterator's bionic. Now he just can't get out of his terminator armour and have plenty of weapon to use:



2009-02-26

Qt Jambi first experience


Being long time KDE user and Java developer - I couldn't ignore Qt Jambi release. Of course I downloaded it and tried as soon as version 1.0 vas released. Yes, at first it was version 1.0, built with Qt 4.3. Only after some time Jambi got version as its Qt.

I was impressed even with first version, but it seemed a bit slow and not yet ready for every day usage. But last week there were some news about Qt 4.5 and LGPL license, so I downloaded last available version of Jambi and started few example apps.

And now I am really impressed! I used to think about Swing as fast and developer-friendly when developing some desktop applications, but now I know Qt is really two heads higher in desktop apps development. Jambi evolved and now there is no problem of setting LD_LIBRARY_PATH and other params, you can simply include jar file with Qt and Jambi libs to your classpath.

Then I started a bit bigger project to have complete application in Jambi. As long as there is no fast and stable notepads for Windows I wrote QEdit. You can see sources here.

Now, after I have some first experience with Jambi I can compare Jambi and Swing:

+ Jambi has better shortcuts system. You don't have to work with root panes and actionmaps, you can just ass some shortcut to your action.
+ QStatusBar. I thought about writing something similar for Swing few month ago, but here you can assign statusTip to your action and that tip will appear in status bar on rollover.
+ QSettings. It is much more usable than Properties for storing your settings.
+ QToolBar. Even if it is possible to do something similar in Swing - you will never spend so much time to do it like Qt can. You can download and run QEdit to see how you can move toolbars to different parts of main window and this position will be saved between sessions.
+ QFontDialog. Almost every Swing application have to implement Font Chooser themselves, while QFontDialog is almost the same in every application.
+ Components already have their scrollbars, you can just enable or disable them. I can't remember at least one case when you don't need to wrap JTextArea into JScrollPane.
+ Wide range of supported graphic formats and components adjust size of graphic automatically.
- Jambi can't see if wrapped C++ classes extend some other classes.
- Jambi QFile is very slow and locking file. I don't like when File object locks real file on file system, only io operation should block it. So don't try to use Qt Jambi for all your tasks at once.
- No way to extend or customize QFileDialog. Even style is not applied to QFileDialog.
- Signals in "action system"... It's just wrong in Java. Thanks that application fails on first start but not in time of invocation if something is wrong in source.
- You can't give both small and large icon to QAction. After providing 22x22 icons some of them looks too smooth in main menu.
- Lack of documentation. If you are not enough documentation and examples from Jambi distribution - even Google will not give you a lot.
- Looks like all the work is performed in blocking QApplication.exec() method. I can't find is there anything like SwingWorker for Qt or Jambi...
- There are lots of possible pitfalls when you are writing huge application, and Swing "experience base" is huge enough to help you in almost everything, plus Swing is pure Java where you can debug everything you want. But where to go with Jambi? There is support in Trolltech for commercial version...

2009-02-16

Java is faster than native


There is a myth that native is much faster than java. Lots of frameworks speculate this myth and using lots of native methods from java. But how often they are implementing same functionality and check native against true-java? They are often diving so deep in own fantasies about native performance that it is almost religion, they just don't accept facts. But how much it takes to JVM to invoke method from native library? And how much is it comparing to raw java interpretation?

I don't like to use things that I don't understand. Almost all methods from Math class is native, and I can't see sources of that classes. There are some functions that would be hard to implement, like Math.sin() or Math.cos(), but there are some easy-to-implement methods. This time I need functionality of method Math.pow(). It is not hard to implement it in java:

private double pow(double value, double pow) {
  double result = value;
  for (int i = 1; i < pow; i++) {
    result = result * value;
  }
  return result;
}

And it is not hard to implement some raw test using case from current developed application:

private static final int times = 1000000;
public static void main(String[] args) {
  // running test 10 times to "hotspot" it
  for (int i = 0; i < 10; i++) {
    // I know, I know, you can never
    // preallocate long, but I like it:
    long start = 0;
    long end = 0;
    start = System.currentTimeMillis();
    for (int j = 0; j < times; j++) {
      pow(10, 10);
    }
    end = System.currentTimeMillis();
    System.out.println("pow: " + (end - start));
    // cleaning JVM every time before next cycle:
    System.gc();
    System.runFinalization();
    System.gc();
    start = System.currentTimeMillis();
    for (int j = 0; j < times; j++) {
      Math.pow(10, 10);
    }
    end = System.currentTimeMillis();
    System.out.println("math: " + (end - start));
    System.gc();
    System.runFinalization();
    System.gc();
    // and running once more to be sure in hotspot:
    start = System.currentTimeMillis();
    for (int j = 0; j < times; j++) {
      pow(10, 10);
    }
    end = System.currentTimeMillis();
    System.out.println("pow: " + (end - start));
    System.gc();
    System.runFinalization();
    System.gc();
    start = System.currentTimeMillis();
    for (int j = 0; j < times; j++) {
      Math.pow(10, 10);
    }
    end = System.currentTimeMillis();
    System.out.println("math: " + (end - start));
    System.gc();
    System.runFinalization();
    System.gc();
  }
}


I just wanted to figure out how slow is my implementation, but results were totally unexpected!

run-single:
pow: 63
math: 797
pow: 78
math: 797
pow: 78
math: 781
pow: 78
math: 781
pow: 63
math: 781
pow: 63
math: 781
pow: 63
math: 797
pow: 63
math: 797
pow: 63
math: 797
pow: 63
math: 782

It's 10 times faster than Math.pow() when raising power of 10!!

Think different. Think faster!

2009-02-12

Chaos Dreadnought conversion


Games Workshop produces two kinds of kits - metal and plastic. I hate metal kits - it is hard to convert them, hard to keep them unbroken, hard to repair them if they're broken. And Chaos Dreadnought is metal model.

Modeling Plastic Chaos Dreadnought!
All you need - Plastic Space Marines Dreadnought, few Terminator torso bits and few decorative chaos bits. And may be some greenstuff.





2008-11-07

Swing and TreeTable component


TreeTable combines functionality of two components - Tree and Table. And it is really comfortable to show some hierarchical information like file system structure or many others.
TreeTable have great Qt implementation, it's fast, useful and pretty. It's a pity there is no standard Swing implementation of this component. But there are lots of third-party implementations. Let’s review them:

JEdit

JEdit uses TreeTable component for Open-Save dialog. It is extremely fast and pretty. But they did it for their own, with package-private constructor and double-linked to other JEdit components. To use it in other projects means to rewrite it almost from scratch, so JEdit is just a good demo of TreeTable possibilities for Swing.



SwingX

SwingLabs has their implementation. It is feature-rich and customizable. But it has some rendering bugs, much slower than JEdit version, amount of code is too big to extend it yourself and it linked a lot to other SwingX packages.


NetBeans

As Tim Boudreau told us, NetBeans only recently got a good implementation of TreeTable. And this implementation looks great! It works almost as fast as JEdit one, has no rendering bugs, needs only few classes to build it (but not all the framework as previous two implementations) and it's easy to use and customize. But one more problem - you may use it only under either GPL2 or CDDL, and you need to download NetBeans sources and copy org.netbeans.swing.outline package to your project to use it.



All of them extends JTable. I think it's not good, JTable is the biggest class I have ever seen, about 9000 lines of code, and SwingX implementation adds another 4000 lines. It is hard to debug and hard to customize to internals. Swing is so big because backward compatibility, but what sense of JTable compatibility for totally new component? How do you think, may be it is time to write Tree, Table and TreeTable LGPL implementation by ourselves using best practices from core swing, SwingX, JEdit and NetBeans?

2008-10-07

Comparing two collections


There is standard task when you have GUI table and database table, and you need to merge differences.
Most of people I know will do it like this:


Set source; // set of records from UI
Set target; // set of records in database
for (Object o : source) {
    // if we have object in source without
    // same object in target - it was added by user:
    if (!target.contains(o)) {
        target.add(o);
    }
}
Iterator iterator = target.iterator();
while (iterator.hasNext()) {
    Object o = iterator.next();
    // if we have object in target without
    // same object in source - it was removed by user:
    if (!source.contains(o)) {
        iterator.remove();
    }
}


We have 2 full collection iterations. In most cases it will not cause big performance issue. But it looks bad and will cause performance problems with slow Collection implementations or with big amounts of data.

After using my brain few minutes I found the best way of comparing, with only one iteration:


Set source; // set of records from UI
Set target; // set of records in database
// just checking:
if (target.equals(source)) {
    return;
}
Iterator iterator = target.iterator();
while (iterator.hasNext()) {
    Object o = iterator.next();
    if (source.contains(o)) {
        // remove all checked objects for future
        source.remove(o);
    }
    else {
        // if source does not contains object -
        // it was removed by user, removing it from target:
        iterator.remove();
    }
}
// all object left in source are added by user.
// Adding them to target:
target.addAll(source);


Simple enough, isn't it?