/home/wpollock1/public_html/restricted/Java2/SearchEngine/com/wpollock/searchengine/MaintenanceWindow.java

package com.wpollock.searchengine;

import java.awt.Font;
import java.awt.event.*;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.*;

import static javax.swing.ListSelectionModel.*;
import static javax.swing.JTable.*;
import static com.wpollock.searchengine.Main.*;

/**
 * @author wpollock
 */
class MaintenanceWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	final static JLabel numItemsIndexed = new JLabel( "0" );
	static JTable fileTable = null;

	public MaintenanceWindow ( final Main mainWindow,
							   final Preferences prefs ) {
		// Create window and set initial size and position:
		setTitle( "Search Engine Maintenance" );
		setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
		// Icon found with Creative Commons License:
		setIconImage( new ImageIcon(getClass()
				.getResource("MagnifyingGlass.jpg")).getImage() );
		positionMaintWindow();  // Set the default window bounds

		// Add components and adjust their layout and properties:

		// Use a Box layout:
		Box mainBox = Box.createVerticalBox();
		add ( mainBox );

		// Add a centered title:
		JLabel title = new JLabel("Search Engine - Index Maintenance");
		title.setFont( new Font("SansSerif", Font.BOLD, 24) );
		Box titleBox = Box.createHorizontalBox();
		titleBox.add( title );
		titleBox.add( Box.createHorizontalStrut(50) );
		mainBox.add( titleBox );
		mainBox.add( Box.createVerticalStrut(10) );
		mainBox.add( Box.createGlue() );

		// Add a table of indexed files.  TODO:
		// Missing files will be shown in red strike-out, out-of-date files
		// will be shown in bold italic.  Col 1 = filename, col 2 = last
		// modification time.  (col 3 = status?)

		fileTable = new JTable( FileListModel.getModel() );
		// Allow selection of multiple whole rows only:
		fileTable.setRowSelectionAllowed( true );
		fileTable.setColumnSelectionAllowed( false );
		fileTable.setSelectionMode( MULTIPLE_INTERVAL_SELECTION );
		fileTable.setAutoResizeMode( AUTO_RESIZE_ALL_COLUMNS );
		JScrollPane scrollPane = new JScrollPane( fileTable );
		fileTable.setFillsViewportHeight(true);
		mainBox.add( scrollPane );

		// Add buttons to add/remove files, and to rebuild index:
		Box btnsBox = Box.createHorizontalBox();
		mainBox.add( btnsBox );
		mainBox.add( Box.createVerticalStrut(10) );
		btnsBox.add( Box.createHorizontalGlue() );

		JButton addFileBtn = new JButton( "Add File..." );
		btnsBox.add( addFileBtn );
		addFileBtn.setMnemonic(KeyEvent.VK_A);
		addFileBtn.setActionCommand( "ADD" );
		addFileBtn.setToolTipText( "Select a file to add to the index" );
		btnsBox.add( Box.createHorizontalGlue() );

		JButton rebuildOutOfDateBtn = new JButton( "Rebuild Out-of-date" );
		btnsBox.add( rebuildOutOfDateBtn );
		rebuildOutOfDateBtn.setMnemonic(KeyEvent.VK_O);
		rebuildOutOfDateBtn.setActionCommand( "REBUILD SELECTED" );
		rebuildOutOfDateBtn.setToolTipText("Re-index any files modified since "
				+ "they were indexed" );
		btnsBox.add( Box.createHorizontalGlue() );

		JButton removeSelectedBtn = new JButton( "Remove Selected Files" );
		btnsBox.add( removeSelectedBtn );
		removeSelectedBtn.setMnemonic(KeyEvent.VK_R);
		removeSelectedBtn.setActionCommand( "REMOVE" );
		removeSelectedBtn.setToolTipText( "Remove selected files from "
				+ "the index" );
		btnsBox.add( Box.createHorizontalGlue() );
/*
		JButton rebuildIndexBtn = new JButton( "Rebuild Index" );
		btnsBox.add( rebuildIndexBtn );
		rebuildIndexBtn.setMnemonic(KeyEvent.VK_I);
		rebuildIndexBtn.setActionCommand( "REBUILD INDEX" );
		rebuildIndexBtn.setToolTipText( "Discard and then rebuild the "
				+ "entire index" );
		btnsBox.add( Box.createHorizontalGlue() );
*/
		// Have footer with window position reset button, and version info
		// (TODO: someplace, there should be a help button too):
		Box footer = Box.createHorizontalBox();
		mainBox.add( footer );
		footer.add( Box.createHorizontalStrut(10) );

		JButton resetBtn = new JButton( "Reset Windows" );
		resetBtn.setMnemonic( KeyEvent.VK_W );
		resetBtn.setActionCommand( "RESET" );
		resetBtn.setToolTipText( "Reset saved window positions "
				+ "to default values" );
		footer.add( resetBtn );
		footer.add( Box.createGlue() );

		footer.add( new JLabel("Number of files indexed: ") );
		numItemsIndexed.setText( "" + FileListModel.getModel().getRowCount() );
		footer.add( numItemsIndexed );
		footer.add( Box.createGlue() );

		footer.add( new JLabel("Search Engine version " + Main.VERSION) );
		footer.add( Box.createHorizontalStrut(10) );

		// Hook up event handlers:

		// Handle window resize and move events
		addComponentListener( (ComponentListener) mainWindow );

		// Reset both windows to default sizes and positions:
		resetBtn.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed( ActionEvent evnt ) {
			   try {
				prefs.clear();
				prefs.put(LAST_DIRECTORY, System.getProperty("user.home") );
				mainWindow.positionWindow();
				positionMaintWindow();
			} catch (BackingStoreException e) {
				e.printStackTrace();
			}
			}
		});

		addFileBtn.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed ( ActionEvent ae ) {
				SwingUtilities.invokeLater( new Runnable() {
					public void run () {
			    	IndexUtils.addFileToIndex();
					}
				});
			}
		});

		rebuildOutOfDateBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.invokeLater( new Runnable() {
					public void run () {
						IndexUtils.updateIndex();
					}
				});
			}
		});

		// Remove selected button: Removes each file from the index that
		// is selected in the file list (the JTable):
		removeSelectedBtn.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
//				SwingUtilities.invokeLater( new Runnable() {
//					public void run () {
						IndexUtils.removeSelectedFilesFromIndex();
//					}
//				});
			}
		});
	}

	void positionMaintWindow () {
		int xPos = prefs.getInt( MAINT_X, -1 );  // "-1" means never set
		int yPos = prefs.getInt( MAINT_Y, -1 );
		int height = prefs.getInt( MAINT_HEIGHT, 500 );
		int width = prefs.getInt( MAINT_WIDTH, 700 );
		setSize( width, height );

	    // if xPos is -1, the window position was never set before,
		// so center it:
		if ( xPos == -1 ) {
			setLocationRelativeTo( null );  // Ctr on Screen
			prefs.putInt( MAINT_X, getX() );
			prefs.putInt( MAINT_Y, getY() );
		} else
			setLocation( xPos, yPos );
	}
}