/* * [OfficialEncoding.java] * * Summary: Displays the official name of any alias encoding, letting your know if it is supported. * * Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2007-07-06 initial release * 1.1 2008-04-05 add build to title. */ package com.mindprod.officialencoding; import com.mindprod.common18.Build; import com.mindprod.common18.CMPAboutJBox; import com.mindprod.common18.Common18; import com.mindprod.common18.FontFactory; import com.mindprod.common18.HybridJ; import com.mindprod.common18.JEButton; import com.mindprod.common18.VersionCheck; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.nio.charset.Charset; /** * Displays the official name of any alias encoding, letting your know if it is supported. *

* It also displays some sample * characters available in that encoding. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2008-04-05 add build to title. * @since 2007-07-06 */ public final class OfficialEncoding extends JApplet { /** * height of Applet box in pixels. Does not include surrounding frame. */ private static final int APPLET_HEIGHT = 254; /** * Width of Applet box in pixels. */ private static final int APPLET_WIDTH = 636; private static final int FIRST_COPYRIGHT_YEAR = 1996; /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2008-04-05"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String TITLE_STRING = "Official Encoding"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String VERSION_STRING = "1.1"; /** * background colour, pale green to match website */ private static final Color BACKGROUND_FOR_BODY = Build.BACKGROUND_FOR_BLENDING; /** * instruction normal color */ private static final Color FOREGROUND_FOR_INSTRUCTIONS = new Color( 0x339911 ); /** * foreground colour for labels */ private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 ); /** * for titles */ private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c ); /** * background colour for official name. Default grey is too dark */ private static final Color UNBACKGROUND_FOR_EDITABLE = new Color( 0xf8f8f8 ); /** * charset display font */ private static final Font FONT_FORS = FontFactory.build( "Monospaced", Font.PLAIN, 12 ); /** * instructions font */ private static final Font FONT_FOR_INSTRUCTIONS = FontFactory.build( "Dialog", Font.PLAIN, 12 ); /** * title font */ private static final Font FONT_FOR_TITLE = FontFactory.build( "Dialog", Font.BOLD, 15 ); /** * for for title second line */ private static final Font FONT_FOR_TITLE2 = FontFactory.build( "Dialog", Font.PLAIN, 14 ); /** * byte form of test pattern to display charSet */ private static final byte[] byteTestPattern; static { final int rows = 4; final int cols = 64; byteTestPattern = new byte[ rows * ( cols + 1 ) - 1 ]; int b = 0; for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < cols; j++ ) { byteTestPattern[ i * ( cols + 1 ) + j ] = ( byte ) Math.max( 32, b++ ); } } for ( int i = 0; i < rows - 1; i++ ) { // add \n to end of rows but the last byteTestPattern[ i * ( cols + 1 ) + cols ] = '\n'; } } /** * about button */ private JButton about; /** * hit to look up encoding */ private JButton okButton; /** * label for alias encoding name */ private JLabel aliasLabel; /** * how to use program */ private JLabel instructions; /** * label for official encoding name */ private JLabel officialLabel; /** * title */ private JLabel title; /** * title, second line */ private JLabel title2; /** * display first 256 chars */ private JTextArea charSetDisplay; /** * encoding user enters */ private JTextField alias; /** * official name of the encoding */ private JTextField official; /** * allocate all components */ private void buildComponents() { title = new JLabel( TITLE_STRING + " " + VERSION_STRING, JLabel.LEFT ); title.setForeground( FOREGROUND_FOR_TITLE ); title.setFont( FONT_FOR_TITLE ); title2 = new JLabel( "released:" + RELEASE_DATE + " build:" + Build.BUILD_NUMBER ); title2.setFont( FONT_FOR_TITLE2 ); title2.setForeground( FOREGROUND_FOR_TITLE ); about = new JEButton( "About" ); about.setToolTipText( "About " + TITLE_STRING + " " + VERSION_STRING ); aliasLabel = new JLabel( "encoding name" ); aliasLabel.setForeground( FOREGROUND_FOR_LABEL ); officialLabel = new JLabel( "official encoding name" ); officialLabel.setForeground( FOREGROUND_FOR_LABEL ); alias = new JTextField(); alias.setEditable( true ); // avoid looking at restricted system property. String defaultEncodingName = Charset. defaultCharset(). name(); alias.setText( defaultEncodingName ); okButton = new JEButton( "Convert" ); okButton.setToolTipText( "Convert encoding to official name" ); official = new JTextField(); official.setBackground( UNBACKGROUND_FOR_EDITABLE ); official.setEditable( false ); charSetDisplay = new JTextArea( 16, 16 ); charSetDisplay.setBackground( UNBACKGROUND_FOR_EDITABLE ); charSetDisplay.setEditable( false ); charSetDisplay.setFont( FONT_FORS ); instructions = new JLabel( "Enter an encoding name and click OK. View the corresponding official equivalent.", JLabel.CENTER ); instructions.setForeground( FOREGROUND_FOR_INSTRUCTIONS ); instructions.setFont( FONT_FOR_INSTRUCTIONS ); } /** * look up the official name of the alias */ private void getOfficialName() { try { final String canonicalName = Charset.forName( alias.getText().trim() ).name(); official.setText( canonicalName ); charSetDisplay.setText( new String( byteTestPattern, canonicalName ) ); } catch ( Exception exception ) { official.setText( "not supported" ); } } /** * register listeners */ private void hookComponents() { about.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // open aboutbox frame new CMPAboutJBox( TITLE_STRING, VERSION_STRING, "Looks up the official name of an encoding.", "", "freeware", RELEASE_DATE, FIRST_COPYRIGHT_YEAR, "Roedy Green", "OFFICIALENCODING", "1.8" ); } } ); okButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { getOfficialName(); } } ); // trigger also if hits enter alias.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { getOfficialName(); } } ); } /** * layout * * @param contentPane JApplet contentPage */ private void layoutComponents( Container contentPane ) { // --0----------1-------2-----3-- // 0 title----- --title2--- about // 1 "encoding" "official"------ // 2 encoding ok official-------- // 3 --- instructions ----------- // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( title, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 10, 10, 5, 5 ), 0, 0 ) ); contentPane.add( title2, new GridBagConstraints( 1, 0, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 10, 5, 5, 5 ), 0, 0 ) ); contentPane.add( about, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 10, 5, 5, 10 ), 0, 0 ) ); contentPane.add( aliasLabel, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 0, 10, 0, 10 ), 0, 0 ) ); contentPane.add( officialLabel, new GridBagConstraints( 3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 0, 10, 0, 10 ), 0, 0 ) ); contentPane.add( alias, new GridBagConstraints( 0, 2, 1, 1, 50.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 10 ), 0, 0 ) ); contentPane.add( okButton, new GridBagConstraints( 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 5, 10, 5, 10 ), 0, 0 ) ); contentPane.add( official, new GridBagConstraints( 2, 2, 2, 1, 50.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets( 5, 10, 5, 10 ), 0, 0 ) ); contentPane.add( charSetDisplay, new GridBagConstraints( 0, 3, 4, 1, 0.0, 100.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 10, 5, 10 ), 0, 0 ) ); contentPane.add( instructions, new GridBagConstraints( 0, 4, 4, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 10, 10, 10 ), 0, 0 ) ); } /** * Allow this Applet to run as an application as well. * * @param args command line arguments ignored. */ public static void main( String args[] ) { HybridJ.fireup( new OfficialEncoding(), TITLE_STRING + " " + VERSION_STRING /* too narrow for usual title */, APPLET_WIDTH, APPLET_HEIGHT ); } // end main /** * Called by the browser or Applet viewer to inform * this Applet that it has been loaded into the system. */ @Override public void init() { // use JDK 1.5 so can do unsigned lookup of default encoding. if ( !VersionCheck.isJavaVersionOK( 1, 8, 0, this ) ) { return; } Common18.setLaf(); Container contentPane = this.getContentPane(); contentPane.setLayout( new GridBagLayout() ); contentPane.setBackground( BACKGROUND_FOR_BODY ); buildComponents(); layoutComponents( contentPane ); hookComponents(); getOfficialName(); this.validate(); this.setVisible( true ); } }