/* * Importer.java * * Created on 7 de Abril de 2004, 10:56 */ package siprp.importer; import com.evolute.utils.arrays.*; import com.evolute.utils.tables.*; import com.evolute.utils.ui.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import jxl.*; /** * * @author psantos */ public class Importer extends CustomJDialog { public static final String NOME = "Nome do Funcion\u00e1rio"; public static final String SEXO = "Sexo"; public static final String NACIONALIDADE = "Nacionalidade"; public static final String DATA_NASCIMENTO = "Data Nascimento"; public static final String NUMERO_MECANOGRAFICO = "N\u00famero Mecanogr\u00e1fico"; public static final String DATA_ADMISSAO = "Data Admiss\u00e3o"; public static final String CATEGORIA = "Categoria"; public static final String LOCAL_TRABALHO = "Local Trabalho"; public static final String FUNCAO = "Fun\u00e7\u00e3o"; public static final String DATA_ADMISSAO_FUNCAO = "Data Admiss\u00e3o na Fun\u00e7\u00e3o"; private JPanel panel; private BaseTable table; private Excel2DArray e2da; private boolean canceled = false; private DataChooserWindow dcw; private String vals[][]; private final int[] DEFAULT_SELECTION = new int[]{ 0, 2, 3, 1, -1, 4, 5, 7, 6, -1 }; public static final String NAMES[] = new String[]{ NOME, SEXO, NACIONALIDADE, DATA_NASCIMENTO, NUMERO_MECANOGRAFICO, DATA_ADMISSAO, CATEGORIA, LOCAL_TRABALHO, FUNCAO, DATA_ADMISSAO_FUNCAO }; private final boolean multipleSelection; /** Creates a new instance of Importer */ public Importer( JFrame modalFrame, String filename ) throws Exception { this( modalFrame, filename, false ); } public Importer( JFrame modalFrame, String filename, boolean multiple ) throws Exception { super( modalFrame, true ); multipleSelection = multiple; setupComponents( filename ); if( modalFrame == null ) { center(); } else { centerSuper(); } show(); } private void setupComponents( String filename ) { setSize( 600, 500 ); GridBagLayout gblGridBag = new GridBagLayout(); GridBagConstraints gbcConstraints = new GridBagConstraints(); panel = ( JPanel ) getContentPane(); panel.setLayout( gblGridBag ); e2da = new Excel2DArray( filename ); String colNames[] = new String[ e2da.rowLength() ]; for( int i = 0; i < colNames.length; i++ ) { colNames[ i ] = " "; } Virtual2DTableModel tm = new Virtual2DTableModel( colNames, null ); tm.setValues( e2da ); table = new BaseTable( tm ); if( !multipleSelection ) { table.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); } else { table.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION ); } table.getTableHeader().setResizingAllowed( true ); table.getTableHeader().setReorderingAllowed( false ); // table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF ); for( int i = 0; i < e2da.rowLength(); i++ ) { table.getColumn( i ).setMinWidth( 50 ); //table.getColumn( i ).setResizable( true ); } JScrollPane sp = new JScrollPane( table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); gbcConstraints.anchor = GridBagConstraints.CENTER; gbcConstraints.fill = GridBagConstraints.BOTH; gbcConstraints.insets = new Insets(5, 5, 5, 5); gbcConstraints.gridwidth = 3; gbcConstraints.gridheight = 3; gbcConstraints.weighty = 3; gbcConstraints.gridx = 0; gbcConstraints.gridy = 3; gblGridBag.setConstraints( sp, gbcConstraints ); panel.add( sp ); JPanel jpButtons = new JPanel(); final JButton jbOK = new JButton( "Escolher" ); jbOK.setEnabled( false ); jpButtons.add( jbOK ); JButton jbCancel = new JButton( "Cancelar" ); jpButtons.add( jbCancel ); gbcConstraints.gridheight = 1; gbcConstraints.weightx = 1; gbcConstraints.weighty = 0; gbcConstraints.anchor = GridBagConstraints.SOUTH; gbcConstraints.fill = GridBagConstraints.BOTH; gbcConstraints.insets = new Insets( 2, 2, 2, 2 ); gbcConstraints.gridwidth = 3; gbcConstraints.gridx = 0; gbcConstraints.gridy = 6; gblGridBag.setConstraints( jpButtons, gbcConstraints ); panel.add( jpButtons ); table.addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent e ) { jbOK.setEnabled( true ); if( e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && table.columnAtPoint( e.getPoint() ) != -1 && table.rowAtPoint( e.getPoint() ) != -1 ) { acceptResult(); } } public void mousePressed( MouseEvent e ) { jbOK.setEnabled( true ); } }); jbOK.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent a ) { acceptResult(); } }); jbCancel.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent a ) { setVisible( false ); dispose(); canceled = true; } }); } private void acceptResult() { if( multipleSelection ) { int rows[] = table.getSelectedRows(); vals = new String[ rows.length ][]; for( int i = 0; i < rows.length; ++i ) { Vector v = new Vector( Arrays.asList( e2da.getRow( rows[ i ] ) ) ); v.add( "" ); vals[ i ] = (String[]) v.toArray( new String[ v.size() ] ); } } else { Vector v = new Vector( Arrays.asList( e2da.getRow( table.getSelectedRow() ) ) ); v.add( "" ); vals = new String[][] { ( String[] )v.toArray( new String[ v.size() ] ) }; } /* System.out.println( "row: " + table.getSelectedRow() ); System.out.print( "vals: " ); for( int i = 0; i < vals.length; i++ ) { System.out.print( "[" + vals[ i ] + "]" ); } System.out.println(); */ setVisible( false ); dispose(); } public Hashtable getData() { if( NAMES == null || vals == null ) { return null; } dcw = new DataChooserWindow( null, NAMES, vals, DEFAULT_SELECTION ); if( canceled || ( dcw == null ) ) { return null; } return dcw.getData( multipleSelection ); } public static void main( String args[] ) { // FileDialog fd = new FileDialog( null, "Escolha um ficheiro Excel:", FileDialog.LOAD ); // fd.setDirectory( System.getProperty( "user.home" ) ); // fd.setFilenameFilter( new FilenameFilter() { // public boolean accept( File dir, String name ) // { // return (name!=null) && (name.indexOf( ".xls" ) != -1); // } // } ); // fd.show(); String filename = "c:\\teste_com_dados.xls"; //String filename = fd.getFile(); if( filename != null ) { //filename = fd.getDirectory() + File.separator + filename; try { Importer importer = new Importer( null, filename, true ); Hashtable hash = importer.getData(); if( hash != null ) { System.out.println( "Data: " + hash ); } } catch( Exception ex ) { ex.printStackTrace(); JOptionPane.showMessageDialog( null, "Erro a importar", "Erro...", JOptionPane.ERROR_MESSAGE ); } } // Importer im = null; // try // { // im = new Importer( null, // "E:\\test.xls" // "C:\\Documents and Settings\\psantos\\My Documents\\Devel\\teste.xls" // "C:\\Documents and Settings\\psantos\\My Documents\\Devel\\Docs\\jexcelapi\\jxlrwtest.xls" // ); // //System.out.println( "Escolhido: " + im.getData() ); // // } // catch( Exception e ) // { // e.printStackTrace(); // } // String[][] all = (String[][]) e.getObjects(); // // for( int j = 0; j < e.rowLength(); j++ ) // { // String[] r = e.getRow( j ); // for( int i = 0; i < r.length; i++ ) // { // System.out.print( "[" + r[ i ] + "]" ); // } // System.out.println(); // } } }