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

org.tango.pogo.pogo_gui.PipeDialog Maven / Gradle / Ivy

//+======================================================================
// $Source:  $
//
// Project:   Tango
//
// Description:  Basic Dialog Class to display info
//
// $Author: pascal_verdier $
//
// Copyright (C) :      2004,2005,2006,2007,2008,2009,2009
//						European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Tango.  If not, see .
//
// $Revision:  $
//
// $Log:  $
//
//-======================================================================

package org.tango.pogo.pogo_gui;

import fr.esrf.tango.pogo.pogoDsl.Pipe;
import fr.esrf.tangoatk.widget.util.ATKGraphicsUtils;
import org.tango.pogo.pogo_gui.tools.OAWutils;
import org.tango.pogo.pogo_gui.tools.PogoException;
import org.tango.pogo.pogo_gui.tools.PopupTable;
import org.tango.pogo.pogo_gui.tools.Utils;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

//===============================================================
/**
 *	JDialog Class to display info
 *
 *	@author  Pascal Verdier
 */
//===============================================================


@SuppressWarnings("MagicConstant")
public class PipeDialog extends JDialog {

	private JFrame	parent;
	private int		returnValue = JOptionPane.OK_OPTION;
    private Pipe    pipe;
	//===============================================================
	/**
	 *	Creates new form PipeDialog
	 */
	//===============================================================
	public PipeDialog(JFrame parent, Pipe pipe)  {
		super(parent, true);
		this.parent = parent;
		this.pipe = pipe;
		initComponents();
		titleLabel.setText("Pipe definition");

        if (pipe!=null) {
            nameText.setText(pipe.getName());
            if (Utils.isSet(pipe.getLabel())) {
                labelText.setText(pipe.getLabel());
            }
            if (Utils.isSet(pipe.getDescription())) {
                descriptionText.setText(pipe.getDescription());
            }
            if (Utils.isSet(pipe.getDisplayLevel())) {
                expertBtn.setSelected(pipe.getDisplayLevel().contains("EXPERT"));
            } else {
                expertBtn.setSelected(false);
            }
            if (Utils.isSet(pipe.getRwType())) {
                writableBtn.setSelected(pipe.getRwType().contains("WRITE"));
            }
        }

		pack();
 		ATKGraphicsUtils.centerDialog(this);
	}

	//===============================================================
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
	//===============================================================
    // //GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        javax.swing.JPanel topPanel = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        javax.swing.JPanel centerPanel = new javax.swing.JPanel();
        javax.swing.JLabel nameLbl = new javax.swing.JLabel();
        nameText = new javax.swing.JTextField();
        javax.swing.JLabel descLabel = new javax.swing.JLabel();
        javax.swing.JScrollPane descriptionScrollPane = new javax.swing.JScrollPane();
        descriptionText = new javax.swing.JTextArea();
        javax.swing.JLabel labelLabel = new javax.swing.JLabel();
        labelText = new javax.swing.JTextField();
        writableBtn = new javax.swing.JRadioButton();
        expertBtn = new javax.swing.JRadioButton();
        javax.swing.JPanel bottomPanel = new javax.swing.JPanel();
        javax.swing.JButton okBtn = new javax.swing.JButton();
        javax.swing.JButton cancelBtn = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        titleLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        titleLabel.setText("Dialog Title");
        topPanel.add(titleLabel);

        getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);

        centerPanel.setLayout(new java.awt.GridBagLayout());

        nameLbl.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        nameLbl.setText("Pipe name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        centerPanel.add(nameLbl, gridBagConstraints);

        nameText.setColumns(15);
        nameText.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 10);
        centerPanel.add(nameText, gridBagConstraints);

        descLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        descLabel.setText("Description :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        centerPanel.add(descLabel, gridBagConstraints);

        descriptionScrollPane.setPreferredSize(new java.awt.Dimension(350, 100));

        descriptionText.setColumns(80);
        descriptionText.setRows(20);
        descriptionScrollPane.setViewportView(descriptionText);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        centerPanel.add(descriptionScrollPane, gridBagConstraints);

        labelLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        labelLabel.setText("Pipe Label:");
        labelLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        centerPanel.add(labelLabel, gridBagConstraints);

        labelText.setColumns(15);
        labelText.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 10);
        centerPanel.add(labelText, gridBagConstraints);

        writableBtn.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        writableBtn.setText("Writable");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 3);
        centerPanel.add(writableBtn, gridBagConstraints);

        expertBtn.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        expertBtn.setText("Expert Only");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 3);
        centerPanel.add(expertBtn, gridBagConstraints);

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        okBtn.setText("OK");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });
        bottomPanel.add(okBtn);

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });
        bottomPanel.add(cancelBtn);

        getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// //GEN-END:initComponents

	//===============================================================
	//===============================================================
    @SuppressWarnings("UnusedParameters")
	private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed

        //  Check inputs
        try {
            if (nameText.getText().trim().isEmpty())
                throw new PogoException("Pipe name ?");
            //  Build name without space if any
            String  name = Utils.checkNameSyntax(nameText.getText(), "name", false);
            nameText.setText(name);
        }
        catch (PogoException e) {
            e.popup(this);
            return;
        }
        returnValue = JOptionPane.OK_OPTION;
		doClose();
	}//GEN-LAST:event_okBtnActionPerformed

	//===============================================================
	//===============================================================
	@SuppressWarnings("UnusedParameters")
    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
		returnValue = JOptionPane.CANCEL_OPTION;
		doClose();
	}//GEN-LAST:event_cancelBtnActionPerformed

	//===============================================================
	//===============================================================
    @SuppressWarnings("UnusedParameters")
	private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
		returnValue = JOptionPane.CANCEL_OPTION;
		doClose();
	}//GEN-LAST:event_closeDialog

	//===============================================================
	/**
	 *	Closes the dialog
	 */
	//===============================================================
	private void doClose() {
	
		if (parent==null)
			System.exit(0);
		else {
			setVisible(false);
			dispose();
		}
	}
	//===============================================================
	//===============================================================
    public Pipe getPipe() {
        pipe = OAWutils.factory.createPipe();
        pipe.setName(nameText.getText().trim());
        pipe.setLabel(labelText.getText().trim());
        pipe.setDescription(descriptionText.getText().trim());
        if (writableBtn.isSelected()) {
            pipe.setRwType("READ_WRITE");
        } else {
        	pipe.setRwType("READ");
        }
        if (expertBtn.isSelected()) {
            pipe.setDisplayLevel("EXPERT");
        } else {
        	pipe.setDisplayLevel("OPERATOR");
        }
        return pipe;
    }
	//===============================================================
	//===============================================================
	public int showDialog() {
		setVisible(true);
		return returnValue;
	}

	//===============================================================
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea descriptionText;
    private javax.swing.JRadioButton expertBtn;
    private javax.swing.JTextField labelText;
    private javax.swing.JTextField nameText;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JRadioButton writableBtn;
    // End of variables declaration//GEN-END:variables
	//===============================================================




    //===============================================================
    /**
     *	Manage the popup summary methods
     */
    //===============================================================
    private static int[] columnSize = {
            120, 150, 40, 80, 400
    };
    private static String[] columnTitle = {
            "Name",
            "Label",
            "Writable",
            "Level",
            "Description"
    };

    //===============================================================
    //===============================================================
    public static void popupSummary(JFrame parent, List pipeList) {
        List> summary = buildSummary(pipeList);
        String title = Integer.toString(pipeList.size()) + "  Pipes";

        PopupTable popupTable = new PopupTable(parent, title, columnTitle, summary);
        int nb = pipeList.size();
        if (nb > 35) nb = 35;
        popupTable.setPreferredSize(columnSize, nb);
        popupTable.setVisible(true);
    }

    //===============================================================
    //===============================================================
    private static List> buildSummary(List pipeList) {
        List> result = new ArrayList<>();
        for (Pipe pipe : pipeList) {
            List line = new ArrayList<>();
            line.add(pipe.getName());
            line.add(pipe.getLabel());
            line.add(""+pipe.getRwType().contains("WRITE"));
            line.add(pipe.getDisplayLevel());
            line.add(Utils.strReplace(pipe.getDescription(), "\\n", "\n"));
            result.add(line);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy