/* * CollectionSpanGridDemo.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package example09; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.eliad.swing.*; import com.eliad.util.*; import com.eliad.model.*; /** * This class demonstrates the use of {@link com.eliad.model.GenericCollectionSpanModel}. * @author Daniel Rosenblatt * @version 1.0 */ public class CollectionSpanGridDemo extends JFrame { private JSmartGrid grid_; private GenericCollectionSpanModel spanModel_ = new GenericCollectionSpanModel(); private void doSpan(boolean on) { GridSelectionModel gsm = grid_.getSelectionModel(); if (gsm.isSelectionEmpty()) return; int firstRow=gsm.getFirstSelectedRow(); int rowCount=gsm.getLastSelectedRow() - firstRow + 1; int firstColumn=gsm.getFirstSelectedColumn(); int columnCount=gsm.getLastSelectedColumn() - firstColumn + 1; Iterator spans=grid_.spanIterator(firstRow, firstColumn, rowCount, columnCount, false); while (spans.hasNext()) { ExtentCell span=(ExtentCell)spans.next(); spanModel_.removeSpan(span.getRow(),span.getColumn()); } if (on) spanModel_.addSpan(firstRow,firstColumn, rowCount, columnCount); } private void addMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.addActionListener( new ActionListener () { public void actionPerformed (ActionEvent evt) { System.exit(0); } } ); fileMenu.add(exitMenuItem); JMenu toolsMenu = new JMenu("Tools"); menuBar.add(toolsMenu); JMenuItem spanOnMenuItem = new JMenuItem("Span On"); spanOnMenuItem.addActionListener( new ActionListener () { public void actionPerformed (ActionEvent evt) { doSpan(true); } } ); JMenuItem spanOffMenuItem = new JMenuItem("Span Off"); spanOffMenuItem.addActionListener( new ActionListener () { public void actionPerformed (ActionEvent evt) { doSpan(false); } } ); toolsMenu.add(spanOnMenuItem); toolsMenu.add(spanOffMenuItem); setJMenuBar(menuBar); } public CollectionSpanGridDemo() { super("Collection Span Example"); addWindowListener ( new WindowAdapter () { public void windowClosing (WindowEvent evt) { System.exit(0); } } ); grid_ = new JSmartGrid(); grid_.setSpanModel(spanModel_); for (int col = 0; col < grid_.getColumnCount(); col++) { grid_.setColumnWidth(col, 50); for (int row = 0; row < grid_.getRowCount(); row++) grid_.setValueAt("" + row + col, row, col); } grid_.setSelectionMode(GridSelectionModel.SINGLE_RECTANGLE_SELECTION); grid_.setAutoCreateColumnHeader(true); grid_.setAutoCreateRowHeader(true); grid_.setColumnAutoResizeMode(JSmartGrid.AUTO_RESIZE_OFF); grid_.getModel().addGridModelListener(spanModel_); JScrollPane js = new JScrollPane(grid_); getContentPane().add(js, BorderLayout.CENTER); addMenuBar(); pack(); } public static void main(String[] args) { CollectionSpanGridDemo sg = new CollectionSpanGridDemo(); Dimension big=Toolkit.getDefaultToolkit().getScreenSize(); Dimension small=sg.getSize(); sg.setLocation((big.width-small.width)/2, (big.height-small.height)/2); sg.setVisible(true); } }