Sunday, December 28, 2014

Fixing USB on a Lego Mindstorms NXT Brick

My son has a Mindstorms NXT set where the center post in the NXT Intelligent Brick's socket broke off, resulting in an unreliable connection with the computer when downloading programs. Replacing the Brick will cost $170 from Lego Education, so I decided to open it up and see if I can fix it. It turns out that, other than figuring out how to take it apart, the repair job is not too difficult.

This article is for informational purpose only. Open up your NXT Brick at your own risk. If you have a problem with your Brick, call Lego technical support first. They may replace it free of charge, particularly if it's under warranty.

The tools you need are:
  • Soldering iron
  • Desoldering tool
  • Solder
  • Phillips screwdriver
You'll need to purchase a USB Female Type-B Port 4-Pin socket to replace the broken one.

Opening the NXT Brick
  1. Open the battery compartment and remove all batteries. Remove 4 Phillips screws holding the battery compartment to the top white shell of the NXT brick. Gently pry off the top shell.
  2. Unscrew 2 Phillips screw holding the LCD display and the circuit board to the battery compartment. Gently pry apart the USB side of the Brick and remove the gray plastic port covers on both ends so that you have enough space to desolder the underside of the USB socket. 
The circuit board will NOT come off completely! The non-USB side has two posts soldered to the battery terminals inside the battery compartment. You may desolder these to take off the entire circuit board but it is not necessary.
There is a rubber button that makes contact with the circuit board through a hole in the battery compartment. This is used to tell the NXT brick that a rechargeable battery is being used. Do NOT lose this!

Replacing the USB Socket
  1. Desolder the USB socket. There are 2 tabs and a cluster of 4 small terminals that must be desoldered. Use a desoldering tool to suck off the solder so you can pry off the broken USB socket. Be careful not to damage the circuit board.
  2. Insert the new USB socket and solder the terminals and tabs back. Test USB connectivity with a computer using Mindstorms NXT program before putting everything back.
Putting It Back Together
  1. Insert the port covers and put the rubber button in place. Be sure to align the port covers properly. Push the circuit board back on to the battery compartment. A little effort may be necessary. There are 2 posts on both ends of the battery compartment that snaps into the port covers.
  2. Screw the display and the circuit board on to the battery compartment.
  3. Slide the white shell on to the top of the Brick via the guides on the port covers. If you took out the front buttons, be sure to put them back before putting the white shell back. Use a little force to snap them together.
  4. Turn it over to see the inside of the battery compartment. Ensure that the rubber button is in place. Put the 4 screws back.
  5. Test your new USB socket by downloading a new program.

Tuesday, February 26, 2013

Streaming iTunes to Xbox 360

Our family has a Mac Mini and we put all our media in iTunes. We also have an Xbox 360. While iTunes is great for streaming media to another Apple device, I was also looking for a way to stream it to the Xbox, so that we can listen to our music and watch our home videos on our TV.

All the solutions I found on the web requires either installing another piece of software (Connect360) or hooking up another device (AppleTV), neither of which is very attractive to me.

As it happens, I also run Parallels on the Mac in order to run some Windows applications, and I hit upon this solution, which I did not see described elsewhere.

On Windows in Parallels, follow these steps:
  1. With Windows shut down, make sure that Parallels's networking is configured as a "Bridge Network" using the "Default Adapter".
  2. Start Windows and then start Windows Media Player (WMP).
  3. In WMP, select View > Library from the menu. Then, under the Stream secondary toolbar menu, select "Turn on home media streaming". If it is already turned on, be sure that "Automatically allow devices to play my media" is checked.
  4. In WMP, right click Music and select "Organize Music Library". Ensure that your Mac's Music folder has been added. This will be "\\psf\Home\Music". Do the same for Videos and Pictures. For Videos, you may also want to add your iTunes Media, which is located in "\\psf\Home\Music\iTunes\iTunes Media".
On the Xbox, follow these steps:
  1. Turn on the console.
  2. After the console starts, press the "Guide" button on your controller. The "Guide" button is the silver button with the lighted X on it.
  3. When the dialog box shows up, use the left "Stick" to select the "Media" pane. Wait a few moment for it to find your computer, then select your computer.
  4. Pick "System Video Player" or "System Music Player" depending on the type of media you want to play.
  5. Navigate to the video or music and select to play.

Note that I have only tried this with Windows 7 and Windows Media Player 12. I am running Parallels with 2 GB  RAM on a Mac Mini with a 2.3 GHz i5 and 8 GB of physical memory. With this configuration, everything seems to stream smoothly with no stutter.


Monday, February 25, 2013

Exposing IQueryable from a Repository

During my hiatus of 2-1/2 years from this blog, I have gone from independent consulting through being employed by a large company to now working for small company. Along the way, I have picked up C#, .NET and other interesting artifacts of the computing world. For that reason, I am expanding my blog to cover not just Java, but any computing topics that sparks my interest at that moment.

Recently, I was asked for my opinion on this C#/LINQ subject: Are there is any downside to returning IQueryable from a Get in a Repository pattern? That question forced me to put my thoughts on the subject in order, which I am setting down here.

Now, I know that purists hate it because it violates the “Separation of Concerns” principle. And there are good (and practical) reasons to be wary:
  • Since IQueryable is an expression container, the programmer may potentially construct an ill-advised query (e.g., adding a filter on an un-indexed field or pulling in a large table in its entirety.)
  • It may make it harder to unit test since IQueryable has a trail leading all the way to the database. This may be alleviated by mocking the data source, but may not produce the same results.
On the other hand, there is also a huge upside, if you need it:
  • Paging. Because you are deferring execution to the database, you can let the database pull in only the exact number of records you need at the point where you need it – in the Controller. Much more efficient than pulling it all into memory and then paging in code. I’d argue that this, in fact, promotes the SoC principle by keeping paging in View-Controller where it properly belongs, instead of passing it along through the business layer into the repository for handling. (One way around this is to encapsulate IQueryable. I am not particularly keen on this but I can't quite put my finger on why.)
Looking on the web, it looks like I may be late to the game as this topic has been argued many times with people's opinions falling on both sides.


Wednesday, August 5, 2009

Custom JTable Cell Renderer (Part 2)

While overriding getTableCellRendererComponent() in DefaultTableCellRenderer allows you to render any arbitrary JComponent in a cell, it is unfortunately all it does. If getTableCellRendererComponent() returns a JCheckBox, then JTable will render it in the appropriate cell including its check state. But the rendered checkbox will not be responsive to any mouse click applied, which of course defeats the purpose of displaying a checkbox inside a cell in the first place.

It is relatively easy to make a cell interactive. First, register a MouseListener on the JTable. When a mouseClicked(MouseEvent evt) is called, you can determine the cell picked using:

int row = table.rowAtPoint(evt.getPoint());
int col = table.columnAtPoint(evt.getPoint());

If we determine that the cell at this row and column can respond to a selection, then we need a way to keep track of the state of that cell. For example, a cell rendering a checkbox needs to keep track of its check state. The JCheckBox returned by getTableCellRendererComponent() cannot be used for this purpose because there is only one and is used as a rendering template. Here is where an arbitrary data object comes in handy. For example, if the cell is a checkbox, then data object for that cell can be a Boolean object. Then you can call the JTable's model's setValueAt(row, col) to store the boolean value of the cell clicked, making it true if it were false and vice versa.

This data object will be passed as the argument to getTableCellRenderer()'s value object parameter. Using the checkbox example above, this will be a Boolean object and depending on whether the value is true or false, set the selection state of the JCheckBox template before returning it. The checked state will be used to render the affected cell accordingly.

Tuesday, August 4, 2009

Custom JTable Cell Renderer (Part 1)

If the data is not a supported type, or if not all the cells in a single column is of the same type, then you will need to write a custom renderer for your JTable.

JTable asks the renderer's getTableCellRendererComponent() what kind of display component each cell should look like in the course of painting the table. Note that the JTable does not actually place a JComponent in each cell. Instead, each cell simply visually mimics the JComponent returned by getTableCellRendererComponent() by painting it in the cell.

For example, if getTableCellRendererComponent() returns a JLabel, then the attributes of that JLabel is used to paint the cell so that the cell looks like a JLabel (but is not really one). In other words, the JLabel is simply used like a data structure to pass information. For this reason, it is important that getTableCellRendererComponent() should not create a new component with each call. For example, the default JTable renderer, DefaultTableCellRenderer (an extension of JLabel), always returns itself when its getTableCellRendererComponent() is called.

In order to display a different "component" in each cell, the simplest method is to extend the DefaultTableCellRenderer. Instantiate in advance (say, in the constructor) all the different types of JComponent you will need (only one of each type is needed). Then when getTableCellRendererComponent() is called, determine which JComponent is to be returned, adjusting their attributes if necessary.

public class MyTableCellRenderer extends DefaultTableCellRenderer
private JCheckBox checkbox;
private JButton button;

public MyTableCellRenderer()
// Create one checkbox and one button. We will always return the same ones.
checkbox = new JCheckBox("Check Box");
button = new JButton("Button");

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
// Render row 0 column 0 as a JCheckBox. Return the pre-created
// JCheckBox after setting the "selected" attribute appropriately
// based on its value in the JTable model.
if (row == 0 && column == 0)
checkbox.setSelected(table.getValue(row, column));
return checkbox;

// Render row 0 column 2 as a JButton.
if (row == 1 && column == 2)
return button;

// Everywhere else, return the usual DefaultTableCellRenderer.
return this;

Monday, August 3, 2009

Using Arbitrary Objects as JTable Data

While we normally supply Strings, JTable can actually accept any arbitrary objects as data. To display the data, its toString() method is called for a string that the JTable can display.

If the data is not of the supported type, then its toString() method must be implemented to return something for JTable to display intelligibly. For example, given a class called ArbitraryObject as follows:

public class ArbitraryObject
private String name;

public ArbitraryObject(String name)
{ = name;

public String toString()
return "The number is " + name; // prepend "The number is" to name
Then you can create a JTable initialized with instances of ArbitraryObject as data:

private ArbitraryObject[][] data =
{ {new ArbitraryObject("1"), new ArbitraryObject("1"), new ArbitraryObject("1")},
{new ArbitraryObject("2"), new ArbitraryObject("2"), new ArbitraryObject("2")},
{new ArbitraryObject("3"), new ArbitraryObject("3"), new ArbitraryObject("3")},
{new ArbitraryObject("4"), new ArbitraryObject("4"), new ArbitraryObject("4")},
{new ArbitraryObject("5"), new ArbitraryObject("5"), new ArbitraryObject("5")},
{new ArbitraryObject("6"), new ArbitraryObject("6"), new ArbitraryObject("6")} };
private String[] headerLabels = { "Column One", "Column Two", "Column Three" }

JTable table = new JTable(data, headerLabels);
add(table, BorderLayout.CENTER);
Here's the screenshot of this JTable:

The utility of this is that you can easily associate each cell in the JTable with an object it represents. JTable "displays" the object with the value returned by the object's toString(). For example, if you select a cell based on what's displayed in it, a listener registered for the JTable can easily retrieve its associated object using getValueAt(row, col) without going through convolutions to look it up. Think of it as a link from the table back to the data it represents:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
ublic void valueChanged(ListSelectionEvent event)
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
ArbitraryObject o = table.getValueAt(row, col);

Sunday, August 2, 2009

JTable Cell Display Based on Data Type

JTable is capable of doing some special rendering based on the data type, such as JCheckBox for boolean data. The supported data type are listed in Sun's Tutorial on JTable. The only restriction is that each column has to be uniformly the same type. To enable this, the getColumnClass() of the JTable's model has to return the type of the data to be displayed:
   DefaultTableModel model = new DefaultTableModel()
* Override to return the data class.
public Class getColumnClass(int c)
return getValueAt(0, c).getClass();


If getColumnClass() is not overridden, then the returned class is Object. In this case, JTable invokes the toString() method of the Object and displayed it as a JLabel.