001// Generated by delombok at Tue Jul 02 13:17:59 BST 2024 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 jakarta.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 @Inject 036 public PdfJsViewerPanelComponentFactory() { 037 super(UiComponentType.SCALAR_NAME_AND_VALUE, PdfJsViewerPanel.class); 038 } 039 040 @Override 041 public ApplicationAdvice appliesTo(final IModel<?> model) { 042 if (!(model instanceof ScalarModel)) { 043 return ApplicationAdvice.DOES_NOT_APPLY; 044 } 045 final org.apache.causeway.viewer.wicket.model.models.ScalarModel scalarModel = (ScalarModel) model; 046 if (!scalarModel.getMetaModel().containsFacet(PdfJsViewerFacet.class)) { 047 return ApplicationAdvice.DOES_NOT_APPLY; 048 } 049 return appliesIf(isPdf(scalarModel.getObject())); 050 } 051 052 private static boolean isPdf(final ManagedObject objectAdapter) { 053 if (objectAdapter == null) { 054 return false; 055 } 056 final Object objectPojo = objectAdapter.getPojo(); 057 if (!(objectPojo instanceof Blob)) { 058 return false; 059 } 060 final Blob blob = (Blob) objectPojo; 061 return CommonMimeType.PDF.matches(blob.getMimeType()); 062 } 063 064 @Override 065 public Component createComponent(final String id, final IModel<?> model) { 066 return new PdfJsViewerPanel(id, (ScalarModel) model); 067 } 068}