import uc.*; import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MainUCFrame extends JFrame implements ActionListener, TacOrdMessageable { JMenuBar menuBar; JTextArea outputArea; JScrollPane scrollPane; JMenuItem continueOnExitButton; JMenuItem endOnExitButton; JMenuItem allowPornographicButton; public MainUCFrame() { setTitle( "UC - " + Commando.getVersion() ); menuBar = new JMenuBar(); menuBar.setBorder( BorderFactory.createRaisedBevelBorder() ); JMenu onExitMenu = new JMenu( "On exit..." ); menuBar.add( onExitMenu ); continueOnExitButton = new JRadioButtonMenuItem( "run in background" ); endOnExitButton = new JRadioButtonMenuItem( "end process" ); onExitMenu.add( continueOnExitButton ); onExitMenu.add( endOnExitButton ); continueOnExitButton.addActionListener( this ); continueOnExitButton.addActionListener( this ); ButtonGroup onExitGroup = new ButtonGroup(); onExitGroup.add( continueOnExitButton ); onExitGroup.add( endOnExitButton ); JMenu allowMenu = new JMenu( "Allow against..." ); menuBar.add( allowMenu ); allowPornographicButton = new JCheckBoxMenuItem( "pornographic sites" ); allowMenu.add( allowPornographicButton ); allowPornographicButton.addActionListener( this ); outputArea = new JTextArea(); outputArea.setEditable( false ); outputArea.setWrapStyleWord( true ); outputArea.setLineWrap( true ); outputArea.setBackground( new Color( 0, 0, 0, 255 ) ); outputArea.setForeground( new Color( 255, 0, 0, 255 ) ); scrollPane = new JScrollPane( outputArea ); JPanel topPanel = new JPanel( new BorderLayout() ); topPanel.add( menuBar, BorderLayout.NORTH ); topPanel.add( scrollPane, BorderLayout.CENTER ); getContentPane().add( topPanel ); pack(); setSize( 400, 250 ); setResizable( true ); this.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { savePreferences(); if( endOnExitButton.isSelected() ) System.exit( 0 ); } } ); } public void actionPerformed( ActionEvent event ) { } public void out( String text ) { synchronized( outputArea ) { outputArea.setText( outputArea.getText() + "\n\n" + text ); trimText(); scrollToBottom(); } } public void out( Object[] text ) { if( text == null ) return; synchronized( outputArea ) { String t = outputArea.getText() + "\n"; for( int i = 0; i < text.length; i++ ) if( text[ i ] != null ) t = t + "\n" + text[ i ].toString(); outputArea.setText( t ); trimText(); scrollToBottom(); } } public synchronized int getAllowCode() { int code = 0; code |= allowPornographicButton.isSelected() ? TacOrd.ALLOW_PORNOGRAPHIC : 0; return code; } public synchronized boolean getContinueOnExit() { return continueOnExitButton.isSelected(); } public synchronized void loadPreferences() { ArrayList text = new ArrayList(); int allowCode = 0; boolean continueOnExit = true; try { text.add( "Opening " + Commando.PREF_FILE + " ..." ); FileInputStream fileIn = new FileInputStream( Commando.PREF_FILE ); DataInputStream dataIn = new DataInputStream( fileIn ); int major = dataIn.readInt(); int minor = dataIn.readInt(); if( major == Commando.MAJORVERSION && minor == Commando.MINORVERSION ) { allowCode = dataIn.readInt(); continueOnExit = dataIn.readBoolean(); text.add( "Read preferences" ); } else { text.add( "Preferences file is for a different version(" + major + '.' + minor + ')' ); } } catch( IOException e ) { text.add( "IOException while reading " + Commando.PREF_FILE ); } if( ( allowCode & TacOrd.ALLOW_PORNOGRAPHIC ) == TacOrd.ALLOW_PORNOGRAPHIC ) text.add( "ALLOW pornographic orders" ); else text.add( "DISALLOW pornographic orders" ); if( continueOnExit ) text.add( "UC process RUNS IN BACKGROUND upon window closure" ); else text.add( "UC process TERMINATES upon window closure" ); continueOnExitButton.setSelected( continueOnExit ); endOnExitButton.setSelected( !continueOnExit ); allowPornographicButton.setSelected( ( allowCode & TacOrd.ALLOW_PORNOGRAPHIC ) == TacOrd.ALLOW_PORNOGRAPHIC ); out( text.toArray() ); } void savePreferences() { try { FileOutputStream fileOut = new FileOutputStream( Commando.PREF_FILE ); DataOutputStream dataOut = new DataOutputStream( fileOut ); dataOut.writeInt( Commando.MAJORVERSION ); dataOut.writeInt( Commando.MINORVERSION ); dataOut.writeInt( getAllowCode() ); dataOut.writeBoolean( getContinueOnExit() ); } catch( IOException e ) { // can't save??? } } public void tacOrdOut( Object[] text ) { out( text ); } void trimText() { final int MAX_LENGTH = 8192; String text = outputArea.getText(); if( text.length() > MAX_LENGTH ) { outputArea.setText( text.substring( text.length() - MAX_LENGTH ) ); } } void scrollToBottom() { JViewport view = scrollPane.getViewport(); int extentHeight = view.getExtentSize().height; int viewHeight = view.getViewSize().height; if( viewHeight > extentHeight ) { Point p = new Point( 0, viewHeight - extentHeight ); view.setViewPosition( p ); scrollPane.repaint(); } } }