⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 desktoputils.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package eti.bi.alphaminer.ui;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.DesktopManager;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import eti.bi.alphaminer.operation.property.OperatorProperty;


/**
 * DesktopUtils is a class responsible for arranging internal frames of a specific
 * desktop pane based on several methods.
 */
public class DesktopUtils {
	protected static final int UNUSED_HEIGHT = 0;
	protected static int nextX; // Next X position
	protected static int nextY; // Next Y position
	protected static final int DEFAULT_OFFSETX = 24;
	protected static final int DEFAULT_OFFSETY = 24;
	protected static int offsetX = DEFAULT_OFFSETX;
	protected static int offsetY = DEFAULT_OFFSETY;

	/**
	 * Arranges all internal windows of a specific desktop pane
	 * so that they are tiled vertically.
	 * @param desktop the specific desktop pane.
	 */
	public static void tileVertical(JDesktopPane desktop) {
		DesktopManager manager = desktop.getDesktopManager();
		if (manager == null) {
			// No desktop manager - do nothing
			return;
		}

		JInternalFrame[] comps = desktop.getAllFrames();
		JInternalFrame comp;
		int count = 0;

		// Count and handle only the internal frames
		for (int i = 0; i < comps.length; i++) {
			comp = comps[i];
			if (comp instanceof OperatorProperty)
				continue;
			if (comp.isVisible()) {
				try {
					if (!comp.isResizable())
						comp.setMaximum(false);

					if (comp.isIcon())
						comp.setIcon(false);
				} catch (java.beans.PropertyVetoException e) {
				}
				count++;
			}
		}

		if (count != 0) {
			double root = Math.sqrt((double) count);
			int rows = (int) root;
			int columns = count / rows;
			int spares = count - (columns * rows);

			Dimension paneSize = desktop.getSize();
			int columnWidth = paneSize.width / columns;

			// We leave some space at the bottom that doesn't get covered
			int availableHeight = paneSize.height - UNUSED_HEIGHT;
			int mainHeight = availableHeight / rows;
			int smallerHeight = availableHeight / (rows + 1);
			int rowHeight = mainHeight;
			int x = 0;
			int y = 0;
			int thisRow = rows;
			int normalColumns = columns - spares;

			for (int i = comps.length - 1; i >= 0; i--) {
				comp = comps[i];
				if (comp instanceof OperatorProperty)
					continue;
				if (comp.isVisible()) {
					manager.setBoundsForFrame(
						(JComponent) comp,
						x,
						y,
						columnWidth,
						rowHeight);
					y += rowHeight;
					if (--thisRow == 0) {
						// Filled the row
						y = 0;
						x += columnWidth;

						// Switch to smaller rows if necessary
						if (--normalColumns <= 0) {
							thisRow = rows + 1;
							rowHeight = smallerHeight;
						} else {
							thisRow = rows;
						}
					}
				}
			}
		}
	} // end of TileAll

	/**
	 * Arranges all internal windows of a specific desktop pane
	 * so that they are tiled horizontally.
	 * @param desktop the specific desktop pane.
	 */
	public static final void tileHorizontal(JDesktopPane desktop) {
		int _resizableCnt = 0;
		JInternalFrame _allFrames[] = desktop.getAllFrames();
		for (int _x = 0; _x < _allFrames.length; _x++) {
			JInternalFrame _frame = _allFrames[_x];
			if (_frame.isVisible()) {
				try {
					if (!_frame.isResizable())
						_frame.setMaximum(false);
					if (_frame.isIcon())
						_frame.setIcon(false);
				} catch (java.beans.PropertyVetoException e) {
					// OK, to take no action here
				}
				if (_frame.isResizable())
					_resizableCnt++;
			}
		} // End for
		int _width = desktop.getBounds().width;
		int _height = arrangeIcons(desktop);
		if (_resizableCnt != 0) {
			int _fHeight = _height / _resizableCnt;
			int _yPos = 0;
			for (int _x = 0; _x < _allFrames.length; _x++) {
				JInternalFrame _frame = _allFrames[_x];
				if ((_frame.isVisible()) && (_frame.isResizable())) {
					_frame.setSize(_width, _fHeight);
					_frame.setLocation(0, _yPos);
					_yPos += _fHeight;
				}
			} // End for
		}
	}

	/**
	 * Arranges icons of all minimized internal windows of a specific desktop pane.
	 * @param desktop the specific desktop pane.
	 */
	public static final int arrangeIcons(JDesktopPane desktop) {
		int _iconCnt = 0;
		JInternalFrame _allFrames[] = desktop.getAllFrames();
		for (int _x = 0; _x < _allFrames.length; _x++)
			if ((_allFrames[_x].isVisible()) && (_allFrames[_x].isIcon()))
				_iconCnt++;
		int _height = desktop.getBounds().height;
		int _yPos = _height;
		if (_iconCnt != 0) {
			int _width = desktop.getBounds().width;
			int _xPos = 0;
			for (int _x = 0; _x < _allFrames.length; _x++) {
				JInternalFrame _frame = _allFrames[_x];
				if ((_frame.isVisible()) && (_frame.isIcon())) {
					Dimension _dim = _frame.getDesktopIcon().getSize();
					int _iWidth = _dim.width;
					int _iHeight = _dim.height;
					if (_yPos == _height)
						_yPos = _height - _iHeight;
					if ((_xPos + _iWidth > _width) && (_xPos != 0)) {
						_xPos = 0;
						_yPos -= _iHeight;
					}
					_frame.getDesktopIcon().setLocation(_xPos, _yPos);
					_xPos += _iWidth;
				} // End if
			} // End for
		} // End if
		return (_yPos);
	} // End method

	/**
	 * Arranges all internal windows of a specific desktop pane
	 * so that they are cascaded.
	 * @param desktop the specific desktop pane.
	 */
	public static void cascadeAll(JDesktopPane desktop) {
		JInternalFrame[] comps = desktop.getAllFrames();
		int count = comps.length;
		nextX = 0;
		nextY = 0;

		for (int i = count - 1; i >= 0; i--) {
			JInternalFrame comp = comps[i];
			if (comp.isVisible()) {
				try {
					if (!comp.isResizable())
						comp.setMaximum(false);
					if (comp.isIcon())
						comp.setIcon(false);
				} catch (java.beans.PropertyVetoException e) {
				}
				cascade(comp, desktop);
			}
		}
	}

	/**
	 * Arranges all internal windows of a specific desktop pane
	 * so that they are minimized.
	 * @param desktop the specific desktop pane.
	 */
	public static void minimizeAll(JDesktopPane desktop) {
		JInternalFrame[] comps = desktop.getAllFrames();
		int count = comps.length;

		for (int i = count - 1; i >= 0; i--) {
			JInternalFrame comp = comps[i];
			if (comp.isVisible()) {
				if (comp.isIconifiable()) {
					try {
						comp.setIcon(true);
					} catch (java.beans.PropertyVetoException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * Cascade an internal window (relative to a previous one) of a specific 
	 * desktop pane.
	 * @param desktop the specific desktop pane.
	 */
	protected static void cascade(Component comp, JDesktopPane desktop) {
		if (comp instanceof OperatorProperty)
			return;

		Dimension paneSize = desktop.getSize();
		//int targetWidth = 3 * paneSize.width / 5;
		//int targetHeight = 3 * paneSize.height / 5;
		int targetWidth = 450;
		int targetHeight = 300;

		comp.setSize(targetWidth, targetHeight);

		DesktopManager manager = desktop.getDesktopManager();
		if (manager == null) {
			//comp.setBounds(0, 0, targetWidth, targetHeight);
			comp.setLocation(0, 0);
			return;
		}

		if (nextX + targetWidth > paneSize.width
			|| nextY + targetHeight > paneSize.height) {
			nextX = 0;
			nextY = 0;
		}

		//manager.setBoundsForFrame((JComponent) comp, nextX, nextY,
		//        targetWidth, targetHeight);
		comp.setLocation(nextX, nextY);

		nextX += offsetX;
		nextY += offsetY;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -