/** * MainFrame.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package MultiDimTable; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import com.eliad.model.*; import com.eliad.model.defaults.*; import com.eliad.swing.*; import com.eliad.swing.extend.*; public class MainFrame extends JFrame { private JPanel contentPane; JMenuBar menuBar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem menuFileExit = new JMenuItem("Exit"); JMenu menuHelp = new JMenu("Help"); JMenuItem menuHelpAbout = new JMenuItem("About"); private JSmartGrid grid_; private MyMultiDimColHeaderModel headerModel_; private DefaultMutableTreeNode root_; //Construct the frame public MainFrame() { super("MultiDimTable Example"); enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { componentInit(); // GUI initialization } catch(Exception e) { e.printStackTrace(); } } private void componentInit() throws Exception { contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(new BorderLayout()); // We build the tree of column headers root_ = makeMultiHeaderTree(); // We initialize the JDBC Adapter for the GridModel MyJdbcGridModel model = new MyJdbcGridModel("jdbc:odbc:EliadExample", "sun.jdbc.odbc.JdbcOdbcDriver"); model.setQuery("SELECT * FROM Invoices"); // HeaderGridModel iniialized with the tree of column headers headerModel_ = new MyMultiDimColHeaderModel(root_); grid_ = new JSmartGrid(model); JScrollPane gridScrollPane = new JScrollPane(grid_); // Some properties configuration grid_.setSelectionPolicy(JSmartGrid.POLICY_SINGLE); grid_.setSelectionUnit(JSmartGrid.UNIT_ROW); grid_.setSelectionBackgroundColor(new Color(255, 204, 153)); grid_.setShowHorizontalLines(false); // Setting up a customized StyleModel / CellRenderer DefaultStyleModel headerStyleModel_ = new DefaultStyleModel(); headerStyleModel_.setRenderer(String.class, new MyColHeaderCellRenderer()); JAdvancedGridHeader smartGridHeader = new JAdvancedGridHeader(grid_, JSmartGrid.HORIZONTAL, headerModel_.getColumnHeaderModel(), headerModel_.getColumnHeaderSpanModel(), headerStyleModel_); smartGridHeader.setAlphaComposite(0.50f); smartGridHeader.setSelectionPolicy(JSmartGrid.POLICY_SINGLE); smartGridHeader.setSelectionUnit(JSmartGrid.UNIT_CELL); // Setting the hierarchical header to the grid grid_.setColumnHeader(smartGridHeader); contentPane.add(gridScrollPane, BorderLayout.CENTER); menuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fileExit_actionPerformed(e); } }); menuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { helpAbout_actionPerformed(e); } }); menuFile.add(menuFileExit); menuHelp.add(menuHelpAbout); menuBar.add(menuFile); menuBar.add(menuHelp); this.setJMenuBar(menuBar); } public void attachController() { MyJdbcGridModel model = (MyJdbcGridModel)grid_.getModel(); RulerModel rm = grid_.getColumnModel(); rm.addRulerModelListener(model); rm.addRulerModelListener(headerModel_); JSmartGrid header = (JSmartGrid)grid_.getColumnHeader(); header.setDraggable(JSmartGrid.HORIZONTAL, true); header.setDraggable(JSmartGrid.VERTICAL, true); DirectColDragController dcdc; header.addGridListener(dcdc = new DirectColDragController(root_,grid_, headerModel_.getColumnHeaderSpanModel())); model.directColumnDragController(dcdc); headerModel_.directColumnDragController(dcdc); } private DefaultMutableTreeNode makeMultiHeaderTree() { DefaultMutableTreeNode invoicesNode = new DefaultMutableTreeNode("INVOICES INFORMATION"); DefaultMutableTreeNode customerNode = new DefaultMutableTreeNode("CUSTOMER"); DefaultMutableTreeNode orderNode = new DefaultMutableTreeNode("ORDER"); DefaultMutableTreeNode productNode = new DefaultMutableTreeNode("PRODUCT"); DefaultMutableTreeNode priceNode = new DefaultMutableTreeNode("Price"); invoicesNode.add(customerNode); invoicesNode.add(orderNode); invoicesNode.add(productNode); invoicesNode.add(priceNode); DefaultMutableTreeNode customerIDNode = new DefaultMutableTreeNode("ID"); DefaultMutableTreeNode customerLastNameNode = new DefaultMutableTreeNode("Last Name"); DefaultMutableTreeNode customerFirstNameNode = new DefaultMutableTreeNode("First Name"); customerNode.add(customerIDNode); customerNode.add(customerLastNameNode); customerNode.add(customerFirstNameNode); DefaultMutableTreeNode orderIDNode = new DefaultMutableTreeNode("ID"); DefaultMutableTreeNode orderDateNode = new DefaultMutableTreeNode("Date"); DefaultMutableTreeNode orderShipNode = new DefaultMutableTreeNode("SHIP"); DefaultMutableTreeNode orderQuantityNode = new DefaultMutableTreeNode("Quantity"); orderNode.add(orderIDNode); orderNode.add(orderDateNode); orderNode.add(orderShipNode); orderNode.add(orderQuantityNode); DefaultMutableTreeNode orderShipDateNode = new DefaultMutableTreeNode("Date"); DefaultMutableTreeNode orderShipViaNode = new DefaultMutableTreeNode("Via"); orderShipNode.add(orderShipDateNode); orderShipNode.add(orderShipViaNode); DefaultMutableTreeNode productIDNode = new DefaultMutableTreeNode("ID"); DefaultMutableTreeNode productNameNode = new DefaultMutableTreeNode("Name"); DefaultMutableTreeNode productCategoryNode = new DefaultMutableTreeNode("Category"); DefaultMutableTreeNode unitPriceNode = new DefaultMutableTreeNode("Unit Price"); productNode.add(productIDNode); productNode.add(productNameNode); productNode.add(productCategoryNode); productNode.add(unitPriceNode); return invoicesNode; } // makeMultiHeaderTree() //File | Exit action performed public void fileExit_actionPerformed(ActionEvent e) { System.exit(0); } //Help | About action performed public void helpAbout_actionPerformed(ActionEvent e) { AboutBox dlg = new AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { fileExit_actionPerformed(null); } } }