Man Page Example

Output - interface:

This is what the GUI looks like on a Mac:

The code:

// File: ManPageViewerND.java
// Date: Jun 23, 2015
// Author: Daniel Da Silva
// Modifications: Nicholas Duchon
// Class: CMSC 335, Summer, 2015
// Purpose: show a GUI - view a man page

import java.awt.BorderLayout;
import java.awt.Font;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

/**
 * Displays a frame that allows the user to look up a man page.
 */
@SuppressWarnings("serial")
public class ManPageViewerND extends JFrame {
   private JTextArea pageView;
   
   public static void main(String[] args) {
      new ManPageViewerND();
  
} // end main

   // try initializer approach
   
{
      setTitle("Man Page Viewer");
      setSize(600, 400);
      setLocationRelativeTo (null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
     
      JPanel topPanel = new JPanel();
      JTextField pageEntry = new JTextField(20);
      JButton lookupButton = new JButton("Lookup");
      topPanel.add(pageEntry);
      topPanel.add(lookupButton);
      
      pageView = new JTextArea();
      pageView.setFont(new Font("monospaced", Font.PLAIN, 12));
      
      add(topPanel, BorderLayout.PAGE_START);       
      add(new JScrollPane(pageView), BorderLayout.CENTER);
     
      lookupButton.addActionListener (e -> getManPage (pageEntry.getText()));
      pageEntry   .addActionListener (e -> getManPage (pageEntry.getText()));
      pageEntry.requestFocus ();
     
      validate();
  
} // end initializer
   
   private void getManPage(String pageEntry) {
      String st = "";
      try {
         ProcessBuilder pb = new ProcessBuilder("man", pageEntry);
         pb.redirectOutput(Redirect.PIPE);
     
         Process process = pb.start();
         Scanner stin = new Scanner (process.getInputStream());
     
         if (process.waitFor() == 0)
            while (stin.hasNextLine())
               st += stin.nextLine() + "\n";
         else
            st = "Error occurred. Maybe the man page doesn't exist?";
      }
      catch (InterruptedException e) {
         e.printStackTrace();
      }
      catch (IOException e) {
         e.printStackTrace();
      }
      st = st.replaceAll ("_\u0008", ""); // remove underlining on a mac
      st = st.replaceAll ("\u0008.", ""); // remove bolding on a mac
      pageView.setText(st);
      pageView.setCaretPosition(0);
   } // end method getManPage
} // end class ManPageViewerND

Comments:

Methods:

Here is the typical approach to initializing a GUI - in this case:

// Original code using constructor and methods
//    private ManPageViewerND() {
//       configureFrame();
//       addControls();
//       validate();
//    } // end no-parameter constructor
//
//    private void configureFrame() {
//       setTitle("Man Page Viewer");
//       setSize(600, 400);
//       setLocationRelativeTo (null);
//       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//       setVisible(true);
//    } // end method configureFrame (part of constructor sequence)
//    
//    private void addControls() {               
//       JPanel topPanel = new JPanel();
//       pageEntry = new JTextField(20);
//       lookupButton = new JButton("Lookup");
//       topPanel.add(pageEntry);
//       topPanel.add(lookupButton);
//       
//       pageView = new JTextArea();
//       pageView.setFont(new Font("monospaced", Font.PLAIN, 12));
//       
//       add(topPanel, BorderLayout.PAGE_START);       
//       add(new JScrollPane(pageView), BorderLayout.CENTER);
//      
//       lookupButton.addActionListener (e -> getManPage ());
//       pageEntry   .addActionListener (e -> getManPage ());
//       pageEntry.requestFocus ();
//    } // end method addControls (part of constructor sequence)



End.