SWT Tips and Samples アプリケーション > 区点コード表

 

区点コード表

区点コード順に文字を表示するアプリケーションです。区点コード、JISコード、SJISコード、EUCコード、Unicodeを表示することができます。コード変換は、Java1.4から導入されたNew I/Oの機能を使っています。そのため動作させるにはJava1.4以上が必要です。

スクリーンショット

このアプリケーションは区点コードで定められた1区1点から94区94点までの文字を表示します。区ごとにページが分かれており、一つのページに94文字が表示されます。テーブル内の文字をクリックすると上部のパネルに各コードが表示されます。文字コードの算出は、CodeConverterというクラスを作って変換処理を行っています。

ソースコード (CodeTable.java)

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class CodeTable {

   Text charText;
   Font font;
   Label[] charLabels;
   int row = 1;

   Text kutenLabel;
   Text unicodeLabel;
   Text eucLabel;
   Text sjisLabel;
   Text jisLabel;

   private Label statusLabel;
   private Text pageText;
   public static void main(String[] args) {
      new CodeTable();
   }

   public CodeTable() {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText("CodeTable");

      font = new Font(display, "MS ゴシック", 35, SWT.NORMAL);

      GridData gd = null;

      shell.setLayout(new GridLayout(1, false));

      createCharInfoPanel(shell);
      createNavigationPanel(shell);
      
      ScrolledComposite sc =
         new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
      sc.setLayoutData(new GridData(GridData.FILL_BOTH));
      Composite c = new Composite(sc, SWT.NONE);
      GridLayout gl = new GridLayout(10, true);
      gl.marginHeight = 0;
      gl.marginWidth = 0;
      gl.verticalSpacing = 1;
      gl.horizontalSpacing = 1;
      c.setLayout(gl);

      Color bgColor = display.getSystemColor(SWT.COLOR_WHITE);
      Color fgColor = display.getSystemColor(SWT.COLOR_BLACK);

      charLabels = new Label[94];
      for (int i = 0; i < 94; i++) {
         Label charLabel = new Label(c, SWT.CENTER);
         charLabel.setBackground(bgColor);
         charLabel.setForeground(fgColor);
         charLabels[i] = charLabel;
         gd = new GridData(GridData.FILL_BOTH);
         charLabels[i].setLayoutData(gd);
         charLabels[i].addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
               Label b = (Label) e.getSource();
               updateStatus(b.getText().charAt(0));
            }
         });
      }
      createStatusPanel(shell);

      updateTable();

      sc.setContent(c);
      sc.setMinSize(300, 300);
      sc.setExpandHorizontal(true);
      sc.setExpandVertical(true);

      shell.setSize(500, 500);
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();
   }

   private void createStatusPanel(Composite parent) {
      statusLabel = new Label(parent, SWT.NORMAL);
      GridData gd = new GridData(GridData.FILL_HORIZONTAL);
      statusLabel.setLayoutData(gd);

   }

   private void createNavigationPanel(Composite parent) {
      Composite c = new Composite(parent, SWT.NONE);
      c.setLayout(new GridLayout(4, false));

      GridData gd;

      Button prevButton = new Button(c, SWT.PUSH);
      prevButton.setText("前へ");
      prevButton.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
            prevPage();
         }
      });

      pageText = new Text(c, SWT.BORDER | SWT.SINGLE | SWT.RIGHT);
      gd = new GridData();
      gd.widthHint = 30;
      pageText.setLayoutData(gd);
      pageText.addSelectionListener(new SelectionAdapter() {
         //Enterが押されたときにこのメソッドが呼ばれる
         public void widgetDefaultSelected(SelectionEvent e) {
            int page = Integer.parseInt(pageText.getText());
            if (page > 0 && page < 95) {
               row = page;
               updateTable();
            }
         }
      });
      pageText.forceFocus();

      Button nextButton = new Button(c, SWT.PUSH);
      nextButton.setText("次へ");
      nextButton.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent e) {
            nextPage();
         }
      });

   }
   private void nextPage() {
      row++;
      if (row > 94) {
         row = 1;
      }
      updateTable();
   }
   private void prevPage() {
      row--;
      if (row < 1) {
         row = 94;
      }
      updateTable();
   }

   private void updateTable() {
      for (int i = 0; i < 94; i++) {
         char c = CodeConverter.getCharFromKutenCode(row, i + 1);
         charLabels[i].setText("" + c);
      }
      pageText.setText("" + row);
      statusLabel.setText("" + row + "区1点から" + row + "区94点の文字を表示");
   }

   private void updateStatus(char c) {
      CodeConverter conv = new CodeConverter(c);
      charText.setText("" + c);
      kutenLabel.setText(conv.getKutenCode());
      sjisLabel.setText(conv.getSJISCode());
      jisLabel.setText(conv.getJISCode());
      eucLabel.setText(conv.getEUCCode());
      unicodeLabel.setText(conv.getUnicode());
   }

   private void createCharInfoPanel(Composite c) {
      Composite panel = new Composite(c, SWT.NONE);
      panel.setLayout(new GridLayout(5, false));
      GridData gd = new GridData(GridData.FILL_HORIZONTAL);
      gd.heightHint = 70;
      panel.setLayoutData(gd);

      charText = new Text(panel, SWT.BORDER | SWT.SINGLE);
      charText.setEditable(false);
      charText.setFont(font);
      charText.setText(" ");
      gd = new GridData();
      gd.verticalSpan = 3;
      gd.heightHint = 56;
      gd.widthHint = 56;
      charText.setLayoutData(gd);

      new Label(panel, SWT.NONE).setText("区点コード: ");
      kutenLabel = createCodeTextField(panel);

      new Label(panel, SWT.NONE).setText("JIS: ");
      jisLabel = createCodeTextField(panel);

      new Label(panel, SWT.NONE).setText("SJIS: ");
      sjisLabel = createCodeTextField(panel);

      new Label(panel, SWT.NONE).setText("EUC: ");
      eucLabel = createCodeTextField(panel);

      new Label(panel, SWT.NONE).setText("Unicode: ");
      unicodeLabel = createCodeTextField(panel);

   }

   private Text createCodeTextField(Composite c) {
      Text text = new Text(c, SWT.NONE | SWT.SINGLE);
      GridData gd = new GridData(GridData.FILL_BOTH);
      text.setLayoutData(gd);
      return text;
   }

}

class CodeConverter {

   static CharsetDecoder jisDecoder = null;
   static CharsetEncoder jisEncoder = null;
   static CharsetEncoder sjisEncoder = null;
   static CharsetEncoder eucEncoder = null;

   static {
      Charset charset = Charset.forName("x-JIS0208");
      jisDecoder = charset.newDecoder();
      jisEncoder = charset.newEncoder();

      charset = Charset.forName("windows-31j");
      sjisEncoder = charset.newEncoder();

      charset = Charset.forName("EUC-JP");
      eucEncoder = charset.newEncoder();

   }

   private int row;
   private int cell;
   private String kutenCode = "";
   private String jisCode = "";
   private String sjisCode = "";
   private String eucCode = "";
   private String unicode = "";
   private char ch;

   public CodeConverter(char c) {
      ch = c;
      decode(ch);
   }
   private void decode(char c) {
      CharBuffer cb = CharBuffer.wrap(new char[] { c });
      encodeToSJIS(cb);
      encodeToEUCCode(cb);
      encodeToJISCode(cb);
      encodeToUnicode();
   }
   private void encodeToSJIS(CharBuffer cb) {
      ByteBuffer bb = null;
      byte[] bytes;
      cb.clear();
      try {
         bb = sjisEncoder.encode(cb);
      } catch (CharacterCodingException e) {
         sjisCode = "未定義";
         return;
      }
      bytes = bb.array();
      sjisCode = "0x" + byteArray2String(bytes);

   }
   private void encodeToEUCCode(CharBuffer cb) {
      ByteBuffer bb = null;
      byte[] bytes;
      cb.clear();
      try {
         bb = eucEncoder.encode(cb);
      } catch (CharacterCodingException e) {
         eucCode = "未定義";
         return;
      }
      bytes = bb.array();
      eucCode = "0x" + byteArray2String(bytes);
   }
   private void encodeToJISCode(CharBuffer cb) {
      ByteBuffer bb = null;
      byte[] bytes;
      cb.clear();

      try {
         bb = jisEncoder.encode(cb);
      } catch (CharacterCodingException e) {
         jisCode = "未定義";
         kutenCode = "未定義";
         return;
      }
      bytes = bb.array();
      jisCode = "0x" + byteArray2String(bytes);

      //JIS code -> Kuten code
      if (bytes.length == 2) {
         row = bytes[0] - 32;
         cell = bytes[1] - 32;
         if (row < 1 || cell < 1 || row > 94 || cell > 94) {
            kutenCode = "";
         } else {
            kutenCode = "" + row + "-" + cell;
         }
      } else {
         kutenCode = "";
      }

   }
   private void encodeToUnicode() {
      //Char -> Unicode code
      unicode = "U+" + Integer.toString((int) ch, 16);
   }

   public static char getCharFromKutenCode(int row, int cell) {
      byte[] bytes = {(byte) (row + 32), (byte) (cell + 32)};
      ByteBuffer bb = ByteBuffer.wrap(bytes);
      CharBuffer cb = null;
      try {
         cb = jisDecoder.decode(bb);
      } catch (CharacterCodingException e) {
         return " ".charAt(0);
      }
      char ch = cb.charAt(0);
      return ch;
   }

   private String byteArray2String(byte[] bytes) {
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < bytes.length; i++) {
         byte b = bytes[i];
         int value;
         value = b < 0 ? b + 256 : b;
         String hexValue = Integer.toString(value, 16);
         hexValue = hexValue.length() == 1 ? "0" + hexValue : hexValue;
         sb.append(hexValue);
      }
      return sb.toString();
   }
   public char getChar() {
      return ch;
   }
   public String getKutenCode() {
      return kutenCode;
   }
   public String getEUCCode() {
      return eucCode;
   }
   public String getJISCode() {
      return jisCode;
   }
   public String getSJISCode() {
      return sjisCode;
   }
   public String getUnicode() {
      return unicode;
   }
}


最新更新日: 2004年8月28日
 
関連リンク
Eclipse API ドキュメント

- PR -

プレゼンテーション作成ソフト無料お試し版配信中

【Sony】大手他社よりも安い!ビジネス向け光・100Mしかも固定IP付!今なら更に初期費用最大15,000円OFF!

オフィス用品・オフィス家具 価 格 交 渉 可! 
◎ 目指せ★業界最安値 ★ ◎ オフィネット・ドットコム株式会社

注文から納品まで驚きの早さ!!【ASKULカタログ】はこちらから・・・

マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。


Copyright(C) 2003,2004 Jasmin Project. All Right Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送