📄 mainwin.java
字号:
import java.awt.*;import java.util.*;import java.awt.event.*;import java.io.*;import java.net.*;import com.sun.java.swing.*;import com.sun.java.swing.border.*;/* * Use line below if you are using: * 1) Java 1.1.6 + Swing 1.0.3 *///import com.sun.java.swing.preview.JFileChooser;/* * You do not need the above if your are using: * 1) Java 1.1.6 or higher + Swing 1.1beta * 2) Java 1.2beta4 or higher * This is because filechooser is now part of regular swing package *//* This class supervises the reading and display of a clog file. It shows a Frame consisting of (New Frame), (Select File) and (Exit) buttons along with a text field showing the current filename. 1. The log file to be read is selected via FileDialog object (openAppFileDlg) 2. An instance of ClogReader class is created with this filename which traverses the log file and supervises the creation of data structures. 3. The data structures created reside in RecordHandler object which is returned. 4. This class (Mainwin) then takes the RecordHandler object along with the data structures creates an instance of ClogDisplay class which handles all drawing, printing, manipulation, etc.*/public class Mainwin extends JFrame implements ActionListener { RecordHandler mainTool; //Object containing data private ClogDisplay disp; //Frame displaying all data private SwingWorker readWorker; //Thread doing reading private String setupFile = "jumpshot.setup"; //file containing default settings private String distributionDir = "./"; //Directory where the distribution is String distributionUrl; private String classDir1 = "js_classes"; //Directory where byte code is stored private String classDir2 = "jumpshot.jar"; private String dataDir = "data"; //Directory name where the ASCII file's are private String logFileDir = "logfiles"; private Object openAppFileDlg; private ApltFileDlg openApltFileDlg; private MyTextField logFileField; String logFileName; private JMenuItem metalMenuItem, selectFileMenuItem; Font frameFont; Color frameBColor, frameFColor; public Dimension dimPG; private HelpDlg helpDlg; Mainwin startwin; private JMenuBar menuBar; //This boolean value tells us if this instance of Mainwin is a child or not //If it is a child then when u exit it only its descendents will be terminated //If however, u exit the father all children will also be exited private boolean child; boolean aplt; jumpshot parent; int dtype; private String about = "Jumpshot, the CLOG viewer, Version 2.0\n" + "mpi-maint@mcs.anl.gov"; private MyButton dispB; private boolean reader_alive; public Mainwin (jumpshot p, String fileName, boolean a, boolean c) { parent = p; aplt = a; child = c; logFileName = fileName; startwin = this; if (logFileName == null) logFileName = new String ("No Name"); setup (); } //setup methods----------------------------------------------------------------- private void setup () { setupUI (); adjustFrameStuff (); setupData (); setupDlgs (); setupPanels (); disableDisp (); setupEventHandlers (); pack (); setSize (getMinimumSize ()); setResizable (false); setVisible (true); if (!(logFileName.equals ("No Name"))) { if (aplt) logFileName = distributionUrl + logFileName; readLogFile (logFileName); } } private void setupUI () { // Force SwingSet to come up in the Cross Platform L&F try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); // If you want the System L&F instead, comment out the above line and // uncomment the following: // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exc) { new ErrorDiag (null, "Error loading L&F: " + exc); } } private void adjustFrameStuff () { if (child) setTitle ("Jumpshot: (Child)"); else setTitle ("Jumpshot"); Toolkit toolkit = getToolkit (); Dimension dimScreen = new Dimension (toolkit.getScreenSize ()); dimPG = new Dimension ((int)Math.rint (dimScreen.width * 0.75), (int)Math.rint (dimScreen.height * 0.5)); frameBColor = Color.lightGray; frameFColor = Color.black; frameFont = new Font ("Serif", Font.PLAIN, 10); } private void setupData () { //Obtain the settings specified by the user. Properties settings = new Properties (); InputStream setin, colorin; String path; if (!aplt) { path = getDataPath (); if (path == null) { new ErrorDiag (null, "Jumpshot cannot locate setup files. Exiting"); return; } setin = getFileIn (path + setupFile); } else { path = getDataUrl (); if (path == null) { new ErrorDiag (null, "Jumpshot cannot locate setup files. Exiting"); return; } setin = getUrlIn (path + setupFile); } try {settings.load (setin); setin.close ();} catch (IOException x) {new ErrorDiag (this, "IO Error:" + x.getMessage ()); return;} String colorFile = settings.getProperty ("COLORFILE", "jumpshot.colors"); if (!aplt) colorin = getFileIn (path + colorFile); else colorin = getUrlIn (path + colorFile); ColorUtil.readColors (this, colorin); reader_alive = false; mainTool = null; } //Bug: Method setCurrentDirectory of JFileChooser does not work completely. So, method //rescanCurrentDirectory has to be called also to get the desired effect. We want //the JFileChooser dialog to open up with a listing of files of the directory from //which Jumpshot was executed private void setupDlgs () { if (!aplt) { Class c = null; openAppFileDlg = null; try {c = Class.forName ("com.sun.java.swing.preview.JFileChooser");} catch (ClassNotFoundException e1) { try {c = Class.forName ("com.sun.java.swing.JFileChooser");} catch (ClassNotFoundException e2) {System.err.println (e2.toString ());} } if (c != null) { try {openAppFileDlg = c.newInstance ();} catch (InstantiationException e) {System.err.println (e.toString ());} catch (IllegalAccessException e) {System.err.println (e.toString ());} } //ExFileFilter filter = new ExFileFilter (new String [] {"clog"}, "clog logfiles"); if (openAppFileDlg != null) { // ROUTINES.invokeMethod (openAppFileDlg, "addChoosableFileFilter", filter, // filter.getClass ()); // ROUTINES.invokeMethod (openAppFileDlg, "setFileFilter", filter, // (filter.getClass ()).getSuperclass ()); File f = new File ((System.getProperties ()).getProperty ("user.dir")); String s = new String ("Select Logfile"); ROUTINES.invokeMethod (openAppFileDlg, "setDialogTitle", s, s.getClass ()); ROUTINES.invokeMethod (openAppFileDlg, "setCurrentDirectory", f, f.getClass ()); ROUTINES.invokeMethod (openAppFileDlg, "rescanCurrentDirectory", null, null); } else selectFileMenuItem.setEnabled (false); } else openApltFileDlg = new ApltFileDlg (this, "Select Logfile"); URL btns_URL = ClassLoader.getSystemResource( "doc/jumpshot.def" ); InputStream helpin = null; try { helpin = btns_URL.openStream(); } catch ( IOException ioerr ) { new ErrorDiag (this, "IO Error: opening " + btns_URL + " causes error" ); } helpDlg = new HelpDlg ((Frame)this, readLines (helpin)); } private void setupPanels () { getContentPane ().setLayout (new GridBagLayout ()); GridBagConstraints con = new GridBagConstraints (); con.anchor = GridBagConstraints.WEST; con.fill = GridBagConstraints.HORIZONTAL; getContentPane ().add (setupMenuBar (), con); con.fill = GridBagConstraints.NONE; //JPanels are used now. But, if the problem with using menus is solved //they should be used instead Border border1, border2 = BorderFactory.createLoweredBevelBorder (); //Logfile field JPanel pl = new JPanel (new FlowLayout ()); border1 = BorderFactory.createEmptyBorder (4, 4, 2, 4); pl.setBorder (BorderFactory.createCompoundBorder (border1, border2)); pl.add (new JLabel ("Logfile")); logFileField = new MyTextField (logFileName, 35, false); pl.add (logFileField); con.gridy = 1; getContentPane ().add (pl, con); con.gridy = 2; con.gridx = 0; dispB = new MyButton ("Display", "Display loaded logfile", this); getContentPane ().add (dispB, con); } private JMenuBar setupMenuBar () { menuBar = new JMenuBar(); //File Menu JMenu file = (JMenu) menuBar.add(new JMenu("File")); JMenuItem menuItem = file.add (new JMenuItem ("New Frame")); menuItem.addActionListener (this); menuItem.setHorizontalTextPosition (JButton.RIGHT); selectFileMenuItem = new JMenuItem ("Select Logfile"); file.add (selectFileMenuItem); selectFileMenuItem.addActionListener (this); selectFileMenuItem.setHorizontalTextPosition (JButton.RIGHT); menuItem = file.add (new JMenuItem ("Exit")); menuItem.addActionListener (this); menuItem.setHorizontalTextPosition (JButton.RIGHT); menuBar.add (file); //Display Menu JMenu dispM = (JMenu) menuBar.add(new JMenu("Display")); ButtonGroup dGroup = new ButtonGroup(); DispListener dispListener = new DispListener(); JRadioButtonMenuItem rbM; rbM = (JRadioButtonMenuItem)dispM.add(new JRadioButtonMenuItem("Time Lines")); dGroup.add(rbM); rbM.addItemListener(dispListener); rbM.setSelected (true); dtype = CONST.TIMELINES; //Setting dtype to time lines rbM = (JRadioButtonMenuItem)dispM.add(new JRadioButtonMenuItem("Mountain Ranges")); dGroup.add(rbM); rbM.addItemListener(dispListener); menuBar.add (dispM); // Sys Options Menu JMenu options = (JMenu) menuBar.add(new JMenu("System")); // Look and Feel Radio control ButtonGroup group = new ButtonGroup(); ToggleUIListener toggleUIListener = new ToggleUIListener(); JMenuItem windowsMenuItem, motifMenuItem, macMenuItem; windowsMenuItem = (JRadioButtonMenuItem) options.add(new JRadioButtonMenuItem("Windows Style Look and Feel")); windowsMenuItem.setSelected(UIManager.getLookAndFeel().getName().equals("Windows")); group.add(windowsMenuItem); windowsMenuItem.addItemListener(toggleUIListener); motifMenuItem = (JRadioButtonMenuItem) options.add(new JRadioButtonMenuItem("Motif Look and Feel")); motifMenuItem.setSelected(UIManager.getLookAndFeel().getName().equals("CDE/Motif")); group.add(motifMenuItem); motifMenuItem.addItemListener(toggleUIListener); metalMenuItem = (JRadioButtonMenuItem) options.add(new JRadioButtonMenuItem("Metal Look and Feel")); metalMenuItem.setSelected(UIManager.getLookAndFeel().getName().equals("Metal")); metalMenuItem.setSelected(true); group.add(metalMenuItem); metalMenuItem.addItemListener(toggleUIListener); macMenuItem = (JRadioButtonMenuItem) options.add(new JRadioButtonMenuItem("Macintosh Look and Feel")); macMenuItem.setSelected(UIManager.getLookAndFeel().getName().equals("Macintosh")); group.add(macMenuItem); macMenuItem.addItemListener(toggleUIListener); // Tooltip checkbox options.add(new JSeparator()); JCheckBoxMenuItem cb = new JCheckBoxMenuItem ("Show ToolTips"); options.add(cb); cb.setSelected(true); cb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBoxMenuItem b = (JCheckBoxMenuItem)e.getSource(); if(b.isSelected()) { ToolTipManager.sharedInstance().setEnabled(true); } else { ToolTipManager.sharedInstance().setEnabled(false); } } }); //Help Menu JMenu help = (JMenu) menuBar.add(new JMenu("Help")); menuItem = help.add (new JMenuItem ("Manual")); menuItem.addActionListener (this); menuItem = help.add (new JMenuItem ("About")); menuItem.addActionListener (this); menuBar.add (help); return menuBar; } private void setupEventHandlers () { this.enableEvents (AWTEvent.ACTION_EVENT_MASK); // Define, instantiate and register a WindowListener object.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -