Package net.raymanoz.ui

Source Code of net.raymanoz.ui.MissingPropertyDialog

package net.raymanoz.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;

import net.raymanoz.ui.helpers.PropertyDescriptionHelper;
import net.raymanoz.util.PlainProperties;
import net.raymanoz.util.Properties;

public class MissingPropertyDialog extends JDialog {

  /**
   *
   */
  private static final long serialVersionUID = -6986981670751708343L;
  private JTable table;
  private JButton btnCopyProperty;
  private JButton btnPasteValue;
  private JButton btnClearValue;
  final private String[] columns = new String[] { "Property Name", "Property", "Value" };
  private boolean modalResultOk = false;
  private DefaultTableModel defaultTableModel = new DefaultTableModel(null, columns) {
      private static final long serialVersionUID = 1L;

      public boolean isCellEditable(int row, int column) {
      return column > 1;
    }
  };
 
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
    try {
      final Set<String> values = new HashSet<String>();
      values.add("pwd_One");
      values.add("Two");
      values.add("PWD_Three");
      values.add("pwd Two");
      values.add("password.Ten");
      PlainProperties properties = new PlainProperties();
      properties.setProperty("acc_Three", "A Test Account Three");
      properties.setProperty("description.Two", "The Account Two");
      MissingPropertyDialog dialog = new MissingPropertyDialog(values, properties, " to enable checking of Scripts in DB");
      dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
      dialog.setVisible(true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private String valueAt(int row, int column) {
    Object value = defaultTableModel.getValueAt(row, column);
    if (value == null) return null;
    return value.toString();
  }
 
 
  public static Map<String, String> askForValues(Collection<String> values, Properties uMProperties, String title){
    MissingPropertyDialog dialog = new MissingPropertyDialog(values, uMProperties, title);
    dialog.setVisible(true);
    if (dialog.modalResultOk) return dialog.getResults();
    return null;
  }
 
  final static int nameColumn = 0;
  final static int descColumn = 1;
  final static int valueColumn = 2;
 
  Map<String, String> getResults(){
    Map<String, String> result = new HashMap<String, String>();
    for(int row = 0; row < defaultTableModel.getRowCount(); row++){
      result.put(valueAt(row, nameColumn), valueAt(row, valueColumn));
    }
    return result;
  }

  boolean isEmpty(String value){
    return (value == null)||(value.trim().isEmpty());
  }
 
  boolean allValuesPopulated(){
    for(int row = 0; row < defaultTableModel.getRowCount(); row++){
      if (isEmpty(valueAt(row, valueColumn))) return false;
    }
    return true;
  }
 
  final String lineTerminator = System.getProperty("line.separator");
 
  private String getAllPropertyNames(){
    StringBuilder builder = new StringBuilder();
    String sep = "";
    for(int row = 0; row < defaultTableModel.getRowCount(); row++){
      builder.append(sep);
      builder.append(helper.propertyDescription(valueAt(row, descColumn)));
      sep = lineTerminator;
    }
    return builder.toString();
  }

  private String getSelectPropertyName(){
    stopEditing();
    int selected = table.getSelectedRow();
    if (selected < 0) return null;
    return valueAt(selected, descColumn);
  }
 
  void stopEditing(){
    if (!table.isEditing()) return;
    table.getCellEditor().stopCellEditing();
  }
 
  boolean okToContinue(){
    stopEditing();
    boolean result = true;
    StringBuilder builder = new StringBuilder();
    for(int row = 0; row < defaultTableModel.getRowCount(); row++){
      if (isEmpty(valueAt(row, valueColumn))) {
        result = false;
        builder.append("\n");
        builder.append(valueAt(row, descColumn));
      }
    }
    if (!result) {
      return JOptionPane.showConfirmDialog(this, "No Value set for following parameters: " + builder.toString(),
          "Not all parameters have values set", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION;
    }
    return result;
  }
 
  void setButtonState(){
    boolean selected = table.getSelectedRow() >= 0;
    btnCopyProperty.setEnabled(selected);
    btnPasteValue.setEnabled(selected);
    btnClearValue.setEnabled(selected);
  }
 
  void copyToClipBoard(String text){
    if (text == null||text.trim().isEmpty()) return;
    StringSelection stringSelection = new StringSelection(text);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents( stringSelection, null );     
  }

  void pasteClipBoardToSelectedValue(){
    stopEditing();
    int selected = table.getSelectedRow();
    if (selected < 0) return;
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable contents = clipboard.getContents(null);
    if (contents == null||!contents.isDataFlavorSupported(DataFlavor.stringFlavor)) return;
    try {
      String value = (String)contents.getTransferData(DataFlavor.stringFlavor);
      defaultTableModel.setValueAt(value, selected, valueColumn);
    }
    catch (Exception ex){
      //highly unlikely since we are using a standard DataFlavor
      System.out.println(ex);
      ex.printStackTrace();
    }
  }
 
  void clearSelected(){
    stopEditing();
    int selected = table.getSelectedRow();
    if (selected < 0) return;
    defaultTableModel.setValueAt("", selected, valueColumn);
  }
 
  /**
   * Create the dialog.
   */
  final private PropertyDescriptionHelper helper;
  public MissingPropertyDialog(Collection<String> values, Properties uMigrateProperties, String title) {
    this.helper = new PropertyDescriptionHelper(uMigrateProperties);
    setTitle("Fill in Parameter Values " + title);
    for (String property: values) {
      defaultTableModel.addRow(new Object[]{property, helper.propertyDescription(property)""});
    }
    setModalityType(ModalityType.APPLICATION_MODAL);
    setBounds(100, 100, 773, 525);
    getContentPane().setLayout(new BorderLayout());
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    getContentPane().add(scrollPane);
    table = new JTable();
    table.setSurrendersFocusOnKeystroke(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setIntercellSpacing(new Dimension(10, 5));
    table.setRowHeight(30);
    table.setRowMargin(5);
    table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
    table.setModel(defaultTableModel);
    table.getColumnModel().getColumn(nameColumn).setMinWidth(0);
    table.getColumnModel().getColumn(nameColumn).setMaxWidth(5);
    table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
     
      @Override
      public void valueChanged(ListSelectionEvent arg0) {
        setButtonState();
      }
    });
    scrollPane.setViewportView(table);
    JPanel buttonPane = new JPanel();
    FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.RIGHT);
    fl_buttonPane.setVgap(7);
    fl_buttonPane.setHgap(10);
    buttonPane.setLayout(fl_buttonPane);
    getContentPane().add(buttonPane, BorderLayout.SOUTH);
    JButton okButton = new JButton("OK");
    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        if (okToContinue()){
          modalResultOk = true;
          dispose();
        }
      }
    });
    okButton.setActionCommand("OK");
    buttonPane.add(okButton);
    getRootPane().setDefaultButton(okButton);
    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        modalResultOk = false;
        dispose();
      }
    });
    cancelButton.setActionCommand("Cancel");
    buttonPane.add(cancelButton);
   
    JPanel panel = new JPanel();
    FlowLayout flowLayout_2 = (FlowLayout) panel.getLayout();
    flowLayout_2.setAlignment(FlowLayout.LEFT);
    flowLayout_2.setVgap(7);
    flowLayout_2.setHgap(7);
    getContentPane().add(panel, BorderLayout.NORTH);
   
    JButton btnNewButton = new JButton("Copy All Names");
    btnNewButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        copyToClipBoard(getAllPropertyNames());
      }
    });
    panel.add(btnNewButton);
   
    btnCopyProperty = new JButton("Copy Property");
    btnCopyProperty.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        copyToClipBoard(getSelectPropertyName());
      }
    });
    btnCopyProperty.setEnabled(false);
    panel.add(btnCopyProperty);
   
    btnPasteValue = new JButton("Paste Value");
    btnPasteValue.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        pasteClipBoardToSelectedValue();
      }
    });
    btnPasteValue.setEnabled(false);
    panel.add(btnPasteValue);
   
    btnClearValue = new JButton("Clear Value");
    btnClearValue.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        clearSelected();
      }
    });
    btnClearValue.setEnabled(false);
    panel.add(btnClearValue);
   
    JPanel panel_2 = new JPanel();
    FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
    flowLayout_1.setHgap(7);
    getContentPane().add(panel_2, BorderLayout.WEST);
   
    JPanel panel_1 = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel_1.getLayout();
    flowLayout.setHgap(7);
    getContentPane().add(panel_1, BorderLayout.EAST);
  }

}
TOP

Related Classes of net.raymanoz.ui.MissingPropertyDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.