@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; AdapterKey<?> other = (AdapterKey<?>) obj; return key.equals(other.getKey()) && role.equals(other.role); }
@Override protected void attachToAnchorageVisual(IVisualPart<Node, ? extends Node> anchorage, String role) { @SuppressWarnings("serial") Provider<? extends IFXAnchor> anchorProvider = anchorage.getAdapter(AdapterKey.get(new TypeToken<Provider<? extends IFXAnchor>>() {})); IFXAnchor anchor = anchorProvider == null ? null : anchorProvider.get(); if (role.equals("START")) { getVisual().setStartAnchor(anchor); } else if (role.equals("END")) { getVisual().setEndAnchor(anchor); } else { throw new IllegalStateException("Cannot attach to anchor with role <" + role + ">."); } }
@Override public void setContent(Object content) { super.setContent(content); if (content == null) { return; } if (!(content instanceof Edge)) { throw new IllegalArgumentException("Content of wrong type!"); } final FXConnection visual = getVisual(); setAdapter( AdapterKey.get( Provider.class, FXDefaultFeedbackPartFactory.SELECTION_FEEDBACK_GEOMETRY_PROVIDER), new Provider<IGeometry>() { @Override public IGeometry get() { return FXUtils.localToParent( visual, FXUtils.localToParent( visual.getCurveNode(), ((FXGeometryNode<?>) visual.getCurveNode()).getGeometry())); } }); setAdapter( AdapterKey.get( Provider.class, FXDefaultFeedbackPartFactory.HOVER_FEEDBACK_GEOMETRY_PROVIDER), new Provider<IGeometry>() { @Override public IGeometry get() { return FXUtils.localToParent( visual, FXUtils.localToParent( visual.getCurveNode(), ((FXGeometryNode<?>) visual.getCurveNode()).getGeometry())); } }); }
@Override public IUndoableOperation commit() { final IUndoableOperation updateVisualOperation = super.commit(); if (updateVisualOperation == null) { return null; } // commit changes to model final FXGeometricShapePart host = getHost(); final FXGeometricShape hostContent = host.getContent(); // determine transformation @SuppressWarnings("serial") Provider<Affine> affineProvider = host.getAdapter( AdapterKey.<Provider<? extends Affine>>get( new TypeToken<Provider<? extends Affine>>() {}, FXTransformPolicy.TRANSFORMATION_PROVIDER_ROLE)); AffineTransform tx = JavaFX2Geometry.toAffineTransform(affineProvider.get()); final AffineTransform oldTransform = hostContent.getTransform(); final AffineTransform newTransform = new AffineTransform( tx.getM00(), tx.getM10(), tx.getM01(), tx.getM11(), tx.getTranslateX(), tx.getTranslateY()); // create operation to write the changes to the model final IUndoableOperation updateModelOperation = new AbstractOperation("Update Model") { @Override public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { hostContent.setTransform(newTransform); return Status.OK_STATUS; } @Override public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { return execute(monitor, info); } @Override public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { hostContent.setTransform(oldTransform); return Status.OK_STATUS; } }; // compose operations IUndoableOperation compositeOperation = new ForwardUndoCompositeOperation(updateVisualOperation.getLabel()) { { add(updateVisualOperation); add(updateModelOperation); } }; return compositeOperation; }
@SuppressWarnings("serial") protected List<IHandlePart<Node, ? extends Node>> createSingleSelectionHandleParts( final IVisualPart<Node, ? extends Node> target, Map<Object, Object> contextMap) { List<IHandlePart<Node, ? extends Node>> handleParts = new ArrayList<IHandlePart<Node, ? extends Node>>(); // handle geometry is in target visual local coordinate space. final Provider<IGeometry> selectionHandlesGeometryInTargetLocalProvider = target.<Provider<IGeometry>>getAdapter( AdapterKey.get( new TypeToken<Provider<? extends IGeometry>>() {}, SELECTION_HANDLES_GEOMETRY_PROVIDER)); // generate handles from selection handles geometry IGeometry selectionHandlesGeometry = (selectionHandlesGeometryInTargetLocalProvider != null) ? selectionHandlesGeometryInTargetLocalProvider.get() : null; if (selectionHandlesGeometry == null) { return handleParts; // empty } // we will need a provider that returns the geometry in scene // coordinates final Provider<IGeometry> selectionHandlesGeometryInSceneProvider = new Provider<IGeometry>() { @Override public IGeometry get() { return FXUtils.localToScene( target.getVisual(), selectionHandlesGeometryInTargetLocalProvider.get()); } }; Provider<BezierCurve[]> selectionHandlesSegmentsInSceneProvider = createSegmentsProvider(selectionHandlesGeometryInSceneProvider); if (selectionHandlesGeometry instanceof ICurve) { // assure the geometry provider that is handed over returns the // geometry in scene coordinates handleParts.addAll( createCurveSelectionHandleParts( target, selectionHandlesSegmentsInSceneProvider, contextMap)); } else if (selectionHandlesGeometry instanceof IShape) { if (selectionHandlesGeometry instanceof Rectangle) { // create corner handles handleParts.addAll( createTightBoundsSelectionHandleParts( Collections.singletonList(target), selectionHandlesSegmentsInSceneProvider, contextMap)); } else { // create segment handles (based on outline) BezierCurve[] segments = selectionHandlesSegmentsInSceneProvider.get(); for (int i = 0; i < segments.length; i++) { IHandlePart<Node, ? extends Node> hp = createSelectionSegmentHandlePart( target, selectionHandlesSegmentsInSceneProvider, segments.length, i, contextMap); if (hp != null) { handleParts.add(hp); } } } } else { throw new IllegalStateException( "Unable to generate handles for this handle geometry. Expected ICurve or IShape, but got: " + selectionHandlesGeometry); } return handleParts; }
@SuppressWarnings("serial") protected List<IHandlePart<Node, ? extends Node>> createHoverHandleParts( final IVisualPart<Node, ? extends Node> target, final HoverBehavior<Node> contextBehavior, final Map<Object, Object> contextMap) { List<IHandlePart<Node, ? extends Node>> handleParts = new ArrayList<IHandlePart<Node, ? extends Node>>(); // handle geometry is in target visual local coordinate space. final Provider<? extends IGeometry> hoverHandlesGeometryInTargetLocalProvider = target.getAdapter( AdapterKey.get( new TypeToken<Provider<? extends IGeometry>>() {}, HOVER_HANDLES_GEOMETRY_PROVIDER)); // generate handles from selection handles geometry IGeometry hoverHandlesGeometry = (hoverHandlesGeometryInTargetLocalProvider != null) ? hoverHandlesGeometryInTargetLocalProvider.get() : null; if (hoverHandlesGeometry == null) { return handleParts; // empty } // we will need a provider that returns the geometry in scene // coordinates final Provider<? extends IGeometry> hoverHandlesGeometryInSceneProvider = new Provider<IGeometry>() { @Override public IGeometry get() { return FXUtils.localToScene( target.getVisual(), hoverHandlesGeometryInTargetLocalProvider.get()); } }; // the handle parts are located based on the segments of the handle // geometry Provider<BezierCurve[]> hoverHandlesSegmentsInSceneProvider = new Provider<BezierCurve[]>() { @Override public BezierCurve[] get() { IGeometry handleGeometry = hoverHandlesGeometryInSceneProvider.get(); if (handleGeometry instanceof IShape) { List<BezierCurve> segments = new ArrayList<BezierCurve>(); for (ICurve os : ((IShape) handleGeometry).getOutlineSegments()) { segments.addAll(Arrays.asList(os.toBezier())); } return segments.toArray(new BezierCurve[] {}); } else if (handleGeometry instanceof ICurve) { return ((ICurve) handleGeometry).toBezier(); } else { throw new IllegalStateException( "Unable to determine handle position: Expected IShape or ICurve but got: " + handleGeometry); } } }; // create segment handles (based on outline) BezierCurve[] segments = hoverHandlesSegmentsInSceneProvider.get(); for (int i = 0; i < segments.length; i++) { IHandlePart<Node, ? extends Node> hp = createHoverSegmentHandlePart( target, hoverHandlesSegmentsInSceneProvider, segments.length, i, contextMap); if (hp != null) { handleParts.add(hp); } } return handleParts; }
/** * Adds (default) {@link AdapterMap} bindings for {@link AbstractViewer} and all sub-classes. May * be overwritten by sub-classes to change the default bindings. * * @param adapterMapBinder The {@link MapBinder} to be used for the binding registration. In this * case, will be obtained from {@link AdapterMaps#getAdapterMapBinder(Binder, Class)} using * {@link AbstractViewer} as a key. * @see AdapterMaps#getAdapterMapBinder(Binder, Class) */ protected void bindAbstractViewerAdapters(MapBinder<AdapterKey<?>, Object> adapterMapBinder) { // bind (default) viewer models as adapters adapterMapBinder.addBinding(AdapterKey.get(ContentModel.class)).to(ContentModel.class); adapterMapBinder.addBinding(AdapterKey.get(ViewportModel.class)).to(ViewportModel.class); adapterMapBinder.addBinding(AdapterKey.get(GridModel.class)).to(GridModel.class); }