package sale;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import sale.events.*;
import sale.stdforms.*;
import util.ListenerHelper;
public class JDisplayFrame extends javax.swing.JFrame implements Display {
private String m_sFrameTitle;
private String m_sFrameTitlePrefix;
private FormSheet m_fsCurrent;
private MenuSheet m_msCurrent;
protected ListenerHelper m_lhListeners = new ListenerHelper();
private static class DFFormSheetContainer implements FormSheetContainer, Serializable {
private transient JDisplayFrame m_jdfOwner;
public DFFormSheetContainer (JDisplayFrame jdfOwner) {
super();
setOwner (jdfOwner);
}
public void setOwner (JDisplayFrame jdfOwner) {
m_jdfOwner = jdfOwner;
}
public void onFormSheetButtonsCleared(FormSheet fs) {
m_jdfOwner.onFormSheetButtonsCleared (fs);
}
public void onFormSheetButtonAdded(FormSheet fs,FormSheet.FormButton fb) {
m_jdfOwner.onFormSheetButtonAdded (fs, fb);
}
public void onFormSheetButtonRemoved(FormSheet fs,FormSheet.FormButton fb) {
m_jdfOwner.onFormSheetButtonRemoved (fs, fb);
}
public void closeFormSheet(FormSheet fs) {
m_jdfOwner.closeFormSheet (fs);
}
public void onFormSheetComponentChanged(FormSheet fs,JComponent jcmpNew) {
m_jdfOwner.onFormSheetComponentChanged (fs, jcmpNew);
}
public void onFormSheetCaptionChanged(FormSheet fs,String sNewCaption) {
m_jdfOwner.onFormSheetCaptionChanged (fs, sNewCaption);
}
}
private DFFormSheetContainer m_dffscContainer = new DFFormSheetContainer (this);
private boolean m_fHadFormSheet = false;
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 void writeObject (ObjectOutputStream oos)
throws IOException {
throw new NotSerializableException ("JDisplayFrame");
}
private void readObject (ObjectInputStream ois)
throws IOException {
throw new NotSerializableException ("JDisplayFrame");
}
public JDisplayFrame() {
super();
setDefaultCloseOperation (DO_NOTHING_ON_CLOSE);
addWindowListener (new java.awt.event.WindowAdapter () {
public void windowClosing (java.awt.event.WindowEvent evt) {
exitForm();
}
});
pack();
}
public void save (ObjectOutputStream oos)
throws IOException {
util.Debug.print ("Writing JDisplayFrame \"" + super.getTitle() + "\".", -1);
oos.writeObject (m_dffscContainer);
oos.writeObject (m_sFrameTitle);
oos.writeObject (m_sFrameTitlePrefix);
util.Debug.print (" Writing JDisplayFrame's FormSheet.", -1);
oos.writeObject (m_fsCurrent);
util.Debug.print (" Writing JDisplayFrame's MenuSheet.", -1);
oos.writeObject (m_msCurrent);
util.Debug.print (" Finished writing JDisplayFrame's MenuSheet.", -1);
oos.writeObject (m_lhListeners);
oos.writeBoolean (m_fHadFormSheet);
oos.writeObject (getBounds());
oos.writeBoolean (isVisible());
util.Debug.print ("Finished writing JDisplayFrame \"" + super.getTitle() + "\"!", -1);
}
public void load (ObjectInputStream ois)
throws IOException, ClassNotFoundException {
m_dffscContainer = (DFFormSheetContainer) ois.readObject();
m_dffscContainer.setOwner (this);
setTitle ((String) ois.readObject());
setFrameTitlePrefix ((String) ois.readObject());
final FormSheet fsCurrent = (FormSheet) ois.readObject();
final MenuSheet msCurrent = (MenuSheet) ois.readObject();
m_lhListeners = (ListenerHelper) ois.readObject();
m_fHadFormSheet = ois.readBoolean();
final java.awt.Rectangle rcBounds = (java.awt.Rectangle) ois.readObject();
final boolean fVisible = ois.readBoolean();
m_dffscContainer = new DFFormSheetContainer (this);
ois.registerValidation (new ObjectInputValidation() {
public void validateObject() {
setBounds (rcBounds);
try {
fsCurrent.setWaitResponse (false);
setFormSheet (fsCurrent);
setMenuSheet (msCurrent);
}
catch (InterruptedException ie) {}
setVisible (fVisible);
}
},
OIV.JDISPLAYFRAME_PRIO);
}
public void setTitle (String sTitle) {
m_sFrameTitle = sTitle;
updateFrameTitle();
}
public String getTitle() {
return m_sFrameTitle;
}
public void setFrameTitlePrefix (String sPrefix) {
m_sFrameTitlePrefix = sPrefix;
updateFrameTitle();
}
public String getFrameTitlePrefix() {
return m_sFrameTitlePrefix;
}
private void updateFrameTitle() {
String sTitle = "";
if (m_sFrameTitlePrefix != null) {
sTitle += m_sFrameTitlePrefix;
if (m_sFrameTitle != null) {
sTitle += " - ";
}
}
if (m_sFrameTitle != null) {
sTitle += m_sFrameTitle;
}
super.setTitle (sTitle);
}
protected void exitForm() {
setVisible (false);
dispose();
}
public void setFormSheet (FormSheet fs)
throws InterruptedException {
util.Debug.print ("setFormSheet (" + fs + ")", -1);
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 (m_dffscContainer);
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");
if (m_fHadFormSheet) {
getRootPane().revalidate();
repaint();
}
else {
m_fHadFormSheet = true;
pack();
}
}
}
fireFormSheetSet (fs);
try {
if (fs.waitResponse()) {
synchronized (getWaiter()) {
util.Debug.print ("JDisplayFrame.setFormSheet: Preparing to wait for " + fs, -1);
while (fs.getDisplay() == m_dffscContainer) {
util.Debug.print ("JDisplayFrame.setFormSheet: Starting to wait for " + fs, -1);
getWaiter().wait();
util.Debug.print ("JDisplayFrame.setFormSheet: Caught notification waiting for " + fs, -1);
}
}
}
}
catch (InterruptedException ie) {
throw ie;
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
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);
}
getRootPane().revalidate();
repaint();
}
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);
}
protected void fireFormSheetSet (FormSheet fs) {
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, true);
((FormSheetListener) listeners[i+1]).formSheetSet (e);
}
}
}
protected 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 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() {
exitForm();
}
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) {
if (m_fsCurrent == null) {
return;
}
synchronized (fs.getComponentLock()) {
getContentPane().remove (m_jcmpComponent);
m_jcmpComponent = fs.getComponent();
if (m_jcmpComponent != null) {
getContentPane().add (m_jcmpComponent, "Center");
}
getRootPane().revalidate();
repaint();
}
}
public void onFormSheetButtonAdded(FormSheet fs,FormSheet.FormButton fb) {
if (m_fsCurrent == null) {
return;
}
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.add (fb.getPeer());
getRootPane().revalidate();
repaint();
}
}
public void onFormSheetButtonRemoved(FormSheet fs,FormSheet.FormButton fb) {
if (m_fsCurrent == null) {
return;
}
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.remove (fb.getPeer());
getRootPane().revalidate();
repaint();
}
}
public void onFormSheetButtonsCleared(FormSheet fs) {
if (m_fsCurrent == null) {
return;
}
synchronized (fs.getButtonsLock()) {
m_jpButtonBar.removeAll();
getRootPane().revalidate();
repaint();
}
}
public static void main(java.lang.String[] args) {
final JDisplayFrame jdf = new JDisplayFrame ();
jdf.setVisible (true);
MenuSheet ms = new MenuSheet ("Main");
ms.add (new MenuSheetItem ("Quit", new Action() {
public void doAction (SaleProcess p, SalesPoint sp) {
jdf.dispose();
}
}));
jdf.setMenuSheet (ms);
final MsgForm mfTest = new sale.stdforms.MsgForm ("Testmessage",
"Dies ist ein Test des JDisplayFrame.\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 {
jdf.setFormSheet (mfTest);
}
catch (InterruptedException e) {}
System.err.println ("FormSheet was " + ((mfTest.isCancelled())?("cancelled."):("closed normally.")));
System.exit (0);
}
}