📄 loaderframe.java
字号:
package loader.main;import javax.swing.*;import java.awt.*;import java.awt.event.*;import loader.viewer.*;import loader.manager.*;import loader.recorder.*;import java.net.*;import java.io.*;import java.util.*;public class LoaderFrame extends JFrame implements ActionListener{ public static Properties pros; static{ pros = new Properties(); try{ pros.load( new FileInputStream( new File( "conf/loader.pros" ) ) ); }catch( Exception e ){ System.err.println( "when loading conf file, failed." ); e.printStackTrace(); System.exit( -1 ); } } private Progress progress; private JButton stop; private JButton log; private JButton reload; private JTextArea logText; private JLabel description; private LoadByURLDialog lburld; private LoadByRecorderDialog lbrecd; private LoaderManager lm; private File logFile; public LoaderFrame( String title ){ super( title ); lm = null; logFile = null; lburld = new LoadByURLDialog( this, "LOAD BY URL" ); lbrecd = new LoadByRecorderDialog( this, "LOAD BY RECORDER" ); Container content = getContentPane(); content.setLayout( new BorderLayout() ); JPanel up = new JPanel( new BorderLayout() ); JToolBar headButton = new JToolBar(); headButton.add( stop = new JButton( "STOP") ); stop.addActionListener( this ); stop.setEnabled( false ); headButton.add( log = new JButton("LOG") ); log.addActionListener( this ); log.setEnabled( false ); headButton.add( reload = new JButton( "RELOAD" ) ); reload.addActionListener( this ); reload.setEnabled( false ); progress = new Progress(); up.add( headButton, BorderLayout.PAGE_START ); up.add( progress, BorderLayout.CENTER ); JPanel down = new JPanel( new BorderLayout() ); logText = new JTextArea(); logText.setEditable( false ); down.add( new JLabel( "HEADER DETAILS" ), BorderLayout.PAGE_START ); down.add( new JScrollPane( logText ), BorderLayout.CENTER ); down.add( (description = new JLabel( "Loader Description" )), BorderLayout.PAGE_END ); JSplitPane total = new JSplitPane( JSplitPane.VERTICAL_SPLIT, up, down ); total.setOneTouchExpandable( true ); total.setDividerLocation( 150 ); JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu( "File" ); JMenuItem lburl = new JMenuItem( "Load By URL..." ); lburl.setActionCommand( "lburl" ); lburl.addActionListener( this ); file.add( lburl ); JMenuItem lbrec = new JMenuItem( "Load By Recorder..." ); lbrec.setActionCommand( "lbrec" ); lbrec.addActionListener( this ); file.add( lbrec ); file.addSeparator(); JMenuItem exit = new JMenuItem( "Exit" ); exit.setActionCommand( "exit" ); exit.addActionListener( this ); file.add( exit ); menuBar.add( file ); JMenu conf = new JMenu( "Configure" ); JMenuItem rmrec = new JMenuItem( "rm Recorder..." ); rmrec.setActionCommand( "rmrec" ); conf.add( rmrec ); conf.addSeparator(); JMenuItem confloader = new JMenuItem( "Configure Loader..." ); confloader.setActionCommand( "confloader" ); conf.add( confloader ); menuBar.add( conf ); JMenu about = new JMenu( "About" ); JMenuItem intro = new JMenuItem( "Introduction..." ); intro.setActionCommand( "intro" ); about.add( intro ); menuBar.add( about ); content.add( total, BorderLayout.CENTER ); setJMenuBar( menuBar ); addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ){ System.exit( 0 ); } }); setSize( 400, 300 ); setVisible( true ); } class LoadByURLDialog extends JDialog { public JTextField urlField; public JTextField locationField; public LoadByURLDialog( Frame f, String title ){ super( f, title, true ); Container content = getContentPane(); content.setLayout( new BorderLayout() ); JPanel cont = new JPanel( new GridLayout(2,2) ); JLabel l; cont.add( (l= new JLabel( "URL: " ) ) ); l.setPreferredSize( new Dimension( 30,20 ) ); cont.add( (urlField = new JTextField()) ); urlField.setPreferredSize( new Dimension(150, 20) ); cont.add( (l = new JLabel( "LOCATION: " )) ); l.setPreferredSize( new Dimension( 30, 20 ) ); cont.add( (locationField = new JTextField() ) ); content.add( cont, BorderLayout.CENTER ); cont = new JPanel( new FlowLayout() ); JButton b; cont.add( ( b = new JButton("OK") ) ); b.setActionCommand( "lburl_ok" ); b.addActionListener( LoaderFrame.this ); cont.add( (b = new JButton("CANCEL")) ); b.setActionCommand( "lburl_cancel" ); b.addActionListener( LoaderFrame.this ); content.add( cont, BorderLayout.SOUTH ); pack(); } } class LoadByRecorderDialog extends JDialog { public JTextField logField; public LoadByRecorderDialog( Frame f, String s ){ super( f, s, true ); Container content = getContentPane(); content.setLayout( new BorderLayout() ); JPanel cont = new JPanel( new FlowLayout() ); cont.add( new JLabel( "LOGLOCATION: " ) ); cont.add( (logField = new JTextField()) ); logField.setPreferredSize( new Dimension( 150, 20) ); content.add( cont, BorderLayout.CENTER ); cont = new JPanel( new FlowLayout() ); JButton b; cont.add( ( b = new JButton("OK") ) ); b.setActionCommand( "lbrec_ok" ); b.addActionListener( LoaderFrame.this ); cont.add( (b = new JButton("CANCEL")) ); b.setActionCommand( "lbrec_cancel" ); b.addActionListener( LoaderFrame.this ); content.add( cont, BorderLayout.SOUTH ); pack(); } } public void actionPerformed( ActionEvent e ){ String command = e.getActionCommand(); if( command.equals( "STOP" ) ){ if( lm != null ){ lm.stop(); } log.setEnabled( true ); stop.setEnabled( false ); System.out.println( "STOP" ); } else if( command.equals( "LOG" ) ){ if( lm != null ){ lm.log( this.logFile ); } reload.setEnabled( true ); log.setEnabled( false ); System.out.println( "LOG" ); } else if( command.equals( "RELOAD" ) ){ LoadRecorder lr = new LoadRecorder(); try{ lr.load( this.logFile ); }catch( Exception ee ){ System.out.println( "when init recorder an error occurred." ); return; } lm = new LoaderManager( lr ); lm.setViewer( progress ); lm.load(); logText.append( this.writeHeader( lm.getRecorder() ) ); stop.setEnabled( true ); reload.setEnabled( false ); System.out.println( "RELOAD" ); } else if( command.equals( "lburl" ) ){ System.out.println( "lburl" ); lburld.setVisible( true ); } else if( command.equals( "lburl_ok" ) ){ System.out.println( "lburl_ok" ); String url = lburld.urlField.getText(); String file = lburld.locationField.getText(); try{ File f = new File( file ); this.logFile = new File( LoaderFrame.pros.getProperty("logBaseFile"), f.getName() + ".log" ); lm = new LoaderManager( new URL( url ), f ); }catch( Exception ee ){ System.out.println( "create loader manager failed." ); ee.printStackTrace(); return; } lm.setViewer( progress ); lm.load(); progress.setURL( url ); progress.setLocation( file ); logText.setText( this.writeHeader( lm.getRecorder() ) ); stop.setEnabled( true ); log.setEnabled( false ); reload.setEnabled( false ); lburld.setVisible( false ); } else if( command.equals( "lburl_cancel" ) ){ System.out.println( "lburl_cancel" ); lburld.setVisible( false ); } else if( command.equals( "lbrec" ) ){ System.out.println( "lbrec" ); lbrecd.setVisible( true ); } else if( command.equals( "lbrec_ok" ) ){ System.out.println( "lbrec_ok" ); String logf = lbrecd.logField.getText(); LoadRecorder lr = new LoadRecorder(); try{ this.logFile = new File( logf ); lr.load( this.logFile ); }catch( Exception ee ){ System.out.println( "when init recorder an error occurred." ); return; } lm = new LoaderManager( lr ); lm.setViewer( progress ); lm.load(); progress.setURL( lr.get( "ResourceUrl" ).get(0) ); progress.setLocation( lr.get( "DestinationFile" ).get(0) ); logText.setText( this.writeHeader( lm.getRecorder() ) ); stop.setEnabled( true ); log.setEnabled( false ); reload.setEnabled( false ); lbrecd.setVisible( false ); } else if( command.equals( "lbrec_cancel" ) ){ System.out.println( "lbrec_cancel" ); lbrecd.setVisible( false ); } else if( command.equals( "exit" ) ){ System.out.println( "exit" ); System.exit( 0 ); } } private String writeHeader( LoadRecorder rec ){ StringBuffer sb = new StringBuffer(); Set<String> keys = rec.keySet(); Iterator<String> i = keys.iterator(); while( i.hasNext() ){ String key = i.next(); sb.append( key + " = " ); sb.append( rec.getString( rec.get( key ) ) ); sb.append( "\n" ); } return sb.toString(); } public static void main( String[] args ){ JFrame f = new LoaderFrame( "loader" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -