Package boco.ejb3PlugIn.gui

Source Code of boco.ejb3PlugIn.gui.RolesSelectionPanel

/***************************************************************************
* Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net  *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  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 Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/

/**
- $Header: /usr/local/cvslocalrepository/EJB3PlugIn/src/boco/ejb3PlugIn/gui/Attic/RolesSelectionPanel.java,v 1.1.2.7 2008/06/02 20:04:32 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.7 $
- $Date: 2008/06/02 20:04:32 $


- $Log: RolesSelectionPanel.java,v $
- Revision 1.1.2.7  2008/06/02 20:04:32  fab
- Eliminate System.out
-
- Revision 1.1.2.6  2008/04/22 06:17:37  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.5  2008/04/19 10:26:49  fab
- Aggiornamento riferimenti licenza
-
-
**/

package boco.ejb3PlugIn.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class RolesSelectionPanel extends JPanel implements ListSelectionListener
{

  /**
   *
   */
  private static final long  serialVersionUID        = 1L;

  private String             projectPath;

  private JList             jListAvailableRoles;
  private JEditorPane       jEditorCondition;
  private JScrollPane       jScrollPaneCondition;
  private JLabel             jLabel2;
  private JScrollPane       jScrollPane2;
  private JScrollPane       jScrollPane1;
  private JLabel             jLabel1;
  private JButton          jButtonRemove;
  private JButton          jButtonAdd;
  private JPanel             jPanelButtons;
  private JList             jListSelectedRoles;

  private DefaultListModel  availableRolesDataModel  = new DefaultListModel();
  private DefaultListModel  selectedRolesDataModel  = new DefaultListModel();

  public RolesSelectionPanel(String projectPath, Vector<String> selectedRoles)
  {
    this.projectPath = projectPath;

    try
    {
      {
        this.setPreferredSize(new java.awt.Dimension(100, 100));
        GridBagLayout thisLayout = new GridBagLayout();
        this.setSize(100, 100);
        thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1};
        thisLayout.rowHeights = new int[] {7, 7, 7, 7, 20};
        thisLayout.columnWeights = new double[] {0.4, 0.1, 0.4};
        thisLayout.columnWidths = new int[] {59, 7, 7};
        this.setLayout(thisLayout);
        this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        {
          jScrollPane1 = new JScrollPane();
          this.add(jScrollPane1, new GridBagConstraints(0, 1, 1, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
          {
            // ListModel jListAvailableRolesModel = new DefaultComboBoxModel(new String[] {"Item One", "Item Two"});
            ListModel jListAvailableRolesModel = new DefaultComboBoxModel(new String[] {});
            jListAvailableRoles = new JList();
            jScrollPane1.setViewportView(jListAvailableRoles);
            jListAvailableRoles.setModel(jListAvailableRolesModel);
            jListAvailableRoles.setFont(new java.awt.Font("Dialog", 0, 10)); //$NON-NLS-1$
          }
        }
        {
          jScrollPane2 = new JScrollPane();
          this.add(jScrollPane2, new GridBagConstraints(2, 1, 1, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
          {
            ListModel jListSelectedRolesModel = new DefaultComboBoxModel(new String[] {});
            jListSelectedRoles = new JList();
            jScrollPane2.setViewportView(jListSelectedRoles);
            jListSelectedRoles.setModel(jListSelectedRolesModel);
            jListSelectedRoles.setFont(new java.awt.Font("Dialog", 0, 10)); //$NON-NLS-1$
            jListSelectedRoles.addListSelectionListener(this);
            jListSelectedRoles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            jListSelectedRoles.setSelectionInterval(0, 1000);
          }
        }
        {
          jPanelButtons = new JPanel();
          GridLayout jPanelButtonsLayout = new GridLayout(2, 1);
          jPanelButtonsLayout.setColumns(1);
          jPanelButtonsLayout.setHgap(5);
          jPanelButtonsLayout.setVgap(5);
          jPanelButtonsLayout.setRows(2);
          jPanelButtons.setLayout(jPanelButtonsLayout);
          this.add(jPanelButtons, new GridBagConstraints(1, 1, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
          {
            jButtonAdd = new JButton();
            jPanelButtons.add(jButtonAdd);
            jButtonAdd.setText(">"); //$NON-NLS-1$
            jButtonAdd.addActionListener(new ActionListener()
            {

              public void actionPerformed(ActionEvent evt)
              {
                jButtonAddActionPerformed(evt);
              }
            });
          }
          {
            jButtonRemove = new JButton();
            jPanelButtons.add(jButtonRemove);
            jButtonRemove.setText("<"); //$NON-NLS-1$
            jButtonRemove.addActionListener(new ActionListener()
            {

              public void actionPerformed(ActionEvent evt)
              {
                jButtonRemoveActionPerformed(evt);
              }
            });
          }
        }
        {
          jLabel1 = new JLabel();
          this.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
          jLabel1.setText(Messages.getString("BeanDialog.availableRoles")); //$NON-NLS-1$
          jLabel1.setFont(new java.awt.Font("Dialog", 1, 10)); //$NON-NLS-1$
        }
        {
          jLabel2 = new JLabel();
          this.add(jLabel2, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
          this.add(getJScrollPaneCondition(), new GridBagConstraints(0, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
          jLabel2.setText(Messages.getString("BeanDialog.selectedRoles")); //$NON-NLS-1$
          jLabel2.setFont(new java.awt.Font("Dialog", 1, 10)); //$NON-NLS-1$
        }
      }

      if (projectPath != null && !projectPath.equals("")) //$NON-NLS-1$
      {
        String role;

        File f = new File(projectPath + File.separator + "resources" + File.separator + "roles.txt"); //$NON-NLS-1$ //$NON-NLS-2$

        LineNumberReader br = new LineNumberReader(new FileReader(projectPath + File.separator + "resources" + File.separator + "roles.txt")); //$NON-NLS-1$ //$NON-NLS-2$

        while ((role = br.readLine()) != null)
        {
          if (!selectedRoles.contains(role))
            availableRolesDataModel.addElement(role);
          else
            selectedRolesDataModel.addElement(role);
        }

        br.close();

        jListAvailableRoles.setModel(availableRolesDataModel);
        jListAvailableRoles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jListAvailableRoles.invalidate();
        jListAvailableRoles.repaint();
      }

      jListSelectedRoles.setModel(selectedRolesDataModel);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jButtonAddActionPerformed(ActionEvent evt)
  {
    int idx = jListAvailableRoles.getSelectedIndex();

    if (idx != -1)
    {
      String selectedRole = (String) availableRolesDataModel.get(idx);

      availableRolesDataModel.remove(idx);
      jListAvailableRoles.setSelectedIndex(idx);
      jListAvailableRoles.ensureIndexIsVisible(idx);

      selectedRolesDataModel.addElement(selectedRole);
      jListSelectedRoles.setSelectedIndex(selectedRolesDataModel.getSize());
      jListSelectedRoles.ensureIndexIsVisible(selectedRolesDataModel.getSize());
    }
  }

  private void jButtonRemoveActionPerformed(ActionEvent evt)
  {
    int idx = jListSelectedRoles.getSelectedIndex();

    if (idx != -1)
    {
      String selectedRole = (String) selectedRolesDataModel.get(idx);

      selectedRolesDataModel.remove(idx);
      jListSelectedRoles.setSelectedIndex(idx);
      jListSelectedRoles.ensureIndexIsVisible(idx);

      availableRolesDataModel.addElement(selectedRole);
      jListAvailableRoles.setSelectedIndex(availableRolesDataModel.getSize());
      jListAvailableRoles.ensureIndexIsVisible(availableRolesDataModel.getSize());
    }
  }

  /** **/

  public Vector<String> getSelectedRoles()
  {
    Vector<String> ret = new Vector<String>();

    for (int i = 0; i < selectedRolesDataModel.size(); i++)
    {
      ret.add(selectedRolesDataModel.get(i).toString());
    }

    return ret;
  }

  public String getSelectedRolesAsString()
  {
    String ret = ""; //$NON-NLS-1$

    for (int i = 0; i < selectedRolesDataModel.size(); i++)
    {
      if (i > 0)
        ret += ","; //$NON-NLS-1$

      ret += "\"" + selectedRolesDataModel.get(i) + "\""; //$NON-NLS-1$ //$NON-NLS-2$
    }

    return ret;
  }

  public void setSelectedRoles(Vector<String> selectedRoles)
  {
    if (selectedRoles==null)
      return;
   
    File f = new File(projectPath + File.separator + "resources" + File.separator + "roles.txt"); //$NON-NLS-1$ //$NON-NLS-2$

    for (int i = 0; i < selectedRoles.size(); i++)
    {
      availableRolesDataModel.removeElement(selectedRoles.get(i));
      selectedRolesDataModel.addElement(selectedRoles.get(i));
    }

    jListAvailableRoles.setModel(availableRolesDataModel);
    jListAvailableRoles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jListAvailableRoles.invalidate();
    jListAvailableRoles.repaint();

    jListSelectedRoles.setModel(selectedRolesDataModel);
    jListSelectedRoles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jListSelectedRoles.invalidate();
    jListSelectedRoles.repaint();
  }

  private JScrollPane getJScrollPaneCondition()
  {
    if (jScrollPaneCondition == null)
    {
      jScrollPaneCondition = new JScrollPane();
      jScrollPaneCondition.setViewportView(getJEditorCondition());
    }
    return jScrollPaneCondition;
  }

  public JEditorPane getJEditorCondition()
  {
    if (jEditorCondition == null)
    {
      jEditorCondition = new JEditorPane();
    }
    return jEditorCondition;
  }

  public void valueChanged(ListSelectionEvent e)
   {
    if(this.jListSelectedRoles==null)
      return;
   
    if(this.jEditorCondition==null)
    {
     
      return;
    }
   
    if (e.getSource().equals(this.jListSelectedRoles))
    {
      if (jListSelectedRoles.getModel().getSize()>0)
      {
        //// System.out.println("Items "+jListSelectedRoles.getModel().getSize());
        this.jEditorCondition.setText("");
        this.jEditorCondition.setEnabled(false);
      }
      else
      {
        //// System.out.println("Items "+0);
        this.jEditorCondition.setEnabled(true);
      }
    }    
   }

  public String getCondition()
  {
    return jEditorCondition.getText();
  }
 
  public void setCondition(String condition)
  {
    jEditorCondition.setText(condition);
  }
}
TOP

Related Classes of boco.ejb3PlugIn.gui.RolesSelectionPanel

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.