土曜日, 1月 26, 2008

To implement Emacs styled "kill-line"

What I found out today is that emacs styled "kill-line" is really a thing that makes you feel you're in fact using the program like a part of you. Just because it will get you to a part of the program, it indeed is going to make you feel better using the software. That kind of application is something that all of us would have to want to program.

To look at what "kill-line" is, try the Java applet editor in my Zhuyin page. The popup menu of the Java applet editor shows the "Cut Line" that is much like "kill-line" and delete the text from where the caret is to the end of the line. "Copy Line" does just as you assume, it selects the portion of line to the end and that part is copied to the system clipboard.

In case you want to know the beautiful Java lines that can accomplish these tasks, here are the codes.


boolean isLineEnd(char ch)
{
return (ch=='\u2029'||ch=='\u2028'||ch=='\u0085'||ch=='\n'||ch=='\r');
}

public int getLineEndPosition()
{
int index=-1;
String text=textArea.getText();
if(!text.isEmpty())
{
int pos=textArea.getCaretPosition();
index=pos;
int len=text.length();
while(!isLineEnd(text.charAt(index)) && ++index<len);
}
return index;
}

void cutLine()
{
int start=textArea.getCaretPosition();
int end=getLineEndPosition();
textArea.setSelectionEnd(end);
textArea.replaceSelection("");
}

void copyLine()
{
int start=textArea.getCaretPosition();
int end=getLineEndPosition();
textArea.setSelectionEnd(end);
textArea.copy();
}

Java is wonderful. >> C/C++ only programmers

Java is wonderful. If anything, I could complain just a few things, like sometimes, they use English that is not entirely correct in the sense that makes me feel that some English speaker is making fun of Japanese. Maybe it is too much of programming on my part (^^;) but anything that is uncertain about the language is just painful. Here is this Java compiler error message. The error message must convey the precise meaning of what is going on in the program -- and not in other part of the world. ^^

Please remove or make sure it appears in the correct subdirectory of the classpath.

木曜日, 1月 24, 2008

Javaで書いたプログラムがもっとも古びず、長持ちする

けったいな意見の持ち主がいて、プログラムを書くと「マイクロソフトに利用されている」という。しかし、プログラムを書くのはプログラマで、プログラムを使うのはユーザーである。同様に、政治的意見を持つのは国民で、政治の結果をもろにかぶるのは市民である。

Javaで書いたプログラムがもっとも古びず、長持ちする。プログラマとしては他に換えがたいメリットがある。95で書いたプログラムは、もう目にする機会もない。

こちら、自作ソフトのページ↓です

Java Programming

月曜日, 1月 21, 2008

Suppose you want to use my OnScreenKeyboard class.

What is good about Java is, as they say -- write once, run anywhere. It means that the everything you write won't be wasted in a way that not only you can have it but also that you have it on your way. Java classes are extendable. Only thing that the programmer must be aware of is to set the classpath right.

In case you want to use my OnScreenKeyboard class, download (easai.jar).
The OnScreenKeyboard class is in easai.util package so import the package to use the class.

import easai.util.*;

...

OnScreenKeyboard keyboard
=new OnScreenKeyboard("Taiwanese",this);
keyboard.init();

OnScreenKeyboard constructor takes a string argument that specifies the language and an ActionListener. This ActionListener is a wonderful thing that makes this component usable for any of the application you wish.
You just have to set the ActionListener to receive all the output from the OnScreenKeyboard application.


public void actionPerformed (ActionEvent e)
{
String command=e.getActionCommand();
String ch=command.substring(0,1);
int pos=textArea.getCaretPosition();
textArea.insert(ch,pos);
}



Please have a look at this application that uses the OnScreenKeyboard class if what is describe above is not entirely clear to you.

/* TaiwaneseApplet.java -- Zhuyin on screen keyboard
*
* Copyright (C) 2008 Erica Asai

* This program is free software;
* you can redistribute it and/or
* modify it under the terms of the
* GNU General Public License
* as published by the Free Software
* Foundation; either version 2
* of the License, or (at your option)
* any later version.
*
* This program is distributed in
* the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without
* even the implied warranty of
* MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the
* GNU General Public License
* for more details.
*
* You should have received a copy
* of the GNU General Public License
* along with this program; if not,
* write to the Free Software
* Foundation, Inc., 59 Temple Place
* - Suite 330, Boston, MA 02111-1307, USA.
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import easai.util.*;

public class TaiwaneseApplet extends JApplet implements ActionListener
{
JTextArea textArea=new JTextArea(15,50);
JButton clear=new JButton("Clear");
JButton larger=new JButton("Font Size (+)");
JButton smaller=new JButton("Font Size (-)");
OnScreenKeyboard keyboard
=new OnScreenKeyboard("Taiwanese",this);

public void init()
{
keyboard.nButtons=10;
keyboard.init();

clear.addActionListener(this);
larger.addActionListener(this);
smaller.addActionListener(this);

JPanel control=new JPanel();
control.setLayout(new FlowLayout());
control.add(larger);
control.add(smaller);

JPanel textPanel=new JPanel();
JScrollPane textScroll=new JScrollPane(textArea);
textPanel.add(textScroll);
textPanel.add(clear);

JPanel panel=new JPanel();
BorderLayout layout=new BorderLayout();
panel.setLayout(layout);

JPanel keyboardPanel=new JPanel();
keyboardPanel.setLayout(new BorderLayout());
keyboardPanel.add(keyboard,BorderLayout.CENTER);
keyboardPanel.add(control,BorderLayout.SOUTH);

panel.add(keyboardPanel,BorderLayout.NORTH);
panel.add(textPanel,BorderLayout.CENTER);

JScrollPane scroll=new JScrollPane(panel);
add(scroll);
}

public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(0<command.length())
{
if(command.equals("Clear"))
{
textArea.setText("");
}
else if(command.equals("Font Size (+)"))
{
keyboard.resizeFont(2);
}
else if(command.equals("Font Size (-)"))
{
keyboard.resizeFont(-2);
}
else
{
String ch=command.substring(0,1);
int pos=textArea.getCaretPosition();
textArea.insert(ch,pos);
}
}
}
}



The way to set the classpath right is a bit technical. The package name of the OnScreenKeyboard class is "easai.util". As that all the Java classes are organized in the most familiar way of directories, You must store the class file in the directory named easai/util as follows.


+--easai--util
--+
+--your Java application directory


Then compile your Java application with a proper classpath option.


javac -classpath ".;.." *.java


Visit this site (http://easai.00freehost.com/Zhuyin/index.html) to see how it runs.

土曜日, 1月 19, 2008

Character.UnicodeBlock: To obtain the character set

In line with the InputContext article the other day to alter keyboard setting and the font range, I would like to present here the following code to obtain the character set and set the locale for the InputContext.

With the use of Character.UnicodeBlock.of(char), and the returned value of the UnicodeBlock information, the locale can be set -- manually. Suppose you have a JTextArea and the cursor is on the area of the text that you want to add some input.

Bopomofo (Zhuyin) is a set of alphabets that is universally used in Taiwan. In the following code, the character under the cursor is examined and the locale is set to Locale.TAIWAN if it is a Zhuyin character.


int pos=textArea.getCaretPosition();
String text=textArea.getText();
char ch=text.charAt(pos);
Character.UnicodeBlock block=Character.UnicodeBlock.of(ch);
Locale current=Locale.US;
if(block==Character.UnicodeBlock.BOPOMOFO)
current=Locale.TAIWAN;

木曜日, 1月 17, 2008

Sun Microsystems to buy MySQL

What does it mean for us, for Sun Microsystems to buy MySQL? The answer: BB+.

Sun Microsystems' ratings unaffected by agreed MySQL acquisition - S&P (2008-1-17)

But for MySQL users, it's a blessing. It means (probably) formal driver supports, documentation supports and package distribution under Java JDK.

I use MySQL with a jdbc driver and from Java application, DB access is as easy as ever. Just like Java. Here is how to open a database connection.


Class.forName(driver).newInstance();
connection = DriverManager.getConnection(openDBString+database,user,password);
statement=connection.createStatement();


I say, Java is wonderful. How about this? To browse through the database and print out whatever satisfies the condition, the following does the job.


ResultSet rs = statement.executeQuery("SELECT * FROM "+tableName+condition);
ResultSetMetaData meta = rs.getMetaData();
int nCol=meta.getColumnCount();
while (rs.next())
{
String buf=template;
for(int i=1;i<=nCol;i++)
{
String columnName=meta.getColumnName(i);
System.out.println(columnName,new String(rs.getBytes(i),"UTF8"));
}
}


I say, Java is wonderful.

...

I will declear new and then they must delete it. > obsolete programmers

But it is new you know.

...any problems? ^^

月曜日, 1月 14, 2008

Select and set a locale for InputContext from the available locales list

Yesterday, I wrote in this blog about InputContext and how to set the language environment. Here are some more codes for listing up all the available languages, from which you can select the Locale and set it for the InputContext for your application.


Locale locales[]=Locale.getAvailableLocales();


A word of caution is that not all listed locales are available for InputContext -- of those, only that has both the language code and the country code can be set to use. But once you have the array of strings for the available locales, this input dialog will do the job nicely to put all locales in scrollable pane and wait for user input. Here, names is the string array that contains the list of locales.


Object selectedValue = JOptionPane.showInputDialog(null,
"Select a locale", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
names,names[0]);


Perhaps it gets more clearer if you look at the code so I put it right here. Here all the locales that only have either the country code or the language code are filtered out. This is what this following code does: the list of locales will be presented and then when a value is selected, the locale will be set for the InputContext.

As of 2008-1-16, the code has been modified so that the list will be sorted out in alphabetical order.

   public void langList(JMenuItem mi)
{
int x=mi.getX();
x+=mi.getWidth();
int y=mi.getY();
Locale locales[]=Locale.getAvailableLocales();
String names[]=new String[locales.length];
String languages[]=new String[locales.length];
String countries[]=new String[locales.length];
int index=0;
for(int i=0;i&lt;locales.length;i++)
{
languages[index]=locales[i].getLanguage();
countries[index]=locales[i].getCountry();
if(!languages[index].isEmpty() && !countries[index].isEmpty())
{
names[index]=locales[i].getDisplayName();
index++;
}
}
int nLocales=index;
int sortedIndex[]=new int[nLocales];
String sortedNames[]=new String[nLocales];
for(int i=0;i&lt;nLocales;i++)
sortedIndex[i]=i;
for(int i=0;i&lt;nLocales;i++)
{
for(int j=i+1;j&lt;nLocales;j++)
{
if(0&lt;names[sortedIndex[i]].compareTo(names[sortedIndex[j]]))
{
int t=sortedIndex[i];
sortedIndex[i]=sortedIndex[j];
sortedIndex[j]=t;
}
}
}
for(int i=0;i&lt;nLocales;i++)
{
sortedNames[i]=names[sortedIndex[i]];
}
if(0&lt;nLocales)
{
Object selectedValue = JOptionPane.showInputDialog(null,
"Set Language Environment", "Language",
JOptionPane.INFORMATION_MESSAGE, null,
sortedNames,sortedNames[0]);
int i=0;
if(selectedValue!=null)
{
while(!((String)selectedValue).equals(names[i]) && ++i&lt;nLocales);
if(i&lt;nLocales)
{
Locale newLocale=new Locale(languages[i],countries[i]);
inputContext.selectInputMethod(newLocale);
}
}
}
}

日曜日, 1月 13, 2008

InputContext and IME

I had such an unpleasant time looking through pages about IME -- as if everybody is using languages that use alphabets only. But for the most of us who use not only alphabets but also other scripts, here are some pieces of advice. Just a few lines -- but you can control IME from Java applications.

InputContext inputContext=null;
inputContext=frame.getInputContext();
inputContext.selectInputMethod(Locale.JAPAN);

This turns on Japanese IME. If you alter the locale to Locale.TAIWAN, or instantiate the class with language and country codes as:
Locale locale=new Locale("hi","IN");

then you can turn on the IME of your choice from your Java application.

Qt: 外部プログラムを起動する

  Qt/C++ のアプリは、外部へ直接アクセスできます。これはネットアプリでは不可能な Qt のメリットです。 外部プログラムを起動することもできます。QProcess::startDetached() を使うと独立したプロセスを立ち上げることができます。 この QProces...