001    package data.stdforms;
002    
003    import java.awt.BorderLayout;
004    import java.awt.Dimension;
005    
006    import javax.swing.BorderFactory;
007    import javax.swing.JPanel;
008    import javax.swing.JTextArea;
009    import javax.swing.event.ListSelectionEvent;
010    import javax.swing.event.ListSelectionListener;
011    
012    import sale.FormSheet;
013    import sale.FormSheetContentCreator;
014    import util.swing.AbstractTableModel;
015    import util.swing.JAbstractTable;
016    import data.stdforms.SingleTableFormSheet;
017    import data.stdforms.TwoTableFormSheet;
018    import data.stdforms.descriptiveformsheet.Descriptive;
019    
020    /**
021     * A FormSheet that can extend Single- and TwoTableFormSheets by a description 
022     * element. The element will be placed underneath the table(s) and have the 
023     * given caption.
024     * 
025     * <p>In order to display the actual description, the records of the data containers
026     * modelled by the table are considered to implement the {@link Descriptive}
027     * interface. If so, their {@link Descriptive#getDescription() getDescription()}
028     * method will be called to get the actual description. If they do not, nothing will
029     * be displayed.
030     * </p>
031     * 
032     * @author Thomas Ryssel
033     * @version 3.3
034     * 
035     * @see Descriptive
036     */
037    public class DescriptiveFormSheet extends FormSheet {
038    
039            /**
040             * ID for serialization.
041             */
042            private static final long serialVersionUID = -3413917194160476340L;
043            
044            /**
045             * Keep a reference to the SingleTableFormSheet (if given).
046             */
047            private SingleTableFormSheet stfsReference = null;
048            
049            /**
050             * Keep a reference to the TwoTableFormSheet (if given).
051             */
052            private TwoTableFormSheet ttfsReference = null;
053    
054            /**
055             * Construct a new DescriptiveFormSheet from the given SingleTableFormSheet.
056             * 
057             * @param stfs The SingleTableFormSheet which to extend.
058             * @param caption Caption of the description box at the bottom.
059             */
060            public DescriptiveFormSheet(SingleTableFormSheet stfs, final String caption) {
061                    super(stfs.getCaption(), stfs.getComponent(), false);
062                    stfsReference = stfs;
063                    addContentCreator(
064                                    new FormSheetContentCreator() {
065                            private static final long serialVersionUID = -3431604916051231570L;
066                            protected void createFormSheetContent(FormSheet fs) {
067                                    JPanel container = new JPanel(new BorderLayout());
068                                    
069                                    final JTextArea description = new JTextArea("");
070                                    description.setPreferredSize(new Dimension(-1, 100));
071                                    description.setBorder(BorderFactory.createTitledBorder(caption));
072                                    container.add(description, BorderLayout.SOUTH);
073                                    
074                                    container.add(stfsReference.getComponent(), BorderLayout.CENTER);
075                                    stfsReference.getTable().getSelectionModel().addListSelectionListener(
076                                            new ListSelectionListener() {
077                                                    public void valueChanged(ListSelectionEvent e) {
078                                                            // we cannot rely on stfsReference.getSelectedRecord() since their is no
079                                                            // guarantee on the order in which the selection listeners to the 
080                                                            // AbstractTableModel are called, therefore getSelectedRecord() could 
081                                                            // return an old reference.
082                                                            JAbstractTable table = (JAbstractTable) stfsReference.getTable();
083                                                            AbstractTableModel model = table.getAbstractTableModel();
084                                                            Object record = model.getRecord(table.getSelectedRow());
085                                                            if (record != null && record instanceof Descriptive) {
086                                                                    description.setText(((Descriptive)record).getDescription());
087                                                            } else {
088                                                                    description.setText("");
089                                                            }
090                                                    }
091                                            }
092                                    );
093                                    fs.setComponent(container);
094                            }
095                    });
096            }
097            
098            /**
099             * Construct a new DescriptiveFormSheet from the given TwoTableFormSheet.
100             * 
101             * @param ttfs The TwoTableFormSheet which to extend.
102             * @param caption Caption of the description box at the bottom.
103             */
104            public DescriptiveFormSheet(TwoTableFormSheet ttfs, final String caption) {
105                    super(ttfs.getCaption(), ttfs.getComponent(), false);
106                    ttfsReference = ttfs;
107                    addContentCreator(
108                                    new FormSheetContentCreator() {
109                            private static final long serialVersionUID = -3431604916051231570L;
110                            protected void createFormSheetContent(FormSheet fs) {
111                                    JPanel container = new JPanel(new BorderLayout());
112                                    
113                                    final JTextArea description = new JTextArea("");
114                                    description.setPreferredSize(new Dimension(-1, 100));
115                                    description.setBorder(BorderFactory.createTitledBorder(caption));
116                                    container.add(description, BorderLayout.SOUTH);
117                                    
118                                    container.add(ttfsReference.getComponent(), BorderLayout.CENTER);
119                                    ttfsReference.getLeftTable().getSelectionModel().addListSelectionListener(
120                                            new ListSelectionListener() {
121                                                    public void valueChanged(ListSelectionEvent e) {
122                                                            // we cannot rely on ttfsReference.getLeftSelectedRecord() since their is no
123                                                            // guarantee on the order in which the selection listeners to the 
124                                                            // AbstractTableModel are called, therefore getLeftSelectedRecord() could 
125                                                            // return an old reference.
126                                                            JAbstractTable table = (JAbstractTable) ttfsReference.getLeftTable();
127                                                            AbstractTableModel model = table.getAbstractTableModel();
128                                                            Object record = model.getRecord(table.getSelectedRow());
129                                                            if (record != null && record instanceof Descriptive) {
130                                                                    description.setText(((Descriptive)record).getDescription());
131                                                            } else {
132                                                                    description.setText("");
133                                                            }
134                                                    }
135                                            }
136                                    );
137                                    fs.setComponent(container);
138                            }
139                    });     
140            }
141            
142            /**
143             * Get the SingleTableFormSheet this formsheet was constructed with (if so),
144             * or <code>null</code> in any other case.
145             * 
146             * @return The original SingleTableFormSheet.
147             */
148            public SingleTableFormSheet getSingleTableFormSheet() {
149                    return stfsReference;
150            }
151            
152            /**
153             * Get the TwoTableFormSheet this formsheet was constructed with (if so),
154             * or <code>null</code> in any other case.
155             * 
156             * @return The original TwoTableFormSheet.
157             */
158            public TwoTableFormSheet getTwoTableFormSheet() {
159                    return ttfsReference;
160            }
161    
162    }