Subclassing NSTextField to Allow Only Numbers

Example project available on GitHub – CINumberField

Yes, I know that one may use NSNumberFormatter to achieve a similar result – but what I don’t like about about that solution is this:

If a user types non-numeric characters into the NSTextField and attempts to tab away from the field, they get the default error sound and their tab attempt is denied.

To me this isn’t user friendly.

I figured I’d take a Javascript-esque approach to the problem: when the user has finished interacting with the text field, check whether said content is numeric. If it isn’t, beat it until it learns.

Witness the incredible simplicity:

NumberField.h

//  NumberField.h
//  Created by Michael Robinson (Code of Interest) on 28/11/10.
#import <Cocoa/Cocoa.h>
 
@interface NumberField : NSTextField { }
 
-(void) textDidEndEditing:(NSNotification *)aNotification;
 
@end

NumberField.m

//  NumberField.m
//  Created by Michael Robinson (Code of Interest) on 28/11/10.
#import <NumberField.h>
 
@implementation NumberField
 
-(void) textDidEndEditing:(NSNotification *)aNotification {
	// replace content with its intValue ( or process the input's value differently )
	[self setIntValue:[self intValue]];
	// make sure the notification is sent back to any delegate
	[[self delegate] controlTextDidEndEditing:aNotification];
}
@end

To use the class, open your NIB in Interface Builder and select the NSTextField you want to limit to numbers only, open the Inspector, open the Identity tab, and paste the name of your new class in the “Class” text field.

Comments (17) | Trackback

Hextractor – Website Colour Scheme Extraction

Extract colours schemes from CSSWhile reading Kenn’s excellent description of his method of managing backups with rsnapshot, I clicked a few links and ended up here: Hextractor.

It’s a site that allows one to type in / paste link / upload CSS and then extracts colours from it, combining all colours into a colour palette. I used it to extract a list of hex codes for the colours we use at ECPod, to save me the effort of using bugzilla to find element colours.

No comments | Trackback

Atheros Drivers – In A Repository!

Now, instead of manually installing Atheros drivers, I can install them from ELRepo!

First, find the device id for the network adapter:

for BUSID in $(/sbin/lspci | awk '{ IGNORECASE=1 } /net/ { print $1 }'); do /sbin/lspci -s $BUSID -m; /sbin/lspci -s $BUSID -n; done

It’ll give output like:

02:00.0 "Ethernet controller" "Atheros Communications" "Atheros AR8132 / L1c Gigabit Ethernet Adapter" -rc0 "Giga-byte Technology" "Unknown device e000"
02:00.0 0200: 1969:1062 (rev c0)

The number we want is on the second line, in this case it’s “1969:1062”.

Copy that, then go to the ELRepo Device ID page to find which driver package to use. In this case, We find:

pci 1969:1062 kmod-atl1e

Now just follow the instructions to add the repository and install the driver package on the ELRepo main page.

For me, using CentOS 5.5:

rpm –import http://elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://elrepo.org/elrepo-release-5-1.el5.elrepo.noarch.rpm

Then finally installing the driver package:

yum –enablerepo=elrepo install kmod-atl1e

No comments | Trackback

Atheros Drivers Centos 5.5

Servers went down. We think it had something to do with the network hardware and / or the drivers ( Atheros AR81Family ) and their compatibility ( or lack thereof ) with our Centos 5.5 boxes.

As our servers are in the PRC, I can’t physically touch them – luckily we have an excellent team of capable admins who can. After being alerted that the servers were down, they went to the datacenter and managed to convince the machines to work.

I’d noticed some similar issues with our development server ( same hardware ). The server crashed quite often when using rsync to backup directories with a large amount of small files, and would need to be hard-reset. Not cool.

So I spent some time tracking down the driver download page – it’s hidden :( and updated our network drivers. We were 5 versions behind! Here it is:

Atheros AR81Family Drivers

I’ve spent the day trying to put the development server under as much stress as possible, using ab, flood and rsync. So far it hasn’t crashed, which is a good sign.

I had a little trouble installing flood on my work machine – ran into errors with Macports installing db46, required for flood.

No comments | Trackback

Make Service Run at Startup – CentOS

Note to self: Stop forgetting this!

Make a service start at boot:

chkconfig httpd --add
chkconfig  httpd  on --level 235

Check if a service has been set this way:

chkconfig --list httpd

From Enabling and disabling services during start up in GNU/Linux.

What is that –level ### stuff?

ID Description
0   Halt
1   Single-User mode
2   Multi-user mode console logins only (without networking)
3   Multi-User mode, console logins only
4   Not used/User-definable
5   Multi-User mode, with display manager as well as console logins (X11)
6   Reboot

Thanks Wikipedia for the table!

Most users run X from one of two runlevels: 3 or 5. Runlevel 3 places your system in multi-user mode with full networking capabilities. The machine will boot to a text-based login prompt with all necessary preconfigured services started. Most servers are run in runlevel 3, as X is not necessary to provide any services utilized by most users. Runlevel 5 is similar to 3, except that it automatically starts X and provides a graphical login screen. Many workstation users prefer this method, because it never forces them to see a command prompt.

From The Official Red Hat Linux Reference Guide.

No comments | Trackback