All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.lafwidget.tabbed.TabPagerManager Maven / Gradle / Ivy

Go to download

Laf-Widget provides support for common "feel" widgets in look-and-feel libraries

There is a newer version: 5.0
Show newest version
/*
 * Copyright (c) 2005-2006 Laf-Widget Kirill Grouchnikov. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of Laf-Widget Kirill Grouchnikov nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
package org.jvnet.lafwidget.tabbed;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.JTabbedPane;
import javax.swing.JWindow;

import org.jvnet.lafwidget.LafWidgetUtilities;
import org.jvnet.lafwidget.tabbed.TabPreviewThread.TabPreviewCallback;
import org.jvnet.lafwidget.tabbed.TabPreviewThread.TabPreviewInfo;

/**
 * Tab pager manager.
 * 
 * @author Kirill Grouchnikov
 */
public class TabPagerManager {
	/**
	 * Singleton instance of the tab pager manager.
	 */
	protected static TabPagerManager instance;

	/**
	 * The tabbed pane that is currently paged.
	 */
	protected JTabbedPane currTabbedPane;

	/**
	 * Index of the central tab.
	 */
	protected int currTabIndex;

	// protected Map smallPreviewMap;
	//
	// protected Map regularPreviewMap;
	//
	/**
	 * Preview window for the left (previous) tab.
	 */
	protected JWindow prevTabWindow;

	/**
	 * Preview window for the central (current) tab.
	 */
	protected JWindow currTabWindow;

	/**
	 * Preview window for the right (next) tab.
	 */
	protected JWindow nextTabWindow;

	/**
	 * Indicates whether the tab pager windows are visible.
	 */
	protected boolean isVisible;

	/**
	 * Implementation of the tab preview callback for the tab pager.
	 * 
	 * @author Kirill Grouchnikov.
	 */
	public class TabPagerPreviewCallback implements TabPreviewCallback {
		/**
		 * The associated preview window.
		 */
		private JWindow previewWindow;

		/**
		 * The associated tab preview control.
		 */
		private TabPreviewControl previewControl;

		/**
		 * Creates a new tab preview callback for the tab pager.
		 * 
		 * @param previewWindow
		 *            The associated preview window.
		 * @param tabPane
		 *            The associated tab preview control.
		 * @param tabIndex
		 *            Tab index.
		 */
		public TabPagerPreviewCallback(JWindow previewWindow,
				JTabbedPane tabPane, int tabIndex) {
			this.previewWindow = previewWindow;
			this.previewControl = new TabPreviewControl(tabPane, tabIndex);
			this.previewWindow.getContentPane().removeAll();
			this.previewWindow.getContentPane().add(this.previewControl,
					BorderLayout.CENTER);
			this.previewWindow.getContentPane().doLayout();
			this.previewControl.doLayout();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.jvnet.lafwidget.tabbed.TabPreviewThread.TabPreviewCallback#start(javax.swing.JTabbedPane,
		 *      int, org.jvnet.lafwidget.tabbed.TabPreviewThread.TabPreviewInfo)
		 */
		public void start(JTabbedPane tabPane, int tabCount,
				TabPreviewInfo tabPreviewInfo) {
			// Nothing to do since the callback was registered
			// for a specific tab.
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.jvnet.lafwidget.tabbed.TabPreviewThread.TabPreviewCallback#offer(javax.swing.JTabbedPane,
		 *      int, java.awt.image.BufferedImage)
		 */
		public void offer(JTabbedPane tabPane, int tabIndex,
				BufferedImage componentSnap) {
			if (TabPagerManager.this.currTabbedPane != tabPane) {
				// has since been cancelled
				return;
			}

			if (!this.previewWindow.isVisible()) {
				// has since been hidden
				return;
			}

			this.previewControl.setPreviewImage(componentSnap);
		}
	}

	/**
	 * Returns the tab pager instance.
	 * 
	 * @return Tab pager instance.
	 */
	public static synchronized TabPagerManager getPager() {
		if (TabPagerManager.instance == null)
			TabPagerManager.instance = new TabPagerManager();
		return TabPagerManager.instance;
	}

	/**
	 * Constructs a new tab pager manager. Is made private to enforce single
	 * instance.
	 */
	private TabPagerManager() {
		// this.smallPreviewMap = new HashMap();
		// this.regularPreviewMap = new HashMap();
		Rectangle virtualBounds = new Rectangle();
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gds = ge.getScreenDevices();
		for (int i = 0; i < gds.length; i++) {
			GraphicsDevice gd = gds[i];
			GraphicsConfiguration gc = gd.getDefaultConfiguration();
			virtualBounds = virtualBounds.union(gc.getBounds());
		}

		int screenWidth = virtualBounds.width;
		int screenHeight = virtualBounds.height;

		this.currTabWindow = new JWindow();
		this.currTabWindow.getContentPane().setLayout(new BorderLayout());
		int currWidth = screenWidth / 3;
		int currHeight = screenHeight / 3;
		this.currTabWindow.setSize(currWidth, currHeight);
		this.currTabWindow.setLocation(currWidth, currHeight);

		int smallWidth = 2 * screenWidth / 9;
		int smallHeight = 2 * screenHeight / 9;
		this.prevTabWindow = new JWindow();
		this.prevTabWindow.getContentPane().setLayout(new BorderLayout());
		this.prevTabWindow.setSize(smallWidth, smallHeight);
		this.prevTabWindow.setLocation(screenWidth / 18, 7 * screenHeight / 18);

		this.nextTabWindow = new JWindow();
		this.nextTabWindow.getContentPane().setLayout(new BorderLayout());
		this.nextTabWindow.setSize(smallWidth, smallHeight);
		this.nextTabWindow.setLocation(13 * screenWidth / 18,
				7 * screenHeight / 18);

		this.isVisible = false;
	}

	/**
	 * Sets the tabbed pane on this tab pager manager.
	 * 
	 * @param jtp
	 *            Tabbed pane to page.
	 */
	private void setTabbedPane(JTabbedPane jtp) {
		if (this.currTabbedPane == jtp)
			return;
		this.currTabbedPane = jtp;
		// this.smallPreviewMap.clear();
		// this.regularPreviewMap.clear();
	}

	/**
	 * Flips the pages.
	 * 
	 * @param tabbedPane
	 *            Tabbed pane.
	 * @param isForward
	 *            if true, the tabs are flipped one page (tab)
	 *            forward, if false, the tabs are flipped one
	 *            page (tab) backward.
	 */
	public synchronized void page(JTabbedPane tabbedPane, boolean isForward) {
		this.setTabbedPane(tabbedPane);
		if (!this.isVisible) {
			this.currTabWindow.setVisible(true);
			this.prevTabWindow.setVisible(true);
			this.nextTabWindow.setVisible(true);
			this.isVisible = true;
			this.currTabIndex = this.currTabbedPane.getSelectedIndex();
		}

		int delta = isForward ? 1 : -1;
		this.currTabIndex += delta;
		if (this.currTabIndex == this.currTabbedPane.getTabCount())
			this.currTabIndex = 0;
		if (this.currTabIndex == -1)
			this.currTabIndex = this.currTabbedPane.getTabCount() - 1;

		int nextTabIndex = this.currTabIndex + 1;
		int prevTabIndex = this.currTabIndex - 1;
		if (nextTabIndex == this.currTabbedPane.getTabCount())
			nextTabIndex = 0;
		if (prevTabIndex == -1)
			prevTabIndex = this.currTabbedPane.getTabCount() - 1;

		TabPreviewThread.TabPreviewInfo currTabPreviewInfo = new TabPreviewThread.TabPreviewInfo();
		currTabPreviewInfo.tabPane = this.currTabbedPane;
		currTabPreviewInfo.tabIndexToPreview = this.currTabIndex;
		currTabPreviewInfo.previewWidth = this.currTabWindow.getWidth() - 4;
		currTabPreviewInfo.previewHeight = this.currTabWindow.getHeight() - 20;
		currTabPreviewInfo.previewCallback = new TabPagerPreviewCallback(
				this.currTabWindow, this.currTabbedPane, this.currTabIndex);
		currTabPreviewInfo.initiator = this;
		TabPreviewPainter previewPainter = LafWidgetUtilities
				.getTabPreviewPainter(currTabPreviewInfo.tabPane);
		if ((previewPainter != null)
				&& (previewPainter.hasPreviewWindow(this.currTabbedPane,
						this.currTabIndex))) {
			TabPreviewThread.getInstance().queueTabPreviewRequest(
					currTabPreviewInfo);
		}

		TabPreviewThread.TabPreviewInfo prevTabPreviewInfo = new TabPreviewThread.TabPreviewInfo();
		prevTabPreviewInfo.tabPane = this.currTabbedPane;
		prevTabPreviewInfo.tabIndexToPreview = prevTabIndex;
		prevTabPreviewInfo.previewWidth = this.prevTabWindow.getWidth() - 4;
		prevTabPreviewInfo.previewHeight = this.prevTabWindow.getHeight() - 20;
		prevTabPreviewInfo.previewCallback = new TabPagerPreviewCallback(
				this.prevTabWindow, this.currTabbedPane, prevTabIndex);
		prevTabPreviewInfo.initiator = this;
		if ((previewPainter != null)
				&& (previewPainter.hasPreviewWindow(this.currTabbedPane,
						prevTabIndex))) {
			TabPreviewThread.getInstance().queueTabPreviewRequest(
					prevTabPreviewInfo);
		}

		TabPreviewThread.TabPreviewInfo nextTabPreviewInfo = new TabPreviewThread.TabPreviewInfo();
		nextTabPreviewInfo.tabPane = this.currTabbedPane;
		nextTabPreviewInfo.tabIndexToPreview = nextTabIndex;
		nextTabPreviewInfo.previewWidth = this.nextTabWindow.getWidth() - 4;
		nextTabPreviewInfo.previewHeight = this.nextTabWindow.getHeight() - 20;
		nextTabPreviewInfo.previewCallback = new TabPagerPreviewCallback(
				this.nextTabWindow, this.currTabbedPane, nextTabIndex);
		nextTabPreviewInfo.initiator = this;
		if ((previewPainter != null)
				&& (previewPainter.hasPreviewWindow(this.currTabbedPane,
						nextTabIndex))) {
			TabPreviewThread.getInstance().queueTabPreviewRequest(
					nextTabPreviewInfo);
		}

	}

	/**
	 * Hides the tab pager windows.
	 * 
	 * @return The index of the center (current) tab.
	 */
	public synchronized int hide() {
		int result = this.isVisible ? this.currTabIndex : -1;
		this.currTabWindow.setVisible(false);
		this.currTabWindow.dispose();
		this.prevTabWindow.setVisible(false);
		this.prevTabWindow.dispose();
		this.nextTabWindow.setVisible(false);
		this.nextTabWindow.dispose();
		this.isVisible = false;
		return result;
	}

	/**
	 * Resets the internal caches.
	 */
	public static void reset() {
		// TabPagerManager.instance.regularPreviewMap.clear();
		// TabPagerManager.instance.smallPreviewMap.clear();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy