package siprp.medicina.processo.ui; import info.clearthought.layout.TableLayout; import info.clearthought.layout.TableLayoutConstraints; import java.awt.Dimension; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JSeparator; public class LeafOptionDialog extends JDialog { private static final Dimension OPTION_SIZE = new Dimension(0,20); private static final Dimension BUTTON_SIZE = new Dimension(0,20); private static final long serialVersionUID = 1L; private static final String CANCEL_LABEL = "Cancelar"; private static String okLabel = "Ok"; private final JLabel labelMessage = new JLabel(); private boolean cancelActive = true; private LeafInputField submitButton = new LeafInputField(); private Map map = null; private OrderedMap orderedMap = null; private List selected = new ArrayList(); private Map mapEnabledForKey = new HashMap(); private Map mapChosenForKey = new HashMap(); private String message = null; private boolean ordered = false; public LeafOptionDialog(Map map) { super(); startup( map, null ); } public LeafOptionDialog(Map map, String message) { super(); this.message = message; startup( map, null ); } public LeafOptionDialog(OrderedMap orderedMap, Map chosen, Map enabled, String message, String okButton) { super(); ordered = true; this.message = message; this.okLabel = okButton; this.mapChosenForKey = chosen; this.mapEnabledForKey = enabled; if( chosen != null ) { for( KeyClass key : chosen.keySet() ) { Boolean isChosen = chosen.get( key ); if( isChosen != null && isChosen ) { selected.add( key ); } } } startup( null, orderedMap ); } private void startup( Map map, OrderedMap orderedMap ) { if( map == null ) { this.map = new HashMap(); } else { this.map = map; } if( orderedMap == null ) { this.orderedMap = new OrderedMap(); } else { this.orderedMap = orderedMap; } setupComponents( map == null ? orderedMap.iterator() : map.keySet().iterator(), map == null ? orderedMap.rows() : map.keySet().size(), map == null ? true : false ); setModal( true ); setUndecorated( true ); setVisible( true ); } private void setupComponents( Iterator iterator, Integer size, boolean ordered ) { KeyClass current = null; double[] cols = new double[] { TableLayout.FILL }; double[] rows = new double[(message == null ? 0 : 2) + size + (cancelActive ? 2 : 0)]; for( int i = 0; i < rows.length; ++i ) { rows[i] = TableLayout.PREFERRED; } TableLayout layout = new TableLayout( cols, rows ); layout.setVGap( 3 ); setContentPane( new LeafGradientPanel() ); getContentPane().setLayout( layout ); int shift = 0; if( message != null ) { labelMessage.setText( message ); getContentPane().add( labelMessage, new TableLayoutConstraints( 0, shift++ ) ); getContentPane().add( new JSeparator(), new TableLayoutConstraints( 0, shift++ ) ); } for( int i = 0; i < size && iterator.hasNext(); ++i ) { current = iterator.next(); LeafInputField component = new LeafInputField(); Object value; if( ordered ) { List values = orderedMap.getValues( current ); value = (values == null || values.size() == 0) ? null : values.get( 0 ); } else { value = map.get( current ); } component.setObject( value ); Boolean isChosen = mapChosenForKey.get( current ); component.setSelected( isChosen != null && isChosen ); Boolean isEnabled = mapEnabledForKey.get( current ); component.setClickable( isEnabled != null && isEnabled ); component.setPreferredSize( OPTION_SIZE ); getContentPane().add( component, new TableLayoutConstraints( 0, i + shift ) ); addListenerToComponent( component ); } if( cancelActive ) { getContentPane().add( new JSeparator(), new TableLayoutConstraints( 0, size + shift++ ) ); submitButton.setObject( ordered ? okLabel : CANCEL_LABEL ); submitButton.setClickable( true ); submitButton.setPreferredSize( BUTTON_SIZE ); getContentPane().add( submitButton, new TableLayoutConstraints( 0, size + shift ) ); addListenerToComponent( submitButton ); } ((JComponent) getContentPane()).setBorder( BorderFactory.createRaisedBevelBorder() ); setSize( layout.preferredLayoutSize( this.getContentPane() ) ); setLocationRelativeTo( getParent() ); } private KeyClass getKeyForValue( Object value ) { if( value != null && map.containsValue( value ) ) { for( KeyClass key : map.keySet() ) { if( map.get( key ).equals( value ) ) { return key; } } } else if( ordered ) { return orderedMap.getKeyForValue( value ); } return null; } private void addListenerToComponent( JComponent component ) { component.addPropertyChangeListener( new PropertyChangeListener() { @Override public void propertyChange( PropertyChangeEvent e ) { if( e.getSource() instanceof LeafInputField ) { LeafInputField source = (LeafInputField) e.getSource(); if( LeafInputField.PROPERTY_CHANGED_CLICK.equals( e.getPropertyName() ) ) { Object value = source.getObject(); if( value != null ) { KeyClass key = getKeyForValue( value ); if( selected.contains( key ) ) { selected.remove( key ); source.setSelected( false ); } else { selected.add( key ); source.setSelected( true ); } } if( !ordered || source.equals( submitButton ) ) { close(); } } } } } ); } public KeyClass getOption() { return selected.isEmpty() ? null : selected.get( 0 ); } public List getSelected() { return selected; } public void close() { setVisible( false ); dispose(); } }