From ddd7e4664c8f311881d6a8f765fe8b15a89826c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Flores?= Date: Thu, 27 May 2004 12:10:17 +0000 Subject: [PATCH] no message git-svn-id: https://svn.coded.pt/svn/SIPRP@102 bb69d46d-e84e-40c8-a05a-06db0d633741 --- trunk/siprp/importer/DataChooserWindow.java | 19 +++- trunk/siprp/importer/Importer.java | 103 ++++++++++++++++---- 2 files changed, 96 insertions(+), 26 deletions(-) diff --git a/trunk/siprp/importer/DataChooserWindow.java b/trunk/siprp/importer/DataChooserWindow.java index 9619074e..d49b3936 100644 --- a/trunk/siprp/importer/DataChooserWindow.java +++ b/trunk/siprp/importer/DataChooserWindow.java @@ -20,22 +20,24 @@ public class DataChooserWindow extends CustomJDialog { private final String labels[]; private final String items[]; + private final String allItems[][]; private final JComboBox combos[]; private final int preselected[]; private boolean canceled = false; /** Creates a new instance of DataChooserWindow */ - public DataChooserWindow( JFrame modalFrame, String names[], String data[] ) + public DataChooserWindow( JFrame modalFrame, String names[], String data[][] ) { this( modalFrame, names, data, null ); } - public DataChooserWindow( JFrame modalFrame, String names[], String data[], int selected[] ) + public DataChooserWindow( JFrame modalFrame, String names[], String data[][], int selected[] ) { super( modalFrame, true ); labels = names; - items = data; + items = data[ 0 ]; + allItems = data; combos = new JComboBox[ labels.length ]; preselected = selected; setupComponents(); @@ -133,7 +135,7 @@ public class DataChooserWindow extends CustomJDialog }); } - public Hashtable getData() + public Hashtable getData( boolean multiple ) { if( canceled ) { @@ -142,7 +144,14 @@ public class DataChooserWindow extends CustomJDialog Hashtable hash = new Hashtable(); for( int i = 0; i < labels.length; ++i ) { - hash.put( labels[ i ], combos[ i ].getSelectedItem() ); + if( multiple ) + { + hash.put( labels[ i ], allItems[ combos[ i ].getSelectedIndex() ] ); + } + else + { + hash.put( labels[ i ], combos[ i ].getSelectedItem() ); + } } return hash; } diff --git a/trunk/siprp/importer/Importer.java b/trunk/siprp/importer/Importer.java index 8d9bfa58..f9fc7bae 100644 --- a/trunk/siprp/importer/Importer.java +++ b/trunk/siprp/importer/Importer.java @@ -41,7 +41,7 @@ public class Importer extends CustomJDialog private Excel2DArray e2da; private boolean canceled = false; private DataChooserWindow dcw; - private String vals[]; + private String vals[][]; private final int[] DEFAULT_SELECTION = new int[]{ 0, 2, 3, 1, -1, 4, 5, 7, 6, -1 }; private final String names[] = new String[]{ @@ -56,14 +56,21 @@ public class Importer extends CustomJDialog 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(); @@ -72,7 +79,6 @@ public class Importer extends CustomJDialog { centerSuper(); } - show(); } @@ -97,6 +103,14 @@ public class Importer extends CustomJDialog 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 ); @@ -174,9 +188,23 @@ public class Importer extends CustomJDialog private void acceptResult() { - Vector v = new Vector( Arrays.asList( e2da.getRow( table.getSelectedRow() ) ) ); - v.add( "" ); - vals = (String[]) v.toArray( new String[]{} ); + 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() ); @@ -194,6 +222,10 @@ public class Importer extends CustomJDialog public Hashtable getData() { + if( names == null || vals == null ) + { + return null; + } dcw = new DataChooserWindow( null, names, vals, DEFAULT_SELECTION ); if( canceled || ( dcw == null ) ) @@ -201,27 +233,56 @@ public class Importer extends CustomJDialog return null; } - return dcw.getData(); + return dcw.getData( multipleSelection ); } public static void main( String args[] ) { - Importer im = null; - try +// 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 ) { - im = new Importer( 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\\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(); - } +// ); +// +//System.out.println( "Escolhido: " + im.getData() ); +// +// } +// catch( Exception e ) +// { +// e.printStackTrace(); +// } // String[][] all = (String[][]) e.getObjects(); //