git-svn-id: https://svn.coded.pt/svn/SIPRP@766 bb69d46d-e84e-40c8-a05a-06db0d633741

0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
Tiago Simão 17 years ago
parent 2011427d99
commit 4f8a37b140

@ -1,62 +0,0 @@
package leaf.ui;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JButton;
public class LeafButton extends JButton
{
private static final long serialVersionUID = 1L;
public LeafButton(String text)
{
super( text );
setOpaque( false );
}
protected void paintComponent( Graphics g )
{
boolean pushed = getModel().isPressed();
// Color borderColor = getBackground();
// Color highlightColor = getBackground();
Color textColor = isEnabled() ? getForeground() : getForeground().brighter().brighter().brighter();
Color textAccent = getBackground();
Color textAccentHot = getBackground();
GradientPaint topGradientUp = new GradientPaint( 0, 0, getBackground().brighter(), 0, getHeight() / 2, getBackground() );
GradientPaint topGradientDown = new GradientPaint( 0, 0, getBackground().brighter(), 0, getHeight() / 2, getBackground() );
GradientPaint bottomGradientDown = new GradientPaint( 0, getHeight() / 2, getBackground(), 0, getHeight(), getBackground().darker() );
GradientPaint bottomGradientUp = new GradientPaint( 0, getHeight() / 2, getBackground(), 0, getHeight(), getBackground().darker().darker() );
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
int height = getHeight() / 2;
g2.setPaint( pushed ? topGradientDown : topGradientUp );
g2.fillRect( 0, 0, getWidth(), height );
g2.setPaint( pushed ? bottomGradientDown : bottomGradientUp );
g2.fillRect( 0, height, getWidth(), height );
// g2.setColor( borderColor );
// g2.drawRect( 0, 0, getWidth() - 1, getHeight() - 1 );
//
// g2.setColor( highlightColor );
// g2.drawRect( 1, 1, getWidth() - 3, getHeight() - 3 );
int x = (getWidth() - g2.getFontMetrics().stringWidth( getText() )) / 2;
int y = getHeight() - ((getHeight() - g2.getFontMetrics().getHeight()) / 2) - 3;
y += 1;
g2.setColor( pushed ? textAccentHot : textAccent );
g2.drawString( getText(), x, y );
y -= 1;
g2.setColor( textColor );
g2.drawString( getText(), x, y );
// super.paintComponent( g );
}
}

@ -1,149 +0,0 @@
package leaf.ui;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.border.BevelBorder;
import com.toedter.calendar.JCalendar;
public class LeafCalendarDialog extends JDialog
{
private static final long serialVersionUID = 1L;
public static final int ABOVE = 0;
public static final int BELOW = 1;
public static final int RIGHT = 0;
public static final int LEFT = 1;
private boolean ok = false;
private boolean clear = false;
private Calendar cal = null;
private boolean enableClean = true;
private boolean enableCancel = true;
private final JCalendar calendarPanel = new JCalendar( null, null, false, false );
/** Creates a new instance of JCalendarDialog */
public LeafCalendarDialog( LeafWindow parentFrame, JComponent parent)
{
super(parentFrame);
this.enableClean = true;
setModal( true );
setupComponents();
setUndecorated( true );
setLocationRelativeTo( parent );
setVisible( true );
}
public LeafCalendarDialog( LeafWindow parentFrame, JComponent parent, boolean enableClean )
{
super(parentFrame);
this.enableClean = enableClean;
setModal( true );
setupComponents();
setUndecorated( true );
setLocationRelativeTo( parent );
setVisible( true );
}
public LeafCalendarDialog( LeafWindow parentFrame, JComponent parent, boolean enableClean, boolean enableCancel )
{
super(parentFrame);
this.enableClean = enableClean;
this.enableCancel = enableCancel;
setModal( true );
setupComponents();
setUndecorated( true );
setLocationRelativeTo( parent );
setVisible( true );
}
private void setupComponents()
{
TableLayout layout = new TableLayout(new double[]{TableLayout.FILL,TableLayout.FILL,TableLayout.FILL}, new double[]{TableLayout.FILL, TableLayout.MINIMUM});
getContentPane().setLayout( layout );
getContentPane().add( calendarPanel, new TableLayoutConstraints(0,0,2,0));
LeafButton okButton = new LeafButton( "OK" );
okButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
ok = true;
close();
}
} );
LeafButton cancelarButton = new LeafButton( "Cancelar" );
cancelarButton.setEnabled( enableCancel );
cancelarButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
ok = false;
close();
}
} );
LeafButton limparButton = new LeafButton( "Limpar" );
limparButton.setEnabled(enableClean);
limparButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
ok = false;
clear = true;
close();
}
} );
getContentPane().add( okButton, new TableLayoutConstraints(0,1) );
getContentPane().add( cancelarButton, new TableLayoutConstraints(1,1) );
getContentPane().add( limparButton, new TableLayoutConstraints(2,1) );
setSize( 250, 250 );
((JComponent) getContentPane()).setBorder( BorderFactory.createBevelBorder( BevelBorder.RAISED ) );
}
public Date getDate()
{
Date result = null;
if( ok )
{
cal = Calendar.getInstance();
cal.set( Calendar.HOUR_OF_DAY, 12 );
cal = calendarPanel.getCalendar();
cal.set( Calendar.YEAR, calendarPanel.getYearChooser().getYear() );
cal.set( Calendar.DAY_OF_MONTH, calendarPanel.getDayChooser().getDay() );
result = cal != null ? cal.getTime() : null;
}
else if( clear )
{
result = new Date( 0 );
}
return result;
}
public void close()
{
setVisible( false );
dispose();
}
}

@ -1,29 +0,0 @@
package leaf.ui;
import java.awt.Color;
import javax.swing.JPanel;
public class LeafGradientPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public LeafGradientPanel()
{
setBackground( new Color(220,220,220) );
}
// @Override
// public void paintComponent( Graphics g )
// {
// Graphics2D g2d = (Graphics2D) g;
// GradientPaint gradientIn = new GradientPaint( 0, 0, getBackground(), getWidth()/2, 0, Color.DARK_GRAY );
// g2d.setPaint( gradientIn );
// g2d.fillRect( 0, 0, getWidth()/2, getHeight() );
//
// GradientPaint gradientOut = new GradientPaint( getWidth()/2, 0, Color.DARK_GRAY, getWidth(),0, getBackground() );
// g2d.setPaint( gradientOut );
// g2d.fillRect( getWidth()/2, 0, getWidth(), getHeight() );
// }
}

@ -1,630 +0,0 @@
package leaf.ui;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import leaf.data.OrderedMap;
public class LeafInputField<ObjClass extends Object> extends JPanel implements FocusListener, MouseListener, PropertyChangeListener
{
public static final String PROPERTY_CHANGED_CONSTANT = "LEAF_INPUT_PROPERTY_CHANGED";
public static final String PROPERTY_CHANGED_CLICK = "LEAF_PROPERTY_CHANGED_CLICK";
private static final long serialVersionUID = 1L;
private static final DateFormat sdf = DateFormat.getDateInstance( DateFormat.SHORT, new Locale( "pt", "PT" ) );
private static final int defaultColorDec = 20;
private int colorDec = defaultColorDec;
public Dimension size = null;
private Color hot = null;
private Color cold = null;
private Color background = null;
private Color endColor = null;
private Color highLightColor = new Color( 180, 255, 180 );
private Object selectedOption = null;
private boolean isEditable = false;
private boolean isClickable = false;
private boolean highLighted = false;
private boolean collapseOptions = true;
private ObjClass object = null;
private GradientPaint outerContour = null;
private GradientPaint outerReversedContour = null;
private GradientPaint innerContour = null;
private GradientPaint innerReversedContour = null;
private GradientPaint gradient = null;
private TableLayout layout = null;
private JComponent thiz = null;
private List<JComponent> theeze = null;
public LeafInputField()
{
super();
setOpaque( false );
setBorder( BorderFactory.createEmptyBorder( 3, 10, 3, 10 ) );
setBackground( Color.WHITE );
hot = getBackground();
cold = new Color( hot.getRed() > colorDec ? hot.getRed() - colorDec : 0, hot.getGreen() > colorDec ? hot.getGreen() - colorDec : 0, hot.getBlue() > colorDec ? hot.getBlue() - colorDec : 0, hot.getAlpha() );
background = cold;
setObject( null );
}
public boolean isCollapseOptions()
{
return collapseOptions;
}
public void setCollapseOptions( boolean collapse )
{
collapseOptions = collapse;
}
private String getStringFromUser()
{
LeafTextDialog textDialog = new LeafTextDialog( getParentWindow(), this, (String) object, true );
return textDialog.getText();
}
private Object getOptionFromUser()
{
if( object instanceof Map )
{
if(((Map) object).size() > 0)
{
LeafOptionDialog<Object> optionDialog = new LeafOptionDialog<Object>( getParentWindow(), (Map) object, null );
return optionDialog.getOption();
}
}
else if( object instanceof OrderedMap )
{
if(((OrderedMap) object).rows() > 0)
{
LeafOptionDialog<Object> optionDialog = new LeafOptionDialog<Object>( getParentWindow(), (OrderedMap<Object>) object, null, null, null, null );
return optionDialog.getOption();
}
}
return null;
}
private Date getDateFromUser()
{
LeafCalendarDialog calendarDialog = new LeafCalendarDialog( getParentWindow(), this );
return calendarDialog.getDate();
}
private LeafWindow getParentWindow()
{
LeafWindow result = null;
for( Container parent = this.getParent(); parent != null; parent = parent.getParent() )
{
if( parent instanceof LeafWindow )
{
result = (LeafWindow) parent;
}
}
return result;
}
protected void paintComponent( Graphics g )
{
Graphics2D g2d = (Graphics2D) g;
int width = getWidth(), height = getHeight();
setColors( width, height );
g2d.setPaint( outerReversedContour );
g2d.fillRect( 15, 0, width / 4, height );
g2d.setPaint( outerContour );
g2d.fillRect( width / 4, 0, width, height );
g2d.setPaint( innerReversedContour );
g2d.fillRect( 15, 0, width / 4, height - 1 );
g2d.setPaint( innerContour );
g2d.fillRect( width / 4, 0, width, height - 1 );
g2d.setPaint( gradient );
g2d.fillRoundRect( 0, 2, width, height - 5, 15, 15 );
super.paintComponent( g );
}
private void setColors( int width, int height )
{
endColor = getGradientEndColor( background );
outerContour = new GradientPaint( width / 4, 0, Color.GRAY, width, 0, this.getParent().getBackground() );
outerReversedContour = new GradientPaint( 15, 0, this.getParent().getBackground(), width / 4, 0, Color.GRAY );
innerContour = new GradientPaint( width / 4, 0, Color.LIGHT_GRAY, width, 0, this.getParent().getBackground() );
innerReversedContour = new GradientPaint( 15, 0, this.getParent().getBackground(), width / 4, 0, Color.LIGHT_GRAY );
gradient = new GradientPaint( 0, 0, background, width, height, endColor );
}
private Color getGradientEndColor( Color startColor )
{
return getParent() != null ? getParent().getBackground() : startColor;
}
public boolean getSelected()
{
return highLighted;
}
public void setSelected( boolean selected )
{
highLighted = selected;
background = selected ? highLightColor : cold;
repaint();
}
public void setEditable( boolean editable )
{
isEditable = editable;
if( !editable )
{
setMouseOver( false );
}
setEnabled( editable );
setObject( object, selectedOption );
repaint();
}
public boolean isEditable()
{
return isEditable;
}
public void setClickable( boolean clickable )
{
isClickable = clickable;
if( !clickable )
{
setMouseOver( false );
}
repaint();
}
public boolean isClickable()
{
return isClickable;
}
private void setupCollapsedPanel()
{
List<Object> values = ((OrderedMap) object).getValues( selectedOption );
theeze = new ArrayList<JComponent>();
if( values != null )
{
double[] rows = new double[] {
TableLayout.MINIMUM
};
double[] cols = new double[values.size() > 0 ? (values.size() * 2 - 1) : 0];
for( int i = 0; i < cols.length; ++i )
{
cols[i] = (i % 2 == 0) ? TableLayout.FILL : TableLayout.MINIMUM;
}
TableLayout layout = new TableLayout( cols, rows );
layout.setHGap( 5 );
thiz.setLayout( layout );
for( int i = 0, a = 0; i < cols.length; ++i )
{
JComponent comp = null;
if( i % 2 == 0 )
{
Object value = values.get( a++ );
comp = new JLabel( value == null ? " " : value.toString() );
theeze.add( comp );
}
else
{
comp = new JSeparator( JSeparator.VERTICAL );
}
thiz.add( comp, new TableLayoutConstraints( i, 0 ) );
}
}
}
private void setupUncollapsedPanel()
{
theeze = new ArrayList<JComponent>();
OrderedMap<Object> map = (OrderedMap<Object>) object;
double[] rows = new double[map.rows() > 0 ? (map.rows() * 2 - 1) : 0];
double[] cols = new double[map.columns() > 0 ? (map.columns() * 2 - 1) : 0];
for( int i = 0; i < rows.length; ++i )
{
rows[i] = (i % 2 == 0) ? TableLayout.MINIMUM : TableLayout.MINIMUM;
}
for( int i = 0; i < cols.length; ++i )
{
cols[i] = (i % 2 == 0) ? TableLayout.FILL : TableLayout.MINIMUM;
}
TableLayout layout = new TableLayout( cols, rows );
layout.setHGap( 5 );
thiz.setLayout( layout );
for( int i = 0, a = 0; i < rows.length; ++i )
{
if( i % 2 == 0 )
{
List<Object> values = map.getRow( a++ );
for( int j = 0, b = 0; j < cols.length; ++j )
{
JComponent comp = null;
if( j % 2 == 0 )
{
Object value = values.get( b++ );
LeafInputField<Object> leaf;
if( value instanceof LeafInputField )
{
leaf = (LeafInputField<Object>) value;
}
else
{
leaf = new LeafInputField<Object>();
leaf.setObject( value );
if( j > 1 )
{
leaf.setEditable( isEditable );
}
}
comp = leaf;
theeze.add( comp );
}
else
{
JSeparator sep = new JSeparator( JSeparator.VERTICAL );
sep.setForeground( cold );
comp = sep;
}
thiz.add( comp, new TableLayoutConstraints( j, i ) );
}
}
else
{
JSeparator sep = new JSeparator();
sep.setForeground( cold );
thiz.add( sep, new TableLayoutConstraints( 0, i, cols.length - 1, i ) );
}
}
}
private void setSelectedObject( Object key )
{
selectedOption = key;
if( object != null && thiz != null )
{
if( object instanceof Map )
{
Object value = null;
value = ((Map) object).get( selectedOption );
String text = value == null ? " " : value.toString();
if( thiz instanceof JTextArea )
{
((JTextArea) thiz).setText( text );
}
else if( thiz instanceof JTextField )
{
((JTextField) thiz).setText( text );
}
else if( thiz instanceof JLabel )
{
((JLabel) thiz).setText( text );
}
}
else if( object instanceof OrderedMap )
{
if( thiz instanceof JPanel && collapseOptions )
{
setupCollapsedPanel();
}
else if( thiz instanceof JPanel && !collapseOptions )
{
setupUncollapsedPanel();
}
}
}
}
public Object getSelectedObject()
{
return selectedOption;
}
public void setObject( ObjClass object, Object selected )
{
this.selectedOption = selected;
setObject( object );
}
public void setObject( ObjClass object )
{
this.object = object;
if( object != null )
{
if( object instanceof Date )
{
Date date = (Date) object;
String text = date.getTime() == 0 ? "" : sdf.format( object );
thiz = isEditable ? new JLabel( text ) : new JTextArea( text );
thiz.setEnabled( false );
}
else if( object instanceof Map )
{
thiz = isEditable ? new JLabel( " " ) : new JTextArea( " " );
setSelectedObject( selectedOption );
thiz.setEnabled( false );
}
else if( object instanceof OrderedMap && collapseOptions )
{
thiz = new JPanel();
setSelectedObject( selectedOption );
}
else if( object instanceof OrderedMap && !collapseOptions )
{
thiz = new JPanel();
setSelectedObject( selectedOption );
}
else if( object instanceof LeafInputField )
{
setObject( (ObjClass) ((LeafInputField) object).getObject(), ((LeafInputField) object).getSelectedObject() );
}
else if( object instanceof String )
{
JTextArea textArea = new JTextArea();
textArea.setEditable( false );
textArea.setText( object == null ? "" : object.toString() );
thiz = textArea;
}
else
{
String toString = object.toString();
if( "".equals( toString ) )
{
toString = " ";
}
thiz = new JTextArea( toString );
}
}
else
{
thiz = new JLabel( " " );
}
reListen();
reLayout();
}
public ObjClass getObject()
{
return this.object;
}
private void reListen()
{
removeListeners();
if( theeze != null && !collapseOptions && !theeze.isEmpty() )
{
for( JComponent current : theeze )
{
if( current instanceof LeafInputField )
{
current.addFocusListener( (LeafInputField) current );
current.addMouseListener( (LeafInputField) current );
current.addPropertyChangeListener( PROPERTY_CHANGED_CONSTANT, this );
}
}
}
else
{
thiz.addFocusListener( this );
thiz.addMouseListener( this );
}
}
private void removeListeners()
{
removeListerensFrom( thiz );
if( theeze != null )
{
for( JComponent comp : theeze )
{
removeListerensFrom( comp );
}
}
}
private void removeListerensFrom( JComponent comp )
{
if( comp != null )
{
FocusListener[] allFocus = thiz.getFocusListeners();
if( allFocus != null )
{
for( FocusListener focusListener : allFocus )
{
comp.removeFocusListener( focusListener );
}
}
MouseListener[] allMouse = thiz.getMouseListeners();
if( allMouse != null )
{
for( MouseListener mouseListener : allMouse )
{
comp.removeMouseListener( mouseListener );
}
}
}
}
private void reLayout()
{
if( thiz != null )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
if( layout == null )
{
layout = new TableLayout( new double[] {
TableLayout.FILL
}, new double[] {
TableLayout.FILL
} );
LeafInputField.this.setLayout( layout );
}
else
{
removeAll();
}
LeafInputField.this.add( thiz, new TableLayoutConstraints( 0, 0 ) );
revalidate();
repaint();
}
} );
size = thiz.getPreferredSize();
thiz.setOpaque( false );
}
}
@Override
public void focusLost( FocusEvent e )
{
repaint();
}
@Override
public void focusGained( FocusEvent e )
{
repaint();
}
@Override
public void mouseReleased( MouseEvent e )
{
}
@Override
public void mousePressed( MouseEvent e )
{
Object old = null;
if( object != null && isEditable )
{
if( object instanceof Date )
{
old = object;
ObjClass newDate = (ObjClass) getDateFromUser();
if( newDate != null )
{
setObject( newDate );
firePropertyChange( PROPERTY_CHANGED_CONSTANT, old, object );
}
}
else if( object instanceof Map && collapseOptions )
{
old = selectedOption;
ObjClass out = (ObjClass) getOptionFromUser();
setObject( object, out );
firePropertyChange( PROPERTY_CHANGED_CONSTANT, old, selectedOption );
}
else if( object instanceof OrderedMap && collapseOptions )
{
old = selectedOption;
ObjClass out = (ObjClass) getOptionFromUser();
setObject( object, out );
firePropertyChange( PROPERTY_CHANGED_CONSTANT, old, object );
}
else if( object instanceof OrderedMap && !collapseOptions )
{
// old = selectedOption;
// ObjClass out = (ObjClass) getOptionFromUser();
// setObject( object, out );
// firePropertyChange( PROPERTY_CHANGED_CONSTANT, old, object );
}
else if( object instanceof String )
{
old = object;
setObject( (ObjClass) getStringFromUser() );
firePropertyChange( PROPERTY_CHANGED_CONSTANT, old, object );
}
}
if( isClickable )
{
firePropertyChange( PROPERTY_CHANGED_CLICK, false, true );
}
// setObject( object, selectedOption );
}
@Override
public void mouseExited( MouseEvent e )
{
setMouseOver( false );
repaint();
}
@Override
public void mouseEntered( MouseEvent e )
{
setMouseOver( true );
repaint();
}
@Override
public void mouseClicked( MouseEvent e )
{
}
private void setMouseOver( boolean mouseOver )
{
if( mouseOver )
{
background = (!highLighted && (isEditable || isClickable)) ? hot : background;
}
else
{
background = (!highLighted && (isEditable || isClickable)) ? cold : background;
}
}
@Override
public void propertyChange( PropertyChangeEvent evt )
{
firePropertyChange( PROPERTY_CHANGED_CONSTANT, evt.getOldValue(), evt.getNewValue() );
}
}

@ -1,103 +0,0 @@
package leaf.ui;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
public class LeafLogic
{
/**
* Declares an Action
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Action
{
/**
* true - this action saves data to de database from the components
* false - this action reads from the database to the components
*/
boolean isSave();
}
/**
* Binds a UI method with an action
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface LeafUIActionBinding
{
/**
* The name of the action this method binds to
*/
String [] action();
}
/**
* Binds a logic methods with a group of actions
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface LeafLogicActionBinding
{
/**
* The name of the action this method binds to
*/
String [] actions();
}
@Action(isSave = false)
public static final String ACTION_STARTUP = "ACTION_STARTUP";
@Action(isSave = false)
public static final String ACTION_CANCEL = "ACTION_CANCEL";
private List<LeafWindow> registeredWindows = new ArrayList<LeafWindow>();
public void addWindow(LeafWindow window)
{
registeredWindows .add(window);
}
public void runAction( String actionName )
{
runAction( actionName, null );
}
public void runAction( String actionName, Object argument )
{
for( LeafWindow window : registeredWindows )
{
window.runAction( actionName, argument );
}
}
public void runActionLater( String actionName )
{
for( LeafWindow window : registeredWindows )
{
window.runActionLater( actionName );
}
}
public void runActionLater( String actionName, Object argument )
{
for( LeafWindow window : registeredWindows )
{
window.runActionLater( actionName, argument );
}
}
}

@ -1,22 +0,0 @@
package leaf.ui;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class LeafLookAndFeel extends MetalLookAndFeel
{
private static final long serialVersionUID = 1L;
@Override
public String getName()
{
return "LEAF";
}
@Override
public String getDescription()
{
return "Evolute's LEAF Look And Feel";
}
}

@ -1,298 +0,0 @@
package leaf.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 javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.ScrollPaneConstants;
import leaf.data.OrderedMap;
public class LeafOptionDialog<KeyClass extends Object> extends JDialog
{
private Dimension OPTION_SIZE = new Dimension( 200, 20 );
private Dimension BUTTON_SIZE = new Dimension( 200, 20 );
private int MAX_VISIBLE_OPTIONS = 20;
private static final long serialVersionUID = 1L;
private String CANCEL_LABEL = "Cancelar";
private String okLabel = null;
private final JLabel labelMessage = new JLabel();
private final JPanel optionsPanel = new JPanel();
private final JScrollPane optionsScrollPane = new JScrollPane();
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 = null;
private Map<KeyClass, Boolean> mapChosenForKey = null;
private String message = null;
private boolean ordered = false;
public LeafOptionDialog( LeafWindow owner, Map<KeyClass, ? extends Object> map, String message)
{
super( owner );
this.mapEnabledForKey = new HashMap<KeyClass, Boolean>();
this.mapChosenForKey = new HashMap<KeyClass, Boolean>();
this.message = message;
cancelActive = false;
for( KeyClass key : map.keySet() )
{
mapEnabledForKey.put( key, true );
}
startup( map, null );
}
public LeafOptionDialog( LeafWindow owner, OrderedMap<KeyClass> orderedMap, Map<KeyClass, Boolean> chosen, Map<KeyClass, Boolean> enabled, String message, String okButton)
{
super( owner );
ordered = true;
this.message = message;
okLabel = okButton;
cancelActive = okButton != null;
this.mapChosenForKey = chosen == null ? new HashMap<KeyClass, Boolean>() : chosen;
this.mapEnabledForKey = enabled == null ? new HashMap<KeyClass, Boolean>() : chosen;
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 );
setUndecorated( true );
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getRootPane().setWindowDecorationStyle(JRootPane.NONE);
setSize( getLayout().minimumLayoutSize( getRootPane() ) );
setLocationRelativeTo( getParent() );
setModal( true );
setVisible( true );
}
private void setupComponents( Iterator<KeyClass> iterator, Integer size, boolean ordered )
{
double[] cols = new double[] {
TableLayout.PREFERRED
};
double[] rows = new double[(message == null ? 0 : 2) + 1 + (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++ ) );
}
setupOptionsComponents( iterator, size );
getContentPane().add( optionsScrollPane, new TableLayoutConstraints( 0, shift++ ) );
if( cancelActive )
{
getContentPane().add( new JSeparator(), new TableLayoutConstraints( 0, shift++ ) );
submitButton.setObject( ordered ? okLabel : CANCEL_LABEL );
submitButton.setClickable( true );
submitButton.setPreferredSize( BUTTON_SIZE );
getContentPane().add( submitButton, new TableLayoutConstraints( 0, shift++ ) );
addListenerToComponent( submitButton );
}
((JComponent) getContentPane()).setBorder( BorderFactory.createRaisedBevelBorder() );
}
private void setupOptionsComponents( Iterator<KeyClass> iterator, Integer size )
{
int maxWidth = OPTION_SIZE.width;
double[] cols = new double[] {
TableLayout.PREFERRED
};
double[] rows = new double[size];
for( int i = 0; i < rows.length; ++i )
{
rows[i] = TableLayout.PREFERRED;
}
TableLayout layout = new TableLayout( cols, rows );
layout.setVGap( 3 );
optionsPanel.setLayout( layout );
KeyClass current = null;
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 );
if(component.size != null && component.size.width > maxWidth )
{
maxWidth = component.size.width;
}
optionsPanel.add( component, new TableLayoutConstraints( 0, i ) );
addListenerToComponent( component );
}
optionsScrollPane.setViewportView( optionsPanel );
optionsScrollPane.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
JScrollBar verticalScrollBar = optionsScrollPane.getVerticalScrollBar();
verticalScrollBar.setUnitIncrement( (int) OPTION_SIZE.getHeight() + 3 );
optionsScrollPane.setVerticalScrollBar( verticalScrollBar );
optionsScrollPane.setVerticalScrollBarPolicy( size > MAX_VISIBLE_OPTIONS ? ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS : ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER );
optionsScrollPane.setPreferredSize( new Dimension( maxWidth + 30 , size > MAX_VISIBLE_OPTIONS ? (MAX_VISIBLE_OPTIONS * ((int) (OPTION_SIZE.getHeight() + 3 )) ) : (size * ((int) (OPTION_SIZE.getHeight() + 3 )) ) ));
}
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() ) )
{
if( !source.equals( submitButton ) )
{
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( okLabel == null || 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();
}
}

@ -1,18 +0,0 @@
package leaf.ui;
public class LeafRuntimeException extends RuntimeException
{
private static final long serialVersionUID = 1L;
private boolean abort = false;
public LeafRuntimeException( boolean all )
{
this.abort = all;
}
public boolean isAbort()
{
return abort;
}
}

@ -1,16 +0,0 @@
package leaf.ui;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
public class LeafScrollBar extends JScrollPane
{
private static final long serialVersionUID = 1L;
public void paintComponent()
{
System.out.println("");
}
}

@ -1,179 +0,0 @@
package leaf.ui;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import leaf.data.OrderedMap;
import siprp.database.cayenne.objects.TrabalhadoresEcd;
import com.evolute.utils.tables.BaseTableModel;
import com.evolute.utils.tables.models.SortableModel;
public class LeafTableModel extends BaseTableModel
{
private static final long serialVersionUID = 1L;
private OrderedMap<Object> map = new OrderedMap<Object>();
public LeafTableModel(String n[])
{
super( n, null );
setChangeable( false );
}
public void clearAll()
{
int i = map.rows();
if( i > 0 )
{
map.clear();
fireTableRowsDeleted( 0, i - 1 );
}
}
@Override
public int innerGetRowCount()
{
return map == null ? 0 : map.rows();
}
@Override
public Object innerGetValueAt( int row, int col )
{
return map.getValueAt( row, col );
}
@Override
public void deleteRow( int row )
{
map.deleteRow( row );
}
public Object getKey( int row )
{
Object result = null;
if( row < map.rows() )
{
result = map.getKeyForRow( row );
}
return result;
}
public void setValues( Collection<? extends Object> v )
{
Iterator<? extends Object> iterator = v.iterator();
while( iterator.hasNext() )
{
Object value = iterator.next();
map.putLast( value, value.toString() );
}
fireTableDataChanged();
}
public void setValues( OrderedMap<? extends Object> map )
{
this.map = (OrderedMap<Object>) map;
fireTableDataChanged();
}
public void order(int ... colNumber )
{
if(colNumber != null)
{
map.order(colNumber);
fireTableDataChanged();
}
}
@Override
public void appendEmptyRow()
{
map.putLast( map.rows(), (Object) null );
}
@Override
public void innerSetValueAt( Object obj, int row, int col )
{
if( isCellEditable( row, col ) && map.getValueAt( row, col ) != null )
{
map.setValueAt( row, col, obj );
}
}
@Override
public boolean isRowEmpty( int row )
{
List<Object> line = map.getRow( row );
return line != null && line.size() > 0;
}
@Override
public boolean innerIsCellEditable( int row, int col )
{
return false;
}
public void printContents()
{
for( int r = 0; r < getRowCount(); r++ )
{
for( int c = 0; c < getColumnCount(); c++ )
{
Object val = getValueAt( r, c );
if( val != null )
{
System.out.print( val.toString() + "\t" );
}
}
System.out.println( "" );
}
}
// public void insertRowAt( Object rowObj, int row )
// {
// values.add( row, rowObj );
// fireTableDataChanged();
// }
//
// public void removeRowAt( int row )
// {
// values.remove( row );
// fireTableDataChanged();
// }
//
// public Object getRowAt( int row )
// {
// return values.elementAt( row );
// }
// public void swapRows( int row1, int row2 )
// {
// if( row1 == row2 || row1 < 0 || row2 < 0 || row1 >= getRowCount() || row2
// >= getRowCount() )
// {
// return;
// }
// // Collections.swap( values, row1, row2 );
// Object row1Data = getRowAt( row1 );
// Object row2Data = getRowAt( row2 );
// if( row1 < row2 )
// {
// removeRowAt( row2 );
// removeRowAt( row1 );
// insertRowAt( row2Data, row1 );
// insertRowAt( row1Data, row2 );
// }
// else
// {
// removeRowAt( row1 );
// removeRowAt( row2 );
// insertRowAt( row1Data, row2 );
// insertRowAt( row2Data, row1 );
// }
// }
}

@ -1,169 +0,0 @@
package leaf.ui;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextArea;
public class LeafTextDialog extends JDialog
{
private static final long serialVersionUID = 1L;
private static final Dimension buttonSize = new Dimension( 30, 20 );
private static final Dimension textSize = new Dimension( 200, 20 );
private static final Dimension expandedTextSize = new Dimension( 300, 200 );
private String text = null;
private String defaultText = null;
private boolean expanded = true;
private final JTextArea textArea = new JTextArea();
private final LeafButton expandButton = new LeafButton( "+" );
private final LeafButton okButton = new LeafButton( "Ok" );
private final LeafButton cancelButton = new LeafButton( "X" );
private TableLayout layout = null;
private TableLayout expandedLayout = null;
/** Creates a new instance of JCalendarDialog */
public LeafTextDialog( LeafWindow parentFrame, JComponent parent, String defaultText, boolean expanded )
{
super( parentFrame );
this.defaultText = defaultText == null ? "" : defaultText;
this.text = this.defaultText;
setModal( true );
setContentPane( new LeafGradientPanel() );
textArea.setText( text );
expandButton.setPreferredSize( buttonSize );
cancelButton.setPreferredSize( buttonSize );
okButton.setPreferredSize( buttonSize );
setupLayout();
setUndecorated( true );
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setLocationRelativeTo( null );
this.expanded = expanded;
expand( expanded );
addListeners();
setVisible( true );
}
private void setupLayout()
{
double[] cols = new double[] {
TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED
};
double[] rows = new double[] {
TableLayout.PREFERRED
};
layout = new TableLayout( cols, rows );
cols = new double[] {
TableLayout.PREFERRED, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.PREFERRED
};
rows = new double[] {
TableLayout.PREFERRED, TableLayout.FILL
};
expandedLayout = new TableLayout( cols, rows );
}
private void placeComponents(boolean expand)
{
if(expand)
{
getContentPane().add( expandButton, new TableLayoutConstraints( 0, 0 ) );
getContentPane().add( new JPanel(), new TableLayoutConstraints( 0, 1 ) );
getContentPane().add( textArea, new TableLayoutConstraints( 1, 0,1,1 ) );
getContentPane().add( okButton, new TableLayoutConstraints( 2, 0 ) );
getContentPane().add( cancelButton, new TableLayoutConstraints( 3, 0 ) );
getContentPane().add( new JPanel(), new TableLayoutConstraints( 2, 1,3,1 ) );
}
else
{
getContentPane().add( expandButton, new TableLayoutConstraints( 0, 0 ) );
getContentPane().add( textArea, new TableLayoutConstraints( 1, 0 ) );
getContentPane().add( okButton, new TableLayoutConstraints( 2, 0 ) );
getContentPane().add( cancelButton, new TableLayoutConstraints( 3, 0 ) );
}
((JComponent) getContentPane()).setBorder( BorderFactory.createRaisedBevelBorder() );
// setSize( expand ? expandedLayout.preferredLayoutSize( this.getContentPane() ) : layout.preferredLayoutSize( this.getContentPane() ) );
setSize( getLayout().preferredLayoutSize( getRootPane() ) );
}
private void setupComponents(boolean expand)
{
getContentPane().setLayout( expand ? expandedLayout : layout);
textArea.setPreferredSize( expand ? expandedTextSize : textSize );
expandButton.setText( expand ? "-" : "+" );
placeComponents(expand);
}
private void expand( boolean expand )
{
setupComponents(expand);
setResizable( expand );
}
private void addListeners()
{
expandButton.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
expanded = !expanded;
expand( expanded );
}
} );
okButton.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
text = textArea.getText();
close();
}
} );
cancelButton.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
text = defaultText;
close();
}
} );
}
public String getText()
{
return text;
}
public void close()
{
setVisible( false );
dispose();
}
}

@ -1,47 +0,0 @@
package leaf.ui;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class LeafTransparentPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private BufferedImage background = null;
public LeafTransparentPanel()
{
updateBackground();
}
private void updateBackground()
{
try
{
Robot rbt = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
background = rbt.createScreenCapture( new Rectangle( 0, 0, (int) dim.getWidth(), (int) dim.getHeight() ) );
} catch( AWTException ex )
{
ex.printStackTrace();
}
}
@Override
public void paintComponent( Graphics g )
{
Point pos = this.getLocationOnScreen();
Point offset = new Point( -pos.x, -pos.y );
g.drawImage( background, offset.x, offset.y, null );
}
}

@ -1,937 +0,0 @@
package leaf.ui;
import static info.clearthought.layout.TableLayoutConstants.FILL;
import static leaf.ui.LeafLogic.ACTION_CANCEL;
import static leaf.ui.LeafLogic.ACTION_STARTUP;
import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import javax.swing.BorderFactory;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableModel;
import leaf.data.Pair;
import leaf.ui.LeafLogic.Action;
import leaf.ui.LeafLogic.LeafLogicActionBinding;
import leaf.ui.LeafLogic.LeafUIActionBinding;
import com.evolute.utils.tables.BaseTable;
import com.evolute.utils.tables.ColumnizedMappable;
import com.evolute.utils.tables.VectorTableModel;
import com.evolute.utils.tracker.TrackableWindow;
public class LeafWindow extends JFrame implements TrackableWindow, ListSelectionListener, TreeSelectionListener, ActionListener, PropertyChangeListener
{
private static final long serialVersionUID = 1L;
private static final int DEFAULT_HEIGHT = 480;
private static final int DEFAULT_WIDTH = 640;
/**
* Registers DataComponent in a list of actions
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionActivation
{
/**
* Array of actions to execute when a select is listened in this
* JComponent
*
* @return
*/
String[] onSelect();
/**
* Array of actions to execute when a change is listened in this
* JComponent
*
* @return
*/
String[] onChange();
}
/**
* Binds an Object to actions
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface LeafObject
{
/**
* Actions that use this field
*/
String[] useWith();
}
/**
* Declares a JPanel as a leaf
*
* @author tsimao
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface LeafPanel
{
}
/**
* This window's logic controller
*/
private final LeafLogic logicController;
private List<JPanel> subPanels = new ArrayList<JPanel>();
/**
* Actions
*/
private Map<String, Action> mapActionByName = new HashMap<String, Action>();
/**
* Fields
*/
private Map<String, List<Object>> mapWindowOnSelectFieldByActionName = new HashMap<String, List<Object>>();
private Map<String, List<Object>> mapWindowOnChangeFieldByActionName = new HashMap<String, List<Object>>();
private Map<String, Field> mapLeafObjectByActionName = new HashMap<String, Field>();
private Map<Field, Object> mapInstanceByField = new HashMap<Field, Object>();
/**
* Methods
*/
private Map<String, List<Method>> mapWindowMethodsByActionName = new HashMap<String, List<Method>>();
private Map<String, Method> mapLogicMethodByActionName = new HashMap<String, Method>();
private Map<Method, Object> mapInstanceByMethod = new HashMap<Method, Object>();
/**
* Meta-info
*/
private Map<Object, Annotation> mapAnnotationByObject = new HashMap<Object, Annotation>();
/**
* Run later actions
*/
private Queue<Pair<String, Object>> listRunLater = new LinkedList<Pair<String, Object>>();
/**
* Creates a new LeafWindow binded with given 'logicController'
*
* @param logicController
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public LeafWindow(LeafLogic logicController)
{
super();
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
this.logicController = logicController;
if( logicController != null )
{
logicController.addWindow( this );
}
}
@Override
public void open()
{
setVisible( true );
}
public void close()
{
SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
setVisible( false );
dispose();
}
} );
}
@Override
public boolean closeIfPossible()
{
close();
return true;
}
@Override
public void refresh()
{
}
/**
* Aborts current action. Aborts pending actions as well if 'all' is true
*
* @param all
*/
public void abortAction( boolean all )
{
runGivenAction( ACTION_CANCEL, null );
throw new LeafRuntimeException( all );
}
protected boolean runAction( String actionName )
{
return runAction( actionName, null );
}
/**
* Returns false if an error occurred
*
* @param actionName
* @param argument
* @return
*/
protected boolean runAction( String actionName, Object argument )
{
boolean ok = true;
if( argument == null )
{
Field field = mapLeafObjectByActionName.get( actionName );
if(field != null)
{
Object instance = mapInstanceByField.get( field );
if( instance != null)
{
try
{
argument = field.get( instance );
} catch( IllegalArgumentException e )
{
e.printStackTrace(System.out);
} catch( IllegalAccessException e )
{
e.printStackTrace( System.out);
}
}
}
}
try
{
runGivenAction( actionName, argument );
} catch( LeafRuntimeException leafRuntimeException )
{
ok = !leafRuntimeException.isAbort();
}
if( ok )
{
runPendingActions();
}
else
{
listRunLater.clear();
}
return ok;
}
public void runActionLater( String action )
{
runActionLater( action, null );
}
public void runActionLater( String action, Object argument )
{
if( action != null && mapActionByName.containsKey( action ) )
{
listRunLater.add( new Pair<String, Object>( action, argument ) );
}
}
public void completeSetup()
{
try
{
loadLeafs();
loadActions();
loadFields();
loadMethods();
runAction( ACTION_STARTUP, null );
setVisible( true );
} catch( Exception e )
{
e.printStackTrace( System.out );
}
}
public static void setupTopBottomSimpleActionsPanel(JPanel where, JPanel top, JPanel bottom)
{
TableLayout layout = new TableLayout(new double[]{TableLayout.FILL}, new double[]{TableLayout.MINIMUM, TableLayout.FILL,TableLayout.MINIMUM});
where.setLayout( layout );
where.add( top, new TableLayoutConstraints(0,0) );
where.add( new JPanel(), new TableLayoutConstraints(0,1) );
where.add( bottom, new TableLayoutConstraints(0,2) );
}
public static void setupSimpleDataPanel( JPanel where, String name, JComponent... field )
{
double[] cols = new double[] {
FILL
};
double[] rows = new double[field.length];
for( int i = 0; i < field.length; rows[i++] = TableLayout.PREFERRED )
;
rows[rows.length - 1] = FILL;
TableLayout layout = new TableLayout( cols, rows );
layout.setHGap( 5 );
layout.setVGap( 5 );
where.setLayout( layout );
if( name != null )
{
where.setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), name ) );
}
for( int i = 0; i < field.length; ++i )
{
where.add( field[i], new TableLayoutConstraints( 0, i ) );
}
}
private void loadLeafs() throws IllegalArgumentException, IllegalAccessException
{
Field fields[] = this.getClass().getDeclaredFields();
if( fields != null )
{
for( Field field : fields )
{
if( field.getAnnotation( LeafPanel.class ) != null && field.get( this ) != null )
{
subPanels.add( (JPanel) field.get( this ) );
}
}
}
}
private void loadActions() throws IllegalArgumentException, IllegalAccessException
{
Field[] allLogicFields = this.logicController.getClass().getFields();
for( Field field : allLogicFields )
{
Action action = field.getAnnotation( Action.class );
if( action != null )
{
String value = (String) field.get( this );
if( value != null )
{
mapActionByName.put( value, action );
mapWindowMethodsByActionName.put( value, new ArrayList<Method>() );
mapWindowOnSelectFieldByActionName.put( value, new ArrayList<Object>() );
mapWindowOnChangeFieldByActionName.put( value, new ArrayList<Object>() );
}
}
}
}
private void loadFields( Field[] fields, Object instance )
{
try
{
for( Field field : fields )
{
ActionActivation componentBehaviour = field.getAnnotation( ActionActivation.class );
if( componentBehaviour != null )
{
String[] allChanges = componentBehaviour.onChange();
if( allChanges != null )
{
for( String onChange : allChanges )
{
if( mapActionByName.containsKey( onChange ) )
{
// valid action
mapAnnotationByObject.put( field.get( instance ), componentBehaviour );
mapWindowOnChangeFieldByActionName.get( onChange ).add( field.get( instance ) );
if( !mapInstanceByField.containsKey( field ) )
{
addListenerForField( componentBehaviour, field, instance );
mapInstanceByField.put( field, instance );
}
}
}
}
String[] allSelect = componentBehaviour.onSelect();
if( allSelect != null )
{
for( String onSelect : allSelect )
{
if( mapActionByName.containsKey( onSelect ) )
{
// valid action
mapAnnotationByObject.put( field.get( instance ), componentBehaviour );
mapWindowOnSelectFieldByActionName.get( onSelect ).add( field.get( instance ) );
if( !mapInstanceByField.containsKey( field ) )
{
addListenerForField( componentBehaviour, field, instance );
mapInstanceByField.put( field, instance );
}
}
}
}
}
LeafObject leafObject = field.getAnnotation( LeafObject.class );
if( leafObject != null )
{
String[] useWith = leafObject.useWith();
if( useWith != null )
{
for( String current : useWith )
{
if( mapActionByName.containsKey( current ) )
{
// valid action
mapLeafObjectByActionName.put( current, field );
mapInstanceByField.put( field, instance );
}
}
}
}
}
} catch( IllegalAccessException exception )
{
exception.printStackTrace( System.out );
}
}
private void loadFields()
{
Field[] allFields = this.getClass().getDeclaredFields();
if( allFields != null )
{
loadFields( allFields, this );
}
allFields = logicController.getClass().getDeclaredFields();
if( allFields != null )
{
loadFields( allFields, logicController );
}
for( JPanel panel : subPanels )
{
allFields = panel.getClass().getDeclaredFields();
if( allFields != null )
{
loadFields( allFields, panel );
}
}
}
private void loadWindowMethods( Method[] windowMethods, Object instance )
{
for( Method method : windowMethods )
{
LeafUIActionBinding actionBinding = method.getAnnotation( LeafUIActionBinding.class );
if( actionBinding != null )
{
String[] actions = actionBinding.action();
for( String actionName : actions )
{
if( mapActionByName.containsKey( actionName ) )
{
// valid action
mapWindowMethodsByActionName.get( actionName ).add( method );
mapAnnotationByObject.put( method, actionBinding );
mapInstanceByMethod.put( method, instance );
}
}
}
}
}
private void loadLogicMethods()
{
Method[] allLogicMethods = this.logicController.getClass().getDeclaredMethods();
if( allLogicMethods != null )
{
for( Method method : allLogicMethods )
{
LeafLogicActionBinding actionBinding = method.getAnnotation( LeafLogicActionBinding.class );
if( actionBinding != null )
{
String[] actions = actionBinding.actions();
if( actions != null )
{
for( String actionName : actions )
{
if( mapActionByName.containsKey( actionName ) )
{
// valid action
mapAnnotationByObject.put( method, actionBinding );
mapLogicMethodByActionName.put( actionName, method );
mapInstanceByMethod.put( method, logicController );
}
}
}
}
}
}
}
private void loadMethods()
{
loadLogicMethods();
Method[] allWindowMethods = this.getClass().getDeclaredMethods();
if( allWindowMethods != null )
{
loadWindowMethods( allWindowMethods, this );
}
for( JPanel panel : subPanels )
{
allWindowMethods = panel.getClass().getDeclaredMethods();
if( allWindowMethods != null )
{
loadWindowMethods( allWindowMethods, panel );
}
}
}
private Object getObjectForAction( String actionName )
{
Object result = null;
Field field = mapLeafObjectByActionName.get( actionName );
if( field != null )
{
Object instance = mapInstanceByField.get( field );
if( instance != null )
{
try
{
result = field.get( instance );
} catch( IllegalArgumentException e )
{
e.printStackTrace();
} catch( IllegalAccessException e )
{
e.printStackTrace();
}
}
}
return result;
}
public void runPendingActions()
{
while( listRunLater.size() > 0 )
{
Pair<String, Object > p = listRunLater.poll();
runAction( p.getCar(), p.getCdr() );
}
}
/**
* Executes given action
*/
private void runGivenAction( String actionName, Object argument )
{
System.out.println( "Running: " + actionName );
try
{
this.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) );
if( actionName != null && mapActionByName.containsKey( actionName ) )
{
Action action = mapActionByName.get( actionName );
if( action.isSave() )
{
Object windowArgument = getObjectForAction( actionName );
if( windowArgument == null )
{
windowArgument = argument;
}
Object logicArgument = windowArgument;
for( Method currentWindowMethod : mapWindowMethodsByActionName.get( actionName ) )
{
Object currentLogicArgument = runWindowMethod( currentWindowMethod, windowArgument != null ? windowArgument : argument );
logicArgument = logicArgument == null ? currentLogicArgument : logicArgument;
}
runLogicMethod( mapLogicMethodByActionName.get( actionName ), logicArgument );
}
else
{
Object windowArgument = runLogicMethod( mapLogicMethodByActionName.get( actionName ), argument );
for( Method currentWindowMethod : mapWindowMethodsByActionName.get( actionName ) )
{
runWindowMethod( currentWindowMethod, windowArgument != null ? windowArgument : argument );
}
}
}
} finally
{
this.setCursor( Cursor.getDefaultCursor() );
}
}
private Object runLogicMethod( Method logicMethod, Object argument ) throws LeafRuntimeException
{
Object result = null;
try
{
if( logicMethod != null )
{
if( logicMethod.getParameterTypes().length > 0 )
{
result = logicMethod.invoke( logicController, argument );
}
else
{
result = logicMethod.invoke( logicController );
}
}
} catch( IllegalArgumentException e )
{
System.out.println("Error in: " + logicMethod.getName() );
System.out.println("Got: " + argument + " expected: " + (logicMethod.getParameterTypes().length > 0 ? logicMethod.getParameterTypes()[0].getCanonicalName() : "(nothing)"));
e.printStackTrace( System.out );
} catch( IllegalAccessException e )
{
e.printStackTrace( System.out );
} catch( InvocationTargetException e )
{
if( e.getCause() instanceof LeafRuntimeException )
{
throw (LeafRuntimeException) e.getCause();
}
else
{
e.printStackTrace( System.out );
}
}
return result;
}
private Object runWindowMethod( Method windowMethod, Object argument ) throws LeafRuntimeException
{
Object result = null;
try
{
if( windowMethod != null )
{
if( windowMethod.getParameterTypes().length > 0 )
{
result = windowMethod.invoke( mapInstanceByMethod.get( windowMethod ), argument );
}
else
{
result = windowMethod.invoke( mapInstanceByMethod.get( windowMethod ) );
}
}
} catch( IllegalArgumentException e )
{
System.out.println("Error in: " + windowMethod.getName() );
System.out.println("Got: " + argument + " expected: " + (windowMethod.getParameterTypes().length > 0 ? windowMethod.getParameterTypes()[0].getCanonicalName() : "(nothing)"));
e.printStackTrace( System.out );
} catch( IllegalAccessException e )
{
e.printStackTrace( System.out );
} catch( InvocationTargetException e )
{
if( e.getCause() instanceof LeafRuntimeException )
{
throw (LeafRuntimeException) e.getCause();
}
else
{
e.printStackTrace( System.out );
}
}
return result;
}
private void addListenerForField( ActionActivation annotation, Field field, Object instance )
{
if( instance instanceof JFrame || instance instanceof JPanel )
{
try
{
Object value = field.get( instance );
if( value instanceof BaseTable )
{
((BaseTable) value).getSelectionModel().addListSelectionListener( this );
}
else if( value instanceof JTree )
{
((JTree) value).addTreeSelectionListener( this );
}
else if( value instanceof JButton )
{
((JButton) value).addActionListener( this );
}
else if( value instanceof LeafInputField )
{
((LeafInputField) value).addPropertyChangeListener( this );
}
} catch( IllegalAccessException e )
{
e.printStackTrace( System.out );
} catch( NullPointerException e )
{
e.printStackTrace( System.out );
}
}
}
private Object getArgumentListSelectionEvent( String actionName, ListSelectionEvent event )
{
Object source = event.getSource();
List<Object> allComponents = mapWindowOnSelectFieldByActionName.get( actionName );
for( Object component : allComponents )
{
if( component instanceof BaseTable && ((BaseTable) component).getSelectionModel().equals( source ) )
{
int [] indexes = ((BaseTable) component).getSelectedRows();
if( indexes != null && indexes.length > 0 )
{
TableModel model = ((BaseTable) component).getModel();
if( model instanceof VectorTableModel )
{
if(indexes.length == 1 && indexes[0] > -1)
{
return ((ColumnizedMappable) ((VectorTableModel) model).getRowAt( indexes[0] )).getID();
}
else
{
List<Object> allSelected = new ArrayList<Object>();
for(int i = 0; i < indexes.length; ++i)
{
allSelected.add( ((ColumnizedMappable) ((VectorTableModel) model).getRowAt( indexes[0] )).getID() );
}
return allSelected;
}
}
else if( model instanceof LeafTableModel )
{
if(indexes.length == 1 && indexes[0] > -1)
{
return ((LeafTableModel) model).getKey( indexes[0] );
}
else
{
List<Object> allSelected = new ArrayList<Object>();
for(int i = 0; i < indexes.length; ++i)
{
allSelected.add( ((LeafTableModel) model).getKey( indexes[0] ));
}
return allSelected;
}
}
}
}
}
return null;
}
private List<String> getActionListSelectionEvent( ListSelectionEvent event )
{
List<String> result = new ArrayList<String>();
if( event.getSource() instanceof DefaultListSelectionModel )
{
DefaultListSelectionModel model = (DefaultListSelectionModel) event.getSource();
BaseTable table = null;
for( List<Object> allComponents : mapWindowOnSelectFieldByActionName.values() )
{
// for each registered table
for( Object component : allComponents )
{
if( component instanceof BaseTable && ((BaseTable) component).getSelectionModel().equals( model ) )
{
table = (BaseTable) component;
}
if( table != null )
{
break;
}
}
if( table != null )
{
break;
}
}
Annotation an = mapAnnotationByObject.get( table );
if( an != null && an instanceof ActionActivation )
{
String[] actions = ((ActionActivation) an).onSelect();
for( String actionName : actions )
{
result.add( actionName );
}
}
}
return result;
}
// returns selected node
private Object getArgumentTreeSelectionEvent( String actionName, TreeSelectionEvent event )
{
List<Object> components = mapWindowOnSelectFieldByActionName.get( actionName );
for( Object component : components )
{
if( component instanceof JTree && event.getPath() != null )
{
Object[] nodes = event.getPath().getPath();
if( nodes != null && nodes.length > 0 )
{
return nodes[nodes.length - 1];
}
}
}
return null;
}
private List<String> getActionTreeSelectionEvent( TreeSelectionEvent event )
{
List<String> result = new ArrayList<String>();
Annotation an = mapAnnotationByObject.get( event.getSource() );
if( an != null && an instanceof ActionActivation )
{
String[] actions = ((ActionActivation) an).onSelect();
for( String actionName : actions )
{
result.add( actionName );
}
}
return result;
}
private List<String> getActionActionEvent( ActionEvent event )
{
List<String> result = new ArrayList<String>();
Annotation an = mapAnnotationByObject.get( event.getSource() );
if( an != null && an instanceof ActionActivation )
{
String[] actions = ((ActionActivation) an).onSelect();
for( String actionName : actions )
{
result.add( actionName );
}
}
return result;
}
private List<String> getActionsForPropertyChangeEvent( PropertyChangeEvent evt )
{
List<String> result = new ArrayList<String>();
Annotation an = mapAnnotationByObject.get( evt.getSource() );
if( an != null )
{
if( an instanceof ActionActivation )
{
if( evt.getSource() instanceof LeafInputField )
{
if( LeafInputField.PROPERTY_CHANGED_CONSTANT.equals( evt.getPropertyName() ) )
{
String[] actions = ((ActionActivation) an).onChange();
for( String actionName : actions )
{
result.add( actionName );
}
}
}
}
}
return result;
}
@Override
public void valueChanged( TreeSelectionEvent event )
{
List<String> actions = getActionTreeSelectionEvent( event );
for( String action : actions )
{
Object argument = getArgumentTreeSelectionEvent( action, event );
if( !runAction( action, argument ) )
{
break;
}
}
}
/**
* Listens to ListSelectionEvents
*/
@Override
public void valueChanged( ListSelectionEvent event )
{
if( !event.getValueIsAdjusting() )
{
List<String> actionNames = getActionListSelectionEvent( event );
for( String action : actionNames )
{
Object argument = getArgumentListSelectionEvent( action, event );
if( !runAction( action, argument ) )
{
break;
}
}
}
}
@Override
public void actionPerformed( ActionEvent event )
{
List<String> actionNames = getActionActionEvent( event );
if( actionNames.size() > 0 )
{
for( int i = 1; i < actionNames.size(); ++i )
{
runActionLater( actionNames.get( i ) );
}
runAction( actionNames.get( 0 ) );
}
}
@Override
public void propertyChange( PropertyChangeEvent evt )
{
List<String> actionNames = getActionsForPropertyChangeEvent( evt );
if( actionNames.size() > 0 )
{
for( int i = 1; i < actionNames.size(); ++i )
{
runActionLater( actionNames.get( i ) );
}
runAction( actionNames.get( 0 ) );
}
}
}
Loading…
Cancel
Save