/** * ClipboardGridDemo.java * * Copyright 2000 Eliad Technologies, Inc. All Rights Reserved. * * This software is the proprietary information of Eliad Technologies, Inc. */ package example12; import com.eliad.swing.*; import com.eliad.model.*; import com.eliad.model.defaults.*; import java.util.*; import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; import javax.swing.*; /** * This demo shows how to exchange data with other applications through * the clipboard. * * @author Patrick Mérissert-Coffinières * @version 1.0 */ class ClipboardGridDemo extends JFrame implements ClipboardOwner { private JSmartGrid grid_; private GenericCollectionSpanModel spanModel_ = new GenericCollectionSpanModel(); private void doSpan(boolean on) { GridSelectionModel gsm = grid_.getSelectionModel(); if (gsm.isSelectionEmpty()) return; int lastRow=gsm.getLastSelectedRow(); int firstRow=gsm.getFirstSelectedRow(); int lastColumn=gsm.getLastSelectedColumn(); int firstColumn=gsm.getFirstSelectedColumn(); Iterator spans=grid_.spanIterator(firstRow, firstColumn, lastRow-firstRow+1, lastColumn-firstColumn+1, false); while (spans.hasNext()) { ExtentCell span=(ExtentCell)spans.next(); spanModel_.removeSpan(span.getRow(),span.getColumn()); } if (on) spanModel_.addSpan(firstRow,firstColumn, lastRow-firstRow+1, lastColumn-firstColumn+1); } private Clipboard clipboard_=Toolkit.getDefaultToolkit().getSystemClipboard(); private int lastRow_; private int firstRow_; private int lastColumn_; private int firstColumn_; private void paste(Transferable data) { try { Reader reader=DataFlavor.stringFlavor.getReaderForText(data); BufferedReader buffer=new BufferedReader(reader); String line=null; int row=firstRow_; // If the clipboard has too few lines or columns, erase cells boolean recordsOver=false; while (row <= lastRow_) { if (!recordsOver) { line=buffer.readLine(); if (line==null) { recordsOver=true; line=""; } } StringTokenizer toker=new StringTokenizer(line,"\t",true); boolean fieldsOver=false; for (int column=firstColumn_;column<=lastColumn_;column++) { String tok; if (toker.hasMoreElements()) tok=toker.nextToken(); else { tok=""; fieldsOver=true; } if ("\t".equals(tok)) tok=""; else if (!fieldsOver) { if (toker.hasMoreElements()) toker.nextToken(); // should be "\t"! else fieldsOver=true; } ExtentCell span=grid_.getSpanOver(row,column); if (span==null || span.getRow()==row && span.getColumn()==column) grid_.setValueAt(tok,row,column); } if (++row>lastRow_) break; } } catch(Exception ex) { ex.printStackTrace(); } } private Transferable copy() { String fmt=""; for (int row=firstRow_;row<=lastRow_;row++) { if (row>firstRow_) fmt+="\n"; for (int column=firstColumn_;column<=lastColumn_;column++) { if (column>firstColumn_) fmt+="\t"; ExtentCell span=grid_.getSpanOver(row,column); if (span!=null && (span.getRow()!=row || span.getColumn()!=column)) continue; fmt+=grid_.getValueAt(row,column); } } return new StringSelection(fmt); } 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); ((JComponent)getContentPane()).setPreferredSize(new Dimension(500, 400)); } public ClipboardGridDemo() { super("Copy/Paste Span Example"); ((JComponent) getContentPane()).setPreferredSize(new Dimension(500, 400)); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); grid_ = new JSmartGrid(20,20); 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 scrl=new JScrollPane(); scrl.setViewportView(grid_); getContentPane().add(scrl); //Create the popup menu. final JPopupMenu popup = new JPopupMenu(); final JMenuItem paste = new JMenuItem("Paste"); paste.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Transferable data=clipboard_.getContents(ClipboardGridDemo.this); paste(data); } } ); popup.add(paste); final JMenuItem copy = new JMenuItem("Copy"); copy.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { Transferable data=copy(); clipboard_.setContents(data,ClipboardGridDemo.this); } } ); popup.add(copy); grid_.addGridListener( new GridAdapter() { public void gridMouseReleased(GridEvent e) { if (((MouseEvent)e.getSourceEvent()).isPopupTrigger()) { JSmartGrid grid=(JSmartGrid)e.getSource(); final int column = e.getColumn(); final int row = e.getRow(); if (column>=0 && row>=0) { final int x=((MouseEvent)e.getSourceEvent()).getX(); final int y=((MouseEvent)e.getSourceEvent()).getY(); GridSelectionModel sm=grid.getSelectionModel(); lastRow_=sm.getLastSelectedRow(); firstRow_=sm.getFirstSelectedRow(); lastColumn_=sm.getLastSelectedColumn(); firstColumn_=sm.getFirstSelectedColumn(); if (grid_.isSelectionEmpty() || rowlastRow_ || columnlastColumn_) { firstRow_=lastRow_=row; firstColumn_=lastColumn_=column; } popup.show(grid, x, y); paste.setEnabled(clipboard_.getContents(this)!=null); } } } } ); addMenuBar(); pack(); } public void lostOwnership(Clipboard clipboard, Transferable contents) { } public static void main(final String args[]) { ClipboardGridDemo frame = new ClipboardGridDemo(); Dimension big=Toolkit.getDefaultToolkit().getScreenSize(); Dimension small=frame.getSize(); frame.setLocation((big.width-small.width)/2, (big.height-small.height)/2); frame.setVisible(true); } }