Archive | open-source RSS feed for this section

How to test Applications using Eclipse ? (Introducing my new book)

16 May

Application testing plays very important role in any development process.  After spending numerous hours on it, I have decided to put what I felt was important in to a short book, which is intended to help developers  to get  started with  application testing using Eclipse.

918Y5X8TILL._AA1500_

The book is very practical, there is almost no theory, just recipes that introduce essential concepts of testing such as Breakpoints and JUnit tests.It is short and strict to the point. You can read it and do all the recipes in one day, and in the end, you will have deep understanding of how to debug your application using Eclipse.

The book is available in paperback as well as ebook formats.

WeightTracker Android App Prototype Demo Video

6 May

Here is one of the applications I was talking about in the last post. This app prototype was created by our team specifically for Ontario Center of Excellence contest. It took us around 3 weeks to develop it. No more words, everything else is in the video. Enjoy :)

 

Java Helper Class: BMI calculator, lb to kg converter, kg to lb converter, feet to cm converter, cm to feet converter

22 Mar

Hey Guy’s,

Long time no seen. Today I want to share with you Java helper class that I have designed for one  of the projects I am working on. This class will be useful for you, if your application works with height and weight conversions, also if you need to calculate Body Mass Index.

My helper class has following functionality:

- Kg to Lb Converter
- Lb to Kg Converter
- BMI (Body Mass Index) Calculator and “Classificator
- Feet to Cm Converter
- Cm to Feet Converter

Here is the class:

import java.text.DecimalFormat;

/**
* This class is used for any calculations connected with Weight and Height
*  - KG to LB converter (and vice versa)
*  - Feet to Cm converter (and vice versa)
*  - BMI Calculation
*
*/
public class HeightWeightHelper {

/**
*
* @param value double that is formatted
* @return double that has 1 decimal place
*/
private double format ( double value) {
  if ( value != 0){
   DecimalFormat df = new DecimalFormat("###.#");
   return Double.valueOf(df.format(value));
 } else {
   return -1;
 }
}

/**
*
* @param lb - pounds
* @return kg rounded to 1 decimal place
*/
public double lbToKgConverter(double lb) {
  return format(lb * 0.45359237 );
}

/**
*
* @param kg - kilograms
* @return lb rounded to 1 decimal place
*/
public double kgToLbConverter(double kg) {
  return format(kg * 2.20462262);
}

/**
*
* @param cm - centimeters
* @return feet rounded to 1 decimal place
*/
public double cmToFeetConverter(double cm) {
  return format(cm * 0.032808399 );
}

/**
*
* @param feet - feet
* @return centimeters rounded to 1 decimal place
*/
public double feetToCmConverter(double feet) {
  return format(feet * 30.48 );
}

/**
*
* @param height in <b>cm</b>
* @param weight in <b>kilograms</b>
* @return BMI index with 1 decimal place
*/
public double getBMIKg (double height, double weight) {
  double meters = height/100;
  return format( weight / Math.pow(meters,2));
}

/**
*
* @param height in <b>feet</b>
* @param weight in <b>pounds</b>
* @return BMI index with 1 decimal place
*/
public double getBMILb (double height, double weight) {
  int inch = (int)(height *12);
  return format((weight*703) / Math.pow(inch, 2));
}

/**
*
* @param bmi (Body Mass Index)
* @return BMI classification based on the bmi number
*/
public String getBMIClassification (double bmi) {

 if (bmi <= 0) return "unknown";
 String classification;

 if (bmi < 18.5) {
  classification = "underweight";
 } else if (bmi < 25) {
  classification = "normal";
 } else if (bmi < 30) {
  classification = "overweight";
 } else {
  classification = "obese";
 }

 return classification;
}

}//end of class

P.S If you want to have JUnit tests for this class let me know, I have them too :)

Regards,

Anatoly

How to setup ADT (Android Development Tools) on 64bit Fedora 17 ?

9 Jan

Quick Intro

Yesterday I was excited to receive a task to install ADT for Eclipse and Android SDK on my machine , I thought it would be a “piece of cake”, so with a high level of excitement I have started….

As I use  my laptop with Fedora 17 (64bit) for all my developments, I have decided that I will put my Android Development Tools in it as well.

Installation…

As my first step I went to the http://developer.android.com/sdk/index.html and thankfully there is ADT bundle for Linux 64-bit, which consists of custom version of Eclipse Juno, equipped with everything one needs for Android development and  Android SDK that includes such tools as Android debugger and Emulator.

After downloading Android SDK I have run Eclipse, and surprisingly, instead of default Eclipse intro, ADT-equipped Eclipse has fancy intro:

adt-intro

ISSUE 1: Android Project Replaced with Android Application Project

After Eclipse has fully loaded, I have decided to create an Android Project. My instructions said that I need to go to File -> New – >”Android Project”. Unfortunately, the version that I have installed does not have option “Android Project”. Closest option to “Android Project” was “Android Application Project”. Here is the screenshot:

Android Development_tools_create_project

After trying to find “Android Project” for at least half an hour, I have discovered that other people have the same issue:

http://stackoverflow.com/questions/11329753/no-android-project-option-in-eclipse
http://stackoverflow.com/questions/11604641/difference-between-android-application-project-and-android-project

Solution:

So my decision was to use “Android Applciation Project”, I have just unchecked option to select launch icon.

I have set Project name, Package name and Activity. Everything seemed to be fine and my project was populated with whole bunch of android related files.

ISSUE 2: R Cannot be resolved to a variable   and adb cannot be found

After project files were generated I have faced my next problem. In my src file I had 2 errors regarding “R” which cannot be resolved. After doing a research again I have found out that even more people have the same issue:

http://stackoverflow.com/questions/7906606/after-installing-android-adt-14-r-cannot-be-resolved
http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error
http://stackoverflow.com/questions/8266100/r-cannot-be-resolved-in-eclipse-android-project
http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable
…………

Most of the advices were to clean and rebuild the project which did not gave me any results. One of the advice was to install ia32-lib, however this library is available for other “distros”, but not Fedora.
After 2 hours of trying different stuff, which did not work, I have found out the cause of the problem.

Reason of the error:

The reason why R cannot be resolved is because R should point to the R.java file, which is auto generated and is located in the gen folder. In our case R.java was not generated, thus Eclipse was not able to resolve it.

Together with “R cannot be resolved” error, Eclipse Error Log gave me one more error which looked like this:

Unexpected exception 'Cannot run program 
"/home/aspektor/adt-bundle-linux-64/sdk/platform-tools/adb": error=2 
No such file or directory' while attempting to get adb version from

It looked to me that these problems are connected.

Solution:

After couple of hours of trying tons of different things, I have realized that the reason of the problem is that Android SDK is initially intended for 32 bits. As I have 64bit Linux I need to install additional 32bit packages to make it work.

Thankfully I have found wonderful guide, that helped me in solving most of my issues:

http://fedoraproject.org/wiki/HOWTO_Setup_Android_Development

I have run this command to install 32bit packages:

# yum install glibc.i686 glibc-devel.i686 libstdc++.i686 
zlib-devel.i686 ncurses-devel.i686 libX11-devel.i686 
libXrender.i686 libXrandr.i686

I have also set up PATH in ~/.bash_profile so adb and other tools can be found:

export ANDROID_SDK_HOME=/home/aspektor/adt-bundle-linux-64/sdk
PATH=$PATH:$HOME/adt-bundle-linux-64/sdk:$HOME/adt-bundle-linux-64/tools
export PATH
# For SDK version r_08 and higher, also add this for adb:
PATH=$PATH:$HOME/adt-bundle-linux64/sdk/platform-tools
export PATH

Finally, I have restarted Eclipse, cleaned and rebuild my project and 5 hours later..... tired but satisfied I have created ADV (emulator) and run my ADT project.

Thankfully it worked:

eclipse-works

After-thoughts…

I think it would be reasonable for Android website to ADVICE (put it in bold and somewhere where everyone could see? ) people who have 64-bit Linux need additional 32 bit libraries. It would save folks like me a lot of time. Not saying that these packages should be per-requisite before downloading bundle :)

If you have any other comments, go ahead, I would love to hear them!

Regards,

Anatoly

Eclipse Platform: Eclipse runs and is usable on GTK+ 3 [STATUS REPORT 20 NOVEMBER]

20 Nov

If you are passionate about SWT as development tool, you will be very excited to know that SWT runs on GTK+ 3 and it is usable! You have probably seen post, couple of weeks ago, that Eclipse starts successfully.  Today I want to show you a screenshot of how Eclipse on GTK+ 3 looks like  after couple of weeks of debugging:

Eclipse screenshot on GTK+3

Image is clickable !

Of course there are number of issues, that should  be resolved, but the progress is obvious!
I will keep you posted :)
Cheers,

Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

Eclipse SWT builds against GTK+ 3 (Status Report 30 October 2012). First look at ControlExample on GTK+ 3

30 Oct

As I have promised before, I’ll be making status reports on migration of SWT to GTK+ 3 more often from now.

Couple of weeks passed, since I’v posted that we are moving with a good speed towards the goal of building against GTK+ 3.0.  And with great pleasure I want to inform you that, last week we were able to build with GTK+ 3.0 and see the shell with Hello World on it.

Today we went even further, and for the first time, I want to show you how ControlExample looks like in GTK+ 3.0 ( to make it work you need to comment out line 108 as BrowserTab produces errors that stops ControlExample to be displayed):

ControlExample picture running on GTK 3

Of course it is far from perfect, but I would say that it is a big step forward considering, that 4 months ago we had bunch of errors (116?) that prevented us from building at all. 

What’s Next ?  Debugging…debugging….debugging….

I will keep you posted :)

Regards,
Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

Eclipse SWT migration to GTK + 3.0 status report: August 2012 – October 2012

16 Oct

Finally, I have decided to give a brief update on where are we standing with migration of SWT from GTK+ 2.0 to GTK+ 3.0.  The reason why I didn’t do  “status report”  earlier is pretty simple – huge amount of work and lack of time.

However, today, after an inspirational morning chat with our Team Lead ( Alexander Kurtakov), and after reading his SWT status reports, I’ve decided to post one of  my own.

Why status starting from August  ?  August was the month when all the “bug-fixing-action” started. Before that, there was some bug fixes from my side as well, but August appeared to be a starting point of major chunk of SWT bugs being fixed.

What was the numbers when I’ve started?

 OVERALL: 

         approximately 180 errors/warnings that needed to be fixed before we can build SWT with GTK+ 3.0

 FROM THEM:

            1. approximately 60 of them were deprecations within GTK+  2.0 library
                       1.1. approximately 30 of them required Cairo graphics library implementation to substitute deprecated methods
                       1.2. approximately 30 of them required substitution with new API methods
            2. approximately 120 of them were deprecations that can be tested only after GTK+ 3  is actually built

ALL of them needed to be version guarded to ensure backward-compatibility

Deadline: In August nobody was sure how much work will be done, but there was a hope that at least some of the errors/warnings will be resolved before January 2013.

What was  my workflow (same workflow persists now) ?

1.  I have opened public GIT repo to store all my bug-fixes
2, I have created solution for every method that is deprecated.
3. For every error/warning, I have opened  separate bug and gave link to my patch in the description of the bug. All bugs are blocked here
3.Patches were reviewed by Eclipse commiters and pushed to Eclipse master branch.

Now you can say:

“ALL THIS IS BORING, GIVE ME SOME NUMBERS! WHERE SWT STANDS NOW?!”

Today is October 16 2012.

In 2,5 months of hard work we have:

- 60 errors/warnings remaining ( 120 errors/warnings resolved)
– Red Hat Eclipse Team Leader  Alexander Kurtakov became SWT commiter
– I got my RHCSA Certificate (off topic but I am still very excited about that fact :) )

Did anyone expect such decline of errors/warnings in such short period?

I don’t think so!

 What’s next ? We are hoping to resolve ALL the issues and build against GTK+ 3 by the middle of December  (pretty good hugh ?)

More detailed SWT October statuses can be found here
Regards,
Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

How to reset root password in Linux (RHEL,FEDORA) ? What to do if you forgot root password ?

18 Sep

Today I want to give you some very useful tips on what to do if you have forgotten your root password.
ATTENTION: This trick will only work if didn’t  setup GRUB password yet.    (if you did – sucks to be you :) )

Use Case:

You want to install something, change permissions on a file  or  do any other action that actually requires root password , which you don’t remember. I would start to panic, If i where you, but wait, there is some small hack you can do, to get back on track.

In this post I will show you how you can become a root without knowing you root password and reset it. I will also give you tips how to protect yourself from this vulnerability.

Let’s start.

1. Boot in Single User Mode

 a. Reboot your computer
 b. Wait until menu say’s to press any key to the Boot/GRUB menu, press any key

   At this point you should see menu similar to this:

grub-menu-image

c. Point to the OS you forgot your  password from and press “e

 
d.  Go to “kernel” line and add word “single” to the end of kernel string.

e. Press “b” to boot with new option for kernel

   Note: If you did something wrong, you could see “black screen of death” but don’t worry, everything you edit in boot menu is temporary, just reboot and you are good to go again.

c. If you did everything correctly you should see command line interface, where you are logged in as “root”.

How cool is that ?

2. Set SELinux to Permissive

a. If you try to change password right away, in RHEL or Fedora you won’t be able to do it because SELinux by default is enforcing.

  Good thing is that we can easily change SELinux enforcing mode. First lets find out the state of SELinux and if it is Enforcing, lets change it to Permissive. Type this in your command line:


$ getenforce

Enforcing

$ setenforce 0

Permissive

4. Change Root Password


$passwd

new password: ******

You will see that all tokens are updated.  Type ‘reboot’, and after you reboot, you will have your NEW root password. (write it down!) :)

But what if someone else would want to change my password in the same way ?

This is very legit question, and it was my first thought when I found out this easy way to change root password. How to protect yourself ? The answer is to put password on Boot menu.

How to lock GRUB/Boot menu ? ( I will show it on GRUB, however GRUB2 works similarly)

Here are a couple of simple steps how to set password on your GRUB:

 1. Open Terminal and login as root:


$ su -

Enter your root password.

2. Now type:

$ grub-md5-crypt

3. Enter password you want your grub (this can be different from your root password)  and click enter you will get md5 encoded string.

Let’s say your  md5 string is:


$1$sdlfksdlfksdlf/

4. Now use your text editor to go to grub.conf file, I use VI:

vi  /boot/grub/grub.conf

after “hidemenu” line enter:

password –md5 < paste here your encrypted md5 string>

(in my case it would be: password –md5 $1$sdlfksdlfksdlf/ )

5. Save the file.

6. To check if it works, try logging to Single User Mode, you will set instead of “e” to edit kernels string, you will need to enter password first.

That’s it for today!

Regards,

Anatoly

SWT: GTK_INCLUDE_INFERIORS worst enemy (Sash Widget)

20 Aug

Good day my fellow Developers and Engineers,

 In my previous post I’ve written about INCLUDE_INFERIORS in Tracker Widget

Today I’ve started working on  Sash Widget’s drawBand() method, and guess what I have found there ?

….”Exactly, my old “friend” OS.GTK_INCLUDE_INFERIORS.”

Why INCLUDE_INFERIORS used in SASH ?

In Sash widget, INCLUDE_INFERIORS call is needed to put stippled rectangle ON TOP of  the parent.handle.

If you try to draw rectangle without INCLUDE_INFERIORS, it appears below the parent.handle, thus you won’t be able to see it.

Hackish solution to GDK INCLUDE_INFERIORS

After “googling” this issue, I have found  this article, that seems very popular, as it is referenced by many websites.

In short, article suggests to create  “cairo” from “gtk_window”, than to create a “surface” that points to this “cairo”, and finally to create new “cairo_new” that uses surface that points to the first “cairo”. Got confused ? Yeah, me too!

Let’s try it again:

 1.  source_surface -> takes as target -> cairo (that references GTK window)
 2.  cairo_new -> uses source_surface (thus drawing on a surface that is on top of cairo)

I think  that this is very hackish way, to achieve effect of INCLUDE_INFERIORS, but it works and I would be fully satisfied with this hack, if I didn’t need to change the color of Sash.

Why Hackish way cannot be used in Sash ?

There are two important things I need to do to replicate  GDK Sash:

1. Make it appear on mouse click
2. Make it stippled or at least make it in some neat light grey colour. ( as stippling is considered outdated , maybe proposal of light grey would be accepted upstream)

Make it appear on mouse click is done by replacing:


OS.gdk_gc_set_function (gc, OS.GDK_XOR);

  with


Cairo.cairo_set_operator(cairo, Cairo.CAIRO_OPERATOR_DIFFERENCE);

Point number 2  is done in Cairo by changing source rgb:


Cairo.cairo_set_source_rgb(cairo, 1, 1, 1);

In theory, to change color of Sash to  light grey, I would just need  to change  RGB number to something like “0.3,0.3,0.3″ in the function above, and  celebrate my success.

 Unfortunately,  hackish way of replicating INCLUDE_INFERIORS, will not allow me to change RGB in this case. As I have said above, we are using cairo as target_surface, thus when I change color of Sash, it will change color of handle itself, and it is definitely not the way, I need it to behave.

Want to try it yourself and get updates on this issue ?

If you want to try it yourself:

http://fedorapeople.org/cgit/aspektor/public_git/eclipse.platform.swt.git/commit/?h=cairo_sash

I have opened a bug regarding this issue. Fastest way to get updates on this issue is to include yourself to CC list in this bug:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=387641

I will also post solution here, so everyone could use it, when this issue is resolved.

Cheers,

Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

SWT migration to GTK+ 3.x : Tracker Dilliemma

17 Jul

Couple of weeks I was trying to solve interesting dilemma, so I decided to share it with you.

 I have submitted patch regarding Tracker widget, where I implement Cairo instead of GDK deprecated methods, more about it here .

However, I got a response from Eclipse that it does not work under one condition:

 !! When you pass DISPLAY instead of SHELL as a surface on which Tracker should operate, Tracker does not produce any output.

Sounds like an “easy-breezy” thing to do, but actually it is not.

Let’s look at Tracker Code again and I will explain why:


void drawRectangles (Rectangle [] rects) {
int   window = OS.gdk_get_default_root_window();
if (parent != null) {
window = OS.GTK_WIDGET_WINDOW (parent.paintHandle());
}
if (window == 0) return;
// My Patch
if(OS.USE_CAIRO){
int  cairo = OS.gdk_cairo_create(window);
if (cairo == 0) error (SWT.ERROR_NO_HANDLES);
Cairo.cairo_set_source_rgb(cairo, 1, 1, 1);
Cairo.cairo_set_line_width(cairo, 1);
Cairo.cairo_set_operator(cairo,Cairo.CAIRO_OPERATOR_DIFFERENCE);
Cairo.cairo_set_antialias(cairo, Cairo.CAIRO_ANTIALIAS_NONE);

for (int i=0; i
Rectangle rect = rects [i];
Cairo.cairo_rectangle (cairo, rect.x, rect.y, rect.width, rect.height);
Cairo.cairo_stroke(cairo);
}
Cairo.cairo_destroy(cairo);
return;
}
int gc = OS.gdk_gc_new (window);
if (gc == 0) return;
int /*long*/ colormap = OS.gdk_colormap_get_system ();
GdkColor color = new GdkColor ();
OS.gdk_color_white (colormap, color);
OS.gdk_gc_set_foreground (gc, color);
OS.gdk_gc_set_subwindow (gc, OS.GDK_INCLUDE_INFERIORS);
OS.gdk_gc_set_function (gc, OS.GDK_XOR);
for (int i=0; i
Rectangle rect = rects [i];
int x = rect.x;
if (parent != null && (parent.style & SWT.MIRRORED) != 0) x = parent.getClientWidth () - rect.width - x;
OS.gdk_draw_rectangle (window, gc, 0, x, rect.y, rect.width, rect.height);
}
OS.g_object_unref (gc);
}

When display is passed to tracker, “parent” variable is null so:

int   window = OS.gdk_get_default_root_window();

is triggered.

Problem is: GDK uses native X11 library, and it allows to draw on root_window (in GNOME, in KDE is not allowed to draw on root), however when you try to do it with Cairo all drawings are clipped by inferiors.

To avoid clipping  GDK uses “wonderful” function:

OS.gdk_gc_set_subwindow (gc, OS.GDK_INCLUDE_INFERIORS);

Unfortunately for me, this function is deprecated and should not be used in newly written code. Also after contacting GTK+ maintainers, I have found out that  drawing on the root window is considered very bad practice, and should not be done.

After some brainstorming, I was advised to create a TRANSPARENT GTK_WINDOW with size of the rectangle, draw rectangle on this transparent window, and move this window depending on the  rectangle coordinates that are passed to the function.. Also I need to use the same XOR method to make rectangle inside the window disappear on mouse-release event, as it happens in the original GDK.

 All of this sounds very logical and appealing. However I am facing another set of challenges.

  •      To make window transparent I need to overwrite “draw” signal, which is not implemented  in SWT, and who knows, if such operation is even allowed in SWT?
  •       GDK way of dealing with rectangles was to redraw rectangle every time mouse moves. As now I have “window” appearing every when method is called – I need to close/reopen window every time, which also is a problem, as I need to find out, If window is opened? and than close it. Much better decision in this case would be to draw one window, and not to redraw it, but than I need to change implementation of the class.

And last question: If I change this implementation, what will happen to the working “when-shell-passed” solution ?

A lot of question and no answers yet….

If you know better solution, or if you have any suggestions regarding this case – please put them as the comment to this post, I appreciate any effort!

I will keep you updated!

Regards,

Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

SWT migration To GTK+3.x : 6 SWT – Cairo Patches (Eclipse Bugzilla)

15 Jun

I am glad that you guys showed big interest to the project I am currently working on.  I have received couple of emails from blog readers asking to share patches they I have pushed upstream.

First of all, I want to mention that patches are still in “pending” stage, but I hope that they will be reviewed very soon.

For those who want to go ahead, and try   them before they are officially reviewed by Eclipse community, here is the link to 6 SWT patches I have pushed to upstream so far:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=380287
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382384
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382391
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382402
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382407
https://bugs.eclipse.org/bugs/show_bug.cgi?id=382626

If you find anything that you think is wrong, please let me know.

Any feedback is appreciated!

Regards,

Anatoly

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

SWT migration to GTK+ 3.x: Use Cairo Instead GDK : First Patch – Tracker Widget

7 Jun

I am continuing series of posts about   SWT migration to GTK+ 3.x . Today, I will  write about patch for SWT Tracker Widget, that omits some of GTK+ 3.x deprecated methods.

When I started working on patch for Tracker, it was very important for me understand what Tracker Widget is intended to do. I found very good implementation of  SWT Tracker widget   here 

Actual code is:


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tracker;

public class Tracker_test {

public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.open();
shell.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
Tracker tracker = new Tracker(shell, SWT.NONE);
tracker.setRectangles(new Rectangle[] { new Rectangle(e.x, e.y, 100, 100), });
tracker.open();
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

When you run this code, you see small rectangle. It appears when you  right-mouse click and disappears when you  release right-mouse click. It looks like this:

From this example, we can see that Tracker is intended to track mouse movements and mouse clicks. It works great, the only problem is that some code in Tracker Widget is outdated, and it won’t built with GTK+ 3.x. I am talking about code in drawRectangles() method inside Tracker.java.

Let’s look at this code:


void drawRectangles (Rectangle [] rects) {
int   window = OS.gdk_get_default_root_window();
if (parent != null) {
window = OS.GTK_WIDGET_WINDOW (parent.paintHandle());
}
if (window == 0) return;
//TODO: Use Cairo
int gc = OS.gdk_gc_new (window);
if (gc == 0) return;
int /*long*/ colormap = OS.gdk_colormap_get_system ();
GdkColor color = new GdkColor ();
OS.gdk_color_white (colormap, color);
OS.gdk_gc_set_foreground (gc, color);
OS.gdk_gc_set_subwindow (gc, OS.GDK_INCLUDE_INFERIORS);
OS.gdk_gc_set_function (gc, OS.GDK_XOR);
for (int i=0; i
Rectangle rect = rects [i];
int x = rect.x;
if (parent != null && (parent.style & SWT.MIRRORED) != 0) x = parent.getClientWidth () - rect.width - x;
OS.gdk_draw_rectangle (window, gc, 0, x, rect.y, rect.width, rect.height);
}
OS.g_object_unref (gc);
}

If we take a look at  GDK documentation , we will find out that gdk_gc_newgdk_color_white gdk_gc_set_foreground, gdk_gc_set_subwindow, gdk_gc_set_function and even  gdk_draw_rectangle are all deprecated since GTK version 2.22 and should not be used in newly written code. And as a hint, there is ‘ // TODO: Use Cairo’.

So I here is some of the changes I made to this code:

1. I replaced gdk_gc_new  with  gdk_cairo_create

2. OS.gdk_gc_set_function (gc, OS.GDK_XOR)  was replaced with equivalent function  Cairo.cairo_set_operator(cairo,Cairo.CAIRO_OPERATOR_DIFFERENCE)  (this function is responsible for rectangle to disappear on mouse release)

3. gdk_draw_rectangle was replaced with cairo_rectangle, as cairo saves everything to buffer first, you need to release changes. In my case I used cairo_stroke. ( it fills just contour of rectangle)

4. gdk_color_white was replaced with  cairo_set_source_rgb(cairo, 1, 1, 1);

5. I also had to add line width,  and antialising (without antialising  line width didn’t work)

So my code looks like this:

if(OS.USE_CAIRO){
int /*long*/ cairo = OS.gdk_cairo_create(window);
if (cairo == 0) error (SWT.ERROR_NO_HANDLES);

Cairo.cairo_set_source_rgb(cairo, 1, 1, 1);
Cairo.cairo_set_line_width(cairo, 1);
Cairo.cairo_set_operator(cairo,Cairo.CAIRO_OPERATOR_DIFFERENCE);
Cairo.cairo_set_antialias(cairo, Cairo.CAIRO_ANTIALIAS_NONE);

for (int i=0; i<rects.length; i++) {
Rectangle rect = rects [i];
Cairo.cairo_rectangle (cairo, rect.x, rect.y, rect.width, rect.height);
Cairo.cairo_stroke(cairo);
}
Cairo.cairo_destroy(cairo);
return;
}

After run with Cairo, I have the same output as GTK version does.

I have posted bug report with my patch here:  https://bugs.eclipse.org/bugs/show_bug.cgi?id=380287  ( As for June 7, patch is not reviewed yet, as Eclipse reviewers are extra-busy with new version of Eclipse coming out very soon)
I have coded couple of other patches, and I will definitely do some posts on them after this first patch is reviewed.

Regards,

Anatoly

Update:

Unfortunately there is one case where current patch does not work properly, as it supposed to. This case is when you pass “display” to the Tracker constructor. In GDK version it is supposed to use GDK_INCLUDE_INFERIORS that puts rectangle on top of all other windows, however Cairo does not have this luxury, and all my attempts to put in on top is clipped by child windows.

I keep on working on it, and I will keep you updated!

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

SWT migration to GTK+ 3.x : What is GDK ? What is GIMP ?

23 May

This is third post related to my new project – migration of SWT from GTK+ 2.x to GTK+ 3.x .

In order to successfully build SWT with GTK+ 3.x all deprecations must be resolved. Big chunk of deprecated methods are GDK methods.

What is GDK ?

GDK is GIMP Drawing Kit. Wikipedia states that GDK  “is a computer graphics library that acts as a wrapper around the low-level drawing and windowing functions provided by the underlying graphics system….GDK lies between the X server and the GTK+ library, handling basic rendering such as drawing primitives, raster graphics (bitmaps), cursors, fonts, as well as window events and drag-and-drop functionality.” ( more on GDK here )

GDK has a lot of tools for most of the use cases  that one might need. I have found very informative GDK reference manual on their website .

What is GIMP ?

If GDK is GIMP Drawing Kit, it would be important to find out – What is GIMP. Thankfully GIMP has huge website that describes what it does in many languages. If you are interested, please visit   http://www.gimp.org  to find out more.

Making story short – GIMP is open-source drawing software used for image editing, free-form drawing and other manipulation with images and photos.

GIMP Screenshot

Image is taken from: http://redskiesatnight.com

GDK was originally developed for GIMP, however  it is used in many  places including SWT as GIMP toolkit called GTK+. With new release of GTK+, several steps needed to be taken to successfully migrate from old GTK+ 2.x to new GTK+ 3.x. As I have already mentioned -  one of the major steps is to implement Cairo library for drawing instead of deprecated GDK methods.

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

SWT migration from GTK+ 2.x to GTK+ 3.x : Introduction

17 May

In the earlier post I was exploring Standard Widget Toolkit. I have created simple application to see how SWT works. In this post I want to show couple of reasons, why SWT cannot be easily migrated from GTK+ 2.x to GTK 3.x .

Let’s approach this issue step by step.

What is GTK + ?   

As I have found out from GTK+ Project website (http://www.gtk.org/)   ” GTK+, or the GIMP Toolkit, is a multi-platform toolkit for creating graphical user interfaces. Offering a complete set of widgets, GTK+ is suitable for projects ranging from small one-off tools to complete application suites.” GTK+ supports most widely used programming languages, thus making it one of the main tools  in User Interface development.

How GTK+ is connected with SWT ?

As I have found out, GTK+ library is widely used in SWT.

How to build SWT with GTK+ 2.x ?

I have pulled SWT source code using tutorial  from here   and built it using following commands:


export JAVA_HOME=/usr/lib/jvm/java

./git/eclipse.platform.swt/bundles/org.eclipse.swt/bin/library

sudo ./build.sh

Everything built just fine.  However, problems started when I tried to built it with GTK+ 3.x.

How to build SWT with GTK+ 3.x ?

To be able to build SWT with GTK+ 3.x I have uncommented line


#define GDK_DISABLE_DEPRECATED

in:


./git/eclipse.platform.swt/bundles/org.eclipse.swt/Eclipse SWT PI/gtk/os.h

After doing it, I tried to built again. Unfortunately this time built was not succesful.

Here is the sample of the output:

Building SWT/GTK+ for linux x86
gcc -O -Wall -DSWT_VERSION=4230  -DLINUX -DGTK -I/usr/lib/jvm/java/include -I/usr/lib/jvm/java/include/linux -fPIC  `pkg-config --cflags gtk+-2.0` -c os.c
os.c: In function ‘Java_org_eclipse_swt_internal_gtk_OS__1gdk_1bitmap_1create_1from_1data’:
os.c:4930:2: warning: implicit declaration of function ‘gdk_bitmap_create_from_data’ [-Wimplicit-function-declaration]
os.c: In function ‘Java_org_eclipse_swt_internal_gtk_OS__1gdk_1color_1white’:
os.c:5078:2: warning: implicit declaration of function ‘gdk_color_white’ [-Wimplicit-function-declaration]
os.c: In function ‘Java_org_eclipse_swt_internal_gtk_OS__1gdk_1draw_1drawable’:
os.c:5273:2: warning: implicit declaration of function ‘gdk_draw_drawable’ [-Wimplicit-function-declaration]
........

Why SWT cannot be built with GTK+ 3.x ?

After small research I have found out that some of the methods of GTK library that SWT uses are deprecated.
Complete guide on what need to be change to migrate from GTK+ 2.x to GTK+ 3.x can be found here .

Most  depreciation are already resolved, except  ones that are supposed to  use Cairo graphics library  for drawing. Next couple of months, I will be working  on implementing Cairo library into SWT instead of deprecated gdk methods, so it could be successfully migrated to GTK+3.x.

More about Cairo in my next posts…

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

What is Standard Widget Toolkit for Eclipse (SWT) ?

15 May

Here I am at my new workplace. Next 8 months I will be working in Eclipse team, on migration of SWT  from GTK2 to GTK3.

If these words does not tell you much, don’t worry, I am pretty much in the same situation, but what I am going to do in the next couple of days is to explore this issue as much as I can, and  write down my findings here.

Let’s start with SWT.  My first stop in finding info about SWT was here: http://www.eclipsepluginsite.com/swt.html

After reading  introductory chapter,  I have  found out that SWT stands for   Standard Widget Toolkit.  As I have understood from the reading, SWT is a collection of tools, intended for creating stand alone java applications or eclipse plugins  with basic UI components (such as buttons, trees etc). What is  special about SWT, is that it uses native OS components, so when you build application with SWT it feels like this application is intended for the particular OS.

I decided to build a simple application using one of the examples provided in the reading.

Simple SWT Application

/*
 *  Name: BlogButton.java
 *  Description:  SWT  button on click changes text and background color
 *
 *  Author: Anatoly Spektor
 *  Date: May 14,2012
 * */

import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.*;

public class BlogButton {

public static void main(String[] args) {
 // creating new object of Display myDisplay
 //in SWT Display manages connection between app and OS
 final Display myDisplay = new Display();
 // creating instance of shell - which is actual window
 Shell myShell = new Shell(myDisplay);

// setting window title
 myShell.setText("Window Title");
 //setting size
 myShell.setSize(400,400);
 // setting layout
 myShell.setLayout(new FillLayout());
 // creating button object in myShell
 // passing PUSH behaviour
 final Button button = new Button(myShell, SWT.PUSH);
 // passing button label
 button.setText("Show Blog URL");

// adding event listener to button
 button.addSelectionListener(new SelectionAdapter() {
 public void widgetSelected(SelectionEvent event) {
 //on selected change button label
 button.setText("myprogrammingblog.com");
 // set text color to red
 button.setForeground(myDisplay.getSystemColor(SWT.COLOR_RED));

}
 });

// open - pushes shell on the top of drawing order and makes it visible
 myShell.open();

// checking if shell is Disposed or not
 while (!myShell.isDisposed()) {
 // check if there is work to do for this shell and if not
 // put it in sleep mode so no CPU is consumed
 if (!myDisplay.readAndDispatch()) myDisplay.sleep();
 }
 // disposing an object
 myDisplay.dispose();
 }
 }

Output:

Before Click:
SWT button before click

After Click:

SWT button after click snapshot

 

Check out more posts on “SWT migration From GTK+ 2 to GTK+ 3″

My new work: Software Developer – Red Hat

8 May

Now I will be working as Software Developer in Red Hat.   For the blog readers it means that along with the tutorials, and other stuff,  I will have Red Hat related posts. I am sure that this experience will be beneficial not only for me, but for all the blog readers, as I will definitely share my findings with you.

My official portfolio: anatolyspektor.com (Anatoly Spektor Official Web Site)

8 May

Hello guys,

As I had a week off from my studies and work, I decided to do something useful. Many of you asked, why I don’t write about myself. So I  have created a small portfolio, where  anyone  can find some info about me and my work. If you are interested – please go to http://www.anatolyspektor.com

 

Any feedback is appreciated!

:)

[VIDEO] Polling Module Final Version (Big Blue Button)

24 Apr

The day a lot of people waited for has come. We finally are releasing our module to public. Here is the video that shows all its features.

 

Check out more posts on “Big Blue Button Polling Module”

Java – How to format string using SampleDateFormat ?

6 Apr

One of the blog readers asked me this question, how to format string using SampleDateFormat to mm/dd/yyy.

Here is a small piece of code, I hope some of you will find it useful.

a. Frist – import 2 libraries:


import java.text.SimpleDateFormat;
import java.util.Date;

b. Here is code:


Date date = new Date(); // creating date object
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm a");
// creating SampleDate object and passing the pattern to constructor ("a" means show AM or PM)
System.out.println( "Here is today's date: " +  formatter.format(date) + ");

//The output: Here is today's date: 04/06/12 10:52 AM

Regards,

Anatoly Spektor

Java: How to split Java String with delimeter comma, space, new line, tab ? [SOLVED]

20 Feb

Sometimes it is very important to split a string on a comma \n \t etc. This piece of code will help you with it.


import java.util.*;

 public class split{

  public  static void main ( String args[]) {
       // this is my string
	String s = "Java,	Programming	is ,,, fun do you agree? ."; split
	String regexp = "[\\s,;\\n\\t]+"; // these are my delimiters
	String [] tokens; // here i will save tokens
	// length is 7 because i know that my string will consist of 7 tokens
	for(int i=0;i<7;i++){
		tokens = s.split(regexp);
		System.out.println(tokens[i]);
	}
   }
}

 // tokens are: Java Programming is fun do you agree?

Check out more posts on “Useful Java Functions and Tutorials”

Follow

Get every new post delivered to your Inbox.

Join 272 other followers

%d bloggers like this: