FileDocCategorySizeDatePackage
Unicode.javaAPI DocExample9253Sun Feb 08 21:33:50 GMT 2004None

Unicode

public class Unicode extends JFrame
Unicode - show a page of Unicode characters. BUG: Times throws a bunch of exceptions on page 2 and 3, that can not be caught as they occur in the AWT thread. On some platforms. TODO - remove GoToPage as a dialog, move its textfield into main GUI.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: Unicode.java,v 1.7 2004/02/09 03:33:50 ian Exp $

Fields Summary
protected final int
COLUMNS
protected final int
ROWS
protected int
startNum
the unicode char at start of current page
protected final int
QUADSIZE
protected JLabel[]
buttons
the buttons that display the characters
protected JLabel
fontName
the font name display
protected JLabel[]
rowLabs
the row labels, in a column at the left
protected GoToPage
gotoPageUI
The page chooser pop-up
protected final int
FONTSIZE
How git to make the font samples
Constructors Summary
public Unicode()
Construct the object including its GUI


	       
	  
		super("Unicode");

		Container cp = getContentPane();

		// Used both for Buttons and Menus
		ResourceBundle b = ResourceBundle.getBundle("UnicodeWidgets");

		JButton quitButton, nextButton, prevButton;
		Panel p = new Panel();
		// Make a grid, add one for labels.
		p.setLayout(new GridLayout(ROWS+1, COLUMNS+1));
		DecimalFormat df2d = new DecimalFormat("00");

		// Add first row, just column labels.
		p.add(new JLabel(""));
		for (int i = 0; i<COLUMNS; i++)
			p.add(new JLabel(Integer.toString(i, 16), JLabel.CENTER));

		// Add subsequent rows, each with an offset label
		for (int i = 0; i<ROWS; i++) {
			JLabel l = new JLabel("0000");	// room for max, i.e. \uFFFF
			p.add(l);
			rowLabs[i] = l;
			for (int j = 0 ; j < COLUMNS; j++) {
				JLabel pb = new JLabel(" ");
				buttons[j][i] = pb;
				p.add(pb);
			}
		}

		// ActionListeners for jumping around; used by buttons and menus
		ActionListener firster = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gotoPage(startNum = 0);
			}
		};
		ActionListener previouser = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			if (startNum > 0)
				gotoPage(startNum -= QUADSIZE);
			}
		};
		ActionListener nexter = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			if (startNum < 65535)
				gotoPage(startNum += QUADSIZE);
			}
		};
		ActionListener laster = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gotoPage(65536 - QUADSIZE);
			}
		};

		cp.add(BorderLayout.NORTH, p);
		fontName = new JLabel("Default font", JLabel.CENTER);
		cp.add(BorderLayout.CENTER, fontName);
		Panel q = new Panel();
		cp.add(BorderLayout.SOUTH, q);
		q.add(prevButton = mkButton(b, "page.prev"));
		prevButton.addActionListener(previouser);

		q.add(nextButton = mkButton(b, "page.next"));
		nextButton.addActionListener(nexter);

		q.add(quitButton = mkButton(b, "exit"));
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				dispose();
				System.exit(0);
			}
		});
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				dispose();
				System.exit(0);
			}
		});

		MenuItem mi;		// used in various spots

		MenuBar mb = new MenuBar();
		setMenuBar(mb);

		String titlebar;
		try { titlebar = b.getString("program"+".title"); }
		catch (MissingResourceException e) { titlebar="Unicode Demo"; }
		setTitle(titlebar);

		ActionListener fontSelector = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String font = e.getActionCommand();
				mySetFont(font, FONTSIZE);
			}
		};

		Menu fontMenu = mkMenu(b, "font");
		// String[] fontList = Toolkit.getDefaultToolkit().getFontList();
		String[] fontList = GraphicsEnvironment.getLocalGraphicsEnvironment().
			getAvailableFontFamilyNames();
		for (int i=0; i<fontList.length; i++) {
			fontMenu.add(mi = new MenuItem(fontList[i]));
			mi.addActionListener(fontSelector);
		}
		mb.add(fontMenu);

		gotoPageUI = new GoToPage("Unicode Page");
		centre(gotoPageUI);

		Menu vm = mkMenu(b,  "page");
		vm.add(mi = mkMenuItem(b, "page", "first"));
		mi.addActionListener(firster);
		vm.add(mi = mkMenuItem(b, "page", "prev"));
		mi.addActionListener(previouser);
		vm.add(mi = mkMenuItem(b, "page", "next"));
		mi.addActionListener(nexter);
		vm.add(mi = mkMenuItem(b, "page", "last"));
		mi.addActionListener(laster);
		vm.add(mi = mkMenuItem(b, "page", "goto"));
		mi.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Unicode.this.gotoPageUI.setVisible(true);
			}
		});
		mb.add(vm);

		Menu hm = mkMenu(b,  "help");
		hm.add(mi = mkMenuItem(b, "help", "about"));
		mb.setHelpMenu(hm);		// needed for portability (Motif, etc.).

		pack();
		// After packing the Frame, centre it on the screen.
		centre(this);

		// start at a known place
		mySetFont(fontList[0], FONTSIZE);
		gotoPage(startNum);
	
Methods Summary
public voidcentre(java.awt.Window c)

		Dimension us = c.getSize(), 
			them = Toolkit.getDefaultToolkit().getScreenSize();
		int newX = (them.width - us.width) / 2;
		int newY = (them.height- us.height)/ 2;
		c.setLocation(newX, newY);
	
private voidgotoPage(int startNum)
Go to a given page of Unicode. At present the parameter is a code value, but it should be a page #.

		// System.out.println("startAt(" + startNum + ")");
		char chars[] = new char[1];
		for (int i = 0; i<ROWS; i++) {
			JLabel l = rowLabs[i];
			// System.out.println("i=" + i + ", JLabel=" + l);
			l.setText(Integer.toString(startNum+(i*COLUMNS), 16));
			// l.validate();		// size may be too big now
			for (int j = 0; j<COLUMNS; j++) {
				chars[0] = (char)(startNum+((j*ROWS)+i));
				JLabel b = buttons[i][j];
				b.setText(new String(chars));
			}
		}
		repaint();
	
public static voidmain(java.lang.String[] av)
"main program" method - construct and show

		// create a Unicode object, tell it to show up
		new Unicode().setVisible(true);
	
public javax.swing.JButtonmkButton(java.util.ResourceBundle b, java.lang.String name)
Convenience routine to make a Button

		String label;
		try { label = b.getString(name+".label"); }
		catch (MissingResourceException e) { label=name; }
		return new JButton(label);
	
public java.awt.MenumkMenu(java.util.ResourceBundle b, java.lang.String name)
Convenience routine to make a Menu

		String menuLabel;
		try { menuLabel = b.getString(name+".label"); }
		catch (MissingResourceException e) { menuLabel=name; }
		return new Menu(menuLabel);
	
public java.awt.MenuItemmkMenuItem(java.util.ResourceBundle b, java.lang.String menu, java.lang.String name)
Convenience routine to make a MenuItem

		String miLabel;
		try { miLabel = b.getString(menu + "." + name + ".label"); }
		catch (MissingResourceException e) { miLabel=name; }
		String key = null;
		try { key = b.getString(menu + "." + name + ".key"); }
		catch (MissingResourceException e) { key=null; }

		if (key == null)
			return new MenuItem(miLabel);
		else
			return new MenuItem(miLabel, new MenuShortcut(key.charAt(0)));
	
private voidmySetFont(java.lang.String font, int sz)

		fontName.setText("Font = " + font);
		Font f = new Font(font, Font.PLAIN, sz);
		for (int i = 0; i<ROWS; i++) {
			for (int j = 0; j<COLUMNS; j++)
				buttons[i][j].setFont(f);
		}
		repaint();