001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.progmodel.facets.object.title;
021
022import org.apache.isis.applib.adapters.Parser;
023import org.apache.isis.applib.profiles.Localization;
024import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
025import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
026import org.apache.isis.core.metamodel.facetapi.FacetHolder;
027import org.apache.isis.core.metamodel.facets.object.title.TitleFacet;
028import org.apache.isis.core.metamodel.runtimecontext.ServicesInjector;
029
030public class TitleFacetUsingParser extends FacetAbstract implements TitleFacet {
031
032    private final Parser parser;
033    private final ServicesInjector dependencyInjector;
034
035    public TitleFacetUsingParser(final Parser parser, final FacetHolder holder, final ServicesInjector dependencyInjector) {
036        super(TitleFacet.class, holder, Derivation.NOT_DERIVED);
037        this.parser = parser;
038        this.dependencyInjector = dependencyInjector;
039    }
040
041    @Override
042    protected String toStringValues() {
043        getDependencyInjector().injectServicesInto(parser);
044        return parser.toString();
045    }
046
047    @Override
048    public String title(final ObjectAdapter adapter, final Localization localization) {
049        if (adapter == null) {
050            return null;
051        }
052        final Object object = adapter.getObject();
053        if (object == null) {
054            return null;
055        }
056        getDependencyInjector().injectServicesInto(parser);
057        return parser.displayTitleOf(object, localization);
058    }
059
060    @Override
061    public String title(ObjectAdapter contextAdapter, ObjectAdapter targetAdapter, Localization localization) {
062        return title(targetAdapter, localization);
063    }
064
065    /**
066     * not API
067     */
068    public String title(final ObjectAdapter adapter, final String usingMask) {
069        if (adapter == null) {
070            return null;
071        }
072        final Object object = adapter.getObject();
073        if (object == null) {
074            return null;
075        }
076        getDependencyInjector().injectServicesInto(parser);
077        return parser.displayTitleOf(object, usingMask);
078    }
079
080
081    // //////////////////////////////////////////////////////
082    // Dependencies (from constructor)
083    // //////////////////////////////////////////////////////
084
085    /**
086     * @return the dependencyInjector
087     */
088    public ServicesInjector getDependencyInjector() {
089        return dependencyInjector;
090    }
091
092
093}