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
020 package org.apache.isis.core.progmodel.facets.object.title;
021
022 import org.apache.isis.applib.adapters.Parser;
023 import org.apache.isis.applib.profiles.Localization;
024 import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
025 import org.apache.isis.core.metamodel.facetapi.FacetAbstract;
026 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
027 import org.apache.isis.core.metamodel.facets.object.title.TitleFacet;
028 import org.apache.isis.core.metamodel.runtimecontext.DependencyInjector;
029
030 public class TitleFacetUsingParser extends FacetAbstract implements TitleFacet {
031
032 private final Parser parser;
033 private final DependencyInjector dependencyInjector;
034
035 public TitleFacetUsingParser(final Parser parser, final FacetHolder holder,
036 final DependencyInjector dependencyInjector) {
037 super(TitleFacet.class, holder, false);
038 this.parser = parser;
039 this.dependencyInjector = dependencyInjector;
040 }
041
042 @Override
043 protected String toStringValues() {
044 getDependencyInjector().injectDependenciesInto(parser);
045 return parser.toString();
046 }
047
048 @Override
049 public String title(final ObjectAdapter adapter, final Localization localization) {
050 if (adapter == null) {
051 return null;
052 }
053 final Object object = adapter.getObject();
054 if (object == null) {
055 return null;
056 }
057 getDependencyInjector().injectDependenciesInto(parser);
058 return parser.displayTitleOf(object, localization);
059 }
060
061 public String title(final ObjectAdapter adapter, final String usingMask) {
062 if (adapter == null) {
063 return null;
064 }
065 final Object object = adapter.getObject();
066 if (object == null) {
067 return null;
068 }
069 getDependencyInjector().injectDependenciesInto(parser);
070 return parser.displayTitleOf(object, usingMask);
071 }
072
073 // //////////////////////////////////////////////////////
074 // Dependencies (from constructor)
075 // //////////////////////////////////////////////////////
076
077 /**
078 * @return the dependencyInjector
079 */
080 public DependencyInjector getDependencyInjector() {
081 return dependencyInjector;
082 }
083
084 }