/**
   * This method returns the tabs for a given node from the cache. Tabs are cached per
   * sessionTreeNode. If the tabs don't exist in the cache, they are created.
   *
   * @param node INode for which to retrieve tabs.
   * @return List of tabs.
   */
  private static List<IDetailTab> getTabs(INode node) {

    if (_logger.isDebugEnabled()) {
      _logger.debug("Loading tabs for: " + node.getUniqueIdentifier());
    }

    NodeCache nodeCache = _sessionTabCache.get(node.getSession());

    if (nodeCache == null) {
      // create cache
      nodeCache = new NodeCache();
      _sessionTabCache.put(node.getSession(), nodeCache);
    }

    nodeCache = findNodeCache(node, nodeCache);

    List<IDetailTab> tabs = nodeCache.getTabs();

    if (tabs == null) {
      // create tabs & store for later
      tabs = createTabs(node);
      nodeCache.setTabs(tabs);
    }

    // display parent details if we have nothing for this node..
    if ((tabs == null || tabs.size() == 0) && node.getParent() != null) {
      return getTabs(node.getParent());
    }

    return tabs;
  }
 public NodeCache getChild(INode pNode) {
   NodeCache child = children.get(pNode.getName());
   if (child == null) {
     child = new NodeCache();
     children.put(pNode.getName(), child);
   }
   return child;
 }
  /**
   * Clear the detail tab cache for a given node.
   *
   * @param node INode to remove from cache.
   */
  public static void clearCacheForNode(INode node) {

    if (_logger.isDebugEnabled()) {
      _logger.debug("Clearing tab cache for: " + node.getUniqueIdentifier());
    }

    NodeCache nodeCache = _sessionTabCache.get(node.getSession());
    if (nodeCache != null) {
      nodeCache = findNodeCache(node, nodeCache);
      nodeCache.reset();
    }
  }
Exemple #4
0
  public DataSet getDataSet() throws Exception {

    INode node = getNode();

    if (node == null) {
      return null;
    }

    if (node instanceof TableNode) {
      TableNode tableNode = (TableNode) node;

      List<IndexInfo> indexes =
          node.getSession().getMetaData().getIndexInfo(tableNode.getTableInfo());
      Comparable<?>[][] dataRows = new Comparable[indexes.size()][];
      int index = 0;
      for (IndexInfo col : indexes) {
        Comparable<?>[] row = new Comparable[COLUMN_LABELS.length];
        dataRows[index++] = row;

        int i = 0;
        row[i++] = col.isNonUnique();
        row[i++] = col.getIndexQualifier();
        row[i++] = col.getSimpleName();
        row[i++] = col.getIndexType();
        row[i++] = col.getOrdinalPosition();
        row[i++] = col.getColumnName();
        row[i++] = col.getSortOrder();
        row[i++] = col.getCardinality();
        row[i++] = col.getPages();
        row[i++] = col.getFilterCondition();
        if (i != COLUMN_LABELS.length)
          throw new RuntimeException("Internal error: ColumnInfoTab: wrong number of columns");
      }
      DataSet dataSet = new DataSet(COLUMN_LABELS, dataRows);

      return dataSet;
    }

    return null;
  }
  private static NodeCache findNodeCache(INode pNode, NodeCache pNodeCache) {
    NodeCache found =
        pNode.getParent() == null ? pNodeCache : findNodeCache(pNode.getParent(), pNodeCache);

    return found.getChild(pNode);
  }
  /**
   * Returns a list of all available tabs for a given node. These tabs can be standard or plugin
   * tabs.
   *
   * @param node for which to find tabs.
   * @return List of tabs
   */
  private static List<IDetailTab> createTabs(INode node) {

    if (_logger.isDebugEnabled()) {
      _logger.debug("Creating tabs for: " + node.getUniqueIdentifier());
    }

    ArrayList<IDetailTab> tabList = new ArrayList<IDetailTab>();

    // create connection info tab if needed
    if (node instanceof DatabaseNode) {

      IDetailTab dbTab = new ConnectionInfoTab();
      dbTab.setNode(node);
      tabList.add(dbTab);
    }

    // create our basic table tabs
    if (node instanceof TableNode) {

      IDetailTab tab1 = new ColumnInfoTab();
      IDetailTab tab2 = new TableInfoTab();
      IDetailTab tab3 = new PreviewTab();
      IDetailTab tab4 = new RowCountTab();
      IDetailTab tab5 = new PrimaryKeysTab();
      IDetailTab tab6 = new ExportedKeysTab();
      IDetailTab tab7 = new ImportedKeysTab();
      IDetailTab tab8 = new IndexesTab();
      IDetailTab tab9 = new PriviligesTab();
      IDetailTab tab10 = new ColumnPriviligesTab();
      IDetailTab tab11 = new RowIdsTab();
      IDetailTab tab12 = new VersionsTab();

      tab1.setNode(node);
      tab2.setNode(node);
      tab3.setNode(node);
      tab4.setNode(node);
      tab5.setNode(node);
      tab6.setNode(node);
      tab7.setNode(node);
      tab8.setNode(node);
      tab9.setNode(node);
      tab10.setNode(node);
      tab11.setNode(node);
      tab12.setNode(node);

      tabList.add(tab1);
      tabList.add(tab2);
      tabList.add(tab3);
      tabList.add(tab4);
      tabList.add(tab5);
      tabList.add(tab6);
      tabList.add(tab7);
      tabList.add(tab8);
      tabList.add(tab9);
      tabList.add(tab10);
      tabList.add(tab11);
      tabList.add(tab12);
    }

    // create extension point tabs
    String databaseProductName =
        node.getSession().getRoot().getDatabaseProductName().toLowerCase().trim();
    String nodeType = node.getType().toLowerCase().trim();

    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint point =
        registry.getExtensionPoint("net.sourceforge.sqlexplorer", "nodeDetailTab");
    IExtension[] extensions = point.getExtensions();

    for (int i = 0; i < extensions.length; i++) {

      IExtension e = extensions[i];

      IConfigurationElement[] ces = e.getConfigurationElements();

      for (int j = 0; j < ces.length; j++) {
        try {

          boolean isValidProduct = false;
          boolean isValidNodeType = false;

          String[] validProducts = ces[j].getAttribute("database-product-name").split(",");
          String[] validNodeTypes = ces[j].getAttribute("node-type").split(",");

          // check if tab is valid for current database product
          for (int k = 0; k < validProducts.length; k++) {

            String product = validProducts[k].toLowerCase().trim();

            if (product.length() == 0) {
              continue;
            }

            if (product.equals("*")) {
              isValidProduct = true;
              break;
            }

            String regex = TextUtil.replaceChar(product, '*', ".*");
            if (databaseProductName.matches(regex)) {
              isValidProduct = true;
              break;
            }
          }

          if (!isValidProduct) {
            continue;
          }

          // check if tab is valid for current node type
          for (int k = 0; k < validNodeTypes.length; k++) {

            String type = validNodeTypes[k].toLowerCase().trim();

            if (type.length() == 0) {
              continue;
            }

            if (type.equals("*")) {
              isValidNodeType = true;
              break;
            }

            String regex = TextUtil.replaceChar(type, '*', ".*");
            if (nodeType.matches(regex)) {
              isValidNodeType = true;
              break;
            }
          }

          if (!isValidNodeType) {
            continue;
          }

          // add tab to list
          IDetailTab tab = (IDetailTab) ces[j].createExecutableExtension("class");
          tab.setNode(node);

          tabList.add(tab);

        } catch (Throwable ex) {
          SQLExplorerPlugin.error("Could not create menu action", ex);
        }
      }
    }

    return tabList;
  }
  /**
   * Creates all the tabs in the detail pane to display the information for a given node.
   *
   * @param composite
   * @param node
   */
  public static void createTabs(Composite composite, INode node) {

    List<IDetailTab> tabs = getTabs(node);

    if (tabs == null || tabs.size() == 0) {
      // no detail found..

      Label label = new Label(composite, SWT.FILL);
      label.setText(
          Messages.getString("DatabaseDetailView.Tab.Unavailable") + " " + node.getLabelText());
      label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

      return;
    }

    // create tabs
    TabFolder tabFolder = new TabFolder(composite, SWT.NULL);

    // only init tabs when the tab becomes active
    tabFolder.addSelectionListener(
        new SelectionListener() {

          public void widgetDefaultSelected(SelectionEvent e) {

            // noop
          }

          public void widgetSelected(SelectionEvent e) {

            TabItem tabItem = (TabItem) e.item;
            IDetailTab tab = (IDetailTab) tabItem.getData();
            if (tab != null) {

              // create composite on tab and fill it..
              Composite detailComposite = new Composite(tabItem.getParent(), SWT.FILL);
              tabItem.setControl(detailComposite);
              detailComposite.setLayout(new FillLayout());
              tab.fillComposite(detailComposite);
              detailComposite.layout();

              // store tab name, so we can reselect when other node is
              // chosen
              DetailTabManager.setActiveTabName(tabItem.getText());
            }
          }
        });

    // add tabs to folder
    int tabIndex = 0;
    for (IDetailTab detailTab : tabs) {

      // create tab
      TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
      tabItem.setText(detailTab.getLabelText());
      tabItem.setToolTipText(detailTab.getLabelToolTipText());

      // store tab so we can fill later
      tabItem.setData(detailTab);

      // reselect same tab as was previous selected
      if (tabItem.getText() != null && _activeTabName != null) {
        if (tabItem.getText().equals(_activeTabName)) {
          tabFolder.setSelection(tabIndex);
        }
      }

      tabIndex++;
    }

    // load data for active tab, default to first one if none is selected
    tabIndex = tabFolder.getSelectionIndex();
    if (tabIndex == -1) {
      tabIndex = 0;
    }

    TabItem tabItem = tabFolder.getItem(tabIndex);
    if (tabItem != null) {
      Composite detailComposite = new Composite(tabItem.getParent(), SWT.FILL);
      tabItem.setControl(detailComposite);
      detailComposite.setLayout(new FillLayout());
      IDetailTab tab = (IDetailTab) tabItem.getData();
      tab.fillComposite(detailComposite);
      detailComposite.layout();
    }

    tabFolder.layout();
  }