/**
     * Called whenever the value of the selection changes.
     *
     * @param e the event that characterizes the change.
     */
    public void valueChanged(final TreeSelectionEvent e) {
      addExpandedNode(getRowForPath(e.getPath()));

      if (updateFromExternalSource) {
        return;
      }

      updateFromInternalSource = true;
      try {
        final ReportDocumentContext renderContext = getRenderContext();
        if (renderContext == null) {
          return;
        }

        final TreePath[] treePaths = getSelectionPaths();
        if (treePaths == null) {
          selectionModel.clearSelection();
          renderContext.getSelectionModel().clearSelection();
          return;
        }

        final DocumentContextSelectionModel selectionModel = renderContext.getSelectionModel();
        final Object[] data = new Object[treePaths.length];
        for (int i = 0; i < treePaths.length; i++) {
          final TreePath path = treePaths[i];
          data[i] = path.getLastPathComponent();
        }
        selectionModel.setSelectedElements(data);
      } finally {
        updateFromInternalSource = false;
      }
    }
 public void redo(final ReportDocumentContext renderContext) {
   final MasterReport report = renderContext.getContextRoot();
   final WriteableDocumentBundle bundle = (WriteableDocumentBundle) report.getBundle();
   final WriteableDocumentMetaData metaData = bundle.getWriteableDocumentMetaData();
   metaData.setBundleAttribute(
       ODFMetaAttributeNames.Meta.NAMESPACE,
       ODFMetaAttributeNames.Meta.KEYWORDS,
       newMetaData.getBundleAttribute(
           ODFMetaAttributeNames.Meta.NAMESPACE, ODFMetaAttributeNames.Meta.KEYWORDS));
   metaData.setBundleAttribute(
       ODFMetaAttributeNames.DublinCore.NAMESPACE,
       ODFMetaAttributeNames.DublinCore.CREATOR,
       newMetaData.getBundleAttribute(
           ODFMetaAttributeNames.DublinCore.NAMESPACE,
           ODFMetaAttributeNames.DublinCore.CREATOR));
   metaData.setBundleAttribute(
       ODFMetaAttributeNames.DublinCore.NAMESPACE,
       ODFMetaAttributeNames.DublinCore.DESCRIPTION,
       newMetaData.getBundleAttribute(
           ODFMetaAttributeNames.DublinCore.NAMESPACE,
           ODFMetaAttributeNames.DublinCore.DESCRIPTION));
   metaData.setBundleAttribute(
       ODFMetaAttributeNames.DublinCore.NAMESPACE,
       ODFMetaAttributeNames.DublinCore.SUBJECT,
       newMetaData.getBundleAttribute(
           ODFMetaAttributeNames.DublinCore.NAMESPACE,
           ODFMetaAttributeNames.DublinCore.SUBJECT));
   metaData.setBundleAttribute(
       ODFMetaAttributeNames.DublinCore.NAMESPACE,
       ODFMetaAttributeNames.DublinCore.TITLE,
       newMetaData.getBundleAttribute(
           ODFMetaAttributeNames.DublinCore.NAMESPACE, ODFMetaAttributeNames.DublinCore.TITLE));
   report.notifyNodePropertiesChanged();
 }
  protected TreeSet<Integer> getExpandedNodes() {
    final ReportDocumentContext renderContext = getRenderContext();
    if (renderContext == null) {
      // dummy operation..
      return new TreeSet<Integer>();
    }

    final Object property =
        renderContext.getProperties().get("::layout-report-tree:expanded-nodes");
    if (property instanceof TreeSet) {
      return (TreeSet<Integer>) property;
    }

    final TreeSet<Integer> retval = new TreeSet<Integer>();
    renderContext.getProperties().put("::layout-report-tree:expanded-nodes", retval);
    return retval;
  }
  public void actionPerformed(final ActionEvent e) {
    final ReportDocumentContext activeContext = getActiveContext();
    if (activeContext == null) {
      return;
    }

    final ReportDesignerContext context = getReportDesignerContext();
    final Component parent = context.getView().getParent();
    final Window window = LibSwingUtil.getWindowAncestor(parent);
    final RowBandingDialog dialog;
    if (window instanceof JDialog) {
      dialog = new RowBandingDialog((JDialog) window);
    } else if (window instanceof JFrame) {
      dialog = new RowBandingDialog((JFrame) window);
    } else {
      dialog = new RowBandingDialog();
    }

    final RowBandingFunction function = findRowbandingFunction(activeContext);
    final AbstractReportDefinition report = activeContext.getReportDefinition();
    if (function == null) {
      final RowBandingFunction newFunction = new RowBandingFunction();
      if (dialog.performEdit(newFunction)) {
        report.getExpressions().add(newFunction);
        activeContext
            .getUndo()
            .addChange(
                ActionMessages.getString("EditRowBandingAction.Text"),
                new ExpressionAddedUndoEntry(report.getExpressions().size() - 1, newFunction));
        report.notifyNodeChildAdded(function);
      }
    } else {
      final RowBandingFunction instance = (RowBandingFunction) function.getInstance();
      if (dialog.performEdit(instance)) {
        final ExpressionCollection expressionCollection = report.getExpressions();
        final int idx = expressionCollection.indexOf(function);
        expressionCollection.set(idx, instance);
        activeContext
            .getUndo()
            .addChange(
                ActionMessages.getString("EditRowBandingAction.Text"),
                new ExpressionEditUndoEntry(idx, function, instance));
        report.fireModelLayoutChanged(report, ReportModelEvent.NODE_PROPERTIES_CHANGED, instance);
      }
    }
  }
 private RowBandingFunction findRowbandingFunction(final ReportDocumentContext activeContext) {
   final Expression[] expressions =
       activeContext.getReportDefinition().getExpressions().getExpressions();
   for (int i = 0; i < expressions.length; i++) {
     final Expression expression = expressions[i];
     if (expression instanceof RowBandingFunction) {
       return (RowBandingFunction) expression;
     }
   }
   return null;
 }
  /** Invoked when an action occurs. */
  public void actionPerformed(final ActionEvent e) {
    final ReportDocumentContext activeContext = getActiveContext();
    if (activeContext == null) {
      return;
    }
    final ReportDesignerContext context = getReportDesignerContext();
    final Component parent = context.getView().getParent();
    final Window window = LibSwingUtil.getWindowAncestor(parent);
    final DocumentMetaDataDialog dialog;
    if (window instanceof JDialog) {
      dialog = new DocumentMetaDataDialog((JDialog) window);
    } else if (window instanceof JFrame) {
      dialog = new DocumentMetaDataDialog((JFrame) window);
    } else {
      dialog = new DocumentMetaDataDialog();
    }

    try {
      final MasterReport report = activeContext.getContextRoot();
      final DocumentBundle bundle = report.getBundle();
      final DocumentMetaData oldMetaData = (DocumentMetaData) bundle.getMetaData().clone();
      final DocumentMetaData result =
          dialog.performEdit(
              oldMetaData, report.getResourceManager(), report.getDefinitionSource());

      if (result == null) {
        return;
      }

      final MetaDataEditUndoEntry undoEntry = new MetaDataEditUndoEntry(oldMetaData, result);
      undoEntry.redo(activeContext);
      activeContext
          .getUndo()
          .addChange(ActionMessages.getString("EditReportPropertiesAction.Text"), undoEntry);
    } catch (CloneNotSupportedException cne) {
      // should not happen
      UncaughtExceptionsModel.getInstance().addException(cne);
    }
  }
    public void run() {
      if (rootband) {
        final int result =
            JOptionPane.showOptionDialog(
                dragContext.getRepresentationContainer(),
                Messages.getInstance()
                    .getString("TocElementDragHandler.BandedOrInlineSubreportQuestion"),
                Messages.getInstance().getString("TocElementDragHandler.InsertSubreport"),
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                new String[] {
                  Messages.getInstance().getString("TocElementDragHandler.Inline"),
                  Messages.getInstance().getString("TocElementDragHandler.Banded"),
                  Messages.getInstance().getString("TocElementDragHandler.Cancel")
                },
                Messages.getInstance().getString("TocElementDragHandler.Inline"));
        if (result == JOptionPane.CLOSED_OPTION || result == 2) {
          return;
        }

        if (result == 0) {
          final ReportDocumentContext context = dragContext.getRenderContext();
          final UndoManager undo = context.getUndo();
          undo.addChange(
              Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
              new ElementEditUndoEntry(
                  parent.getObjectID(), parent.getElementCount(), null, subReport));
          parent.addElement(subReport);
        } else {
          final AbstractRootLevelBand arb = (AbstractRootLevelBand) parent;

          final ReportDocumentContext context = dragContext.getRenderContext();
          final UndoManager undo = context.getUndo();
          undo.addChange(
              Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
              new BandedSubreportEditUndoEntry(
                  parent.getObjectID(), arb.getSubReportCount(), null, subReport));
          arb.addSubReport(subReport);
        }
      } else {
        final ReportDocumentContext context = dragContext.getRenderContext();
        final UndoManager undo = context.getUndo();
        undo.addChange(
            Messages.getInstance().getString("TocElementDragHandler.UndoEntry"),
            new ElementEditUndoEntry(
                parent.getObjectID(), parent.getElementCount(), null, subReport));
        parent.addElement(subReport);
      }

      dragContext
          .getRenderContext()
          .getSelectionModel()
          .setSelectedElements(new Object[] {subReport});
    }
  public FieldDefinition[] getFields() {
    if (renderContext == null) {
      return EMPTY_FIELDS;
    }

    final ReportDataSchemaModel model = renderContext.getReportDataSchemaModel();
    final String[] columnNames = model.getColumnNames();
    final FieldDefinition[] fields = new FieldDefinition[columnNames.length];
    final DataSchema dataSchema = model.getDataSchema();
    for (int i = 0; i < columnNames.length; i++) {
      final String columnName = columnNames[i];
      final DataAttributes attributes = dataSchema.getAttributes(columnName);
      if (attributes == null) {
        throw new IllegalStateException(
            "No data-schema for expression with name '" + columnName + '\'');
      }
      if (ReportDataSchemaModel.isFiltered(attributes, dataAttributeContext)) {
        continue;
      }
      fields[i] = new DataSchemaFieldDefinition(columnName, attributes, dataAttributeContext);
    }
    return fields;
  }
 public ContextAwareDataSchemaModel getDataSchemaModel() {
   return context.getReportDataSchemaModel();
 }
    public void run() {
      final ReportDocumentContext context = dragContext.getRenderContext();
      if (rootband) {
        final int result =
            JOptionPane.showOptionDialog(
                dragContext.getRepresentationContainer(),
                Messages.getString(
                    "CrosstabReportElementDragHandler.BandedOrInlineSubreportQuestion"),
                Messages.getString("CrosstabReportElementDragHandler.InsertSubreport"),
                JOptionPane.YES_NO_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                new String[] {
                  Messages.getString("CrosstabReportElementDragHandler.Inline"),
                  Messages.getString("CrosstabReportElementDragHandler.Banded"),
                  Messages.getString("CrosstabReportElementDragHandler.Cancel")
                },
                Messages.getString("CrosstabReportElementDragHandler.Inline"));
        if (result == JOptionPane.CLOSED_OPTION || result == 2) {
          return;
        }

        if (result == 0) {
          final UndoManager undo = context.getUndo();
          undo.addChange(
              Messages.getString("CrosstabReportElementDragHandler.UndoEntry"),
              new ElementEditUndoEntry(
                  parent.getObjectID(), parent.getElementCount(), null, subReport));
          parent.addElement(subReport);
        } else {
          final AbstractRootLevelBand arb = (AbstractRootLevelBand) parent;
          final UndoManager undo = context.getUndo();
          undo.addChange(
              Messages.getString("CrosstabReportElementDragHandler.UndoEntry"),
              new BandedSubreportEditUndoEntry(
                  parent.getObjectID(), arb.getSubReportCount(), null, subReport));
          arb.addSubReport(subReport);
        }
      } else {
        final UndoManager undo = context.getUndo();
        undo.addChange(
            Messages.getString("CrosstabReportElementDragHandler.UndoEntry"),
            new ElementEditUndoEntry(
                parent.getObjectID(), parent.getElementCount(), null, subReport));
        parent.addElement(subReport);
      }

      final ReportDesignerContext designerContext = dragContext.getDesignerContext();
      final Window window = LibSwingUtil.getWindowAncestor(designerContext.getView().getParent());
      final AbstractReportDefinition reportDefinition =
          designerContext.getActiveContext().getReportDefinition();

      try {
        // Create the new subreport tab - this is where the contents of the Crosstab
        // dialog will go.  Zoom the crosstab canvas to 150% as crosstab has a lot of elements to
        // display
        subReport.setDataFactory(reportDefinition.getDataFactory());
        subReport
            .getReportDefinition()
            .setAttribute(ReportDesignerBoot.DESIGNER_NAMESPACE, ReportDesignerBoot.ZOOM, 1.5f);

        final ResourceBundleFactory rbf = subReport.getResourceBundleFactory();
        subReport.setResourceBundleFactory(rbf);

        final int idx = designerContext.addSubReport(designerContext.getActiveContext(), subReport);
        designerContext.setActiveDocument(designerContext.getReportRenderContext(idx));
      } catch (ReportDataFactoryException e) {
        UncaughtExceptionsModel.getInstance().addException(e);
      }

      // Prompt user to either create or use an existing data-source.
      final SubReportDataSourceDialog crosstabDataSourceDialog;
      if (window instanceof Dialog) {
        crosstabDataSourceDialog = new SubReportDataSourceDialog((Dialog) window);
      } else if (window instanceof Frame) {
        crosstabDataSourceDialog = new SubReportDataSourceDialog((Frame) window);
      } else {
        crosstabDataSourceDialog = new SubReportDataSourceDialog();
      }

      // User has prompted to select a data-source.  Get the selected query
      final String queryName = crosstabDataSourceDialog.performSelection(designerContext);
      if (queryName != null) {
        subReport.setQuery(queryName);

        // Invoke Crosstab dialog
        final InsertCrosstabGroupAction crosstabAction = new InsertCrosstabGroupAction();
        crosstabAction.setReportDesignerContext(designerContext);
        crosstabAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""));
      }

      dragContext
          .getRenderContext()
          .getSelectionModel()
          .setSelectedElements(new Object[] {subReport});
    }