package sale;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.io.*;
import sale.stdforms.*;
import sale.events.*;
public class JDisplayDialog extends JDialog implements Display, FormSheetContainer {
private transient Object m_oWaiter;
private Object getWaiter() {
if (m_oWaiter == null) {
m_oWaiter = new Object();
}
return m_oWaiter;
}
private transient JComponent m_jcmpComponent;
private transient JPanel m_jpButtonBar;
private transient FormSheet m_fsCurrent;
private transient MenuSheet m_msCurrent;
protected transient EventListenerList m_ellListeners = new EventListenerList();
private void writeObject (ObjectOutputStream oos)
throws IOException {
throw new NotSerializableException ("JDisplayDialog");
}
public JDisplayDialog() {
super();
getContentPane().setLayout (new java.awt.BorderLayout());
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
if (m_fsCurrent != null) {
m_fsCurrent.cancel();
}
}
});
}
public JDisplayDialog (JFrame jfOwner) {
super (jfOwner);
getContentPane().setLayout (new java.awt.BorderLayout());
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
if (m_fsCurrent != null) {
m_fsCurrent.cancel();
}
}
});
}
public void closeFormSheet (FormSheet fs) {
boolean fExplicit = true;
fs.detachDisplay();
if (m_fsCurrent == fs) {
m_fsCurrent = null;
}
else {
fExplicit = false;
}
formSheetClosed();
synchronized (getWaiter()) {
getWaiter().notifyAll();
}
fireFormSheetRemoved (fs, fExplicit);
}
protected void formSheetClosed() {
setVisible (false);
dispose();
}
public void dispose() {
try {
setFormSheet (null);
}
catch (InterruptedException e) {}
setMenuSheet (null);
super.dispose();
}
public void onFormSheetCaptionChanged (FormSheet fs, String sNewCaption) {
setTitle (sNewCaption);
}
public void onFormSheetComponentChanged (FormSheet fs, JComponent jcmpNew) {
synchronized (fs.getComponentLock()) {
getContentPane().remove (m_jcmpComponent);
m_jcmpComponent = fs.getComponent();
if (m_jcmpComponent != null) {
getContentPane().add (m_jcmpComponent, "Center");
}
pack();
}
}
public void onFormSheetButtonAdded (FormSheet fs, FormSheet.FormButton fb) {
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.add (fb.getPeer());
pack();
}
}
public void onFormSheetButtonRemoved (FormSheet fs, FormSheet.FormButton fb) {
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.remove (fb.getPeer());
pack();
}
}
public void onFormSheetButtonsCleared (FormSheet fs) {
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.removeAll();
pack();
}
}
public void setFormSheet (FormSheet fs)
throws InterruptedException {
if (m_fsCurrent != null) {
FormSheet fsTemp = m_fsCurrent;
if (fs != null) {
m_fsCurrent = null;
}
fsTemp.cancel();
}
getContentPane().removeAll();
if (fs != null) {
synchronized (fs.getComponentLock()) {
synchronized (fs.getButtonsLock()) {
setTitle (fs.getCaption());
fs.attach (this);
m_fsCurrent = fs;
m_jcmpComponent = fs.getComponent();
if (m_jcmpComponent != null) {
getContentPane().add (m_jcmpComponent, "Center");
}
m_jpButtonBar = new JPanel (false);
fs.fillBtnPanel (m_jpButtonBar);
getContentPane().add (m_jpButtonBar, "South");
pack();
}
}
fireFormSheetSet (fs);
if (fs.waitResponse()) {
synchronized (getWaiter()) {
while (fs.getDisplay() == this) {
getWaiter().wait();
}
}
}
}
}
public void closeFormSheet() {
if (m_fsCurrent != null) {
closeFormSheet (m_fsCurrent);
}
}
public void popUpFormSheet (FormSheet fs)
throws InterruptedException {
JDisplayDialog jdd = new JDisplayDialog();
jdd.setVisible (true);
try {
jdd.setFormSheet (fs);
}
catch (InterruptedException e) {
if (fs.getDisplay() == jdd) {
fs.cancel();
}
throw e;
}
}
public void setMenuSheet (MenuSheet ms) {
if (m_msCurrent != null) {
m_msCurrent.setVisible (false);
}
m_msCurrent = ms;
if (m_msCurrent != null) {
m_msCurrent.setVisible (true);
setJMenuBar (ms.getMenuBar());
}
else {
setJMenuBar (null);
}
pack();
}
public boolean isUseableDisplay() {
return true;
}
public void addFormSheetListener (FormSheetListener fsl) {
m_ellListeners.add (FormSheetListener.class, fsl);
}
public void removeFormSheetListener (FormSheetListener fsl) {
m_ellListeners.remove (FormSheetListener.class, fsl);
}
protected void fireFormSheetSet (FormSheet fs) {
FormSheetEvent e = null;
Object[] listeners = m_ellListeners.getListenerList();
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==FormSheetListener.class) {
if (e == null)
e = new FormSheetEvent (this, fs, true);
((FormSheetListener) listeners[i+1]).formSheetSet (e);
}
}
}
protected void fireFormSheetRemoved (FormSheet fs, boolean fExplicit) {
FormSheetEvent e = null;
Object[] listeners = m_ellListeners.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 static void main(java.lang.String[] args) {
final JDisplayDialog jdd = new JDisplayDialog ();
jdd.show ();
MenuSheet ms = new MenuSheet ("Main");
ms.add (new MenuSheetItem ("Quit", new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
jdd.dispose();
}
}));
jdd.setMenuSheet (ms);
final MsgForm mfTest = new sale.stdforms.MsgForm ("Testmessage",
"Dies ist ein Test des JFormSheetFrames.\n\n" +
"Wir verwenden dazu ein veraendertes MsgForm.");
mfTest.addButton ("Toggle Caption", 1, new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
if (mfTest.getCaption().equals ("Testmessage")) {
mfTest.setCaption ("Geaendert !");
}
else {
mfTest.setCaption ("Testmessage");
}
}
});
mfTest.addButton ("Add button", 2, new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
mfTest.addButton ("Tester", 700, null);
mfTest.getButton (2).setEnabled (false);
mfTest.getButton (3).setEnabled (true);
}
});
mfTest.addButton ("Remove button", 3, new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
mfTest.removeButton (700);
mfTest.getButton (2).setEnabled (true);
mfTest.getButton (3).setEnabled (false);
}
});
mfTest.getButton (3).setEnabled (false);
final JComponent[] ajcmp = new JComponent[1];
ajcmp[0] = new JLabel ("Tester");
mfTest.addButton ("Toggle Component", 4, new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
ajcmp[0] = mfTest.setComponent (ajcmp[0]);
}
});
try {
jdd.setFormSheet (mfTest);
}
catch (InterruptedException e) {}
System.err.println ("FormSheet was " + ((mfTest.isCancelled())?("cancelled."):("closed normally.")));
System.exit (0);
}
}