public void actionPerformed(final ActionEvent aEvt) {
      try {
        final ScriptableDataFactory dataFactory = produceFactory();
        DataFactoryEditorSupport.configureDataFactoryForPreview(dataFactory, designTimeContext);
        final DataPreviewDialog previewDialog =
            new DataPreviewDialog(ScriptableDataSourceEditor.this);

        final ScriptablePreviewWorker worker =
            new ScriptablePreviewWorker(dataFactory, queryNameTextField.getText());
        previewDialog.showData(worker);

        final ReportDataFactoryException factoryException = worker.getException();
        if (factoryException != null) {
          ExceptionDialog.showExceptionDialog(
              ScriptableDataSourceEditor.this,
              Messages.getString("ScriptableDataSourceEditor.PreviewError.Title"),
              Messages.getString("ScriptableDataSourceEditor.PreviewError.Message"),
              factoryException);
        }
      } catch (Exception e) {
        ExceptionDialog.showExceptionDialog(
            ScriptableDataSourceEditor.this,
            Messages.getString("ScriptableDataSourceEditor.PreviewError.Title"),
            Messages.getString("ScriptableDataSourceEditor.PreviewError.Message"),
            e);
      }
    }
 private QueryAddAction() {
   final URL resource =
       ScriptableDataSourceEditor.class.getResource(
           "/org/pentaho/reporting/ui/datasources/scriptable/resources/Add.png");
   if (resource != null) {
     putValue(Action.SMALL_ICON, new ImageIcon(resource));
   } else {
     putValue(Action.NAME, Messages.getString("ScriptableDataSourceEditor.AddQuery.Name"));
   }
   putValue(
       Action.SHORT_DESCRIPTION,
       Messages.getString("ScriptableDataSourceEditor.AddQuery.Description"));
 }
    public void actionPerformed(final ActionEvent e) {
      // Find a unique query name
      String queryName = Messages.getString("ScriptableDataSourceEditor.Query");
      for (int i = 1; i < 1000; ++i) {
        final String newQueryName =
            Messages.getString("ScriptableDataSourceEditor.Query") + ' ' + i;
        if (!queries.containsKey(newQueryName)) {
          queryName = newQueryName;
          break;
        }
      }

      final DataSetQuery newQuery = new DataSetQuery(queryName, "");
      queries.put(newQuery.getQueryName(), newQuery);

      inModifyingQueryNameList = true;
      updateQueryList();
      queryNameList.setSelectedValue(newQuery, true);
      inModifyingQueryNameList = false;
      updateComponents();
    }
  private JPanel createQueryListPanel() {
    final QueryRemoveAction queryRemoveAction = new QueryRemoveAction();
    queryNameList.addListSelectionListener(queryRemoveAction);

    final JPanel theQueryButtonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    theQueryButtonsPanel.add(new BorderlessButton(new QueryAddAction()));
    theQueryButtonsPanel.add(new BorderlessButton(queryRemoveAction));

    final JPanel theQueryControlsPanel = new JPanel(new BorderLayout());
    theQueryControlsPanel.add(
        BorderLayout.WEST,
        new JLabel(Messages.getString("ScriptableDataSourceEditor.AvailableQueries")));
    theQueryControlsPanel.add(BorderLayout.EAST, theQueryButtonsPanel);

    final JPanel queryListPanel = new JPanel(new BorderLayout());
    queryListPanel.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8));
    queryListPanel.add(BorderLayout.NORTH, theQueryControlsPanel);
    queryListPanel.add(BorderLayout.CENTER, new JScrollPane(queryNameList));
    return queryListPanel;
  }
  private void init(final DesignTimeContext designTimeContext) {
    if (designTimeContext == null) {
      throw new NullPointerException();
    }
    this.designTimeContext = designTimeContext;

    setTitle(Messages.getString("ScriptableDataSourceEditor.Title"));
    setModal(true);

    previewAction = new PreviewAction();

    queryNameTextField = new JTextField(null, 0);
    queryNameTextField.setColumns(35);
    queryNameTextField.getDocument().addDocumentListener(new QueryNameTextFieldDocumentListener());

    queryTextArea = new RSyntaxTextArea();
    queryTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
    queryTextArea.getDocument().addDocumentListener(new QueryDocumentListener());

    initScriptTextArea = new RSyntaxTextArea();
    initScriptTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);

    shutdownScriptTextArea = new RSyntaxTextArea();
    shutdownScriptTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);

    languageField =
        new JList(new DefaultComboBoxModel(InternalBSFManager.getRegisteredLanguages()));
    languageField.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    languageField.getSelectionModel().addListSelectionListener(new UpdateLanguageHandler());

    queryNameList = new JList();
    queryNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    queryNameList.setVisibleRowCount(5);
    queryNameList.addListSelectionListener(new QueryNameListSelectionListener());
    queryNameList.setCellRenderer(new QueryNameListCellRenderer());

    final QueryRemoveAction removeQueryAction = new QueryRemoveAction();
    queryNameList.addListSelectionListener(removeQueryAction);

    super.init();
  }
  protected Component createContentPane() {
    final JPanel initScriptContentHolder = new JPanel(new BorderLayout());
    initScriptContentHolder.add(
        BorderLayout.NORTH,
        new JLabel(Messages.getString("ScriptableDataSourceEditor.InitScript")));
    initScriptContentHolder.add(
        BorderLayout.CENTER, new RTextScrollPane(500, 600, initScriptTextArea, true));

    final JPanel shutdownScriptContentHolder = new JPanel(new BorderLayout());
    shutdownScriptContentHolder.add(
        BorderLayout.NORTH,
        new JLabel(Messages.getString("ScriptableDataSourceEditor.ShutdownScript")));
    shutdownScriptContentHolder.add(
        BorderLayout.CENTER, new RTextScrollPane(500, 600, shutdownScriptTextArea, true));

    final JPanel queryDetailsNamePanel = new JPanel(new BorderLayout());
    queryDetailsNamePanel.add(
        new JLabel(Messages.getString("ScriptableDataSourceEditor.QueryName")), BorderLayout.NORTH);
    queryDetailsNamePanel.add(queryNameTextField, BorderLayout.CENTER);

    final JPanel queryContentHolder = new JPanel(new BorderLayout());
    queryContentHolder.add(
        BorderLayout.NORTH,
        new JLabel(Messages.getString("ScriptableDataSourceEditor.QueryLabel")));
    queryContentHolder.add(BorderLayout.CENTER, new RTextScrollPane(500, 300, queryTextArea, true));

    // Create the query details panel
    final JPanel queryDetailsPanel = new JPanel(new BorderLayout());
    queryDetailsPanel.setBorder(new EmptyBorder(0, 8, 8, 8));
    queryDetailsPanel.add(BorderLayout.NORTH, queryDetailsNamePanel);
    queryDetailsPanel.add(BorderLayout.CENTER, queryContentHolder);

    final JPanel previewButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    previewButtonPanel.add(new JButton(previewAction));

    final JPanel queryContentPanel = new JPanel(new BorderLayout());
    queryContentPanel.add(BorderLayout.NORTH, createQueryListPanel());
    queryContentPanel.add(BorderLayout.CENTER, queryDetailsPanel);

    final JTabbedPane scriptsTabPane = new JTabbedPane();
    scriptsTabPane.addTab(
        Messages.getString("ScriptableDataSourceEditor.QueryTab"), queryContentPanel);
    scriptsTabPane.addTab(
        Messages.getString("ScriptableDataSourceEditor.InitScriptTab"), initScriptContentHolder);
    scriptsTabPane.addTab(
        Messages.getString("ScriptableDataSourceEditor.ShutdownScriptTab"),
        shutdownScriptContentHolder);

    final JLabel languageLabel =
        new JLabel(Messages.getString("ScriptableDataSourceEditor.Language"));
    languageLabel.setBorder(new EmptyBorder(0, 0, 3, 0));

    final JPanel languagesPanel = new JPanel(new BorderLayout());
    languagesPanel.setBorder(new EmptyBorder(8, 8, 8, 0));
    languagesPanel.add(BorderLayout.NORTH, languageLabel);
    languagesPanel.add(BorderLayout.CENTER, new JScrollPane(languageField));

    final JPanel contentPanel = new JPanel(new BorderLayout());
    contentPanel.add(BorderLayout.WEST, languagesPanel);
    contentPanel.add(BorderLayout.CENTER, scriptsTabPane);
    contentPanel.add(BorderLayout.SOUTH, previewButtonPanel);
    return contentPanel;
  }
 private PreviewAction() {
   putValue(Action.NAME, Messages.getString("ScriptableDataSourceEditor.Preview.Name"));
 }