You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.6 KiB

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.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextArea;
import siprp.ui.SIPRPFrame;
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( SIPRPFrame.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();
}
}