001// Generated by delombok at Mon Oct 02 22:30:47 BST 2023
002/*
003 *  Licensed to the Apache Software Foundation (ASF) under one
004 *  or more contributor license agreements.  See the NOTICE file
005 *  distributed with this work for additional information
006 *  regarding copyright ownership.  The ASF licenses this file
007 *  to you under the Apache License, Version 2.0 (the
008 *  "License"); you may not use this file except in compliance
009 *  with the License.  You may obtain a copy of the License at
010 *
011 *        http://www.apache.org/licenses/LICENSE-2.0
012 *
013 *  Unless required by applicable law or agreed to in writing,
014 *  software distributed under the License is distributed on an
015 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 *  KIND, either express or implied.  See the License for the
017 *  specific language governing permissions and limitations
018 *  under the License.
019 */
020package org.apache.causeway.extensions.pdfjs.wkt.ui.components;
021
022import javax.inject.Inject;
023import org.apache.wicket.Component;
024import org.apache.wicket.model.IModel;
025import org.apache.causeway.applib.value.Blob;
026import org.apache.causeway.applib.value.NamedWithMimeType.CommonMimeType;
027import org.apache.causeway.core.metamodel.object.ManagedObject;
028import org.apache.causeway.extensions.pdfjs.metamodel.facet.PdfJsViewerFacet;
029import org.apache.causeway.viewer.commons.model.components.UiComponentType;
030import org.apache.causeway.viewer.wicket.model.models.ScalarModel;
031import org.apache.causeway.viewer.wicket.ui.ComponentFactoryAbstract;
032
033@org.springframework.stereotype.Component
034public class PdfJsViewerPanelComponentFactory extends ComponentFactoryAbstract {
035    private static final long serialVersionUID = 1L;
036
037    @Inject
038    public PdfJsViewerPanelComponentFactory() {
039        super(UiComponentType.SCALAR_NAME_AND_VALUE, PdfJsViewerPanel.class);
040    }
041
042    @Override
043    public ApplicationAdvice appliesTo(final IModel<?> model) {
044        if (!(model instanceof ScalarModel)) {
045            return ApplicationAdvice.DOES_NOT_APPLY;
046        }
047        final org.apache.causeway.viewer.wicket.model.models.ScalarModel scalarModel = (ScalarModel) model;
048        if (!scalarModel.getMetaModel().containsFacet(PdfJsViewerFacet.class)) {
049            return ApplicationAdvice.DOES_NOT_APPLY;
050        }
051        return appliesIf(isPdf(scalarModel.getObject()));
052    }
053
054    private static boolean isPdf(final ManagedObject objectAdapter) {
055        if (objectAdapter == null) {
056            return false;
057        }
058        final Object objectPojo = objectAdapter.getPojo();
059        if (!(objectPojo instanceof Blob)) {
060            return false;
061        }
062        final Blob blob = (Blob) objectPojo;
063        return CommonMimeType.PDF.matches(blob.getMimeType());
064    }
065
066    @Override
067    public Component createComponent(final String id, final IModel<?> model) {
068        return new PdfJsViewerPanel(id, (ScalarModel) model);
069    }
070}