forked from Coded/SIPRP
git-svn-id: https://svn.coded.pt/svn/SIPRP@750 bb69d46d-e84e-40c8-a05a-06db0d633741
parent
99bc93789e
commit
e6ea2e43c4
@ -0,0 +1,491 @@
|
||||
package siprp.clientes;
|
||||
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.aacute;
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
|
||||
import static com.evolute.utils.strings.UnicodeLatin1Map.otilde;
|
||||
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 java.util.Vector;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
import org.apache.cayenne.CayenneDataObject;
|
||||
|
||||
import siprp.database.cayenne.objects.EmailPlanoDeActuacao;
|
||||
import siprp.database.cayenne.objects.Estabelecimentos;
|
||||
import siprp.database.cayenne.objects.Legislacao;
|
||||
import siprp.database.cayenne.objects.LegislacaoEstabelecimento;
|
||||
import siprp.database.cayenne.objects.PostoDeTrabalho;
|
||||
import siprp.database.cayenne.objects.PostoDeTrabalhoEstabelecimento;
|
||||
import siprp.database.cayenne.providers.PlanoActuacaoDAO;
|
||||
|
||||
import com.evolute.utils.dataui.ControllableComponent;
|
||||
import com.evolute.utils.tables.VectorTableModel;
|
||||
|
||||
public class EstabelecimentoPlanoActuacaoPanel extends JPanel implements ControllableComponent
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final Dimension PREFERRED_DIMENSION_EMAIL = new Dimension(200,0);
|
||||
|
||||
private static final Dimension PREFERRED_DIMENSION_LEGISLACAO = new Dimension(400,0);
|
||||
|
||||
private static final Dimension PREFERRED_DIMENSION_POSTO_TRABALHO = new Dimension(300,0);
|
||||
|
||||
private final VectorTableModel<EmailPlanoDeActuacao> modelEmail = new VectorTableModel<EmailPlanoDeActuacao>( new String[]{ "Endere" + ccedil + "o" } );
|
||||
|
||||
private final VectorTableModel<LegislacaoEstabelecimento> modelLegislacao = new VectorTableModel<LegislacaoEstabelecimento>( new String[]{ "Descri" + ccedil + atilde + "o" } );
|
||||
|
||||
private final VectorTableModel<PostoDeTrabalhoEstabelecimento> modelPostoTrabalho= new VectorTableModel<PostoDeTrabalhoEstabelecimento>( new String[]{ "Descri" + ccedil + atilde + "o" } );
|
||||
|
||||
private final JTable tableEmail = new JTable( modelEmail );
|
||||
|
||||
private final JTable tableLegislacao = new JTable( modelLegislacao );
|
||||
|
||||
private final JTable tablePostoTrabalho = new JTable( modelPostoTrabalho );
|
||||
|
||||
private final JScrollPane scrollEmail = new JScrollPane( tableEmail, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
|
||||
|
||||
private final JScrollPane scrollLegislacao = new JScrollPane( tableLegislacao, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
|
||||
|
||||
private final JScrollPane scrollPostoTrabalho = new JScrollPane( tablePostoTrabalho, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
|
||||
|
||||
private final JPanel panelEmail = new JPanel();
|
||||
|
||||
private final JPanel panelLegislacao = new JPanel();
|
||||
|
||||
private final JPanel panelPostoTrabalho = new JPanel();
|
||||
|
||||
private final JButton buttonEmailAdicionar = new JButton("Adicionar");
|
||||
|
||||
private final JButton buttonLegislacaoCriar = new JButton("Criar");
|
||||
|
||||
private final JButton buttonLegislacaoAdicionar = new JButton("Adicionar");
|
||||
|
||||
private final JButton buttonPostoTrabalhoCriar = new JButton("Criar");
|
||||
|
||||
private final JButton buttonPostoTrabalhoAdicionar = new JButton("Adicionar");
|
||||
|
||||
private final JButton buttonEmailRemover = new JButton("Remover");
|
||||
|
||||
private final JButton buttonLegislacaoRemover = new JButton("Remover");
|
||||
|
||||
private final JButton buttonPostoTrabalhoRemover = new JButton("Remover");
|
||||
|
||||
private final PlanoActuacaoDAO provider = new PlanoActuacaoDAO();
|
||||
|
||||
private Estabelecimentos estabelecimento = null;
|
||||
|
||||
public EstabelecimentoPlanoActuacaoPanel()
|
||||
{
|
||||
startupComponents();
|
||||
startupLayout();
|
||||
placeComponents();
|
||||
startupListeners();
|
||||
}
|
||||
|
||||
private void startupLayout()
|
||||
{
|
||||
startupEmailLayout();
|
||||
startupPostoTrabalhoLayout();
|
||||
startupLegislacaoLayout();
|
||||
double [] cols = new double[]{ TableLayout.FILL, TableLayout.FILL, TableLayout.PREFERRED };
|
||||
double [] rows = new double[]{ TableLayout.FILL };
|
||||
TableLayout layout = new TableLayout(cols,rows);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
this.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupEmailLayout()
|
||||
{
|
||||
double [] cols = new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL };
|
||||
double [] rows = new double[]{ TableLayout.FILL, TableLayout.MINIMUM };
|
||||
TableLayout layout = new TableLayout(cols,rows);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelEmail.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupPostoTrabalhoLayout()
|
||||
{
|
||||
double [] cols = new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL };
|
||||
double [] rows = new double[]{ TableLayout.FILL, TableLayout.MINIMUM };
|
||||
TableLayout layout = new TableLayout(cols,rows);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelPostoTrabalho.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupLegislacaoLayout()
|
||||
{
|
||||
double [] cols = new double[]{ TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL };
|
||||
double [] rows = new double[]{ TableLayout.FILL, TableLayout.MINIMUM };
|
||||
TableLayout layout = new TableLayout(cols,rows);
|
||||
layout.setHGap( 5 );
|
||||
layout.setVGap( 5 );
|
||||
panelLegislacao.setLayout( layout );
|
||||
}
|
||||
|
||||
private void startupComponents()
|
||||
{
|
||||
panelLegislacao.setPreferredSize( PREFERRED_DIMENSION_LEGISLACAO );
|
||||
panelLegislacao.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + otilde + "es" ) );
|
||||
|
||||
panelPostoTrabalho.setPreferredSize( PREFERRED_DIMENSION_POSTO_TRABALHO );
|
||||
panelPostoTrabalho.setBorder( BorderFactory.createTitledBorder( "Postos de trabalho" ) );
|
||||
|
||||
panelEmail.setPreferredSize( PREFERRED_DIMENSION_EMAIL );
|
||||
panelEmail.setBorder( BorderFactory.createTitledBorder( "E-mails" ) );
|
||||
|
||||
buttonEmailAdicionar.setEnabled( false );
|
||||
buttonEmailRemover.setEnabled( false );
|
||||
|
||||
buttonLegislacaoCriar.setEnabled( false );
|
||||
buttonLegislacaoAdicionar.setEnabled( false );
|
||||
buttonLegislacaoRemover.setEnabled( false );
|
||||
|
||||
buttonPostoTrabalhoCriar.setEnabled( false );
|
||||
buttonPostoTrabalhoAdicionar.setEnabled( false );
|
||||
buttonPostoTrabalhoRemover.setEnabled( false );
|
||||
}
|
||||
|
||||
private void placeComponents()
|
||||
{
|
||||
|
||||
panelEmail.add( scrollEmail, new TableLayoutConstraints( 0, 0, 2, 0 ) );
|
||||
panelEmail.add( buttonEmailAdicionar, new TableLayoutConstraints( 0, 1 ) );
|
||||
panelEmail.add( buttonEmailRemover, new TableLayoutConstraints( 1, 1 ) );
|
||||
|
||||
panelLegislacao.add( scrollLegislacao, new TableLayoutConstraints( 0, 0, 3, 0 ) );
|
||||
panelLegislacao.add( buttonLegislacaoCriar, new TableLayoutConstraints( 0, 1 ) );
|
||||
panelLegislacao.add( buttonLegislacaoAdicionar, new TableLayoutConstraints( 1, 1 ) );
|
||||
panelLegislacao.add( buttonLegislacaoRemover, new TableLayoutConstraints( 2, 1 ) );
|
||||
|
||||
panelPostoTrabalho.add( scrollPostoTrabalho, new TableLayoutConstraints( 0, 0, 3, 0 ) );
|
||||
panelPostoTrabalho.add( buttonPostoTrabalhoCriar, new TableLayoutConstraints( 0, 1 ) );
|
||||
panelPostoTrabalho.add( buttonPostoTrabalhoAdicionar, new TableLayoutConstraints( 1, 1 ) );
|
||||
panelPostoTrabalho.add( buttonPostoTrabalhoRemover, new TableLayoutConstraints( 2, 1 ) );
|
||||
|
||||
this.add( panelLegislacao, new TableLayoutConstraints( 0,0 ) );
|
||||
this.add( panelPostoTrabalho, new TableLayoutConstraints( 1,0 ) );
|
||||
this.add( panelEmail, new TableLayoutConstraints( 2,0 ) );
|
||||
|
||||
}
|
||||
|
||||
private void refresh()
|
||||
{
|
||||
clear();
|
||||
load();
|
||||
}
|
||||
|
||||
private void load()
|
||||
{
|
||||
if( estabelecimento != null )
|
||||
{
|
||||
modelEmail.setValues( new Vector<EmailPlanoDeActuacao>( estabelecimento.getEmailPlanoDeActuacaoArray() ) );
|
||||
modelLegislacao.setValues( new Vector<LegislacaoEstabelecimento>( estabelecimento.getLegislacaoEstabelecimentoArray() ) );
|
||||
modelPostoTrabalho.setValues( new Vector<PostoDeTrabalhoEstabelecimento>( estabelecimento.getPostoDeTrabalhoEstabelecimentoArray() ) );
|
||||
}
|
||||
}
|
||||
|
||||
private void startupListeners()
|
||||
{
|
||||
buttonEmailAdicionar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
adicionarEmail();
|
||||
}
|
||||
} );
|
||||
buttonEmailRemover.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
removeSelected(tableEmail);
|
||||
}
|
||||
} );
|
||||
buttonLegislacaoCriar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
criarLegislacao();
|
||||
}
|
||||
} );
|
||||
buttonLegislacaoAdicionar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
adicionarLegislacao();
|
||||
}
|
||||
} );
|
||||
buttonLegislacaoRemover.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
removeSelected(tableLegislacao);
|
||||
}
|
||||
} );
|
||||
buttonPostoTrabalhoCriar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
criarPostoTrabalho();
|
||||
}
|
||||
} );
|
||||
buttonPostoTrabalhoAdicionar.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
adicionarPostoTrabalho();
|
||||
}
|
||||
} );
|
||||
buttonPostoTrabalhoRemover.addActionListener( new ActionListener()
|
||||
{
|
||||
@Override
|
||||
public void actionPerformed( ActionEvent e )
|
||||
{
|
||||
removeSelected(tablePostoTrabalho);
|
||||
}
|
||||
} );
|
||||
tableEmail.getSelectionModel().addListSelectionListener( new ListSelectionListener()
|
||||
{
|
||||
@Override
|
||||
public void valueChanged( ListSelectionEvent e )
|
||||
{
|
||||
if( !e.getValueIsAdjusting() )
|
||||
{
|
||||
buttonEmailRemover.setEnabled( tableEmail.getSelectedRowCount() > 0 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
tableLegislacao.getSelectionModel().addListSelectionListener( new ListSelectionListener()
|
||||
{
|
||||
@Override
|
||||
public void valueChanged( ListSelectionEvent e )
|
||||
{
|
||||
if( !e.getValueIsAdjusting() )
|
||||
{
|
||||
buttonLegislacaoRemover.setEnabled( tableLegislacao.getSelectedRowCount() > 0 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
tablePostoTrabalho.getSelectionModel().addListSelectionListener( new ListSelectionListener()
|
||||
{
|
||||
@Override
|
||||
public void valueChanged( ListSelectionEvent e )
|
||||
{
|
||||
if( !e.getValueIsAdjusting() )
|
||||
{
|
||||
buttonPostoTrabalhoRemover.setEnabled( tablePostoTrabalho.getSelectedRowCount() > 0 );
|
||||
}
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
private void criarLegislacao()
|
||||
{
|
||||
String legislacao = JOptionPane.showInputDialog( "Criar legisla" + ccedil + atilde + "o" );
|
||||
Legislacao legislacaoObject = criarLegislacao( legislacao );
|
||||
if( legislacaoObject != null )
|
||||
{
|
||||
adicionarLegislacao( legislacaoObject );
|
||||
}
|
||||
}
|
||||
|
||||
private Legislacao criarLegislacao( String legislacao )
|
||||
{
|
||||
Legislacao result = null;
|
||||
if( legislacao != null )
|
||||
{
|
||||
if( legislacao.trim().length() > 0 )
|
||||
{
|
||||
legislacao = legislacao.trim();
|
||||
result = provider.createLegislacao( legislacao );
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "Legisla" + ccedil + atilde + "o inv" + aacute + "lida", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void adicionarLegislacao()
|
||||
{
|
||||
Legislacao [] legislacoes = provider.getAllLegislacoesNotInEstabelecimento( estabelecimento );
|
||||
Legislacao legislacao = (Legislacao) JOptionPane.showInputDialog(
|
||||
this,
|
||||
"Postos de trabalho",
|
||||
"Adicionar posto de trabalho", JOptionPane.QUESTION_MESSAGE, null, legislacoes, null );
|
||||
adicionarLegislacao( legislacao );
|
||||
}
|
||||
|
||||
private void adicionarLegislacao( Legislacao legislacao )
|
||||
{
|
||||
if( legislacao != null )
|
||||
{
|
||||
provider.addLegislacaoToEstabelecimento( legislacao, estabelecimento );
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private void criarPostoTrabalho()
|
||||
{
|
||||
String postoTrabalho = JOptionPane.showInputDialog( "Criar posto de trabalho" );
|
||||
PostoDeTrabalho postoTrabalhoObject = criarPostoTrabalho( postoTrabalho );
|
||||
if( postoTrabalhoObject != null )
|
||||
{
|
||||
adicionarPostoTrabalho( postoTrabalhoObject );
|
||||
}
|
||||
}
|
||||
|
||||
private PostoDeTrabalho criarPostoTrabalho( String postoTrabalho )
|
||||
{
|
||||
PostoDeTrabalho result = null;
|
||||
if( postoTrabalho != null )
|
||||
{
|
||||
if( postoTrabalho.trim().length() > 0 )
|
||||
{
|
||||
postoTrabalho = postoTrabalho.trim();
|
||||
result = provider.createPostoTrabalho( postoTrabalho );
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "Posto de trabalho inv" + aacute + "lido", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void adicionarPostoTrabalho()
|
||||
{
|
||||
PostoDeTrabalho [] postos = provider.getAllPostosNotInEstabelecimento( estabelecimento );
|
||||
PostoDeTrabalho postoDeTrabalho = (PostoDeTrabalho) JOptionPane.showInputDialog(
|
||||
this,
|
||||
"Postos de trabalho",
|
||||
"Adicionar posto de trabalho", JOptionPane.QUESTION_MESSAGE, null, postos, null );
|
||||
adicionarPostoTrabalho( postoDeTrabalho );
|
||||
}
|
||||
|
||||
private void adicionarPostoTrabalho( PostoDeTrabalho postoTrabalho )
|
||||
{
|
||||
if( postoTrabalho != null )
|
||||
{
|
||||
provider.addPostoTrabalhoToEstabelecimento( postoTrabalho, estabelecimento );
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private void adicionarEmail()
|
||||
{
|
||||
adicionarEmail( JOptionPane.showInputDialog( "Inserir e-mail" ) );
|
||||
}
|
||||
|
||||
private void adicionarEmail( String email )
|
||||
{
|
||||
if( email != null && email.trim().length() > 0 )
|
||||
{
|
||||
email = email.trim();
|
||||
boolean valid = email.toUpperCase().matches( "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}" );
|
||||
if( valid )
|
||||
{
|
||||
provider.addEmailToEstabelecimento( email, estabelecimento);
|
||||
refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
JOptionPane.showMessageDialog( this, "Endere" + ccedil + "o de e-mail inv" + aacute + "lido", "Erro", JOptionPane.ERROR_MESSAGE, null );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeSelected( JTable table )
|
||||
{
|
||||
VectorTableModel<? extends CayenneDataObject> model = (VectorTableModel<? extends CayenneDataObject>) table.getModel();
|
||||
int indexes [] = table.getSelectedRows();
|
||||
if( indexes != null && indexes.length > 0 )
|
||||
{
|
||||
for( int i = 0; i < indexes.length; ++i )
|
||||
{
|
||||
removeSelected( model, indexes[i] );
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private void removeSelected( VectorTableModel<? extends CayenneDataObject> model, int index )
|
||||
{
|
||||
provider.delete( model.getRowAt( index ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
modelEmail.clearAll();
|
||||
modelLegislacao.clearAll();
|
||||
modelPostoTrabalho.clearAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fill( Object value )
|
||||
{
|
||||
if( value != null )
|
||||
{
|
||||
if( value instanceof Integer )
|
||||
{
|
||||
estabelecimento = provider.getEstabelecimentoByID( (Integer) value );
|
||||
}
|
||||
setEnabled( true );
|
||||
}
|
||||
else
|
||||
{
|
||||
setEnabled( false );
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object save()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnabled( boolean enabled )
|
||||
{
|
||||
tableEmail.setEnabled( enabled );
|
||||
tableLegislacao.setEnabled( enabled );
|
||||
tablePostoTrabalho.setEnabled( enabled );
|
||||
buttonEmailAdicionar.setEnabled( enabled );
|
||||
buttonLegislacaoAdicionar.setEnabled( enabled );
|
||||
buttonPostoTrabalhoAdicionar.setEnabled( enabled );
|
||||
buttonLegislacaoCriar.setEnabled( enabled );
|
||||
buttonPostoTrabalhoCriar.setEnabled( enabled );
|
||||
tableEmail.clearSelection();
|
||||
tableLegislacao.clearSelection();
|
||||
tablePostoTrabalho.clearSelection();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,215 @@
|
||||
package siprp.clientes;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.AffineTransformOp;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.JFileChooser;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.evolute.utils.dataui.ControllableComponent;
|
||||
|
||||
public class ImagePanel extends JPanel implements MouseListener, ControllableComponent
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final int WIDTH_THUMBNAIL = 64;
|
||||
|
||||
public static final int HEIGHT_THUMBNAIL = 64;
|
||||
|
||||
public static final int WIDTH_FULL = 512;
|
||||
|
||||
public static final int HEIGHT_FULL = 512;
|
||||
|
||||
private final int w;
|
||||
|
||||
private final int h;
|
||||
|
||||
private boolean thumb = false;
|
||||
|
||||
private BufferedImage scaledImage = null;
|
||||
|
||||
private BufferedImage image = null;
|
||||
|
||||
public ImagePanel( boolean thumbnail )
|
||||
{
|
||||
thumb = thumbnail;
|
||||
w = thumb ? WIDTH_THUMBNAIL : WIDTH_FULL;
|
||||
h = thumb ? HEIGHT_THUMBNAIL : HEIGHT_FULL;
|
||||
this.setSize( new Dimension(w,h) );
|
||||
this.setPreferredSize( new Dimension(w,h) );
|
||||
this.setOpaque( false );
|
||||
this.setLayout( new BorderLayout() );
|
||||
this.addMouseListener( this );
|
||||
this.setToolTipText( "Alterar logotipo " );
|
||||
fill( null );
|
||||
}
|
||||
|
||||
public void fill( Object object )
|
||||
{
|
||||
byte[] array = (byte[]) object;
|
||||
image = null;
|
||||
scaledImage = null;
|
||||
try
|
||||
{
|
||||
if( array != null )
|
||||
{
|
||||
ByteArrayInputStream input = new ByteArrayInputStream( array );
|
||||
scaledImage = ImageIO.read( input );
|
||||
input = new ByteArrayInputStream( array );
|
||||
image = ImageIO.read( input );
|
||||
}
|
||||
} catch( IOException e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
private void changeImage( File file )
|
||||
{
|
||||
image = null;
|
||||
scaledImage = null;
|
||||
try
|
||||
{
|
||||
if( file != null && file.exists() )
|
||||
{
|
||||
scaledImage = ImageIO.read( file );
|
||||
image = ImageIO.read( file );
|
||||
}
|
||||
} catch( IOException e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
private void scaleImage()
|
||||
{
|
||||
if( scaledImage != null )
|
||||
{
|
||||
this.setBorder( BorderFactory.createEmptyBorder() );
|
||||
if( scaledImage.getWidth() > w )
|
||||
{
|
||||
AffineTransform af = AffineTransform.getScaleInstance( ( (double) w) / ( (double) scaledImage.getWidth() ), ( (double) w ) / ( (double) scaledImage.getWidth() ) );
|
||||
AffineTransformOp afOp = new AffineTransformOp( af, AffineTransformOp.TYPE_BILINEAR );
|
||||
scaledImage = afOp.filter( scaledImage, null );
|
||||
}
|
||||
if( scaledImage.getHeight() > h )
|
||||
{
|
||||
AffineTransform af = AffineTransform.getScaleInstance( ( (double) h) / ( (double) scaledImage.getHeight() ), ( (double) h ) / ( (double) scaledImage.getHeight() ) );
|
||||
AffineTransformOp afOp = new AffineTransformOp( af, AffineTransformOp.TYPE_BILINEAR );
|
||||
scaledImage = afOp.filter( scaledImage, null );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.setBorder( BorderFactory.createLineBorder( Color.GRAY, 1 ) );
|
||||
}
|
||||
}
|
||||
|
||||
private void refresh()
|
||||
{
|
||||
scaleImage();
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean imageUpdate( Image img, int infoflags, int x, int y, int w, int h )
|
||||
{
|
||||
boolean loaded = !imageUpdate( img, infoflags, x, y, w, h );
|
||||
if( !loaded )
|
||||
{
|
||||
refresh();
|
||||
}
|
||||
return !loaded;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent( Graphics g )
|
||||
{
|
||||
super.paintComponent( g );
|
||||
if( scaledImage != null )
|
||||
{
|
||||
int shiftX = ( w - scaledImage.getWidth() ) / 2;
|
||||
int shiftY = ( h - scaledImage.getHeight() ) / 2;
|
||||
g.drawImage( scaledImage, 0 + shiftX, 0 + shiftY, w - shiftX, h - shiftY, this );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked( MouseEvent e )
|
||||
{
|
||||
if( this.isEnabled() )
|
||||
{
|
||||
JFileChooser fc = new JFileChooser();
|
||||
int returned = fc.showOpenDialog( this );
|
||||
if( JFileChooser.APPROVE_OPTION == returned )
|
||||
{
|
||||
changeImage( fc.getSelectedFile() );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered( MouseEvent e )
|
||||
{
|
||||
this.setCursor( new Cursor( Cursor.HAND_CURSOR ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited( MouseEvent e )
|
||||
{
|
||||
this.setCursor( new Cursor( Cursor.DEFAULT_CURSOR ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed( MouseEvent e )
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased( MouseEvent e )
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
fill( null );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object save()
|
||||
{
|
||||
byte [] result = null;
|
||||
try
|
||||
{
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ImageIO.write( image, "PNG", out );
|
||||
result = out.toByteArray();
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue