private boolean isPredicateApplicable(Predicate predicate, DocumentWindow documentWindow) { Object document = documentWindow.getDocument(); Object view = documentWindow.getView(); Class<?> actualDocType = document != null ? document.getClass() : Object.class; Class<?> actualViewType = view != null ? view.getClass() : Object.class; Class<?> requestedDocType = predicate.getDocType(); Class<?> requestedViewType = predicate.getViewType(); return requestedDocType.isAssignableFrom(actualDocType) && requestedViewType.isAssignableFrom(actualViewType); }
/** * Gets the document window listeners registered for the given window selector. * * @param documentWindow The document window. * @return The array of listeners which may be empty. */ final Listener[] getListeners(DocumentWindow documentWindow) { ArrayList<Listener> listeners = new ArrayList<>(); for (Map.Entry<Listener, Set<Predicate>> entry : this.listeners.entrySet()) { Listener listener = entry.getKey(); Set<Predicate> predicates = entry.getValue(); for (Predicate predicate : predicates) { if (isPredicateApplicable(predicate, documentWindow) && predicate.test(documentWindow)) { listeners.add(listener); break; } } } return listeners.toArray(new Listener[listeners.size()]); }
public PixelPosStatusLineElementProvider() { DocumentWindowManager.getDefault().addListener(DocumentWindowManager.Predicate.view(ProductSceneView.class), this); SnapApp.getDefault().getPreferences().addPreferenceChangeListener(this); updateSettings(); pixelPosLabel = new JLabel(); pixelPosLabel.setPreferredSize(new Dimension(120, 20)); pixelPosLabel.setHorizontalAlignment(SwingConstants.CENTER); geoPosLabel = new JLabel(); geoPosLabel.setPreferredSize(new Dimension(200, 20)); geoPosLabel.setHorizontalAlignment(SwingConstants.CENTER); zoomLevelLabel = new JLabel(); zoomLevelLabel.setPreferredSize(new Dimension(150, 20)); zoomLevelLabel.setHorizontalAlignment(SwingConstants.CENTER); panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); panel.add(Box.createHorizontalGlue()); panel.add(new JSeparator(SwingConstants.VERTICAL)); panel.add(pixelPosLabel); panel.add(new JSeparator(SwingConstants.VERTICAL)); panel.add(geoPosLabel); panel.add(new JSeparator(SwingConstants.VERTICAL)); panel.add(zoomLevelLabel); }
/** * Adds a document window listener for any document type. * * @param listener The listener. */ public final void addListener(Listener listener) { //noinspection unchecked addListener(Predicate.any(), listener); }