home *** CD-ROM | disk | FTP | other *** search
Wrap
package asp.wizard; import com.sun.java.swing.CellEditor; import com.sun.java.swing.DefaultCellEditor; import com.sun.java.swing.DefaultListModel; import com.sun.java.swing.JButton; import com.sun.java.swing.JComboBox; import com.sun.java.swing.JComponent; import com.sun.java.swing.JLabel; import com.sun.java.swing.JList; import com.sun.java.swing.JPanel; import com.sun.java.swing.JScrollPane; import com.sun.java.swing.JTable; import com.sun.java.swing.ListModel; import com.sun.java.swing.ListSelectionModel; import com.sun.java.swing.event.AncestorEvent; import com.sun.java.swing.event.AncestorListener; import com.sun.java.swing.table.DefaultTableModel; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Rectangle; import java.awt.event.ActionListener; import java.lang.reflect.Array; import java.util.TooManyListenersException; import java.util.Vector; public class ListToTablePanel extends JPanel implements AncestorListener { private static final int LIST_WIDTH = 100; private static final int BUTTON_INSET = 3; private JLabel _lblList; private JList _lboList; private JScrollPane _scpList; private JLabel _lblTable; protected JTable _tblTable; protected JScrollPane _scpTable; private JButton _btnToRight; private JButton _btnToLeft; private JButton _btnAllToRight; private JButton _btnAllToLeft; protected JButton _btnMoveUp; protected JButton _btnMoveDown; private JComboBox _cboTable; private XferItemListener _xferItemListener; public ListToTablePanel() { this.initComponents(); this.initLayout(); this.initListeners(); } private JLabel setupLabel(JLabel l, Component comp) { l.setFont(new Font("Dialog", 0, 11)); if (comp != null) { l.setLabelFor(comp); } return l; } protected void addComponent(Component comp, GridBagLayout gbl, GridBagConstraints gbc, int x, int y, int width, int height, double weightx, double weighty, int anchor, int fill, int insettop, int insetleft, int insetbottom, int insetright) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = width; gbc.gridheight = height; gbc.weightx = weightx; gbc.weighty = weighty; gbc.anchor = anchor; gbc.fill = fill; gbc.insets.top = insettop; gbc.insets.left = insetleft; gbc.insets.bottom = insetbottom; gbc.insets.right = insetright; ((Container)this).add(comp, gbc); } protected void initComponents() { this._lboList = new JList(); this._lboList.setFixedCellWidth(100); this._lblList = this.setupLabel(new JLabel("List:"), this._lboList); this._scpList = new JScrollPane(this._lboList); this._btnAllToRight = new JButton(">>"); this._btnToRight = new JButton(">"); this._btnToLeft = new JButton("<"); this._btnAllToLeft = new JButton("<<"); this._tblTable = new JTable(); this._lblTable = this.setupLabel(new JLabel("Table:"), this._tblTable); this._tblTable.setSelectionMode(1); this._scpTable = JTable.createScrollPaneForTable(this._tblTable); this._scpTable.setBackground(Color.white); this._tblTable.getTableHeader().setReorderingAllowed(false); this._tblTable.getTableHeader().setResizingAllowed(false); this._btnMoveUp = new JButton("Up"); this._btnMoveDown = new JButton("Down"); this._cboTable = new JComboBox(); } protected void initLayout() { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); ((Container)this).setLayout(gbl); this.addComponent(this._cboTable, gbl, gbc, 0, 0, 1, 1, (double)0.0F, (double)0.0F, 18, 2, 0, 0, 0, 0); this.addComponent(this._lblList, gbl, gbc, 0, 1, 1, 1, (double)0.0F, (double)0.0F, 17, 0, 0, 0, 0, 0); this.addComponent(this._lblTable, gbl, gbc, 2, 1, 1, 1, (double)0.0F, (double)0.0F, 18, 0, 0, 0, 0, 0); this.addComponent(this._scpList, gbl, gbc, 0, 2, 1, 5, (double)0.0F, (double)0.0F, 17, 1, 0, 0, 0, 0); this.addComponent(this._btnToRight, gbl, gbc, 1, 2, 1, 1, (double)0.0F, (double)1.0F, 15, 2, 0, 3, 0, 3); this.addComponent(this._scpTable, gbl, gbc, 2, 2, 2, 4, (double)1.0F, (double)1.0F, 17, 1, 0, 0, 0, 0); this.addComponent(this._btnAllToRight, gbl, gbc, 1, 3, 1, 1, (double)0.0F, (double)0.0F, 10, 2, 5, 3, 0, 3); this.addComponent(this._btnAllToLeft, gbl, gbc, 1, 4, 1, 1, (double)0.0F, (double)0.0F, 10, 2, 5, 3, 0, 3); this.addComponent(this._btnToLeft, gbl, gbc, 1, 5, 1, 1, (double)0.0F, (double)1.0F, 11, 2, 5, 3, 0, 3); this.addComponent(this._btnMoveUp, gbl, gbc, 2, 6, 1, 1, (double)1.0F, (double)0.0F, 13, 0, 0, 0, 0, 0); this.addComponent(this._btnMoveDown, gbl, gbc, 3, 6, 1, 1, (double)0.0F, (double)0.0F, 13, 0, 0, 0, 0, 0); } private void initListeners() { this._xferItemListener = null; ActionListener listener = new L2TPActionListener(this); this._btnMoveUp.addActionListener(listener); this._btnMoveDown.addActionListener(listener); this._btnAllToLeft.addActionListener(listener); this._btnToLeft.addActionListener(listener); this._btnToRight.addActionListener(listener); this._btnAllToRight.addActionListener(listener); ((JComponent)this).addAncestorListener(this); } private void moveTableSelection(boolean up) { CellEditor cellEditor = this.getTable().getCellEditor(); if (cellEditor != null) { cellEditor.cancelCellEditing(); } DefaultTableModel tblModel = (DefaultTableModel)this._tblTable.getModel(); ListSelectionModel selModel = this._tblTable.getSelectionModel(); int startIndex = selModel.getMinSelectionIndex(); int endIndex = selModel.getMaxSelectionIndex(); if (startIndex != -1 && endIndex != -1) { int newStartIndex = -1; if (up && startIndex > 0) { tblModel.moveRow(startIndex, endIndex, startIndex - 1); selModel.setSelectionInterval(startIndex - 1, endIndex - 1); newStartIndex = startIndex - 1; } else if (!up && endIndex < tblModel.getRowCount() - 1) { for(int i = endIndex; i >= startIndex; --i) { tblModel.moveRow(i, i, i + 1); } selModel.setSelectionInterval(startIndex + 1, endIndex + 1); newStartIndex = startIndex + 1; } if (newStartIndex != -1) { Rectangle rectStartIndex = this._tblTable.getCellRect(newStartIndex, 0, true); this._tblTable.scrollRectToVisible(rectStartIndex); } } } public void addXferItemListener(XferItemListener l) throws TooManyListenersException { if (this._xferItemListener != null) { throw new TooManyListenersException("ListToTablePanel can only have one listener"); } else { this._xferItemListener = l; } } public void removeXferItemListener(XferItemListener l) { this._xferItemListener = null; } protected void xferToRight(XferItemEvent e) { DefaultListModel lm = (DefaultListModel)this.getList().getModel(); DefaultTableModel tm = (DefaultTableModel)this.getTable().getModel(); int[] selIndices = null; JList lstSrc = this.getList(); if (e.getDirection() == 600) { int numitems = lstSrc.getModel().getSize(); selIndices = new int[numitems]; for(int i = 0; i < numitems; selIndices[i] = i++) { } } else { selIndices = lstSrc.getSelectedIndices(); } int selectedCount = Array.getLength(selIndices); Object item = null; Vector aRow = null; int tableSelMax = this.getTable().getSelectionModel().getMaxSelectionIndex(); int tableRowCount = tm.getRowCount(); int initTableRowCount = tableRowCount; for(int i = 0; i < selectedCount; ++i) { item = lm.getElementAt(selIndices[i]); aRow = new Vector(); this._xferItemListener.fillRow(item, aRow); if (aRow.size() > 0) { if (tableSelMax >= 0 && tableSelMax < tableRowCount && initTableRowCount > 1) { tm.insertRow(tableSelMax, aRow); } else { tm.addRow(aRow); } ++tableSelMax; ++tableRowCount; } } ((Component)this).repaint(); } protected void xferToLeft(XferItemEvent e) { CellEditor cellEditor = this.getTable().getCellEditor(); if (cellEditor != null && cellEditor instanceof DefaultCellEditor) { DefaultCellEditor dce = (DefaultCellEditor)cellEditor; dce.cancelCellEditing(); } DefaultTableModel tm = (DefaultTableModel)this.getTable().getModel(); Object item = null; Vector aRow = null; int rowCount = tm.getRowCount(); int tableSelMin; int tableSelMax; if (e.getDirection() == 400) { tableSelMin = 0; tableSelMax = rowCount - 1; } else { tableSelMin = this.getTable().getSelectionModel().getMinSelectionIndex(); tableSelMax = this.getTable().getSelectionModel().getMaxSelectionIndex(); } tableSelMax = tableSelMax >= 0 && tableSelMax < rowCount ? tableSelMax : -1; tableSelMin = tableSelMin >= 0 && tableSelMin < rowCount ? tableSelMin : 0; int deleteCount = tableSelMax - tableSelMin + 1; for(int i = 0; i < deleteCount && tableSelMin != -1; ++i) { tm.removeRow(tableSelMin); } ((Component)this).repaint(); } protected synchronized void processXferItemEvent(XferItemEvent e) { if (this._xferItemListener != null) { if (e.getDirection() != 400 && e.getDirection() != 500) { if (e.getDirection() == 600 || e.getDirection() == 700) { this.xferToRight(e); } } else { this.xferToLeft(e); } } } public JLabel getListLabel() { return this._lblList; } public JLabel getTableLabel() { return this._lblTable; } public JList getList() { return this._lboList; } public void setListModel(ListModel listModel) { if (listModel != null) { this._lboList.setModel(listModel); this._lboList.invalidate(); Container rootContainer = this._lboList.getTopLevelAncestor(); if (rootContainer != null) { rootContainer.validate(); } if (listModel.getSize() > 0) { this._lboList.setSelectedIndex(0); } } } public JTable getTable() { return this._tblTable; } public JButton getToRightButton() { return this._btnToRight; } public JButton getAllToRightButton() { return this._btnAllToRight; } public JButton getToLeftButton() { return this._btnToLeft; } public JButton getAllToLeftButton() { return this._btnAllToLeft; } public JButton getMoveUpButton() { return this._btnMoveUp; } public JButton getMoveDownButton() { return this._btnMoveDown; } public JComboBox getTableComboBox() { return this._cboTable; } public void ancestorAdded(AncestorEvent evt) { } public void ancestorMoved(AncestorEvent evt) { } public void ancestorRemoved(AncestorEvent evt) { CellEditor cellEditor = this._tblTable.getCellEditor(); if (cellEditor != null) { cellEditor.stopCellEditing(); } } public void commitChanges() { CellEditor cellEditor = this._tblTable.getCellEditor(); if (cellEditor != null) { cellEditor.stopCellEditing(); } } // $FF: synthetic method static void access$0(ListToTablePanel $0, boolean $1) { $0.moveTableSelection($1); } // $FF: synthetic method static JButton access$1(ListToTablePanel $0) { return $0._btnAllToLeft; } // $FF: synthetic method static JButton access$2(ListToTablePanel $0) { return $0._btnToLeft; } // $FF: synthetic method static JButton access$3(ListToTablePanel $0) { return $0._btnAllToRight; } // $FF: synthetic method static JButton access$4(ListToTablePanel $0) { return $0._btnToRight; } }