/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.example.browser;

import com.sun.jini.example.browser.EntryTreePanel;
import com.sun.jini.outrigger.JavaSpaceAdmin;
import com.sun.jini.outrigger.AdminIterator;
import net.jini.core.entry.Entry;
import java.awt.Toolkit;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 * A browser utility to browse entries in a specified space.
 *
 * @author Sun Microsystems, Inc.
 *
 * @version 0.2 06/04/98
 */
class SpaceBrowser extends JFrame {
  //private SpaceEntryPanel entryPanel;
  private final static int MINIMUM_WINDOW_WIDTH = 320;
  private Browser browser;

  public SpaceBrowser(JavaSpaceAdmin admin, Browser browser) {
    super("SpaceBrowser");

    this.browser = browser;
    // init main components
    SpaceEntryPanel entryPanel = new SpaceEntryPanel(admin);

    // add menu and attr panel
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(new BrowserMenuBar(entryPanel), "North");
    getContentPane().add(entryPanel, "Center");

    validate();
    pack();
    setSize(((getSize().width < MINIMUM_WINDOW_WIDTH) ? MINIMUM_WINDOW_WIDTH : getSize().width),
	    getSize().height);

    // center in parent frame
    Rectangle bounds = browser.getBounds();
    Dimension dialogSize = getPreferredSize();
    int xpos = bounds.x + (bounds.width - dialogSize.width)/ 2;
    int ypos = bounds.y + (bounds.height - dialogSize.height)/2;
    setLocation((xpos < 0) ? 0 : xpos,
		(ypos < 0) ? 0 : ypos);
  }


  class BrowserMenuBar extends JMenuBar {
    private EntryTreePanel entryPanel;

    public BrowserMenuBar(EntryTreePanel entryPanel) {
      this.entryPanel = entryPanel;
      JMenuItem mitem;

      // "File" Menu
      JMenu fileMenu = (JMenu) add(new JMenu("File"));
      mitem = (JMenuItem) fileMenu.add(new JMenuItem("Refresh"));
      mitem.addActionListener(browser.wrap(new ActionListener() {
	public void actionPerformed(ActionEvent ev) {
	  BrowserMenuBar.this.entryPanel.refreshPanel();
	}
      }));
      mitem = (JMenuItem) fileMenu.add(new JMenuItem("Close"));
      mitem.addActionListener(browser.wrap(new ActionListener() {
	public void actionPerformed(ActionEvent ev) {
	  SpaceBrowser.this.setVisible(false);
	}
      }));
    }
  }

  // Allocate only one empty array of entries
  private static final Entry[] emptyArray = new Entry[0];

  class SpaceEntryPanel extends EntryTreePanel {
    private JavaSpaceAdmin admin;

    public SpaceEntryPanel(JavaSpaceAdmin admin) {
      super(false);	// Entries are not editable.
      this.admin = admin;

      refreshPanel();
    }

    protected Entry[] getEntryArray() {
      try {
	AdminIterator iter = admin.contents(null, null, 128);
	List acc = new java.util.LinkedList();

	// Accumulate entries in List
	Entry e;
	while ((e = iter.next()) != null) 
	    acc.add(e);	
	iter.close();

	// Construct array from List
	return (Entry[])acc.toArray(emptyArray);
      } catch (Throwable t) {
	Browser.logger.log(Level.INFO, "obtaining entries failed", t);
      }

      return null;
    }
  }
}

