Posts Tagged ‘eclipse’

20090825 “is the CPU pegged?”, and friends

Lessons I learnt today

is the CPU pegged?

When is the CPU pegged? message shows up in the LogCat console tab, it means you are not filling in quickly enough the audio buffer for an AudioTrack. Or that it seems so. I happened to locate where the message is in Android’s source code, but since I am not familiar at all with it, I am just doing a quick guess which was pseudo-confirmed when the messages stopped as soon as I began refilling quicker the audio buffer.

Thread.stop is deprecated – and it will be ignored!

Docs say:

This method is deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state.

As to why is that deprecated on the basis that using it can cause stability issues, I am afraid I can’t elucidate the answer yet.

I am using an strange solution where I add a method called doStop to the class that extends Thread, with the intention that it sets an internal boolean bar (mIsRunning) to false in that case, and returns from its run method as soon as mIsRunning is false. The reason why I am using this is because I saw that in an SDK tutorial, but still feels a bit strange.

Incidentally I also took the opportunity to refresh my mind regarding Java threads, which had been much forgotten.

Sometimes LogCat stops reporting messages

Close and reopen the emulator and Eclipse and it will be sorted out.

Things I still need to investigate

  • Need to find out a better way to determine if a buffer needs audio data. I am missing some kind of callback, SDL style. Currently I am just pushing audio data using a thread which sleeps for a certain amount of time, but I sometimes get AudioFlinger – write blocked for xxx ms messages if the buffer is not sufficiently empty
  • What’s more efficient and in which cases, regarding the use of integers, shorts, floats… I am using a LookUpTable for storing precalculated Math.sin values but I am unsure if I am optimizing too early…
  • Would love to find more AudioTrack examples and documentation. There’s an impressive scarcity of resources about AudioTrack while there’s a cornucopia of samples for MediaPlayer, JetPlayer and etc. AudioTrack must be feeling discriminated: there is not even an AudioTrack sample program in the official SDK samples!

Edit: added the link to Java Threads tutorial which I forgot to add.

20090317 Eclipse tricks

Shortcuts

These are the ones I use more often and that immediately come to my mind right now. I have ignored the usual CTRL+C, CTRL+V ones ;-)

CTRL + Space
code sense: a little dropdown will show up, with available functions/variables depending on what you have already written
Alt + /
They call it autocomplete, I would call it suggest. It’s not exactly the same than code sense. Try it out.
CTRL + F
Search in the currently edited file
CTRL + H
Global search (in the project, workspace, etc)
CTRL + D
Delete the current line

Searching and replacing

To my surprise, most Eclipse users I have spoken to don’t use the more advanced options from the Find/Replace dialog, when it can save you some precious time.

  • I always enable the Wrap search check box, so that I don’t have to worry whether I’m above or after the word I’m looking for.
  • The Incremental check box might be useful sometimes. If you enable it, when you begin typing in the text you’re looking for, it will begin searching at the same time (it won’t wait for you to press the Find button). It’s similar to Firefox’s Search as you type feature.

But the crown jewel is the Regular expressions check box. Regexp’s can save you literally hours of agonising and boring manual replacements. I’ll demonstrate you why with a simple example:

Imagine we have a php file where we’re accessing the members of an associative array. Something along the lines of this:

<?php
$user['name'] = $user['first_name'] . ' ' . $user['last_name'];
echo $user['name'];
?>

Then one day you need to replace those associative arrays with objects. The above code will no longer be valid and you’ll need to update it manually.

It won’t matter too much if you’re just updating a few lines, but imagine the code was longer and you had to perform the same action in several places. Suddenly having to replace $user['name'] with $user->name everywhere, manually, doesn’t seem that simple or easy…

But if you enable the Regular expressions check box and enter this in the Find field:

\$user\['(\w+)'\]

and this in the Replace With field…

\$user->$1

… and then press Replace All, your code is quickly updated to the new working version:

<?php
$user->name = $user->first_name . ' ' . $user->last_name;
echo $user->name;
?>

Isn’t that super great? And that’s just a small example, but I hope it serves to show you how awesome that forgotten and ignored feature is.

Something great too is that neither regular expressions nor support for them are new or unique to Eclipse. Once you learn how to use them, you can use those abilities in lots of places. If you’re interested in regular expressions, there are literally TONS of tutorials and resources about them, but here’s a good starting point. However, the best way to learn is to practise as much as you can, and then a little bit more :-)

The tasks list

Leave short comments in the code like the following ones:

// TODO change to use config settings
// FIXME this never returns a value
// XXX refactor

… and Eclipse will show them all in a nice tasks list, which you can access in the bottom area of the IDE, clicking on the Tasks tab.

Eclipse's tasks list

You can also check them off as you complete them. In that case they would show up after the non-completed ones, but I personally prefer to delete them from the source code when they are done. It’s up to you :)

So that’s for now. Hope you enjoyed the tricks, and they help you make more with less ;-) If you have any other trick that I overlooked, please post it in the comments, I would love to hear about it!

20090211 Stack overbooooom

Stack overflow

This could be one of the reasons why I prefer to install a completely new version of Eclipse rather than updating its existing features – and probably why I haven’t done so since I set up Eclipse in this computer ;-)

For the record, this happened when I clicked “Select required” while having PDT SDK 1.0.5. highlighted.

Let’s see what happens when the next release of Eclipse comes out…

20081210 Add SVN support to your Eclipse installation

UPDATE March 2009:

Mr doob informed me this installation method has been superseded in later versions of the IDE, and the connector and all other SVN components are now very easy to install, which kind of invalidates this article. However, the rest of advice regarding the use of SVN in Eclipse is still valid. So take this with a pinch of salt :)

Surprisingly, Eclipse comes by default with CVS support only, which is not very use(ful|d) at this point. You can do all your SVN duties from outside Eclipse, using something like kdesvn, TortoiseSVN, etc, but it is really powerful to be able to do everything from Eclipse. Specially for comparing between versions, checking the changes on one file, etc, it’s really handy.

Unfortunately, the installation procedure is horrible and confusing. So no wonder everybody just opt for using external tools. I have installed it several times in several computers and using different operating systems, and I always forget how to do that. But this time I decided to write it down so that not only I can remember how to do that, but also people who are using Eclipse with FDT, Flex, etc… can benefit from SVN without having to resort to external tools.

I will use Eclipse Ganymede which uses a slightly different naming for installing new features, but you will probably find your way if using an older version of Eclipse. If not, just post a comment and I’ll try to help :-)

A bit of background

(aka Why can’t I just select a new feature to be installed and have it Just-Working?)

It seems that because of bizarre licensing problems they can’t distribute the whole feature in just one file, but they had to split the SVN support in two parts: the plug-in itself, which talks to Eclipse and that is distributed from the Ganymede update site, and the SVN Connector, which deals with the SVN server and all that, and is NOT distributed from the eclipse servers, but from Polarion.

So that’s why the installation process is somehow divided in two parts.

Installing the plug-in

Go to Help — Software Updates — Available Software

Enter “svn” in the field which says “type filter text”.

When it shows the matches, check the feature called “Subversive SVN Team Provider”

Click Install…, and then Finish.

It might ask you to restart the workbench; you can do that if you feel better that way.

Note: if you can’t get any result when entering “svn” in the field, you might need to add http://download.eclipse.org/technology/subversive/0.7/update-site/ to the list of software sites so that you can install the plug in.

Installing a connector

Wooohoo! You have the Team Provider installed, but it is absolutely useless without a connector. Let’s install one. Where do you get connectors from? Good question. Somewhere in Eclipse literature you can find a link to this page, which provides links to update sites which will enable you to download one of these mysterious connectors.

What really matters here is that the update site url is http://www.polarion.org/projects/subversive/download/eclipse/2.0/ganymede-site/

Copy and paste that link and in Eclipse go to Help — Software Updates — Available Software

Click on Add site…, and paste the url you just copied. Accept and it should load the available installable features at that location.

Check the following two:

  • Subversive SVN connectors
  • SVNKit 1.2.0 Implementation (or the highest available version)

and click Install…, and Finish.

Now it would be a good idea to say yes when it asks you to restart the workbench.

Selecting the connector

Not only do you have to install the connector, you might need to tell Eclipse that you want to use the connector. Just for being safe, once you have restarted the workbench, go to Preferences — Team — SVN — SVN Connector.

And make sure that SVNKit 1.2.0 (or the one you selected before) is selected.

Alternative methods

The official method and support page from Polarion is here, just in case they changed YET again the install method in Eclipse. It seems there can’t be a new Eclipse release without changing the way of installing new features, as if it wasn’t hard enough to install Subversive…

Using SVN in Eclipse

You might probably be wondering how to use this fantastic thing now. Well, you obviously need a SVN repository to begin with, but if you’ve followed until here, you know what we are talking about. So let’s create a project to show how to start using SVN here.

Right click over the Project Explorer and select New… — Project… from the pop-up menu. You should have a new option for SVN in the project types list. Open the SVN folder, highlight Projects from SVN and click Next.

Now enter the SVN repository details, which you should already know: the url, user, password, etc. If you don’t use the classic trunk/branches/tags structure, you can deactivate that from the Advanced tab, if you uncheck the “Enable structure detection”.

Once you’re content with the folder and everything, it will ask you for which resources to check out as a project. And it will show you the repository tree for you to browse and select the specific folder you want to check out. Select it, click Finish.

In the next window it asks you what to do, regarding the creation of the project. Check out as a project configured using the New Project Wizard is like creating a normal project from scratch with the typical assistant, only that it is prepopulated with the contents from the SVN repository location you just selected. This is very useful for creating PHP projects, or FDT projects, for example, so that the project acquires the appropriate nature and you get instant gratification from the beginning (i.e. source highlighting, auto-completion, useful contextual options, etc …).

You could also select Check out as a project with the name specified, which will then create a folder with the name you enter, in your current workspace. Very useful for checking different versions of the same repository (for example into your web_root) into the same parent folder, and modifying them in parallel.

Then click Next, change the workspace if you feel like that, and Finish.

Eclipse will check out things from the repo and set up the new project. If you look at it in the project explorer, it has the svn location right next to its name, to denote it is a svn project.

Comparing and reverting

There is more new stuff. Unfold the project to see all the files and open one of them. Edit it and change something, save it. Now go out, leave the computer alone and have a walk. You could also go to an ice rink, there are lots of temporary rinks open right now in London. If that’s not your thing, you could also head for the pub, which is an all-time winner option. Unless you head hurts… Anyway, what was that thing you changed? You don’t remember at this point, do you? Wouldn’t it be great to compare it with the repository version? But we said we didn’t want to use external tools, and we won’t do that. Right click over the modified file (which has a “>” in front of its name, to denote it has been modified and has not been checked in yet) and select Compare with… Latest from repository. A new tab will open showing the differences between versions. You can even modify the version you changed, using the repository one as reference.

But what if you don’t like those changes? Well, you could just wipe out everything by right clicking over the file and selecting Replace with … Latest from repository. Bam! it’s gone, you can start from scratch again.

Checking things in (commiting)

Let’s imagine you have been doing some work and now are happy with it and want to check that into the repository. It’s a matter of right clicking over the changed files and selecting Team… — Commit….

If you have right clicked over a folder, it will show you which files from that folder will be included in the commit. This is your last chance for unselecting files you don’t want to include.

On the other hand, if you have clicked over a file which is not yet in the repository, it will complain and say it’s not under version control. In this case you need to manually add them first. Right click over the file you want to put under version control (which has a question mark over its icon to show it’s not in SVN yet) and select Team — Add to version control…. It will then be included in the next check-ins.

Deleting stuff

This is a very common mistake we do when we do not understand how svn works. If something is under version control, NEVER EVER DELETE IT FROM THE FILE SYSTEM MANUALLY. You need to delete it via subversion. Fortunately, that is very easy with this plug-in: just right click over it and select Delete. Then you’ll probably have to check in the parent folder, so that subversion registers the new state of the folder (i.e. this folder has now one file less). Otherwise you risk creating a little (or big, depends on your case) wave of chaos and confusion in subsequent commits and updates.

Updates

By the way, updating is as easy as right clicking over something and then selecting Team — Update.

This is only a very little demonstration of what you can do with subversive; the best way of finding out is to experiment with the different options there. Let me know if there is some area I could expand in this mini tutorial or if something wasn’t clear enough, and I’ll try to clarify. Happy SVN’ing!

20071121 Eclipse word wrapping, volume 3

Some background first: I have been using eclipse for almost four years now and if there’s something which keeps me going back to other editors, it is the word wrapping feature (or its lack of it).

First there was not such a feature, or it was enabled for simple text editors only – not the PHP editor. That meant you had to choose between text highlighting and word wrapping. In practical terms, you ended up ditching eclipse and using another editor, because it’s just unpractical to keep remembering which file had long enough lines for deserving word wrapping and opening it with a different editor.

Then somehow the feature appeared but guess what? it’s not available if you’re using the new PHP plug in, Eclipse PDT, which I’m using because it’s better.

I have found a Google Summer Of Code project exclusively dedicated to provide word wrapping in Eclipse. And I don’t know if I should be happy or sad that such a thing deserves a project on its own. By reading at the author’s blog, it seems there’s a horrible mess with the editors. No wonder the options appeared and disappeared in each version.

This experimental plugin works, but it does strange things with the line numbers. The author warns about it so I’m not complaining about that, I’m just pointing at that. And the fact that there are little programs outside there (Crimson Editor, PSPad, TextMate…) which are able to do word wrapping without any fuss. Hey, it’s even available with a couple of keystrokes in some programs!

So please Eclipse developers, instead of adding more superfeatures such as folding for and if blocks (which may be useful) could you please focus on this insignificant, apparently irrelevant feature which can become very annoying, specially when editing super long text strings in javascript? (or when trying to highlight a part of a very long line while making sure the mouse keeps on the same line and so it doesn’t jump to the next line, thus ruining all the selection and you having to start from the beginning).

It would make all of us super happy and even the world would be a better place.

Please? please? Can I have one for Christmas?

I don’t want to write an Eclipse word wrapping, volume 4.