/* * [Converter.java] * * Summary: Converter Amanuensis : shows you how to interconvert basic Java types. * * Copyright: (c) 1997-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 1997-12-17 initial release. * 1.1 1998-01-03 clean up minor typos * test routine to ensure all * suggested code compiles. * examples all initialise to make clean compiles. * generates HTML table for those without Java. * 1.2 1998-01-04 make into both application and applet * 1.3 1998-01-13 split into two classes. * don't show initialisation, except for test. * 1.4 1998-02-14 descriptions now to, from order. * show NumberFormatExceptions * box a little wider. * get rid of nugatory setSize, setBounds * remove FontSaver. * 1.5 1998-05-08 Activator html * tidy with SlickEdit * compile under Symantec Visual Cafe 2.5 * 1.6 1998-06-23 rename SymItem to TheListener. * 1.7 1998-08-07 add +-'0' to char conversion options. * 1.8 1998-11-11 new address and phone. * 1.9 1998-11-29 generate better HTML, with &amp &quote etc. * 2.0 1998-12-13 add "alternatively" comments. * add radix conversions on byte, char, short * 2.1 1998-12-13 increase size of Window * 2.2 1998-12-14 switch over to using a jar file. * check Java version * slightly different colour scheme. * use com.mindprod.converter package. * 2.3 1998-12-25 misc comment and HTML tidying. * 2.4 1999-01-29 Double.parseDouble and Float.parseFloat for JDK 1.2 * 2.5 1999-08-14 add *best* comment to align better with *or * new phone number. * 2.6 1999-08-23 DecimalFormat for float, Float, double and Double * 2.7 1999-09-19 add Menu AboutBox * 2.8 1999-10-05 rename jdk 2 back to jdk 1.2 * 2.9 1999-10-18 prefer new XXXX to XXXX.valueOf * remove trim requirement on Double.valueOf(String); * 3.0 1999-12-03 toHexString toBinaryString toOctalString * 3.1 2001-04-26 faster boolean --> Boolean * 3.2 2001-06-21 prefer valueOf to toString * 3.3 2001-08-01 add comments about when sign extension occurs. * 3.4 2001-12-01 improved boolean -> String : String.valueOf(t); * 3.5 2002-04-01 add new way to convert String -> boolean * 3.6 2002-04-05 lower case package name, following Sun conventions * 3.7 2002-04-22 notes on locale-sensitivity * 3.8 2004-05-07 add JDK 1.5 boxing-unboxing int<->Integer etc. * short <-> Integer. * add Short type. * 3.9 2004-05-10 add IEEE conversions longBitsToDouble etc. * 4.0 2004-05-30 add about box * 4.1 2004-06-28 better labelling on generated HTML * 4.2 2004-07-07 notes about format rounding. * 4.3 2005-06-17 improved consistency of bat files. * 4.4 2005-12-04 more instructions * 4.5 2005-12-09 use of JDK 1.5+ Integer.valueOf(int) and * Long.valueOf(long) * 4.6 2005-12-15 use of Character.digit( char c, int radix ) * 4.7 2005-12-16 use of + '0' to get to char. * \n after each comment to make code easier to read * 4.8 2006-03-04 reformat with IntelliJ, add Javadoc * 4.9 2007-05-08 add PAD files and a new icon. * 5.0 2008-03-03 correct spelling mistakes, display build and release date. Convert to JDK 1.5 * 5.1 2008-03-25 use enums, split off Type and Generate classes. add signed/unsigned Byte. * 5.2 2008-03-26 better code for unsigned Bytes. * 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * correct table entries for unsigned conversions that could not be handled purely with autoboxing. * 5.4 2011-12-03 configurable Look and Feel * 5.5 2011-12-30 tweaked some conversions involving Integer, Long and unsigned. */ package com.mindprod.converter; import com.mindprod.common18.Build; import com.mindprod.common18.CMPAboutJBox; import com.mindprod.common18.FontFactory; import com.mindprod.common18.HybridJ; import com.mindprod.common18.Laf; import com.mindprod.common18.VersionCheck; import javax.swing.JApplet; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JTextArea; 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.awt.event.ItemEvent; import java.awt.event.ItemListener; import static com.mindprod.converter.Type.how; /* * TODO: use JPrep to embed HTML markup in code samples. */ /** * Converter Amanuensis : shows you how to interconvert basic Java types. *

* The CMP Conversion Amanuensis Shows how to interconvert the 17 basic types: boolean, byte, unsigned byte, short, * char, int, long, float, double, String, Boolean, Short, Character, Integer, Long, Float, Double Converter.java is the * GUI shell portion, ConvertGrid.java is the guts. * * @author Roedy Green, Canadian Mind Products * @version 5.5 2011-12-30 tweaked some conversions involving Integer, Long and unsigned. * @since 1997-12-17 */ public final class Converter extends JApplet { /** * true if you want additional debugging output. */ private static final boolean DEBUGGING = false; /** * height of Applet box in pixels. Does not include surrounding frame. */ private static final int APPLET_HEIGHT = 552; /** * Width of Applet box in pixels. */ private static final int APPLET_WIDTH = 582; private static final int FIRST_COPYRIGHT_YEAR = 1997; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1997-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2011-12-30"; private static final String TITLE_STRING = "Conversion Amanuensis"; private static final String VERSION_STRING = "5.5"; /** * Applet background a light pink */ private static final Color BACKGROUND_FOR_APPLET = new Color( 0xfff0f0 ); /** * background colour for instructions. Default grey is too dark */ private static final Color BACKGROUND_FOR_INSTRUCTIONS = new Color( 0xf8f8f8 ); private static final Color FOREGROUND_FOR_INSTRUCTIONS = new Color( 0x008000 ); private static final Color FOREGROUND_FOR_LABEL = new Color( 0x0000b0 ); // Use our own colours. private static final Color FOREGROUND_FOR_RESULT = Color.BLACK; /** * for titles */ private static final Color FOREGROUND_FOR_TITLE = new Color( 0xdc143c ); /** * instructions font */ private static final Font FONT_FOR_INSTRUCTIONS = com.mindprod.common18.FontFactory.build( "Dialog", Font.PLAIN, 12 ); /** * for for titles and About buttons */ private static final Font FONT_FOR_TITLE = FontFactory.build( "Dialog", Font.BOLD, 16 ); /** * for for title second line */ private static final Font FONT_FOR_TITLE2 = FontFactory.build( "Dialog", Font.PLAIN, 14 ); // DECLARE COMPONENTS /** * whether to use JDK 1.5 autoboxing */ private JCheckBox autoboxing; /** * type of the source */ private JComboBox from; /** * type of the target */ private JComboBox to; /** * = decoration */ private JLabel equalLabel; /** * label from */ private JLabel fromLabel; /** * title */ private JLabel title; /** * title, second line */ private JLabel title2; /** * label to */ private JLabel toLabel; /** * first line of instructions */ private JTextArea instructions; /** * generated code to do the conversion */ private JTextArea results; /** * build a menu with Look & Feel and About across the top */ private void buildMenu() { // turn on anti-alias System.setProperty( "swing.aatext", "true" ); final JMenuBar menubar = new JMenuBar(); setJMenuBar( menubar ); final JMenu lafMenu = Laf.buildLookAndFeelMenu(); if ( lafMenu != null ) { menubar.add( lafMenu ); } final JMenu menuHelp = new JMenu( "Help" ); menubar.add( menuHelp ); final JMenuItem aboutItem = new JMenuItem( "About" ); menuHelp.add( aboutItem ); aboutItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { // open about frame new CMPAboutJBox( TITLE_STRING, VERSION_STRING, "Generates Java source to interconvert basic types,", "e.g. double <=> String", "freeware", RELEASE_DATE, FIRST_COPYRIGHT_YEAR, "Roedy Green", "CONVERTER", "1.8" ); } } ); } /** * create and initialise all the components */ private void createComponents() { autoboxing = new JCheckBox( "autoboxing", false ); title = new JLabel( TITLE_STRING + " " + VERSION_STRING ); title.setFont( FONT_FOR_TITLE ); title.setForeground( FOREGROUND_FOR_TITLE ); title2 = new JLabel( "released:" + RELEASE_DATE + " build:" + Build.BUILD_NUMBER ); title2.setFont( FONT_FOR_TITLE2 ); title2.setForeground( FOREGROUND_FOR_TITLE ); to = new JComboBox<>( Type.values() ); // turn off the write-in feature to.setEditable( false ); toLabel = new JLabel( "to", JLabel.CENTER ); toLabel.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) ); toLabel.setForeground( FOREGROUND_FOR_LABEL ); from = new JComboBox<>( Type.values() ); // turn off the write-in feature from.setEditable( false ); fromLabel = new JLabel( "from", JLabel.CENTER ); fromLabel.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) ); fromLabel.setForeground( FOREGROUND_FOR_LABEL ); equalLabel = new JLabel( "=", JLabel.CENTER ); equalLabel.setFont( FontFactory.build( "Dialog", Font.BOLD, 15 ) ); equalLabel.setForeground( FOREGROUND_FOR_LABEL ); results = new JTextArea( "", 0, 0 ); results.setEditable( false ); results.setFont( FontFactory.build( "Dialog", Font.PLAIN, 15 ) ); results.setForeground( FOREGROUND_FOR_RESULT ); instructions = new JTextArea( "Select the \"to\" target type on the left and the \"from\" source type on the right.\n" + "Then check if you want JDK 1.5+ autoboxing. Then copy/paste one of the\n" + "suggested conversion techniques into your own code." ); instructions.setForeground( FOREGROUND_FOR_INSTRUCTIONS ); instructions.setBackground( BACKGROUND_FOR_INSTRUCTIONS ); instructions.setEditable( false ); instructions.setMargin( new Insets( 4, 4, 4, 4 ) ); instructions.setFont( FONT_FOR_INSTRUCTIONS ); // Start display off at something interesting: from.setSelectedItem( Type.FLOAT ); to.setSelectedItem( Type.WRAPPEDDOUBLE ); results.setText( how( Type.FLOAT, Type.WRAPPEDDOUBLE, false /* no autobox to start */ ) ); } /** * hook up components to listeners */ private void hookComponents() { ItemListener theListener = new ItemListener() { /** * Notice any change to one of the list box selectors. * @param event details of just what the user clicked. */ public void itemStateChanged( ItemEvent event ) { results.setText( how( ( Type ) from.getSelectedItem(), ( Type ) to.getSelectedItem(), autoboxing.isSelected() ) ); } }; // hook up so display will change if any widgets touched. from.addItemListener( theListener ); to.addItemListener( theListener ); autoboxing.addItemListener( theListener ); } /** * add fields to layout * * @param contentPane where to put the fields */ private void layoutGridBag( Container contentPane ) { // layout // ----0--1----- 2----- 3---- // 0 -----------title title2 0 // 2 to from---------------- 2 // 3 autobox "to" = "from" - 3 // 4 ---- results----------- 4 // 5 ---- instructions ---- 5 // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( title, new GridBagConstraints( 0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets( 10, 10, 10, 5 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( title2, new GridBagConstraints( 3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets( 10, 5, 10, 10 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( to, new GridBagConstraints( 0, 2, 2, 1, .5, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 10, 5, 0 ), 150, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( from, new GridBagConstraints( 3, 2, 1, 1, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 0, 5, 10 ), 150, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( autoboxing, new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets( 0, 10, 5, 0 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( toLabel, new GridBagConstraints( 1, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 5, 0 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( equalLabel, new GridBagConstraints( 2, 3, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 5, 10 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( fromLabel, new GridBagConstraints( 3, 3, 1, 1, 0.0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 0, 0, 5, 0 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( results, new GridBagConstraints( 0, 4, 4, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0, 10, 5, 10 ), 0, 0 ) ); // x y w h wtx wty anchor fill T L B R padx pady contentPane.add( instructions, new GridBagConstraints( 0, 5, 4, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets( 0, 10, 10, 10 ), 0, 0 ) ); } /** * Allow this Applet to run as as application as well. * * @param args command line arguments ignored. */ public static void main( String args[] ) { HybridJ.fireup( new Converter(), TITLE_STRING + " " + VERSION_STRING, APPLET_WIDTH, APPLET_HEIGHT ); } // end main /** * Called by the browser or Applet viewer to inform * this Applet that it is being reclaimed and that it should destroy * any resources that it has allocated. */ @Override public void destroy() { autoboxing = null; equalLabel = null; from = null; fromLabel = null; instructions = null; results = null; title = null; title2 = null; to = null; toLabel = null; } /** * Called by the browser or Applet viewer to inform * this Applet that it has been loaded into the system. */ @Override public void init() { // needs 1.2 to use Entities which use ArrayList. if ( !VersionCheck.isJavaVersionOK( 1, 8, 0, this ) ) { // effectively abort return; } buildMenu(); Container contentPane = this.getContentPane(); contentPane.setBackground( BACKGROUND_FOR_APPLET ); contentPane.setLayout( new GridBagLayout() ); createComponents(); hookComponents(); layoutGridBag( contentPane ); this.validate(); this.setVisible( true ); } // end init }