Package org.pentaho.reporting.designer.core.editor.groups

Source Code of org.pentaho.reporting.designer.core.editor.groups.EditGroupDetailsDialog$AddSelectionAction

/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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 Lesser General Public License for more details.
*
* Copyright (c) 2009 Pentaho Corporation.  All rights reserved.
*/

package org.pentaho.reporting.designer.core.editor.groups;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.pentaho.reporting.designer.core.Messages;
import org.pentaho.reporting.designer.core.editor.ReportRenderContext;
import org.pentaho.reporting.designer.core.util.IconLoader;
import org.pentaho.reporting.designer.core.util.undo.EditGroupUndoEntry;
import org.pentaho.reporting.engine.classic.core.RelationalGroup;
import org.pentaho.reporting.engine.classic.core.modules.gui.commonswing.SwingUtil;
import org.pentaho.reporting.libraries.designtime.swing.BorderlessButton;
import org.pentaho.reporting.libraries.designtime.swing.CommonDialog;
import org.pentaho.reporting.libraries.designtime.swing.bulk.DefaultBulkListModel;
import org.pentaho.reporting.libraries.designtime.swing.bulk.RemoveBulkAction;
import org.pentaho.reporting.libraries.designtime.swing.bulk.SortBulkDownAction;
import org.pentaho.reporting.libraries.designtime.swing.bulk.SortBulkUpAction;

/**
* Todo: Document Me
*
* @author Thomas Morgner
*/
public class EditGroupDetailsDialog extends CommonDialog
{
  private class AddSelectionAction extends AbstractAction implements ListSelectionListener
  {
    private ListSelectionModel selectionModel;

    /**
     * Defines an <code>Action</code> object with a default description string and default icon.
     */
    private AddSelectionAction(final ListSelectionModel selectionModel)
    {
      this.selectionModel = selectionModel;
      putValue(Action.SMALL_ICON, IconLoader.getInstance().getFowardArrowIcon());
      putValue(Action.SHORT_DESCRIPTION, "EditGroupDetailsDialog.AddColumn");
      selectionModel.addListSelectionListener(this);
      setEnabled(selectionModel.isSelectionEmpty() == false);
    }

    public void valueChanged(final ListSelectionEvent e)
    {
      setEnabled(selectionModel.isSelectionEmpty() == false);
    }

    /**
     * Invoked when an action occurs.
     */
    public void actionPerformed(final ActionEvent e)
    {
      final DefaultListModel data = getGroupFieldsModel();
      final DefaultListModel fields = getAvailableFieldsModel();
      for (int i = 0; i < fields.getSize(); i++)
      {
        if (selectionModel.isSelectedIndex(i))
        {
          data.addElement(fields.getElementAt(i));
        }
      }
    }
  }

  private DefaultBulkListModel availableFieldsModel;
  private DefaultBulkListModel groupFieldsModel;

  private JTextField nameTextField;

  public EditGroupDetailsDialog()
      throws HeadlessException
  {
    init();
  }

  public EditGroupDetailsDialog(final Frame owner)
      throws HeadlessException
  {
    super(owner);
    init();
  }

  public EditGroupDetailsDialog(final Dialog owner)
      throws HeadlessException
  {
    super(owner);
    init();
  }

  protected void init()
  {
    setTitle(Messages.getString("EditGroupDetailsDialog.Title"));
    setModal(true);

    availableFieldsModel = new DefaultBulkListModel();
    groupFieldsModel = new DefaultBulkListModel();
    nameTextField = new JTextField(25);

    super.init();

    pack();
    SwingUtil.centerDialogInParent(this);
  }

  protected Component createContentPane()
  {
    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(createNamePanel(), BorderLayout.NORTH);
    contentPane.add(createSelectionPane(), BorderLayout.CENTER);
    return contentPane;
  }

  protected DefaultBulkListModel getAvailableFieldsModel()
  {
    return availableFieldsModel;
  }

  protected DefaultBulkListModel getGroupFieldsModel()
  {
    return groupFieldsModel;
  }

  public String getGroupName()
  {
    return nameTextField.getText();
  }

  public void setGroupName(final String name)
  {
    this.nameTextField.setText(name);
  }

  public String[] getFields()
  {
    final DefaultListModel data = this.getGroupFieldsModel();
    final String[] fields = new String[data.size()];
    for (int i = 0; i < data.size(); i++)
    {
      fields[i] = (String) data.get(i);
    }
    return fields;
  }

  public void setFields(final String[] groupFields)
  {
    final DefaultListModel data = getGroupFieldsModel();
    data.clear();
    for (int i = 0; i < groupFields.length; i++)
    {
      data.addElement(groupFields[i]);
    }
  }

  public boolean editGroupData(final String name,
                               final String[] groupFields,
                               final ReportRenderContext reportRenderContext)
  {
    if (reportRenderContext == null)
    {
      throw new NullPointerException();
    }

    setGroupName(name);
    setFields(groupFields);

    nameTextField.setText(name);
    final String[] columnNames = reportRenderContext.getReportDataSchemaModel().getColumnNames();
    final DefaultListModel availableFieldsModel = getAvailableFieldsModel();
    availableFieldsModel.clear();
    for (int i = 0; i < columnNames.length; i++)
    {
      availableFieldsModel.addElement(columnNames[i]);
    }

    if (performEdit() == false)
    {
      return false;
    }

    return true;
  }

  public EditGroupUndoEntry editGroup(final RelationalGroup group,
                                      final ReportRenderContext reportRenderContext)
  {
    final String oldName = group.getName();
    final String[] oldFields = group.getFieldsArray();
    if (editGroupData(oldName, oldFields, reportRenderContext))
    {
      return new EditGroupUndoEntry(group.getObjectID(), oldName, getGroupName(), oldFields, getFields());
    }
    return null;
  }

  private JPanel createNamePanel()
  {
    final JPanel theNamePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
    theNamePanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
    theNamePanel.add(new JLabel(Messages.getString("EditGroupDetailsDialog.Name")));
    theNamePanel.add(nameTextField);
    return theNamePanel;
  }

  private JPanel createSelectionPane()
  {
    final JList availableFields = new JList(availableFieldsModel);
    final JList groupFields = new JList(groupFieldsModel);

    final JPanel tablesPane = new JPanel();
    tablesPane.setLayout(new GridBagLayout());

    final JLabel tablesColumnsLabel = new JLabel(Messages.getString("EditGroupDetailsDialog.AvailableFields"));
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.WEST;
    tablesPane.add(tablesColumnsLabel, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.weighty = 5;
    gbc.gridheight = 1;
    gbc.weightx = 2;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(0, 5, 5, 5);
    final JScrollPane comp = new JScrollPane
        (availableFields, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    tablesPane.add(comp, gbc);

    final JButton columnsAdd = new BorderlessButton(new AddSelectionAction(availableFields.getSelectionModel()));
    final JLabel columnsLabel = new JLabel(Messages.getString("EditGroupDetailsDialog.SelectedItems"));

    final ListSelectionModel columnsSelectionModel = groupFields.getSelectionModel();
    final JButton columnsSortUp = new BorderlessButton(new SortBulkUpAction(groupFieldsModel, columnsSelectionModel));
    final JButton columnsSortDown = new BorderlessButton(new SortBulkDownAction(groupFieldsModel, columnsSelectionModel));
    final JButton columnsRemove = new BorderlessButton(new RemoveBulkAction(groupFieldsModel, columnsSelectionModel));

    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.weightx = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    tablesPane.add(columnsLabel, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 3;
    gbc.gridy = 2;
    gbc.insets = new Insets(5, 5, 5, 5);
    tablesPane.add(columnsSortUp, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 4;
    gbc.gridy = 2;
    gbc.insets = new Insets(5, 5, 5, 5);
    tablesPane.add(columnsSortDown, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 5;
    gbc.gridy = 2;
    gbc.insets = new Insets(5, 5, 5, 5);
    tablesPane.add(columnsRemove, gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.gridwidth = 4;
    gbc.insets = new Insets(0, 5, 5, 0);
    tablesPane.add(new JScrollPane
        (groupFields, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), gbc);

    gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.insets = new Insets(5, 5, 5, 0);
    tablesPane.add(columnsAdd, gbc);

    return tablesPane;
  }
}
TOP

Related Classes of org.pentaho.reporting.designer.core.editor.groups.EditGroupDetailsDialog$AddSelectionAction

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.