Friday, March 20, 2009

Always successful Grant statement in MySQL

If you do not want to fail a grant statement in Mysql, you can always include "Create" privilege in the statement. In this way, you can grant privilege to a table that is not existing yet. The table can be a table even in a non-existing database.

E.g

Grant create, select on db.tbl to user@localhost identified by 'TEST'

This statement will alway successfully executed on any mysql server.

Thursday, March 12, 2009

Regular Expressions in Java and C#

This is a summary from reading "Introduction to Regular Expressions" written by Larry Mak

The basic idea of using regular expression is to match and to replace. The former determines if the pattern is in the string, and if so, find it. Replace changes the string according to the pattern to another pattern.

In C#, Regex in
System.Text.RegularExpressions is used to do the match. In Java, Pattern and Matcher in package java.util.regex are used do the work.

C#:
Regex.IsMatch( string data, "Hello" )

Java:
Pattern pat = Pattern.compile("Hello");
Matcher m = pat.matcher( data );
if ( m.find() )

To define a boundary in regular expression, you use "\b"

To match more than once in same string, in C#, you write something like below to loop all the matches:
   for ( Match m = Regex.Match( str, patternToMatch); m.Success; m = m.NextMatch() )
In Java, you use matcher's find method:
   Pattern pat = Pattern.compile(pattern);
Matcher m = pat.matcher( s );
while ( m.find() ){
System.out.println( m.group() );
}
Capturing groups are numbered by counting their opening parentheses from left to right.
E.g. $(\d+)\.(\d\d). For each matching, it can be further detailed to capturing groups.
In C#, GroupCollection gc = m.Groups;
In Java, matcher.group(i)

Replacement
In C#, use Regex.Replace( str, search, replace );
In Java, use matcher.replaceAll() method

One powerful operation you can do with regular expression is to change the
string with what you captured. you define the groups in order from 1,
and you can rearrange the groups by reorganize the order or groups,
for example, $1$3$2.






Tuesday, March 10, 2009

Refresh on GridBagLayout

I have to work on a GUI in Java recently. Since it works with JDK1.5 so that the most convinient and flexiable GroupLayout can't be a candidate. The NetBean 6.5 does not provide automatic conversion from GroupLayout to GridBagLayout as what has been provided in JBuilder3.x back to more than 10 years in 1998, and the visual Editor for eclipse does not fully support free GUI design, so that I have to code it by hand. Once again it reminds me that Borland's tools are the best.

This is a good chance for me to refresh the knowledge on layout managers in Java. Most of the content below is from http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html.

GridBagLayout is the second most flexiable and powerful layout manager next to GroupLayout. It places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

This resizing behavior is based on weights the program assigns to individual components in the GridBagLayout. The way the program specifies the size and position characteristics of its components is by specifying constraints for each component. The preferred approach to set constraints on a component is to use the Container.add variant, passing it a GridBagConstraints object.

It is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance

Other attributes are easy to understand, below are the ones need to pay more attention to.

weightx, weighty

Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

Tuesday, March 03, 2009

Mouse problem in Word 2007

Symptom:

the mouse simply does not select any thing or locate to any position in work document.
keyboard is still working.

Related tries:
- remove HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word\Data
- disable add-ins under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\Addins

Failed in my case.

Final solution:
-start word in secure mode:winword /a. this is because under normal start, the mouse does not work in the option dialog.
-work options-->add-ins
disable suspected add-ins will get the problem fixed. in my case, the expired power designer add-in caused the failure of mouse function in word 2007.