Package org.pentaho.reporting.designer.core.settings.ui

Source Code of org.pentaho.reporting.designer.core.settings.ui.FormatEditorDialog

/*!
* 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) 2002-2013 Pentaho Corporation..  All rights reserved.
*/

package org.pentaho.reporting.designer.core.settings.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.pentaho.reporting.designer.core.ReportDesignerContext;
import org.pentaho.reporting.designer.core.settings.SettingsMessages;
import org.pentaho.reporting.designer.core.util.IconLoader;
import org.pentaho.reporting.designer.core.util.table.ArrayTableModel;
import org.pentaho.reporting.designer.core.util.table.ElementMetaDataTable;
import org.pentaho.reporting.engine.classic.core.metadata.AttributeMetaData;
import org.pentaho.reporting.libraries.designtime.swing.BorderlessButton;
import org.pentaho.reporting.libraries.designtime.swing.CommonDialog;
import org.pentaho.reporting.libraries.designtime.swing.bulk.SortBulkDownAction;
import org.pentaho.reporting.libraries.designtime.swing.bulk.SortBulkUpAction;

public class FormatEditorDialog extends CommonDialog
{
  private static class AddEntryAction extends AbstractAction
  {
    private ArrayTableModel tableModel;

    private AddEntryAction(final ArrayTableModel tableModel)
    {
      this.tableModel = tableModel;
      putValue(Action.SMALL_ICON, IconLoader.getInstance().getAddIcon());
      putValue(Action.SHORT_DESCRIPTION, SettingsMessages.getInstance().getString("FormatEditorDialog.AddEntry.Description"));
    }

    public void actionPerformed(final ActionEvent e)
    {
      tableModel.add(null);
    }
  }

  private class RemoveEntryAction extends AbstractAction implements ListSelectionListener
  {
    private ListSelectionModel selectionModel;
    private ArrayTableModel tableModel;
    private JTable table;

    private RemoveEntryAction(final ArrayTableModel tableModel,
                              final ListSelectionModel selectionModel,
                              final JTable table)
    {
      this.tableModel = tableModel;
      this.table = table;
      putValue(Action.SMALL_ICON, IconLoader.getInstance().getRemoveIcon());
      putValue(Action.SHORT_DESCRIPTION, SettingsMessages.getInstance().getString("FormatEditorDialog.RemoveEntry.Description"));


      this.selectionModel = selectionModel;
      this.selectionModel.addListSelectionListener(this);
    }

    public void actionPerformed(final ActionEvent e)
    {
      stopCellEditing();

      final int maxIdx = selectionModel.getMaxSelectionIndex();
      final ArrayList<Integer> list = new ArrayList<Integer>();
      for (int i = selectionModel.getMinSelectionIndex(); i <= maxIdx; i++)
      {
        if (selectionModel.isSelectedIndex(i))
        {
          list.add(0, i);
        }
      }

      for (int i = 0; i < list.size(); i++)
      {
        final Integer dataEntry = list.get(i);
        tableModel.remove(dataEntry);
      }
    }

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

    protected void stopCellEditing()
    {
      if (table.getCellEditor() != null)
      {
        table.getCellEditor().stopCellEditing();
      }
    }

  }

  public static class Result
  {
    private String[] dateFormats;
    private String[] numberFormats;

    public Result(final String[] dateFormats, final String[] numberFormats)
    {
      this.dateFormats = dateFormats;
      this.numberFormats = numberFormats;
    }

    public String[] getDateFormats()
    {
      return dateFormats;
    }

    public String[] getNumberFormats()
    {
      return numberFormats;
    }
  }

  private ArrayTableModel dateFormatModel;
  private ArrayTableModel numberFormatModel;
  private ElementMetaDataTable dateFormatTable;
  private ElementMetaDataTable numberFormatTable;

  /**
   * Creates a new modal dialog.
   */
  public FormatEditorDialog()
  {
    init();
  }

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

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

  protected void init()
  {
    setTitle(SettingsMessages.getInstance().getString("FormatEditorDialog.Title"));
    super.init();
  }

  protected String getDialogId()
  {
    return "ReportDesigner.Core.FormatEditor";
  }

  protected Component createContentPane()
  {
    dateFormatModel = new ArrayTableModel();
    dateFormatModel.setColumnTitle(SettingsMessages.getInstance().getString("FormatEditorDialog.DateFormat"));
    numberFormatModel = new ArrayTableModel();
    numberFormatModel.setColumnTitle(SettingsMessages.getInstance().getString("FormatEditorDialog.NumberFormat"));

    dateFormatTable = new ElementMetaDataTable();
    dateFormatTable.setModel(dateFormatModel);
    numberFormatTable = new ElementMetaDataTable();
    numberFormatTable.setModel(numberFormatModel);

    final JPanel panel = new JPanel(new GridLayout(1, 2));
    panel.add(createTablePanel(numberFormatTable, numberFormatModel));
    panel.add(createTablePanel(dateFormatTable, dateFormatModel));
    return panel;
  }

  private JPanel createTablePanel(final ElementMetaDataTable dateFormatTable,
                                  final ArrayTableModel dateFormatModel)
  {
    final ListSelectionModel selectionModel = dateFormatTable.getSelectionModel();
    final Action addGroupAction = new AddEntryAction(dateFormatModel);
    final Action removeGroupAction = new RemoveEntryAction(dateFormatModel, selectionModel, dateFormatTable);

    final Action sortUpAction = new SortBulkUpAction(dateFormatModel, selectionModel, dateFormatTable);
    final Action sortDownAction = new SortBulkDownAction(dateFormatModel, selectionModel, dateFormatTable);

    final JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    buttonsPanel.add(new BorderlessButton(sortUpAction));
    buttonsPanel.add(new BorderlessButton(sortDownAction));
    buttonsPanel.add(Box.createHorizontalStrut(20));
    buttonsPanel.add(new BorderlessButton(addGroupAction));
    buttonsPanel.add(new BorderlessButton(removeGroupAction));

    final JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(new EmptyBorder(2, 2, 2, 2));
    panel.add(new JScrollPane(dateFormatTable), BorderLayout.CENTER);
    panel.add(buttonsPanel, BorderLayout.NORTH);
    return panel;
  }

  public Result editArray(final String[] dateFormats,
                          final String[] numberFormats)
  {
    numberFormatModel.setType(String.class);
    numberFormatModel.setValueRole(AttributeMetaData.VALUEROLE_NUMBERFORMAT);
    numberFormatModel.setData(numberFormats, String.class);

    dateFormatModel.setType(String.class);
    dateFormatModel.setValueRole(AttributeMetaData.VALUEROLE_NUMBERFORMAT);
    dateFormatModel.setData(dateFormats, String.class);

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

    if (dateFormatTable.getCellEditor() != null)
    {
      dateFormatTable.getCellEditor().stopCellEditing();
    }
    if (numberFormatTable.getCellEditor() != null)
    {
      numberFormatTable.getCellEditor().stopCellEditing();
    }

    final String[] dateFormatString = new String[dateFormatModel.getSize()];
    for (int i = 0; i < dateFormatModel.getSize(); i++)
    {
      dateFormatString[i] = String.valueOf(dateFormatModel.get(i));
    }

    final String[] numberFormatString = new String[numberFormatModel.getSize()];
    for (int i = 0; i < numberFormatModel.getSize(); i++)
    {
      numberFormatString[i] = String.valueOf(numberFormatModel.get(i));
    }

    // process the array ..
    return new Result(dateFormatString, numberFormatString);
  }

  public ReportDesignerContext getReportDesignerContext()
  {
    return dateFormatTable.getReportDesignerContext();
  }

  public void setReportDesignerContext(final ReportDesignerContext reportDesignerContext)
  {
    if (reportDesignerContext != null)
    {
      this.dateFormatTable.setReportDesignerContext(reportDesignerContext);
      this.numberFormatTable.setReportDesignerContext(reportDesignerContext);
    }
    else
    {
      this.dateFormatTable.setReportDesignerContext(null);
      this.numberFormatTable.setReportDesignerContext(null);
    }
  }
}
TOP

Related Classes of org.pentaho.reporting.designer.core.settings.ui.FormatEditorDialog

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.