Esempio n. 1
0
  /**
   * Adds a tab to diagram
   *
   * @param diagram
   */
  protected void addTab(Diagram diagram) {

    // do not add if tab already exists
    if (getTab(diagram) != null) {
      return;
    }

    // if the diagram key is null, and the exercise does not allow switching to the diagram type,
    // do not create a tab for this diagram.
    if (diagram.getKey() == null
        && !Exercise.getExercise().canSwitchToNullKeyDiagramType(diagram.getType())) {
      return;
    }

    // no existing tab was found, so add a new one.
    MainTab tab = new MainTab(diagram);
    tabContainer.add(tab);

    Dimension tabPreferredSize = tab.getPreferredSize(-1, -1);
    tab.setSize(tabPreferredSize.width, tabPreferredSize.height);
    tabs.add(tab);

    viewport.layout();
    BoundedRangeModel hModel = viewport.getHModel();

    // if there are more tabs than will fit in the viewport, add the buttons for scrolling.
    if (hModel.getRange() != hModel.getExtent() && !scrollButtonsAdded && hModel.getExtent() > 0) {
      addScrollButtons();
      viewport.layout();
    }
  }
Esempio n. 2
0
  /** Adds scroll buttons to the tab bar */
  private void addScrollButtons() {
    scrollButtonsAdded = true;

    final BoundedRangeModel hModel = viewport.getHModel();
    ActionListener scrollListener =
        new ActionListener() {

          public void actionPerformed(ActionEvent event) {
            if (event.getAction().equals("scroll_left")) {
              // scroll left
              hModel.setValue(hModel.getValue() - hModel.getScrollIncrement() / 4);
            } else if (event.getAction().equals("scroll_right")) {
              // scroll right
              hModel.setValue(hModel.getValue() + hModel.getScrollIncrement() / 4);
            }
          }
        };

    final BButton left = new BButton("<", scrollListener, "scroll_left");
    scrollingTabContainer.add(left, BorderLayout.WEST);

    final BButton right = new BButton(">", scrollListener, "scroll_right");
    scrollingTabContainer.add(right, BorderLayout.EAST);

    hModel.addChangeListener(
        new ChangeListener() {

          public void stateChanged(ChangeEvent event) {
            left.setEnabled(hModel.getValue() > hModel.getMinimum());
            right.setEnabled(hModel.getValue() < hModel.getMaximum() - hModel.getExtent());
          }
        });
  }