Install cygwin and cygwinports packages using apt-cyg

I recently started using apt-cyg and like it a lot. It allows easy installation of packages from Cygwin and Cygwin ports repositories.

It is inspired by Debian’s apt but it does not support anything like a sources.list and it can actually only work against one repo (mirror) at a time. Well, I guess that’s bearable since there are really only two sources (main cygwin and cygwin ports). But I might try apt-cyg-multi which claims to have added the missing cross-repository functionality.

For now, I found it convenient to configure these bash aliases (use a suitable Cygwin mirror and Cygwin Ports mirror for your location):

alias cyg='apt-cyg -m http://mirrors.kernel.org/sources.redhat.com/cygwin/'
alias cyp='apt-cyg -m http://mirrors.kernel.org/sources.redhat.com/cygwinports/'

Update, June 2013: There is a promising-looking apt-cyg fork on github. I just started using it.

Extremely simple one-level drop-down menu using CSS

The CSS below styles an unordered list with nested sub-lists as a horizontal menu with drop-down sub-menus (only one level). I tried to keep the code minimal with no effort for good looks. You can add your own styling as you like.

I have tested this successfully in IE8, IE9 and recent versions of Firefox, Chrome, Safari and Opera. I consider IE6 and IE7 broken beyond repair and basically ignore them.

Please test it in your browser at http://jsfiddle.net/Yk6MS/4/, and let me know about any problems you might find.

Style sheet:

.menu,
.menu ul {
  background-color: tan;
}
.menu > li {
  display: inline-block;
  white-space: nowrap;
}
.menu ul {
  position: absolute;
  display: none;  
}
.menu > li:hover {
  position: relative;
}
.menu > li:hover ul {
  display: block;
}

Example HTML:

<ul class="menu">
  <li>Abra
     <ul>
      <li>Amfdf rt</li>
      <li>Brr zz</li>
     </ul>
  </li>
  <li>Kadabra
     <ul>
      <li>Kala mala</li>
      <li>Hauki dauki</li>
     </ul>
  </li>
  <li>Simsalabim
    <ul>
      <li>Dudel didel</li>
      <li>Dudel döh</li>
      <li>Labedi bap</li>
    </ul>
  </li>
</ul>

JRuby on Rails 3.2

I currently have to work on an application using JRuby on Rails 3.2. My background is 12+ years of Java centric web development using Eclipse or IntelliJ.

There are a few things that I love about working with Java in Eclipse or IntelliJ that I miss when working with Rails:

  • Java’s backwards compatibility
  • Java’s cross-platform consistency
  • That Java has a static type system and is a compiled language
  • The relatively rigid syntax of Java that prevents idiosyncratic DSLs
  • Being able to control mutability, extensibility and visibility at compile-time (private, final)
  • Java’s distinction of interfaces vs implementation classes
  • Powerful auto-completion and Refactoring in the IDE
  • Powerful code inspections and quickfixes in the IDE
  • IDE and compiler feedback about mistakes and design flaws in my code
  • The powerful and rich ORM features of JPA and Hibernate

Dynamic language fans might cringe at some of the points listed above. Feel free to provide constructive feedback or well-reasoned criticism.

There are however a couple of things that I like about Ruby on Rails 3.2:

  • Dependency management using gems and bundler
  • A very standardized project layout (directory structure and conventions)
  • Code generation (as a convenient way to quickly create sample code)
  • Out-of the box integration and support for JQuery, CoffeeScript, SASS
  • Cool gems like colorbox-rails for turning a regular view into a modal dialog
  • The asset pipeline

I am still in the early phase of my Rails experience and I will try to post more about it as things evolve.

Save video stream as file using rtmpdump (even on Windows)

The rtmpdump tool can help you save video streams as local files.

It comes with most Linux distributions, e.g. on Ubuntu or Debian (with sudo):
sudo apt-get install rtmpdump

On Windows, the most convenient way is probably via Cygwin and Cygwin Ports:

  1. Install Cygwin
  2. Install Cygwin Ports
  3. On the package selection screen, select “rtmpdump”

Read the manual page and study the options:
man rtmpdump

Project Jigsaw notes and links

Goals
– Help fix problems with the classpath
– Allow modularization of JDK/JRE
– Enable using JSE subsets
– Improve performance (startup time, download time, etc.)

OSGi was considered too complex and not well-suited for JDK modularization
But: Project Penrose tries to make sure that OSGi can be implemented on top of Jigsaw.

Project is in Phase 1 (exploration, prototyping)
Phase 2 (reference implementation) in very early stages

Project website
Big picture of the Design

A lot of work to do
Was recently deferred from Java 8 to Java 9

Challenges:
– Dynamic modularization
– JEE containers
– Needs a reflective API
– Fundamental changes, require a lot of QA (testing, etc.)

But Java 8 might already introduce a simplified way of using JSE subset:
“JSE Profiles”

Module descriptor: module-info.java
http://openjdk.java.net/projects/jigsaw/doc/lang-vm.html#jigsaw-1.1

Modules are versioned
Only one version of a module can exist

Repositories: HTTP server or local files

Packager to generate deb, rpm packages etc. from Java modules
translate the module metadata to package metadata

JDK modularization
http://openjdk.java.net/projects/jigsaw/doc/jdk-modularization.html

Backwards compat:
Classpath-mode (legacy), Module mode

But: No more dependecies on internal classes (e.g. sun.misc.*) will be possible
No more rt.jar, tools.jar

A font chooser combobox for Swing or AWT components

Useful if you want to let the user choose the font to use for certain components of a Swing UI:

package net.doepner.ui.text;

import java.awt.Component;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Comparator;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

public class FontChooser extends JComboBox<Font> {

	public FontChooser(final Component... components) {

		final Font[] fonts = GraphicsEnvironment
				.getLocalGraphicsEnvironment()
				.getAllFonts();

		Arrays.sort(fonts, new Comparator<Font>() {
			@Override
			public int compare(Font f1, Font f2) {
				return f1.getName().compareTo(f2.getName());
			}
		});

		for (Font font : fonts) {
			if (font.canDisplayUpTo(font.getName()) == -1) {
				addItem(font);
			}
		}

		addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				final Font font = (Font) e.getItem();
				for (Component comp : components) {
					setFontPreserveSize(comp, font);
				}
			}
		});
		
		setRenderer(new FontCellRenderer());
	}
	
	private static class FontCellRenderer 
			implements ListCellRenderer<Font> {
		
		protected DefaultListCellRenderer renderer = 
				new DefaultListCellRenderer();
		
		public Component getListCellRendererComponent(
				JList<? extends Font> list, Font font, int index, 
				boolean isSelected, boolean cellHasFocus) {
			
			final Component result = renderer.getListCellRendererComponent(
					list, font.getName(), index, isSelected, cellHasFocus);
			
			setFontPreserveSize(result, font);
			return result;
		}
	}

	private static void setFontPreserveSize(final Component comp, Font font) {
		final float size = comp.getFont().getSize();
		comp.setFont(font.deriveFont(size));
	}
}

Java DSL for object-oriented type-safe CSS styling

I am looking for something like this Java-CSS-Library, to implement server-generated CSS stylesheets backed by a type-safe object-oriented model of CSS classes and rule sets.

This is part of my ongoing quest for the perfect “pure Java” web development framework that would allow me to

  • Focus on my strongest area of expertise: Elegant Java code
  • Use refactorings and other advanced Java tooling in Eclipse or IntelliJ Community Edition
  • Ignore the HTML/HTTP vs JVM objects impedance mismatch as much as possible

Sharing IntelliJ project configuration (.idea, *.iml) in version control

Me and my team do exactly what Christof Schablins describes in this blog post and what the Jetbrains folks recommend here: We use IntelliJ IDEA and share *.iml files (modules) and most files in .idea directory (project config) in the Version Control System (in our case CVS), including shared ant configs, shared run configs for Tomcat 6 and JUnit tests, shared project specific code inspection profile, shared data source definitions, etc.

Overall it works great. But we noticed a few things:

  • Needed an IntelliJ Path variable TOMCAT_HOME because devs have Tomcat installed in different places
  • Dialog “Do you want to add workspace.xml to CVS” keeps popping up (despite an entry in .cvsignore file within the .idea directory)
  • If devs have different sets of IntelliJ plugins enabled, each plugin adds its default code inspection rules to the shared profile (even if the project does not use them, e.g. Ruby stuff in a pure Java project). So developers have to be cautious not to check in those changes (or accept the bloat they cause)

So my question is: Have you come across these or similar issues and did you solve them?

Turn org.w3c.dom.NodeList into Iterable

I recently posted some reusable code that facilitates reverse iteration over a list in Java.

Here is another potentially useful Java method related to iteration, from my buddy Jordan Armstrong:

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
  /**
   * @param n An XML node list
   * @return A newly created Iterable for the given node list, allowing 
   *         iteration over the nodes in a for-each loop. The iteration 
   *         behavior is undefined if concurrent modification of the node list
   *         occurs.
   */
  public static Iterable<Node> iterable(final NodeList n) {
    return new Iterable<Node>() {

      @Override
      public Iterator<Node> iterator() {

        return new Iterator<Node>() {

          int index = 0;

          @Override
          public boolean hasNext() {
            return index < n.getLength();
          }

          @Override
          public Node next() {
            if (hasNext()) {
              return n.item(index++);
            } else {
              throw new NoSuchElementException();
            }  
          }

          @Override
          public void remove() {
            throw new UnsupportedOperationException();
          }
        };
      }
    };
  }