1 /* 2 * This file is part of the LibreOffice project. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 10 * Licensed to the Apache Software Foundation (ASF) under one or more 11 * contributor license agreements. See the NOTICE file distributed 12 * with this work for additional information regarding copyright 13 * ownership. The ASF licenses this file to you under the Apache 14 * License, Version 2.0 (the "License"); you may not use this file 15 * except in compliance with the License. You may obtain a copy of 16 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 17 */ 18 package org.libreoffice.report.pentaho.model; 19 20 import java.io.Serializable; 21 22 import java.util.ArrayList; 23 import java.util.HashMap; 24 import java.util.List; 25 import java.util.Map; 26 27 import org.jfree.report.structure.Element; 28 29 30 /** 31 * Holds one style type, either an automatic, common or master style. This is a 32 * marker container that defines the nature of the styles contained within this 33 * container. (Yeah, it is awkward, but that's how the document model describes 34 * it.) 35 * 36 * The style family can be one of paragraph, text, section, table, table-column, 37 * table-row, table-cell, table-page, chart, default, drawing-page, graphic, 38 * presentation, control and ruby. 39 * 40 * @since 07.03.2007 41 */ 42 public class OfficeStyles extends Element 43 { 44 45 private static class StyleKey implements Serializable 46 { 47 48 private static final long serialVersionUID = 4931878927362887477L; 49 private final String family; 50 private final String name; 51 StyleKey(final String family, final String name)52 private StyleKey(final String family, final String name) 53 { 54 if (family == null) 55 { 56 throw new NullPointerException(); 57 } 58 this.family = family; 59 this.name = name; 60 } 61 62 @Override equals(final Object obj)63 public boolean equals(final Object obj) 64 { 65 if (this != obj) 66 { 67 if (obj == null || getClass() != obj.getClass()) 68 { 69 return false; 70 } 71 72 final StyleKey styleKey = (StyleKey) obj; 73 74 if (!family.equals(styleKey.family) || (name != null ? !name.equals(styleKey.name) : styleKey.name != null)) 75 { 76 return false; 77 } 78 } 79 return true; 80 } 81 82 @Override hashCode()83 public int hashCode() 84 { 85 int result = family.hashCode(); 86 result = 31 * result + (name != null ? name.hashCode() : 0); 87 return result; 88 } 89 } 90 private final Map<String,PageLayout> pageStyles; 91 private final Map<String,DataStyle> dataStyles; 92 private final Map<StyleKey,OfficeStyle> styles; 93 private final List<Element> otherChildren; 94 OfficeStyles()95 public OfficeStyles() 96 { 97 this.styles = new HashMap<StyleKey,OfficeStyle>(); 98 this.dataStyles = new HashMap<String,DataStyle>(); 99 this.pageStyles = new HashMap<String,PageLayout>(); 100 this.otherChildren = new ArrayList<Element>(); 101 } 102 getStyle(final String family, final String name)103 public OfficeStyle getStyle(final String family, final String name) 104 { 105 return styles.get(new StyleKey(family, name)); 106 } 107 addStyle(final OfficeStyle style)108 public void addStyle(final OfficeStyle style) 109 { 110 if (style == null) 111 { 112 throw new NullPointerException(); 113 } 114 final String styleFamily = style.getStyleFamily(); 115 if (styleFamily == null) 116 { 117 throw new NullPointerException(); 118 } 119 if (style.getStyleName() == null) 120 { 121 throw new NullPointerException(); 122 } 123 styles.put(new StyleKey(styleFamily, style.getStyleName()), style); 124 } 125 addPageStyle(final PageLayout style)126 public void addPageStyle(final PageLayout style) 127 { 128 pageStyles.put(style.getStyleName(), style); 129 } 130 getPageStyle(final String name)131 public PageLayout getPageStyle(final String name) 132 { 133 return pageStyles.get(name); 134 } 135 addDataStyle(final DataStyle style)136 public void addDataStyle(final DataStyle style) 137 { 138 dataStyles.put(style.getStyleName(), style); 139 } 140 getDataStyle(final String name)141 public DataStyle getDataStyle(final String name) 142 { 143 return dataStyles.get(name); 144 } 145 addOtherNode(final Element node)146 public void addOtherNode(final Element node) 147 { 148 otherChildren.add(node); 149 } 150 getAllDataStyles()151 public DataStyle[] getAllDataStyles() 152 { 153 return dataStyles.values().toArray(new DataStyle[dataStyles.size()]); 154 } 155 getAllPageStyles()156 public PageLayout[] getAllPageStyles() 157 { 158 return pageStyles.values().toArray(new PageLayout[pageStyles.size()]); 159 } 160 getAllStyles()161 public OfficeStyle[] getAllStyles() 162 { 163 return styles.values().toArray(new OfficeStyle[styles.size()]); 164 } 165 getOtherStyles()166 public Element[] getOtherStyles() 167 { 168 return otherChildren.toArray(new Element[otherChildren.size()]); 169 } 170 containsStyle(final String family, final String name)171 public boolean containsStyle(final String family, final String name) 172 { 173 return styles.containsKey(new StyleKey(family, name)); 174 } 175 containsDataStyle(final String styleName)176 public boolean containsDataStyle(final String styleName) 177 { 178 return dataStyles.containsKey(styleName); 179 } 180 } 181