Code Search for Developers
 
 
  

Window.java from GridBlocks at Krugle


Show Window.java syntax highlighted

/*
 * Copyright (c) 2007
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * Window.java
 */

package fi.hip.gb.client.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import fi.hip.gb.core.Config;

/**
 * Replaces normal JOptionPane with resizeable window. Makes it easy to add normal control
 * buttons like ok and close. Supports layouts by cascading windows.
 * 
 * @author Juho Karppinen
 */
public class Window extends JDialog implements WindowListener {

    /** panel for buttons */
	private JComponent btns = new JPanel();
	
	/** are we using layouts */
	private boolean fan = false;
	
	/** used by layouts, last x position */
	private static int offsetX = (int)(300*Math.random());
	
	/** used by layouts, last y position */
	private static int offsetY = (int)(200*Math.random());
    
    private static final long serialVersionUID = 2576605647738201416L;
    
	/**
	 * Initializes the dialog which is not
	 * shown until {@link Window#showWindow()} method is called.
	 * @param ownerFrame owner for the dialog
	 * @param windowTitle title of the dialog
	 */	
	public Window(Frame ownerFrame, String windowTitle) {
		super(ownerFrame, windowTitle);
	}
	
	/**
	 * Initializes the dialog which is not
	 * shown until {@link Window#showWindow()} method is called.
	 * @param ownerDialog owner for the dialog
	 * @param windowTitle title of the dialog
	 */	
	public Window(Dialog ownerDialog, String windowTitle) {
		super(ownerDialog, windowTitle);
	}
	
	/**
	 * Initializes the window using layouts. The dialog is not shown
	 * until {@link Window#showWindow()} method is called.
	 * 
	 * @param ownerFrame owner for the dialog
	 * @param windowTitle title of the dialog 
	 * @param fan shoud layouts be used
	 */
	public Window(Frame ownerFrame, String windowTitle, boolean fan) { 
		this(ownerFrame, windowTitle);
		fan = true;
	}
    
    private void init() {
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.getContentPane().setLayout(new BorderLayout());
        this.btns.setLayout(new GridLayout(1, 0));
        this.addWindowListener(this);
    }
	
	/**
	 * Shows dialog
	 */
	public void showWindow() {        
		getContentPane().add(BorderLayout.SOUTH, btns);
		
		final int MAX_WIDTH = fan ? 
			Toolkit.getDefaultToolkit().getScreenSize().width - Window.offsetX - 100 :
			Toolkit.getDefaultToolkit().getScreenSize().width - 100;
		final int MAX_HEIGHT = fan ?
			Toolkit.getDefaultToolkit().getScreenSize().height - Window.offsetY - 100 :
			Toolkit.getDefaultToolkit().getScreenSize().height - 100;
	
		final int MIN_WIDTH = 100;
		final int MIN_HEIGHT = 50;
	
		// don't allow window be oversized
		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				Component c = e.getComponent();
	
				int width = c.getWidth();
				int height = c.getHeight();
	
				boolean resize = true;
				if (width < MIN_WIDTH)
					width = MIN_WIDTH;
				else if (width > MAX_WIDTH)
					width = MAX_WIDTH;
				else
					resize = false;
	
				if (height < MIN_HEIGHT) {
					height = MIN_HEIGHT;
					resize = true;
				} else if (height > MAX_HEIGHT) {
					height = MAX_HEIGHT;
					resize = true;
				} else
					resize = false;
	
				if (resize)
					c.setSize(width, height);
			}
	
			public void componentMoved(ComponentEvent e) {}
			public void componentShown(ComponentEvent e) {}
			public void componentHidden(ComponentEvent e) {}
		});
		
		//	arrange windows nicely
		if(fan) {
		    Window.offsetX += 20;
		    Window.offsetY += 20;
			int x = 50 + (Window.offsetX)%(MAX_WIDTH - getWidth()-50);
			int y = 50 + (Window.offsetY)%(MAX_HEIGHT - getHeight()-50);
			setLocation(x, y);
		} else {
			pack();
			Utils.center(this);
		}
		
		requestFocus();
		setVisible(true);
	}
	
	/**
	 * Sets main component to the dialog. If called multiple times, old component
	 * will be replaced.
	 * 
	 * @param component component to be added
	 */
	public void setContent(JComponent component) {
		component.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		getContentPane().add(BorderLayout.CENTER, component);
	}
	
	/**
	 * Sets the icon to the left part of dialog.
	 * @param iconType default Java icons. 0=error, 1=information, 2=warning, 3=question
	 */
	public void setIcon(int iconType) {
		Icon icon = null;
		if(iconType == 0)
			icon = UIManager.getIcon("OptionPane.errorIcon");
		else if(iconType == 1)
			icon = UIManager.getIcon("OptionPane.informationIcon");
		else if(iconType == 2)
			icon = UIManager.getIcon("OptionPane.warningIcon");
		else if(iconType == 3)
			icon = UIManager.getIcon("OptionPane.questionIcon");

		JLabel label = new JLabel(icon);
		label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		getContentPane().add(BorderLayout.WEST, label);
	}
	
	/**
	 * Adds button to the row of buttons.
	 * @param btn button to be added
	 */
	public void addButton(JButton btn) {
		btns.add(btn);
	}

	/**
	 * Adds generic OK button to the row of buttons. The dialog will
	 * be closed if user click this button.
	 */
	public void addOkButton() {
		JButton okBtn =
			new JButton(Config.getLocalized("window.buttons.ok"));
		okBtn.setToolTipText(Config.getLocalized("window.buttons.ok.tip"));
		okBtn.setMnemonic(KeyEvent.VK_O);
		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		getRootPane().setDefaultButton(okBtn);
		addButton(okBtn);
	}

	/**
	 * Adds generic Close button to the row of buttons. The dialog will
	 * be closed if user click this button.
	 */
	public void addCloseButton() {
		JButton closeBtn =
			new JButton(Config.getLocalized("window.buttons.close"));
		closeBtn.setToolTipText(Config.getLocalized("window.buttons.close.tip"));
		closeBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Window.this.windowClosing(null);
			}
		});
		closeBtn.setMnemonic(KeyEvent.VK_C);
		getRootPane().setDefaultButton(closeBtn);
		addButton(closeBtn);
	}
	
    /*
     * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
     */
    public void windowOpened(WindowEvent e) {
    }
    
    /*
     * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
     */
    public void windowClosing(WindowEvent e) {
        dispose();
    }

    /*
     * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
     */
    public void windowClosed(WindowEvent e) {
    }

    /*
     * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
     */
    public void windowIconified(WindowEvent e) {
    }

    /*
     * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
     */
    public void windowDeiconified(WindowEvent e) {
    }

    /*
     * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
     */
    public void windowActivated(WindowEvent e) {
    }

    /*
     * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
     */
    public void windowDeactivated(WindowEvent e) {
    }
}



See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  myproxy/
    MyProxyGUI.java
    MyProxyOptions.java
    PasswordDialog.java
    package.html
  treetable/
    AbstractTreeTableModel.java
    DynamicTreeTableModel.java
    JTreeTable.java
    QuestionCellEditorModel.java
    RightClickAdapter.java
    TreeTableCellRenderer.java
    TreeTableModel.java
    TreeTableModelAdapter.java
    package.html
  AgentProgressIndicator.java
  EnabledLabel.java
  JobListItem.java
  SoundObserver.java
  TextObserver.java
  Utils.java
  Window.java
  package.html