Example #1
0
 /**
  * Registers an {@link Adjustable} for participation in synchronized scrolling.
  *
  * @param adjustable the adjustable
  */
 public void participateInSynchronizedScrolling(Adjustable adjustable) {
   if (adjustable == null) return;
   if (synchronizedAdjustables.contains(adjustable)) return;
   synchronizedAdjustables.add(adjustable);
   setParticipatingInSynchronizedScrolling(adjustable, true);
   adjustable.addAdjustmentListener(this);
 }
Example #2
0
  /**
   * Wires a {@link JCheckBox} to the adjustment synchronizer, in such a way that:
   *
   * <ol>
   *   <li>state changes in the checkbox control whether the adjustable participates in synchronized
   *       adjustment
   *   <li>state changes in this {@link AdjustmentSynchronizer} are reflected in the {@link
   *       JCheckBox}
   * </ol>
   *
   * @param view the checkbox to control whether an adjustable participates in synchronized
   *     adjustment
   * @param adjustable the adjustable
   * @throws IllegalArgumentException if view is null
   * @throws IllegalArgumentException if adjustable is null
   */
  public void adapt(final JCheckBox view, final Adjustable adjustable) {
    CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
    CheckParameterUtil.ensureParameterNotNull(view, "view");

    if (!synchronizedAdjustables.contains(adjustable)) {
      participateInSynchronizedScrolling(adjustable);
    }

    // register an item lister with the check box
    //
    view.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            switch (e.getStateChange()) {
              case ItemEvent.SELECTED:
                if (!isParticipatingInSynchronizedScrolling(adjustable)) {
                  setParticipatingInSynchronizedScrolling(adjustable, true);
                }
                break;
              case ItemEvent.DESELECTED:
                if (isParticipatingInSynchronizedScrolling(adjustable)) {
                  setParticipatingInSynchronizedScrolling(adjustable, false);
                }
                break;
            }
          }
        });

    observable.addObserver(
        new Observer() {
          @Override
          public void update(Observable o, Object arg) {
            boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
            if (view.isSelected() != sync) {
              view.setSelected(sync);
            }
          }
        });
    setParticipatingInSynchronizedScrolling(adjustable, true);
    view.setSelected(true);
  }