forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
251 lines
6.3 KiB
251 lines
6.3 KiB
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<KeyClass extends Object> 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<String> submitButton = new LeafInputField<String>();
|
|
|
|
private Map<KeyClass, ? extends Object> map = null;
|
|
|
|
private OrderedMap<KeyClass> orderedMap = null;
|
|
|
|
private List<KeyClass> selected = new ArrayList<KeyClass>();
|
|
|
|
private Map<KeyClass, Boolean> mapEnabledForKey = new HashMap<KeyClass, Boolean>();
|
|
|
|
private Map<KeyClass, Boolean> mapChosenForKey = new HashMap<KeyClass, Boolean>();
|
|
|
|
private String message = null;
|
|
|
|
private boolean ordered = false;
|
|
|
|
public LeafOptionDialog(Map<KeyClass, ? extends Object> map)
|
|
{
|
|
super();
|
|
startup( map, null );
|
|
}
|
|
|
|
public LeafOptionDialog(Map<KeyClass, ? extends Object> map, String message)
|
|
{
|
|
super();
|
|
this.message = message;
|
|
startup( map, null );
|
|
}
|
|
|
|
public LeafOptionDialog(OrderedMap<KeyClass> orderedMap, Map<KeyClass, Boolean> chosen, Map<KeyClass, Boolean> 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<KeyClass, ? extends Object> map, OrderedMap<KeyClass> orderedMap )
|
|
{
|
|
if( map == null )
|
|
{
|
|
this.map = new HashMap<KeyClass, Object>();
|
|
}
|
|
else
|
|
{
|
|
this.map = map;
|
|
}
|
|
if( orderedMap == null )
|
|
{
|
|
this.orderedMap = new OrderedMap<KeyClass>();
|
|
}
|
|
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<KeyClass> 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<Object> component = new LeafInputField<Object>();
|
|
Object value;
|
|
if( ordered )
|
|
{
|
|
List<Object> 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<Object> source = (LeafInputField<Object>) 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<KeyClass> getSelected()
|
|
{
|
|
return selected;
|
|
}
|
|
|
|
public void close()
|
|
{
|
|
setVisible( false );
|
|
dispose();
|
|
}
|
|
|
|
}
|