forked from Coded/SIPRP
git-svn-id: https://svn.coded.pt/svn/SIPRP@858 bb69d46d-e84e-40c8-a05a-06db0d633741
parent
c131154353
commit
d10f7ee396
@ -1,4 +1,4 @@
|
|||||||
package siprp.higiene.gestao;
|
package siprp.higiene.gestao.legislacao;
|
||||||
|
|
||||||
import info.clearthought.layout.TableLayout;
|
import info.clearthought.layout.TableLayout;
|
||||||
import info.clearthought.layout.TableLayoutConstraints;
|
import info.clearthought.layout.TableLayoutConstraints;
|
||||||
@ -0,0 +1,442 @@
|
|||||||
|
package siprp.higiene.gestao.legislacao;
|
||||||
|
|
||||||
|
import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
|
||||||
|
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
|
||||||
|
import static com.evolute.utils.strings.UnicodeLatin1Map.iacute;
|
||||||
|
import info.clearthought.layout.TableLayout;
|
||||||
|
import info.clearthought.layout.TableLayoutConstraints;
|
||||||
|
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.awt.LayoutManager;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.beans.PropertyChangeEvent;
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.swing.BorderFactory;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTree;
|
||||||
|
import javax.swing.event.TreeSelectionEvent;
|
||||||
|
import javax.swing.event.TreeSelectionListener;
|
||||||
|
import javax.swing.tree.DefaultMutableTreeNode;
|
||||||
|
import javax.swing.tree.DefaultTreeModel;
|
||||||
|
import javax.swing.tree.TreePath;
|
||||||
|
import javax.swing.tree.TreeSelectionModel;
|
||||||
|
|
||||||
|
import leaf.data.Validator;
|
||||||
|
import leaf.ui.LeafButton;
|
||||||
|
import leaf.ui.LeafTextAreaEditor;
|
||||||
|
import leaf.ui.TreeTools;
|
||||||
|
import siprp.database.cayenne.objects.HsLegislacao;
|
||||||
|
import siprp.database.cayenne.objects.HsLegislacaoCategoria;
|
||||||
|
import siprp.logic.HigieneSegurancaLogic;
|
||||||
|
|
||||||
|
public class GerirLegislacaoPanel extends JPanel
|
||||||
|
{
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private final JPanel panelGeral = new JPanel();
|
||||||
|
|
||||||
|
private final JPanel panelCategorias = new JPanel();
|
||||||
|
|
||||||
|
private final JPanel panelButtonsGeral = new JPanel();
|
||||||
|
|
||||||
|
private final LeafButton buttonGeralCriar = new LeafButton( "Criar" );
|
||||||
|
|
||||||
|
private final LeafButton buttonGeralRemover = new LeafButton( "Remover" );
|
||||||
|
|
||||||
|
private final JPanel panelButtonsCategoriasCategoria = new JPanel();
|
||||||
|
|
||||||
|
private final LeafButton buttonCategoriaCriar = new LeafButton( "Criar" );
|
||||||
|
|
||||||
|
private final LeafButton buttonCategoriaEditar = new LeafButton( "Editar" );
|
||||||
|
|
||||||
|
private final LeafButton buttonCategoriaRemover = new LeafButton( "Remover" );
|
||||||
|
|
||||||
|
private final JPanel panelButtonsCategoriasLegislacao = new JPanel();
|
||||||
|
|
||||||
|
private final LeafButton buttonCategoriasLegislacaoCriar = new LeafButton( "Criar" );
|
||||||
|
|
||||||
|
private final LeafButton buttonCategoriasLegislacaoRemover = new LeafButton( "Remover" );
|
||||||
|
|
||||||
|
private final DefaultMutableTreeNode rootGeral = new DefaultMutableTreeNode();
|
||||||
|
|
||||||
|
private final DefaultTreeModel modelGeral = new DefaultTreeModel( rootGeral );
|
||||||
|
|
||||||
|
private final JTree treeGeral = new JTree( modelGeral );
|
||||||
|
|
||||||
|
private final JScrollPane scrollGeral = new JScrollPane( treeGeral );
|
||||||
|
|
||||||
|
private final DefaultMutableTreeNode rootCategorias = new DefaultMutableTreeNode();
|
||||||
|
|
||||||
|
private final DefaultTreeModel modelCategorias = new DefaultTreeModel( rootCategorias );
|
||||||
|
|
||||||
|
private final JTree treeCategorias = new JTree(modelCategorias);
|
||||||
|
|
||||||
|
private final JScrollPane scrollCategorias = new JScrollPane( treeCategorias );
|
||||||
|
|
||||||
|
private HsLegislacao selectedLegislacaoGeral = null;
|
||||||
|
|
||||||
|
private HsLegislacao selectedLegislacaoEspecifica = null;
|
||||||
|
|
||||||
|
private HsLegislacaoCategoria selectedCategoria = null;
|
||||||
|
|
||||||
|
private final LeafTextAreaEditor panelData = new LeafTextAreaEditor( new Validator<String>()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean isValid( String object )
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
|
||||||
|
public GerirLegislacaoPanel()
|
||||||
|
{
|
||||||
|
startupComponents();
|
||||||
|
startupLayout();
|
||||||
|
placeComponents();
|
||||||
|
setupListeners();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startupComponents()
|
||||||
|
{
|
||||||
|
panelGeral.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + atilde + "o de aplica" + ccedil + atilde + "o geral" ) );
|
||||||
|
panelButtonsGeral.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + atilde + "o" ) );
|
||||||
|
|
||||||
|
panelCategorias.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + atilde + "o espec" + iacute + "fica" ) );
|
||||||
|
panelButtonsCategoriasCategoria.setBorder( BorderFactory.createTitledBorder( "Categoria" ) );
|
||||||
|
panelButtonsCategoriasLegislacao.setBorder( BorderFactory.createTitledBorder( "Legisla" + ccedil + atilde + "o" ) );
|
||||||
|
|
||||||
|
treeCategorias.setRootVisible( false );
|
||||||
|
treeGeral.setRootVisible( false );
|
||||||
|
treeCategorias.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
|
||||||
|
treeGeral.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startupLayout()
|
||||||
|
{
|
||||||
|
LayoutManager layout = new TableLayout( new double[] {
|
||||||
|
TableLayout.FILL, TableLayout.FILL
|
||||||
|
}, new double[] {
|
||||||
|
TableLayout.FILL, TableLayout.FILL
|
||||||
|
} );
|
||||||
|
this.setLayout( layout );
|
||||||
|
|
||||||
|
layout = new TableLayout( new double[] {
|
||||||
|
TableLayout.MINIMUM, TableLayout.FILL
|
||||||
|
}, new double[] {
|
||||||
|
TableLayout.MINIMUM, TableLayout.FILL
|
||||||
|
} );
|
||||||
|
panelGeral.setLayout( layout );
|
||||||
|
|
||||||
|
layout = new TableLayout( new double[] {
|
||||||
|
TableLayout.MINIMUM, TableLayout.FILL
|
||||||
|
}, new double[] {
|
||||||
|
TableLayout.MINIMUM, TableLayout.MINIMUM, TableLayout.FILL
|
||||||
|
} );
|
||||||
|
panelCategorias.setLayout( layout );
|
||||||
|
|
||||||
|
layout = new GridLayout( 2, 1, 5, 5 );
|
||||||
|
panelButtonsGeral.setLayout( layout );
|
||||||
|
|
||||||
|
layout = new GridLayout( 3, 1, 5, 5 );
|
||||||
|
panelButtonsCategoriasCategoria.setLayout( layout );
|
||||||
|
|
||||||
|
layout = new GridLayout( 2, 1, 5, 5 );
|
||||||
|
panelButtonsCategoriasLegislacao.setLayout( layout );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void placeComponents()
|
||||||
|
{
|
||||||
|
|
||||||
|
panelButtonsGeral.add( buttonGeralCriar );
|
||||||
|
panelButtonsGeral.add( buttonGeralRemover );
|
||||||
|
|
||||||
|
panelButtonsCategoriasCategoria.add( buttonCategoriaCriar );
|
||||||
|
panelButtonsCategoriasCategoria.add( buttonCategoriaEditar );
|
||||||
|
panelButtonsCategoriasCategoria.add( buttonCategoriaRemover );
|
||||||
|
|
||||||
|
panelButtonsCategoriasLegislacao.add( buttonCategoriasLegislacaoCriar );
|
||||||
|
panelButtonsCategoriasLegislacao.add( buttonCategoriasLegislacaoRemover );
|
||||||
|
|
||||||
|
panelGeral.add( panelButtonsGeral, new TableLayoutConstraints( 0, 0 ) );
|
||||||
|
panelGeral.add( scrollGeral, new TableLayoutConstraints( 1, 0, 1, 1 ) );
|
||||||
|
|
||||||
|
panelCategorias.add( panelButtonsCategoriasCategoria, new TableLayoutConstraints( 0, 0 ) );
|
||||||
|
panelCategorias.add( panelButtonsCategoriasLegislacao, new TableLayoutConstraints( 0, 1 ) );
|
||||||
|
panelCategorias.add( scrollCategorias, new TableLayoutConstraints( 1, 0, 1, 2 ) );
|
||||||
|
|
||||||
|
this.add( panelGeral, new TableLayoutConstraints( 0, 0 ) );
|
||||||
|
this.add( panelCategorias, new TableLayoutConstraints( 0, 1 ) );
|
||||||
|
this.add( panelData, new TableLayoutConstraints( 1, 0, 1, 1 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupListeners()
|
||||||
|
{
|
||||||
|
treeCategorias.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void valueChanged( TreeSelectionEvent e )
|
||||||
|
{
|
||||||
|
TreePath selectionPath = e.getNewLeadSelectionPath();
|
||||||
|
if( selectionPath != null )
|
||||||
|
{
|
||||||
|
Object last = selectionPath.getLastPathComponent();
|
||||||
|
if( last instanceof DefaultMutableTreeNode )
|
||||||
|
{
|
||||||
|
Object userObject = ((DefaultMutableTreeNode)last).getUserObject();
|
||||||
|
if( userObject instanceof HsLegislacao )
|
||||||
|
{
|
||||||
|
selectedLegislacaoEspecifica = (HsLegislacao) userObject;
|
||||||
|
}
|
||||||
|
else if( userObject instanceof HsLegislacaoCategoria )
|
||||||
|
{
|
||||||
|
selectedCategoria = (HsLegislacaoCategoria) userObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
treeGeral.clearSelection();
|
||||||
|
selectedLegislacaoGeral = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selectedLegislacaoEspecifica = null;
|
||||||
|
selectedCategoria = null;
|
||||||
|
}
|
||||||
|
refreshData();
|
||||||
|
setEnable();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
treeGeral.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void valueChanged( TreeSelectionEvent e )
|
||||||
|
{
|
||||||
|
TreePath selectionPath = e.getNewLeadSelectionPath();
|
||||||
|
if( selectionPath != null )
|
||||||
|
{
|
||||||
|
Object last = selectionPath.getLastPathComponent();
|
||||||
|
if( last instanceof DefaultMutableTreeNode )
|
||||||
|
{
|
||||||
|
Object userObject = ((DefaultMutableTreeNode)last).getUserObject();
|
||||||
|
if( userObject instanceof HsLegislacao )
|
||||||
|
{
|
||||||
|
selectedLegislacaoGeral = (HsLegislacao) userObject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
treeCategorias.clearSelection();
|
||||||
|
selectedLegislacaoEspecifica = null;
|
||||||
|
selectedCategoria = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
selectedLegislacaoGeral = null;
|
||||||
|
}
|
||||||
|
refreshData();
|
||||||
|
setEnable();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
panelData.addPropertyChangeListener( LeafTextAreaEditor.ACTION_SAVE, new PropertyChangeListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void propertyChange( PropertyChangeEvent evt )
|
||||||
|
{
|
||||||
|
String newValue = (String) evt.getNewValue();
|
||||||
|
if( selectedLegislacaoEspecifica != null )
|
||||||
|
{
|
||||||
|
selectedLegislacaoEspecifica.setDescription( newValue );
|
||||||
|
selectedLegislacaoEspecifica.save();
|
||||||
|
}
|
||||||
|
else if( selectedLegislacaoGeral != null )
|
||||||
|
{
|
||||||
|
selectedLegislacaoGeral.setDescription( newValue );
|
||||||
|
selectedLegislacaoGeral.save();
|
||||||
|
}
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonCategoriaCriar.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
criarCategoria();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonCategoriaEditar.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
editarCategoria();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonCategoriaRemover.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
removerCategoria();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonCategoriasLegislacaoCriar.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
criarCategoriaLegislacao();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonCategoriasLegislacaoRemover.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
removerCategoriaLegislacao();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonGeralCriar.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
criarGeral();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonGeralRemover.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
removerGeral();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void criarCategoria()
|
||||||
|
{
|
||||||
|
String categoriaString = JOptionPane.showInputDialog( this, "Nova categoria", "" );
|
||||||
|
if( categoriaString != null )
|
||||||
|
{
|
||||||
|
HsLegislacaoCategoria novaCategoria = new HsLegislacaoCategoria();
|
||||||
|
novaCategoria.setDescription( categoriaString );
|
||||||
|
novaCategoria.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void editarCategoria()
|
||||||
|
{
|
||||||
|
if( selectedCategoria != null )
|
||||||
|
{
|
||||||
|
String categoriaString = JOptionPane.showInputDialog( this, "Nova categoria", selectedCategoria.getDescription() );
|
||||||
|
if( categoriaString != null )
|
||||||
|
{
|
||||||
|
selectedCategoria.setDescription( categoriaString );
|
||||||
|
selectedCategoria.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removerCategoria()
|
||||||
|
{
|
||||||
|
if( selectedCategoria != null )
|
||||||
|
{
|
||||||
|
selectedCategoria.setDeletedDate( new Date() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void criarCategoriaLegislacao()
|
||||||
|
{
|
||||||
|
if( selectedCategoria != null )
|
||||||
|
{
|
||||||
|
HsLegislacao novaLegislacao = new HsLegislacao();
|
||||||
|
novaLegislacao.setDescription( "" );
|
||||||
|
novaLegislacao.setToHsLegislacaoCategoria( selectedCategoria );
|
||||||
|
novaLegislacao.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removerCategoriaLegislacao()
|
||||||
|
{
|
||||||
|
if( selectedLegislacaoEspecifica != null )
|
||||||
|
{
|
||||||
|
selectedLegislacaoEspecifica.setDeletedDate( new Date() );
|
||||||
|
selectedLegislacaoEspecifica.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removerGeral()
|
||||||
|
{
|
||||||
|
if( selectedLegislacaoGeral != null )
|
||||||
|
{
|
||||||
|
selectedLegislacaoGeral.setDeletedDate( new Date() );
|
||||||
|
selectedLegislacaoGeral.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void criarGeral()
|
||||||
|
{
|
||||||
|
HsLegislacao legislacao = new HsLegislacao();
|
||||||
|
legislacao.setDescription( "" );
|
||||||
|
legislacao.save();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reload()
|
||||||
|
{
|
||||||
|
rootGeral.removeAllChildren();
|
||||||
|
rootCategorias.removeAllChildren();
|
||||||
|
TreeTools.merge( rootGeral, HigieneSegurancaLogic.getLegislacaoGeral() );
|
||||||
|
TreeTools.merge( rootCategorias, HigieneSegurancaLogic.getLegislacaoEspecifica() );
|
||||||
|
TreeTools.refreshTree( treeGeral, rootGeral );
|
||||||
|
TreeTools.refreshTree( treeCategorias, rootCategorias );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void refreshData()
|
||||||
|
{
|
||||||
|
buttonCategoriaEditar.setEnabled( false );
|
||||||
|
buttonCategoriaRemover.setEnabled( false );
|
||||||
|
buttonCategoriasLegislacaoCriar.setEnabled( false );
|
||||||
|
buttonCategoriasLegislacaoRemover.setEnabled( false );
|
||||||
|
buttonGeralRemover.setEnabled( false );
|
||||||
|
panelData.setEnabled( false );
|
||||||
|
if( selectedLegislacaoEspecifica != null )
|
||||||
|
{
|
||||||
|
panelData.setValue( selectedLegislacaoEspecifica.getDescription() );
|
||||||
|
panelData.setEnabled( true );
|
||||||
|
}
|
||||||
|
else if( selectedLegislacaoGeral != null )
|
||||||
|
{
|
||||||
|
panelData.setValue( selectedLegislacaoGeral.getDescription() );
|
||||||
|
panelData.setEnabled( true );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
panelData.setValue( null );
|
||||||
|
}
|
||||||
|
if( selectedCategoria != null )
|
||||||
|
{
|
||||||
|
buttonCategoriaEditar.setEnabled( true );
|
||||||
|
buttonCategoriaRemover.setEnabled( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setEnable()
|
||||||
|
{
|
||||||
|
buttonCategoriaEditar.setEnabled( selectedCategoria != null );
|
||||||
|
buttonCategoriaRemover.setEnabled( selectedCategoria != null );
|
||||||
|
buttonCategoriasLegislacaoCriar.setEnabled( selectedCategoria != null );
|
||||||
|
buttonCategoriasLegislacaoRemover.setEnabled( selectedLegislacaoEspecifica != null );
|
||||||
|
buttonGeralRemover.setEnabled( selectedLegislacaoGeral != null );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
package leaf.data;
|
||||||
|
|
||||||
|
public interface Validator<OBJECT_CLASS extends Object>
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if given object is valid
|
||||||
|
* @param object
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean isValid(OBJECT_CLASS object);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,143 @@
|
|||||||
|
package leaf.ui;
|
||||||
|
|
||||||
|
import info.clearthought.layout.TableLayout;
|
||||||
|
import info.clearthought.layout.TableLayoutConstraints;
|
||||||
|
|
||||||
|
import java.awt.Insets;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.event.CaretEvent;
|
||||||
|
import javax.swing.event.CaretListener;
|
||||||
|
|
||||||
|
import leaf.data.Validator;
|
||||||
|
|
||||||
|
public class LeafTextAreaEditor extends JPanel
|
||||||
|
{
|
||||||
|
|
||||||
|
public static final String ACTION_SAVE = "ACTION_SAVE";
|
||||||
|
|
||||||
|
public static final String ACTION_REVERT = "ACTION_REVERT";
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private static final String ICON_NAME_SAVE = "leaf/ui/icons/save.png";
|
||||||
|
|
||||||
|
private static final String ICON_NAME_REVERT = "leaf/ui/icons/revert.png";
|
||||||
|
|
||||||
|
private final LeafButton buttonSave = LeafIconButton.createButton( ICON_NAME_SAVE );
|
||||||
|
|
||||||
|
private final LeafButton buttonRevert = LeafIconButton.createButton( ICON_NAME_REVERT );
|
||||||
|
|
||||||
|
private final JTextArea fieldText = new JTextArea();
|
||||||
|
|
||||||
|
private final JScrollPane scroll = new JScrollPane(fieldText);
|
||||||
|
|
||||||
|
private final Validator<String> validator;
|
||||||
|
|
||||||
|
private String initialValue = "";
|
||||||
|
|
||||||
|
public LeafTextAreaEditor( Validator<String> validator )
|
||||||
|
{
|
||||||
|
|
||||||
|
this.validator = validator;
|
||||||
|
startupComponents();
|
||||||
|
startupLayout();
|
||||||
|
placeComponents();
|
||||||
|
setupListeners();
|
||||||
|
enableButtons( false );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue( String value )
|
||||||
|
{
|
||||||
|
this.initialValue = value == null ? "" : value;
|
||||||
|
fieldText.setText( initialValue );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startupComponents()
|
||||||
|
{
|
||||||
|
fieldText.setWrapStyleWord( true );
|
||||||
|
fieldText.setLineWrap( true );
|
||||||
|
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
|
||||||
|
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
|
||||||
|
buttonSave.setMargin( new Insets(0,0,0,0) );
|
||||||
|
buttonRevert.setMargin( new Insets(0,0,0,0) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startupLayout()
|
||||||
|
{
|
||||||
|
TableLayout layout = new TableLayout(
|
||||||
|
new double[]{ TableLayout.FILL, TableLayout.MINIMUM, TableLayout.MINIMUM },
|
||||||
|
new double[]{ TableLayout.MINIMUM, TableLayout.FILL } );
|
||||||
|
this.setLayout( layout );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void placeComponents()
|
||||||
|
{
|
||||||
|
this.add( scroll, new TableLayoutConstraints( 0, 1, 2, 1) );
|
||||||
|
this.add( buttonSave, new TableLayoutConstraints( 1, 0 ) );
|
||||||
|
this.add( buttonRevert, new TableLayoutConstraints( 2, 0 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupListeners()
|
||||||
|
{
|
||||||
|
buttonSave.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
buttonRevert.addActionListener( new ActionListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent e )
|
||||||
|
{
|
||||||
|
revert();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
fieldText.addCaretListener( new CaretListener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void caretUpdate( CaretEvent e )
|
||||||
|
{
|
||||||
|
enableButtons( !initialValue.equals( fieldText.getText() ) && ( validator == null || validator.isValid( fieldText.getText() ) ) );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void enableButtons( boolean enable )
|
||||||
|
{
|
||||||
|
buttonRevert.setEnabled( enable );
|
||||||
|
buttonSave.setEnabled( enable );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void save()
|
||||||
|
{
|
||||||
|
enableButtons( false );
|
||||||
|
firePropertyChange( ACTION_SAVE, initialValue, fieldText.getText() );
|
||||||
|
initialValue = fieldText.getText();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void revert()
|
||||||
|
{
|
||||||
|
fieldText.setText( initialValue );
|
||||||
|
enableButtons( false );
|
||||||
|
firePropertyChange( ACTION_REVERT, fieldText.getText(), initialValue );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEnabled( boolean enabled )
|
||||||
|
{
|
||||||
|
fieldText.setEnabled( enabled );
|
||||||
|
if( enabled )
|
||||||
|
{
|
||||||
|
fieldText.requestFocusInWindow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
After Width: | Height: | Size: 936 B |
|
After Width: | Height: | Size: 876 B |
Loading…
Reference in new issue