/*
  * (non-Javadoc)
  *
  * @see java.lang.Object#finalize()
  */
 @Override
 protected void finalize() throws Throwable {
   updateQueue.shutdown();
   super.finalize();
 }
  public MetaDataDeclarationEditor(AbstractDataReader reader, final boolean showMetaDataEditor) {
    super(new BorderLayout());

    backGroundGray = this.getBackground();

    this.reader = reader;

    metadataTable = new MetaDataTable();

    previewTable = new PreviewTable();

    ExtendedJScrollPane metadataPane = new ExtendedJScrollPane(metadataTable);

    ExtendedJScrollPane dataPane =
        new ExtendedJScrollPane(previewTable) {

          private static final long serialVersionUID = 1L;

          // Only show column header if there is no MetaDataTable
          @Override
          public void setColumnHeaderView(Component view) {
            if (!showMetaDataEditor) {
              super.setColumnHeaderView(view);
            }
          } // alternative work around: dataPane.setColumnHeader(null);
        };

    metadataColumnListener =
        new TableColumnModelListener() {

          @Override
          public void columnSelectionChanged(ListSelectionEvent e) {}

          @Override
          public void columnRemoved(TableColumnModelEvent e) {}

          @Override
          public void columnMoved(TableColumnModelEvent e) {}

          @Override
          public void columnMarginChanged(ChangeEvent e) {
            assert previewTable.getColumnCount() == metadataTable.getColumnCount();
            for (int i = 0; i < previewTable.getColumnCount(); i++) {
              int columnwidth = metadataTable.getColumnModel().getColumn(i).getWidth();
              int oldwidth = previewTable.getColumnModel().getColumn(i).getPreferredWidth();
              if (oldwidth == columnwidth) {
                continue;
              }
              previewTable.getColumnModel().getColumn(i).setPreferredWidth(columnwidth);
            }
            previewTable.doLayout();
            previewTable.repaint();
          }

          @Override
          public void columnAdded(TableColumnModelEvent e) {}
        };

    if (showMetaDataEditor) {

      metadataPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

      // synchronize the the scroll bars:
      metadataPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      JScrollBar bar = metadataPane.getVerticalScrollBar();
      JScrollBar dummyBar =
          new JScrollBar() {

            private static final long serialVersionUID = 1L;

            @Override
            public void paint(Graphics g) {
              // Color c = getParent().getBackground();
              g.setColor(backGroundGray);
              g.fillRect(0, 0, this.getSize().width, this.getSize().height);
            }
          };

      dummyBar.setPreferredSize(bar.getPreferredSize());
      metadataPane.setVerticalScrollBar(dummyBar);

      final JScrollBar bar1 = metadataPane.getHorizontalScrollBar();
      JScrollBar bar2 = dataPane.getHorizontalScrollBar();
      bar2.addAdjustmentListener(
          new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
              bar1.setValue(e.getValue());
            }
          });

      /*
       * Synchronize the column margins if they are changed:
       */

      // propagate metadataTable margin changes to previewTable:
      metadataTable.getColumnModel().addColumnModelListener(metadataColumnListener);

      metadataPane.setPreferredSize(new Dimension(400, 103)); // ugly
      this.add(metadataPane, BorderLayout.NORTH);
    }

    this.add(dataPane, BorderLayout.CENTER);
    this.doLayout();

    updateQueue.start();
  }