package sale.multiwindow;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import sale.*;
import sale.events.*;
import util.*;
import java.io.*;
public class MultiWindowHandle extends Object implements FormSheetContainer,
Display,
Serializable {
private Integer m_iID;
private transient JInternalFrame m_jifFrame = null;
private transient MultiWindow m_mwOwner = null;
private FormSheet m_fsFormSheet = null;
private MenuSheet m_msMenuSheet = null;
private String m_sDisplayCaption = null;
private transient JComponent m_jcmpComponent = null;
private transient JPanel m_jpButtonPanel = null;
private transient JComponent m_jcmpContentPane = null;
private ListenerHelper m_lhListeners = new ListenerHelper();
private boolean m_fIsNew;
private transient Object m_oWaiter;
private final Object getWaiter() {
if (m_oWaiter == null) {
m_oWaiter = new Object();
}
return m_oWaiter;
}
private void writeObject (ObjectOutputStream oos) throws IOException {
util.Debug.print ("Writing MultiWindowHandle \"" + getDisplayCaption() + "\".", -1);
oos.defaultWriteObject();
oos.writeObject (getFrame().getTitle());
oos.writeObject (getFrame().getBounds());
util.Debug.print ("Finished writing MultiWindowHandle \"" + getDisplayCaption() + "\".", -1);
}
private void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
getFrame().setTitle ((String) ois.readObject());
final java.awt.Rectangle rcBounds = (java.awt.Rectangle) ois.readObject();
ois.registerValidation (new ObjectInputValidation() {
public void validateObject() {
getFrame().setBounds (rcBounds);
if (m_fsFormSheet != null) {
synchronized (m_fsFormSheet.getComponentLock()) {
synchronized (m_fsFormSheet.getButtonsLock()) {
onFormSheetComponentChanged (m_fsFormSheet, m_fsFormSheet.getComponent());
m_jpButtonPanel = null;
m_fsFormSheet.fillBtnPanel (getButtonPanel());
m_jcmpContentPane.validate();
}
}
}
}
},
OIV.MULTIWINDOW_HANDLE_PRIO);
}
MultiWindowHandle (Integer iNewID) {
super();
m_iID = iNewID;
m_jifFrame = null;
}
final void setOwner (MultiWindow mwOwner) {
m_mwOwner = mwOwner;
if (m_mwOwner != null) {
m_fIsNew = (m_mwOwner.getViewMode() == MultiWindow.TABBED);
}
}
private final void fireMenuSheetChanged (MenuSheet msNewMenuSheet) {
if (m_mwOwner != null) m_mwOwner.onMenuSheetChanged (this, msNewMenuSheet);
}
private final void fireDisplayCaptionChanged (String sNewCaption) {
if (m_mwOwner != null) m_mwOwner.onDisplayCaptionChanged (this, sNewCaption);
}
private final void fireDisplayFocusGained() {
if (m_mwOwner != null) m_mwOwner.onDisplayFocusGained (this, m_iID);
}
private final void fireDisplayClosing() {
if (m_mwOwner != null) m_mwOwner.onDisplayClosing (this);
}
private final void fireComponentChanged (JComponent jcmpNewComponent) {
if (m_mwOwner != null) m_mwOwner.onComponentChanged (this, jcmpNewComponent);
}
public void closeFormSheet (FormSheet fs) {
closeFormSheet (true);
}
public void onFormSheetCaptionChanged (FormSheet fs, String sNewCaption) {
String sTitle = getDisplayCaption() + ((sNewCaption != null)?" - " + sNewCaption:"");
getFrame().setTitle (sTitle);
fireDisplayCaptionChanged (sTitle);
}
public void onFormSheetComponentChanged (FormSheet fs, JComponent jcmpNewComponent) {
synchronized (fs.getComponentLock()) {
if (m_jcmpComponent != null) {
m_jcmpContentPane.remove (m_jcmpComponent);
}
if (jcmpNewComponent != null) {
m_jcmpContentPane.add (jcmpNewComponent, "Center");
}
m_jcmpComponent = jcmpNewComponent;
fireComponentChanged (m_jcmpComponent);
}
}
public void onFormSheetButtonAdded (FormSheet fs, FormSheet.FormButton fbNewButton) {
synchronized (fs.getButtonsLock()) {
getButtonPanel().add (fbNewButton.getPeer());
m_jcmpContentPane.validate();
m_jcmpContentPane.repaint();
}
}
public void onFormSheetButtonRemoved (FormSheet fs, FormSheet.FormButton fbRemovedButton) {
synchronized (fs.getButtonsLock()) {
getButtonPanel().remove (fbRemovedButton.getPeer());
getButtonPanel().validate();
getButtonPanel().repaint();
}
}
public void onFormSheetButtonsCleared (FormSheet fs) {
synchronized (fs.getButtonsLock()) {
getButtonPanel().removeAll();
getButtonPanel().validate();
getButtonPanel().repaint();
}
}
public void setFormSheet (FormSheet fsNewFormSheet)
throws InterruptedException {
closeFormSheet (fsNewFormSheet == null);
if (fsNewFormSheet != null) {
synchronized (fsNewFormSheet.getComponentLock()) {
synchronized (fsNewFormSheet.getButtonsLock()) {
m_fsFormSheet = fsNewFormSheet;
fsNewFormSheet.attach (this);
onFormSheetCaptionChanged (m_fsFormSheet, m_fsFormSheet.getCaption());
JComponent jcmp = m_fsFormSheet.getComponent();
onFormSheetComponentChanged (m_fsFormSheet, jcmp);
m_jpButtonPanel = null;
m_fsFormSheet.fillBtnPanel (getButtonPanel());
m_jcmpContentPane.validate();
fireComponentChanged (jcmp);
}
}
}
fireFormSheetSet();
if ((fsNewFormSheet != null) && (fsNewFormSheet.waitResponse())) {
synchronized (getWaiter()) {
while (fsNewFormSheet.getDisplay() == this) {
getWaiter().wait();
}
}
}
}
public void closeFormSheet() {
closeFormSheet (true);
}
public void popUpFormSheet (FormSheet fs)
throws InterruptedException {
JDisplayDialog jdd = new JDisplayDialog (m_mwOwner);
jdd.setVisible (true);
try {
jdd.setFormSheet (fs);
}
catch (InterruptedException e) {
if (fs.getDisplay() == jdd) {
fs.cancel();
}
throw e;
}
}
public void setMenuSheet (MenuSheet msNewMenuSheet) {
if (m_msMenuSheet != null) {
m_msMenuSheet.setVisible (false);
}
m_msMenuSheet = msNewMenuSheet;
if (msNewMenuSheet == null) {
getFrame().setJMenuBar (null);
}
else {
getFrame().setJMenuBar (m_msMenuSheet.getMenuBar());
m_msMenuSheet.setVisible (true);
}
fireMenuSheetChanged (msNewMenuSheet);
}
public boolean isUseableDisplay() {
return true;
}
public void addFormSheetListener (FormSheetListener fsl) {
m_lhListeners.add (FormSheetListener.class, fsl);
}
public void removeFormSheetListener (FormSheetListener fsl) {
m_lhListeners.remove (FormSheetListener.class, fsl);
}
final Integer getID() { return m_iID; }
private final void fireFormSheetSet() {
FormSheetEvent e = null;
Object[] listeners = m_lhListeners.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FormSheetListener.class) {
if (e == null)
e = new FormSheetEvent (this, m_fsFormSheet, true);
((FormSheetListener) listeners[i+1]).formSheetSet (e);
}
}
}
private final void fireFormSheetRemoved (FormSheet fs, boolean fExplicit) {
FormSheetEvent e = null;
Object[] listeners = m_lhListeners.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FormSheetListener.class) {
if (e == null)
e = new FormSheetEvent (this, fs, fExplicit);
((FormSheetListener) listeners[i+1]).formSheetRemoved (e);
}
}
}
public MenuSheet getMenuSheet() {
return m_msMenuSheet;
}
public FormSheet getFormSheet() {
return m_fsFormSheet;
}
private void closeFormSheet (boolean fExplicit) {
if (m_fsFormSheet != null) {
FormSheet fs = m_fsFormSheet;
m_fsFormSheet.detachDisplay();
m_fsFormSheet = null;
onFormSheetCaptionChanged (null, null);
getContentPane().removeAll();
getContentPane().validate();
getContentPane().repaint();
m_jcmpComponent = null;
m_jpButtonPanel = null;
synchronized (getWaiter()) {
getWaiter().notifyAll();
}
fireFormSheetRemoved (fs, fExplicit);
}
}
final JComponent getComponent() { return m_jcmpComponent; }
final JComponent getContentPane() { return m_jcmpContentPane; }
final JPanel getButtonPanel() {
if (m_jpButtonPanel == null) {
m_jpButtonPanel = new JPanel();
getContentPane().add (m_jpButtonPanel, "South");
}
return m_jpButtonPanel;
}
final JInternalFrame getFrame() {
if (m_jifFrame == null) {
m_jifFrame = new JInternalFrame ("", true, false, true, true);
m_jifFrame.setDefaultCloseOperation (JInternalFrame.DO_NOTHING_ON_CLOSE);
m_jifFrame.addInternalFrameListener (new InternalFrameAdapter() {
public void internalFrameClosing (InternalFrameEvent evt) {
closeDisplay();
}
public void internalFrameActivated (InternalFrameEvent evt) {
fireDisplayFocusGained();
}
});
m_jcmpContentPane = (JPanel) m_jifFrame.getContentPane();
m_jifFrame.setBounds (0, 0, 400, 200);
}
return m_jifFrame;
}
final void transformToPane() {
if (m_msMenuSheet != null) {
getFrame().setJMenuBar (m_msMenuSheet.getMenuBar());
m_msMenuSheet.setVisible (true);
}
m_jcmpContentPane = new JPanel (new java.awt.BorderLayout());
if (m_jcmpComponent != null) {
m_jcmpContentPane.add (m_jcmpComponent, "Center");
}
if (m_jpButtonPanel != null) {
m_jcmpContentPane.add (m_jpButtonPanel, "South");
}
getFrame().setContentPane (m_jcmpContentPane);
if (m_fIsNew) getFrame().pack();
else getFrame().validate();
m_fIsNew = false;
}
final void transformToTab() {
getFrame().setJMenuBar (null);
if (m_msMenuSheet != null) {
m_msMenuSheet.setVisible (false);
}
getFrame().setContentPane (new JPanel());
}
public void setDisplayCaption (String sNewCaption) {
m_sDisplayCaption = sNewCaption;
onFormSheetCaptionChanged (m_fsFormSheet, (m_fsFormSheet != null)?m_fsFormSheet.getCaption():null);
}
public String getDisplayCaption() {
return (m_sDisplayCaption != null)?m_sDisplayCaption:"";
}
void closeDisplay() {
if (m_jifFrame == null) {
return;
}
closeFormSheet (true);
fireDisplayClosing();
JInternalFrame jif = m_jifFrame;
m_jifFrame = null;
jif.setVisible (false);
try {
jif.dispose();
}
catch (NullPointerException npe) {
}
}
}