/* ==============================================
* Simtools : The tools library used in JSynoptic
* ==============================================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 1999-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
* Nicolas Brodu
*
* Changes
* -------
* 18-mai-2005 : Initial public release (Benjamin Levy);
*
*/
package simtools.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.beans.PropertyVetoException;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
/**
* @author zxmultran034
*
* OpenCenter Project - EADS Astrium
*/
public class DesktopMode implements DesktopCardPanelMode {
//
// static members
//
/** default frame height */
private static final int DEFAULT_HEIGHT = 200;
/** minimum frame width */
private static final int MIN_WIDTH = 40;
/** minimum frame height */
private static final int MIN_HEIGHT = 40;
/** next frame X coordinate */
private int _frameX;
/** next frame Y coordinate */
private int _frameY;
/** desktop witdh */
private int _frameMaxWidth;
/** desktop height */
private int _frameMaxHeight;
/** desktop */
private JDesktopPane _framesPane = new JDesktopPane();
/** frames listener */
protected FramesListener _framesListener = new FramesListener();
/** the desktopCardPanel in wich the components are layed out */
protected DesktopCardPanel _desktopCardPanel;
/**
* @param cardPanel
* the desktopCardPanel in wich the components are layed out
*/
protected DesktopMode(DesktopCardPanel desktopCardPanel) {
_desktopCardPanel = desktopCardPanel;
}
//DesktopCardPanelMode implementation
/**
* Adds a new component to the desktop
*
* @param d
* the component
*/
public void addComponent(JComponent d) {
if (_framesPane.getComponentCount() == 0) {
initDesktop();
}
ComponentFrame f = new ComponentFrame(d, _desktopCardPanel
.is_scrollable(), _desktopCardPanel.is_closable());
if (_framesListener != null) {
f.addPropertyChangeListener(_framesListener);
f.addInternalFrameListener(_framesListener);
}
_framesPane.add(f);
f.reshape(_frameX, _frameY, _frameMaxWidth, DEFAULT_HEIGHT);
computeNextFramePosition();
try {
f.setSelected(false);
} catch (java.beans.PropertyVetoException pve) {
}
}
/**
* Removes a component from the desktop
*
* @param d
* the component
*/
public void removeComponent(JComponent d) {
ComponentFrame f = (ComponentFrame) getContainer(d);
if (f != null) {
_framesPane.remove(f);
return;
}
}
/**
* returns the container in wich the desktop components are layed out
*
* @return the container or null if the components are not layed out in a
* container (external)
*/
public Container getContentPane() {
return _framesPane;
}
/**
* Selects a component
*
* @param d
* the component
*/
public void selectComponent(JComponent d) {
ComponentFrame f = (ComponentFrame) getContainer(d);
if (f != null) {
try {
f.setSelected(true);
} catch (java.beans.PropertyVetoException pve) {
}
}
}
/**
* Updates the name of a component
*
* @param d
* the component
*/
public void updateComponentName(JComponent d) {
ComponentFrame f = (ComponentFrame) getContainer(d);
if (f != null) {
f.setTitle(d.getName());
}
}
/**
*
* returns the Container that contains the specified component
*
* @param d
* the component
*
* @return the container
*/
public Container getContainer(JComponent d) {
JInternalFrame[] tab = _framesPane.getAllFrames();
if (tab == null) {
return null;
}
for (int i = 0; i < tab.length; i++) {
ComponentFrame f = (ComponentFrame) tab[i];
if (f.getComponent() == d) {
return f;
}
}
return null;
}
/**
* Computes next frame coordinates
*/
protected void computeNextFramePosition() {
_frameX += MIN_WIDTH;
_frameY += MIN_WIDTH;
if ((_frameY + DEFAULT_HEIGHT) >= _frameMaxHeight) {
_frameY = 0;
}
if (_frameX >= _frameMaxWidth) {
_frameX = 0;
}
}
/**
* init desktop size and next frame coordinates
*/
private void initDesktop() {
_frameX = 0;
_frameY = 0;
Dimension d = _framesPane.getSize();
if (d.width < MIN_WIDTH) {
_frameMaxWidth = MIN_WIDTH;
} else {
_frameMaxWidth = d.width;
}
if (d.height < MIN_HEIGHT) {
_frameMaxHeight = MIN_HEIGHT;
} else {
_frameMaxHeight = d.height;
}
}
/**
* The JInternalFrame holder for the components in desktop mode The
* constructor creates an invisible frame whithout any component inside. If
* needed, a scollpane is added to enable component scrolling.
*/
public class ComponentFrame extends JInternalFrame {
private JComponent _component;
private JScrollPane _scrollPane;
/**
* Creates an inner frame to display a component in the DesktopCardPanel
* using cascade mode. The frame is closable.
*
* @param d
* the component
* @param scrollable
* =true if scrolling is required
*/
public ComponentFrame(JComponent d, boolean scrollable) {
this(d, scrollable, true);
}
/**
* Creates an inner frame to display a component in the DesktopCardPanel
* using cascade mode
*
* @param d
* the component
* @param scrollable
* =true if scrolling is required
* @param closable
* =true if the frame can be closed
*/
public ComponentFrame(JComponent d, boolean scrollable, boolean closable) {
super("", true, closable, true, true);
_component = d;
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
if (scrollable) {
_scrollPane = new JScrollPane(_component,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
_scrollPane.getViewport().setScrollMode(DesktopCardPanel.SCROLLING_MODE);
getContentPane().add(BorderLayout.CENTER, _scrollPane);
} else {
getContentPane().add(BorderLayout.CENTER, _component);
}
if (d.getName() != null) {
setTitle(d.getName());
}
pack();
show();
}
/**
* Gets the inner component
*
* @return the inner component
*/
public JComponent getComponent() {
return _component;
}
/**
* Checks if scrolling is allowed
*
* @return true if scrolling is allowed
*/
public boolean isScrollable() {
return (_scrollPane == null) ? false : true;
}
}
/**
* The frames listener. The following events are caught: frame is maximised
* which leads to single panel mode frame is closed which leads to remove
* the component frame is activated which leads to select the component
*/
public class FramesListener extends InternalFrameAdapter implements
java.beans.PropertyChangeListener {
//
// PropertyChangeListener interface
//
public void propertyChange(java.beans.PropertyChangeEvent e) {
ComponentFrame f = (ComponentFrame) e.getSource();
JComponent d = f.getComponent();
if (e.getPropertyName().equals(JInternalFrame.IS_MAXIMUM_PROPERTY)) {
Boolean b = (Boolean) e.getNewValue();
if (b.equals(Boolean.TRUE)) {
_desktopCardPanel.setMode(DesktopCardPanel.CARD_MODE);
selectComponent(d);
}
}
}
//
// InternalFrameListener interface
//
public void internalFrameClosing(InternalFrameEvent e) {
ComponentFrame f = (ComponentFrame) e.getSource();
f.setVisible(false);
if (f.isSelected()) {
try {
f.setSelected(false);
} catch (PropertyVetoException pve) {
}
}
_desktopCardPanel.removeComponent(f.getComponent());
}
/* (non-Javadoc)
* @see javax.swing.event.InternalFrameListener#internalFrameActivated(javax.swing.event.InternalFrameEvent)
*/
public void internalFrameActivated(InternalFrameEvent e) {
ComponentFrame f = (ComponentFrame) e.getSource();
selectComponent(f.getComponent());
//select the component in the desktop card panel.
_desktopCardPanel.selectComponent(f.getComponent());
}
}
}