Posts Tagged ‘java’

Writing jEdit macros in Java (in Eclipse)

Wednesday, December 30th, 2009

Right now I don’t know which one sucks more: doing complex beanshell macros in a text editor or doing a proper plugin with an ant deploy script and a reloader (helper) plugin. So, here comes another crappy solution: writing jEdit macros in pure java, with real content assist and code completion.

  1. in the plugin manager install the JavaMacros plugin
  2. in Eclipse create a jEdit_Java_Macros java project
  3. Project Properties: Java Build Path: Libraries: Add External Jars —-» add jedit.jar and JavaMacros.jar
  4. create package: macros.CompiledMacros
  5. create a CompiledMacros subfolder in your personal macros dir
  6. create a class (HelloWorld for example); at the interfaces section add “MacroClass”; you can add a constructor or a main method, but it doesn’t make a difference
  7. now, oh my, you would think about setting the bin path to the Macros folder in the settings directory (Default output folder), but don’t do that – that will just mess up your macros folder, instead use this jEdit macro to automatically copy, reload and execute the compiled class file:
macroName = "CompiledMacros/HelloWorld";
from = "Q:/dev/workspace-java-jedit/jEdit_Java_Macros/bin/macros/";
to = "Q:/.jedit/macros/";

void reloadClassMacro(macroName, from, to) {
import java.io.*;
from += macroName + ".class";
to += macroName + ".class";

in = new FileInputStream(from);
out = new FileOutputStream(to);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
in.close();
out.close();
Macros.loadMacros();
macro = Macros.getMacro(macroName);
macro.invoke(view);
}

reloadClassMacro(macroName, from, to);
return;

Now assign this macro to key in jEdit and it will automatically rerun the class macro. Since class macros are tied to a given location (meaning that if you copy the class file to another directory or you rename it it will not work) this method is useful for developing macros; in the end one might want to copy the final code into a bsh file (with cleaning out the class wrapper of course).

Finally, if you happen to be on Windows you can use this autohotkey script to “call” the macro from within Eclipse (I call the AHK script with Win+F12 (#F12) and I assigned the classloader to F12 in jEdit so that’s why I forward F12 there):

#F12::
Process, Exist, eclipse.exe
If ErrorLevel
{
WinActivate, ahk_pid %ErrorLevel%
Send ^s
}
IfWinExist jEdit
{
WinActivate
Send {F12}
}
return

EDIT: no, I was wrong, this sucks too; I can’t add internal classes, like eventlisteners… thank you very much, jEdit.

Catalogizer software

Monday, December 28th, 2009

I have tried many and did not like either one; enough said. Finally I decided to write one for ourselves: first I wanted to do it in full java, but right now I’m hesitating -

  • I need multiple users (so that we can upload from my machine or the laptop in the same time)
  • proper database is a must (I’m tired of hashmap dumps, xml files and half baked lucene caches)
  • simple gui (I don’t want a command line tool while swing may be too steep for me)
Either way, on the client side I must have a crawler; right now the crawler is finished and does what I need: it reads directories and saves them as xml files. Can read mp3, zip, 7zip, rar and DOS descript.ion files (which is supported by Total Commander for example). It mainly uses XML Directory Listing, jID3, J7Zip and JUnRar (these are Apache/LGPL/free libraries) – I had been thinking about the exif info and the avi metadata, but the exif info is way too complex to be fully stored (and I just don’t need it) while the avi metadata is a total pain in the back in java (JMF player realization, broken/legacy libs etc).

I’ll upload the source to Google Code sooner or later, til then the binary is here:
http://www.rosamez.com/download/xml-dir-listing-ex.7z
(this is only the crawler; it’s a command line application! Example usage:

java -jar xml-dir-listing-ex.jar -o c:\test.xml -m -a -dion c:\temp)

Java verbosity

Thursday, August 20th, 2009

I have a massive (butt ugly, huge etc.) XML file, which I want to read into the memory, modify some of the nodes, then save it back into XML. It’s not that I like PHP, because I don’t, just to show how ridiculous things are:

PHP

$doc->save($fileName)

JAVA

private void saveToFile(Document doc, String fileName) {
StringWriter writer = new StringWriter();
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
writer.close();
String flat = writer.toString();
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write(flat);
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}

I didn’t write proper exception handling in the above examples of course and I’m sure Java gurus out there can write better and nicer code than me, so let’s say, this is just a useless rant.

JavaFX a year later

Monday, August 11th, 2008

Though not in the mood, nearly a year ago had a small post about JavaFX, let me revisit the whole idea for a moment: JavaFX is (should be) the Java RIA platform, brought to you by Sun, the almighty. One year passed by, mostly unnoticed: the beta sdk’s been out for a couple of days (neither crap, nor cool), we got some demos (mostly nothing new), no drums or fireworks, just one lousy intro video. Sun, IBM, Novell: remnants of a forgotten era.

Just another funny thing: the JavaFX site (not the blog, blog) itself. I usually prefer html over flash and I do love js frameworks (including mootools), but what the Java guys did here: holy crap! And the demos shown there… Take the “Immersive dimensions” 3D thingy for example: my C64 with Mercenary/Elite did better 3D (with its 1Mhz CPU). In this one year not much has changed (except for, face the inevitable, the cool little Adobe Air apps popping up here and there); I feel somewhat tricked.

Why do I love jsp files?

Thursday, March 20th, 2008

Because writing jsp is like taking away candy from a baby. Real men don’t use procedures and functions, real men don’t use goto or gosub either. Real men write code from A to B copy pasting their way through the jungles of evil, concentrating on the goals! This is xp/agility at its best: just do it.

Legacy code is killing me.