All Downloads are FREE. Search and download functionalities are using the official Maven repository.

decodes.gui.PropertyEditDialog Maven / Gradle / Ivy

Go to download

A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.

The newest version!
/*
 *  $Id$
 */
package decodes.gui;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.opendcs.gui.GuiConstants;
import org.opendcs.gui.PasswordWithShow;

import decodes.dbeditor.TimeZoneSelector;
import decodes.util.PropertySpec;

import java.awt.event.*;
import java.io.File;
import java.util.EnumSet;
import java.util.ResourceBundle;

import ilex.util.AsciiUtil;
import ilex.util.EnvExpander;
import ilex.util.StringPair;
import ilex.util.TextUtil;

/**
 * Dialog for editing a single property name and value.
 */
@SuppressWarnings("serial")
public class PropertyEditDialog extends GuiDialog
{
	private static ResourceBundle genericLabels = PropertiesEditDialog.getGenericLabels();
	private JButton okButton = new JButton();
	private JButton cancelButton = new JButton();
	private String name, value;
	private JComponent valueField = new JTextField();
	private BorderLayout borderLayout1 = new BorderLayout();
	private JTextField nameField = new JTextField();
	private boolean changed;
	private PropertySpec propSpec = null;
	private static JFileChooser fileChooser = null;
	private JTextArea descArea = new JTextArea();

	/**
	 * Construct dialog with frame owner.
	 * 
	 * @param owner the owner frame
	 * @param name the property name
	 * @param value the property value edited in a JTextField
	 */
	public PropertyEditDialog(JFrame owner, String name, String value)
	{
		this(owner, name, value, null);
	}
	
	/**
	 * Construct dialog with frame owner.
	 * 
	 * @param owner the owner frame
	 * @param name the property name
	 * @param value the property value edited in a JTextField
	 * @param propSpec the Specification for this property
	 */
	public PropertyEditDialog(JFrame owner, String name, String value,
		PropertySpec propSpec)
	{
		super(owner, "Edit Property", true);
		this.setTitle(genericLabels
			.getString("PropertyEditDialog.editProperty"));
		this.propSpec = propSpec;
		init(name, value);
	}

	/**
	 * Construct dialog with dialog owner.
	 * 
	 * @param owner the owner dialog
	 * @param name the property name
	 * @param value the property value edited in a JTextField
	 */
	public PropertyEditDialog(JDialog owner, String name, String value)
	{
		this(owner, name, value, null);
	}
	
	/**
	 * Construct dialog with dialog owner.
	 * 
	 * @param owner the owner dialog
	 * @param name the property name
	 * @param value the property value edited in a JTextField
	 * @param propSpec the Specification for this property
	 */
	public PropertyEditDialog(JDialog owner, String name, String value,
		PropertySpec propSpec)
	{
		super(owner, "Edit Property", true);
		this.setTitle(genericLabels
			.getString("PropertyEditDialog.editProperty"));
		this.propSpec = propSpec;
		init(name, value);
	}
	

	/**
	 * Post-Initialize dialog with name & value.
	 * 
	 * @param name the property name
	 * @param value the property value edited in a JTextField
	 */
	private void init(String name, String value)
	{
		this.name = name;
		this.value = value;
		changed = false;
		try
		{
			jbInit();
			getRootPane().setDefaultButton(okButton);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		nameField.setText(name);
		setValue(value);
		pack();
		
		addWindowListener(new WindowAdapter()
		{
			boolean started = false;

			public void windowActivated(WindowEvent e)
			{
				if (!started)
					nameField.requestFocus();
				started = true;
			}
		});
	}

	/** Initializes gui components. */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	void jbInit() throws Exception
	{
		setTitle(genericLabels.getString("PropertyEditDialog.editPropertyValue"));
		JPanel mainPanel = new JPanel(borderLayout1);
		getContentPane().add(mainPanel);
		JPanel southButtonPanel = 
			new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
		mainPanel.add(southButtonPanel, BorderLayout.SOUTH);

		okButton.setText(genericLabels.getString("PropertiesEditDialog.OK"));
		okButton.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				okPressed();
			}
		});
		cancelButton.setText(genericLabels.getString("cancel"));
		cancelButton.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				cancelPressed();
			}
		});
		southButtonPanel.add(okButton, null);
		southButtonPanel.add(cancelButton, null);

		JPanel centerPropPanel = new JPanel(new GridBagLayout());
		centerPropPanel.add(new JLabel(genericLabels.getString("name") + ":"), 
			new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.NONE, 
				new Insets(10, 10, 5, 2), 0, 0));
		centerPropPanel.add(nameField, 
			new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
				new Insets(10, 0, 5, 10), 0, 0));
		
		centerPropPanel.add(new JLabel(genericLabels.getString("PropertyEditDialog.value")), 
			new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.NONE, 
				new Insets(5, 10, 5, 2), 0, 0));

		if (name != null 
		 && name.toLowerCase().contains("password")
		 && !name.equalsIgnoreCase("passwordCheckerClass"))
			valueField = new PasswordWithShow(GuiConstants.DEFAULT_PASSWORD_WITH);
		else if (propSpec != null)
		{
			if (propSpec.getType().equals(PropertySpec.BOOLEAN))
			{
				valueField = new JComboBox(new String[] { "", "True", "False" });
			}
			else if (propSpec.getType().equals(PropertySpec.TIMEZONE))
				valueField = new TimeZoneSelector();
			else if (propSpec.getType().startsWith(PropertySpec.DECODES_ENUM))
			{
				String enumName = propSpec.getType().substring(2);
				EnumComboBox ecb = new EnumComboBox(enumName, "");
				if (ecb.getModel().getSize() > 0)
					valueField = ecb;
			}
			else if (propSpec.getType().equals(PropertySpec.FILENAME)
				 || propSpec.getType().equals(PropertySpec.DIRECTORY))
			{
				JButton selectButton = new JButton(genericLabels.getString("select"));
				selectButton.addActionListener(new java.awt.event.ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						selectPressed();
					}
				});
				centerPropPanel.add(selectButton, 
					new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0,
						GridBagConstraints.WEST, GridBagConstraints.NONE,
						new Insets(5, 5, 5, 10), 0, 0));
			}
			else if (propSpec.getType().startsWith(PropertySpec.JAVA_ENUM))
			{
				String enumName = propSpec.getType().substring(2);
				ClassLoader cl = Thread.currentThread().getContextClassLoader();
				Class enumClass = cl.loadClass(enumName);
				EnumSet es = EnumSet.allOf(enumClass);
				Object ov[] = es.toArray();
				String enumValues[] = new String[ov.length];
				for(int i = 0; i");
				for(int i=0; i");
					else
						sb.append(descText.charAt(i));
				JLabel da = new JLabel(sb.toString());
				da.setBorder(new LineBorder(Color.gray));
				descComp = da;
			}
			else // It is dynamic
			{
//				nameField.setEditable(true);
				descArea.setText(propSpec.getDescription());
				descArea.setLineWrap(true);
				descArea.setWrapStyleWord(true);
				descArea.setEditable(true);
				JScrollPane jsp = new JScrollPane(descArea,
					JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
					JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jsp.setPreferredSize(new Dimension(190, 66));
				jsp.setBorder(new TitledBorder("Description"));
				descComp = jsp;
			}

			centerPropPanel.add(descComp, new GridBagConstraints(0, 2, 3, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(20, 10, 20, 10), 0, 0));
		}
	}

	protected void selectPressed()
	{
		if (fileChooser == null)
		{
			fileChooser = new JFileChooser(
				EnvExpander.expand("$DECODES_INSTALL_DIR"));
		}
		if (propSpec.getType().equals(PropertySpec.DIRECTORY))
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		else if (propSpec.getType().equals(PropertySpec.FILENAME))
			fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		if (fileChooser.showDialog(this, 
			genericLabels.getString("select")) == JFileChooser.APPROVE_OPTION)
		{
			File f = fileChooser.getSelectedFile();
			setValue(f.getPath());
		}
	}

	/**
	 * Called when 'OK' is pressed.
	 */
	void okPressed()
	{
		if (!name.equals(nameField.getText()))
		{
			name = nameField.getText().trim();
			if (name.length() == 0)
			{
				showError("Name cannot be empty.");
				return;
			}
			changed = true;
		}
		String nv = getValueText().trim();
		if (nv.length() > 0 && propSpec != null)
		{
			if (propSpec.getType().equals(PropertySpec.INT))
			{
				try { Long.parseLong(nv); }
				catch(Exception ex)
				{
					showError("Invalid integer value '" + nv + "'!");
					return;
				}
			}
			else if (propSpec.getType().equals(PropertySpec.NUMBER))
			{
				try { Double.parseDouble(nv); }
				catch(Exception ex)
				{
					showError("Invalid number value '" + nv + "'!");
					return;
				}
			}
			else if (propSpec.getType().equals(PropertySpec.HOSTNAME))
			{
				for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy