<commit_before>//----------------------------------------------------------------------------//
//                                                                            //
//                      S c o r e S h e e t B r i d g e                       //
//                                                                            //
//  Copyright (C) <NAME> 2000-2007. All rights reserved.               //
//  This software is released under the GNU General Public License.           //
//  Contact author at <EMAIL> to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//
package omr.score;

import omr.score.common.PagePoint;
import omr.score.common.ScorePoint;
import omr.score.entity.System;

import omr.selection.Selection;
import omr.selection.SelectionHint;
import omr.selection.SelectionObserver;
import omr.selection.SelectionTag;

import omr.score.common.PixelPoint;
import omr.sheet.Sheet;

import omr.util.Implement;
import omr.util.Logger;

import java.awt.Rectangle;

/**
 * Class <code>ScoreSheetBridge</code> is in charge of keeping in sync the
 * (sheet) Pixel Selection and the Score Selection. There should exactly one
 * instance of this class per score (and thus per sheet).
 *
 * <dl>
 * <dt><b>Selection Inputs:</b></dt><ul>
 * <li>PIXEL Location (if not already bridging)
 * <li>SCORE Location (if not already bridging)
 * </ul>
 *
 * <dt><b>Selection Outputs:</b></dt><ul>
 * <li>PIXEL Location
 * <li>SCORE Location
 * </ul>
 * </dl>
 *
 * @author <NAME>
 * @version $Id$
 */
public class ScoreSheetBridge
    implements SelectionObserver
{
    //~ Static fields/initializers ---------------------------------------------

    /** Usual logger utility */
    private static final Logger logger = Logger.getLogger(
        ScoreSheetBridge.class);

    //~ Instance fields --------------------------------------------------------

    /** The sheet instance */
    private Sheet sheet;

    /** The score instance */
    private Score score;

    /** The sheet location selection */
    private final Selection pixelSelection;

    /** The score location selection */
    private final Selection scoreSelection;

    /** Needed to force only one-way sync at a time */
    private volatile boolean bridging;

    //~ Constructors -----------------------------------------------------------

    //------------------//
    // ScoreSheetBridge //
    //------------------//
    /**
     * Creates a new ScoreSheetBridge object.
     *
     * @param score the related score (and thus the related sheet)
     */
    public ScoreSheetBridge (Score score)
    {
        this.score = score;
        sheet = score.getSheet();

        // Keep reference of the bridged selections
        pixelSelection = sheet.getSelection(SelectionTag.SHEET_RECTANGLE);
        scoreSelection = sheet.getSelection(SelectionTag.SCORE_RECTANGLE);

        // Register to  Selections
        pixelSelection.addObserver(this);
        scoreSelection.addObserver(this);
    }

    //~ Methods ----------------------------------------------------------------

    //---------//
    // getName //
    //---------//
    /**
     * Report the name of this observer
     *
     * @return name of the bridge
     */
    @Implement(SelectionObserver.class)
    public String getName ()
    {
        return "Score-Sheet-Bridge";
    }

    //--------//
    // update //
    //--------//
    /**
     * Notification of selection objects (disabled when already bridging, to
     * avoid endless loop)
     *
     * @param selection the originating selection object
     * @param hint processing hint, if any
     */
    @Implement(SelectionObserver.class)
    public void update (Selection     selection,
                        SelectionHint hint)
    {
        if (logger.isFineEnabled()) {
            logger.fine("Bridge : selection updated " + selection);
        }

        if (!bridging) {
            bridging = true; // Prevent re-entry

            Object entity = selection.getEntity();

            if (logger.isFineEnabled()) {
                logger.fine("Bridge " + selection.getTag() + ": " + entity);
            }

            Rectangle rect = (Rectangle) entity;

            switch (selection.getTag()) {
            case SHEET_RECTANGLE :

                // Forward to Score side
                if (rect != null) {
                    PagePoint pagPt = sheet.getScale()
                                           .toPagePoint(
                        new PixelPoint(
                            rect.x + (rect.width / 2),
                            rect.y + (rect.height / 2)));

                    if (pagPt != null) {
                        // Which system ?
                        final System system = score.pageLocateSystem(pagPt);
                        ScorePoint   scrPt = system.toScorePoint(pagPt);
                        scoreSelection.setEntity(new Rectangle(scrPt), hint);
                    }
                } else {
                    scoreSelection.setEntity(null, hint);
                }

                break;

            case SCORE_RECTANGLE :

                // Forward to Sheet side
                if (rect != null) {
                    // We forge a ScorePoint from the display point
                    ScorePoint scrPt = new ScorePoint(rect.x, rect.y); // ???

                    // The enclosing system
                    System     system = score.scoreLocateSystem(scrPt);
                    PagePoint  pagPt = system.toPagePoint(scrPt);
                    PixelPoint pt = sheet.getScale()
                                         .toPixelPoint(pagPt, null);
                    pixelSelection.setEntity(new Rectangle(pt), hint);
                } else {
                    pixelSelection.setEntity(null, hint);
                }

                break;

            default :
                logger.severe("Unexpected selection event from " + selection);
            }

            bridging = false;
        }
    }
}<commit_msg>Switched from Selection to standard EventBus<commit_after>//----------------------------------------------------------------------------//
//                                                                            //
//                      S c o r e S h e e t B r i d g e                       //
//                                                                            //
//  Copyright (C) <NAME> 2000-2007. All rights reserved.               //
//  This software is released under the GNU General Public License.           //
//  Contact author at <EMAIL> to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//
package omr.score;

import omr.score.common.PagePoint;
import omr.score.common.PixelPoint;
import omr.score.common.PixelRectangle;
import omr.score.common.ScorePoint;
import omr.score.common.ScoreRectangle;
import omr.score.entity.System;

import omr.selection.LocationEvent;
import omr.selection.ScoreLocationEvent;
import omr.selection.SheetLocationEvent;

import omr.sheet.Sheet;

import omr.util.Implement;
import omr.util.Logger;

import org.bushe.swing.event.EventService;
import org.bushe.swing.event.EventSubscriber;

/**
 * Class <code>ScoreSheetBridge</code> is in charge of keeping in sync the
 * (sheet) Pixel Selection and the Score Selection. There should be exactly one
 * instance of this class per score (and thus per sheet).
 *
 * @author <NAME>
 * @version $Id$
 */
public class ScoreSheetBridge
    implements EventSubscriber<LocationEvent>
{
    //~ Static fields/initializers ---------------------------------------------

    /** Usual logger utility */
    private static final Logger logger = Logger.getLogger(
        ScoreSheetBridge.class);

    //~ Instance fields --------------------------------------------------------

    /** The sheet instance */
    private final Sheet sheet;

    /** The score instance */
    private final Score score;

    /** The event service to use */
    private final EventService eventService;

    /** Needed to force only one-way sync at a time */
    private volatile boolean bridging;

    //~ Constructors -----------------------------------------------------------

    //------------------//
    // ScoreSheetBridge //
    //------------------//
    /**
     * Creates a new ScoreSheetBridge object.
     *
     * @param score the related score (and thus the related sheet)
     */
    public ScoreSheetBridge (Score score)
    {
        this.score = score;
        sheet = score.getSheet();
        eventService = sheet.getEventService();

        // Register to event service
        eventService.subscribeStrongly(LocationEvent.class, this);
    }

    //~ Methods ----------------------------------------------------------------

    //---------//
    // getName //
    //---------//
    /**
     * Report the name of this observer
     *
     * @return name of the bridge
     */
    public String getName ()
    {
        return "Score-Sheet-Bridge";
    }

    //--------//
    // update //
    //--------//
    /**
     * Notification of selection objects (disabled when already bridging, to
     * avoid endless loop)
     *
     * @param event the notified event
     */
    @Implement(EventSubscriber.class)
    public void onEvent (LocationEvent event)
    {
        if (logger.isFineEnabled()) {
            logger.fine("Bridge : onEvent " + event);
        }

        if (!bridging) {
            bridging = true; // Prevent re-entry

            if (event instanceof SheetLocationEvent) {
                // Forward to Score side
                SheetLocationEvent sheetLocation = (SheetLocationEvent) event;
                ScoreRectangle     scoreRect = null;

                if (sheetLocation.rectangle != null) {
                    PagePoint pagPt = sheet.getScale()
                                           .toPagePoint(
                        new PixelPoint(
                            sheetLocation.rectangle.x +
                            (sheetLocation.rectangle.width / 2),
                            sheetLocation.rectangle.y +
                            (sheetLocation.rectangle.height / 2)));

                    if (pagPt != null) {
                        // Which system ?
                        System     system = score.pageLocateSystem(pagPt);
                        ScorePoint scrPt = system.toScorePoint(pagPt);
                        scoreRect = new ScoreRectangle(scrPt);
                    }
                }

                eventService.publish(
                    new ScoreLocationEvent(
                        this,
                        event.hint,
                        sheetLocation.movement,
                        scoreRect));
            } else if (event instanceof ScoreLocationEvent) {
                // Forward to Sheet side
                ScoreLocationEvent scoreLocation = (ScoreLocationEvent) event;
                PixelRectangle     pixRect = null;

                if (scoreLocation.rectangle != null) {
                    // We forge a ScorePoint from the display point
                    ScorePoint scrPt = new ScorePoint(
                        scoreLocation.rectangle.x,
                        scoreLocation.rectangle.y);

                    // The enclosing system
                    System     system = score.scoreLocateSystem(scrPt);
                    PagePoint  pagPt = system.toPagePoint(scrPt);
                    PixelPoint pixPt = sheet.getScale()
                                            .toPixelPoint(pagPt, null);
                    pixRect = new PixelRectangle(pixPt);
                }

                eventService.publish(
                    new SheetLocationEvent(
                        this,
                        event.hint,
                        scoreLocation.movement,
                        pixRect));
            }

            bridging = false;
        }
    }
}
{'fraction_non_alphanumeric': 0.16059671639303275, 'fraction_numerical': 0.0016668055671305942, 'mean_word_length': 1.193784277879342, 'pattern_counts': {'":': 1, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4111579', 'n_tokens_mistral': 2879, 'n_tokens_neox': 2667, 'n_words': 760}