1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /*
25  * IconSet.java
26  *
27  * Created on 12. September 2003, 17:16
28  */
29 package com.sun.star.wizards.web.data;
30 
31 import java.util.Hashtable;
32 import java.util.Map;
33 import java.util.Vector;
34 
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.container.NoSuchElementException;
37 import com.sun.star.container.XNameAccess;
38 import com.sun.star.i18n.NumberFormatIndex;
39 import com.sun.star.lang.WrappedTargetException;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.util.DateTime;
43 import com.sun.star.wizards.common.*;
44 import com.sun.star.wizards.common.Helper.DateUtils;
45 
46 /**
47  * @author  rpiterman
48  */
49 public class CGSettings extends ConfigGroup
50 {
51 
52     public String soTemplateDir;
53     public String soGalleryDir;
54     public String workPath = null;
55     public String cp_WorkDir;
56     public ConfigSet cp_Exporters = new ConfigSet(CGExporter.class);
57     public ConfigSet cp_Layouts = new ConfigSet(CGLayout.class);
58     public ConfigSet cp_Styles = new ConfigSet(CGStyle.class);
59     public ConfigSet cp_IconSets = new ConfigSet(CGIconSet.class);
60     public ConfigSet cp_BackgroundImages = new ConfigSet(CGImage.class);
61     public ConfigSet cp_SavedSessions = new ConfigSet(CGSessionName.class);
62     public ConfigSet cp_Filters = new ConfigSet(CGFilter.class);
63     public ConfigSet savedSessions = new ConfigSet(CGSessionName.class);
64     public CGSession cp_DefaultSession = new CGSession();
65     public String cp_LastSavedSession;
66     private Map exportersMap = new Hashtable();
67     private XMultiServiceFactory xmsf;
68     String[] resources;
69     public Formatter formatter;
70     public static final int RESOURCE_PAGES_TEMPLATE = 0;
71     public static final int RESOURCE_SLIDES_TEMPLATE = 1;
72     public static final int RESOURCE_CREATED_TEMPLATE = 2;
73     public static final int RESOURCE_UPDATED_TEMPLATE = 3;
74     public static final int RESOURCE_SIZE_TEMPLATE = 4;
75 
CGSettings(XMultiServiceFactory xmsf_, String[] resources_, Object document)76     public CGSettings(XMultiServiceFactory xmsf_, String[] resources_, Object document)
77     {
78         xmsf = xmsf_;
79         try
80         {
81             soTemplateDir = FileAccess.getOfficePath(xmsf, "Config", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING);
82             soGalleryDir = FileAccess.getOfficePath(xmsf, "Gallery", "share", PropertyNames.EMPTY_STRING);
83             root = this;
84             formatter = new Formatter(xmsf, document);
85             resources = resources_;
86         }
87         catch (Exception ex)
88         {
89             ex.printStackTrace();
90         }
91     }
92     private static final CGExporter[] EMPTY_ARRAY_1 = new CGExporter[0];
93 
getExporters(String mime)94     public CGExporter[] getExporters(String mime)
95     {
96         CGExporter[] exps = (CGExporter[]) exportersMap.get(mime);
97         if (exps == null)
98         {
99             exportersMap.put(mime, exps = createExporters(mime));
100         }
101         return exps;
102     }
103 
createExporters(String mime)104     private CGExporter[] createExporters(String mime)
105     {
106         Object[] exporters = cp_Exporters.items();
107         Vector v = new Vector();
108         for (int i = 0; i < exporters.length; i++)
109         {
110             if (((CGExporter) exporters[i]).supports(mime))
111             {
112                 try
113                 {
114                     v.add(exporters[i]);
115                 }
116                 catch (Exception ex)
117                 {
118                     ex.printStackTrace();
119                 }
120             }
121         }
122         return (CGExporter[]) v.toArray(new CGExporter[v.size()]);
123     }
124 
125     /**
126      * call after read.
127      * @param xmsf
128      * @param document the background document. used for date/number formatting.
129      */
configure(XMultiServiceFactory xmsf)130     public void configure(XMultiServiceFactory xmsf) throws Exception
131     {
132         workPath = FileAccess.connectURLs(soTemplateDir, cp_WorkDir);
133         calcExportersTargetTypeNames(xmsf);
134     }
135 
calcExportersTargetTypeNames(XMultiServiceFactory xmsf)136     private void calcExportersTargetTypeNames(XMultiServiceFactory xmsf) throws Exception
137     {
138         Object typeDetect = xmsf.createInstance("com.sun.star.document.TypeDetection");
139         XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, typeDetect);
140         for (int i = 0; i < cp_Exporters.getSize(); i++)
141         {
142             calcExporterTargetTypeName(xNameAccess, (CGExporter) cp_Exporters.getElementAt(i));
143         }
144     }
145 
calcExporterTargetTypeName(XNameAccess xNameAccess, CGExporter exporter)146     private void calcExporterTargetTypeName(XNameAccess xNameAccess, CGExporter exporter)
147             throws NoSuchElementException,
148             WrappedTargetException
149     {
150         if (!exporter.cp_TargetType.equals(PropertyNames.EMPTY_STRING))
151         {
152             exporter.targetTypeName =
153                     (String) Properties.getPropertyValue(
154                     (PropertyValue[]) xNameAccess.getByName(exporter.cp_TargetType),
155                     "UIName");
156         }
157     }
158     FileAccess fileAccess;
159 
getFileAccess()160     FileAccess getFileAccess() throws Exception
161     {
162         return getFileAccess(xmsf);
163     }
164 
getFileAccess(XMultiServiceFactory xmsf)165     FileAccess getFileAccess(XMultiServiceFactory xmsf) throws Exception
166     {
167         if (fileAccess == null)
168         {
169             fileAccess = new FileAccess(xmsf);
170         }
171         return fileAccess;
172     }
173 
174     public class Formatter
175     {
176 
177         private long docNullTime;
178         private int dateFormat,  numberFormat;
179         private DateUtils dateUtils;
180 
Formatter(XMultiServiceFactory xmsf, Object document)181         public Formatter(XMultiServiceFactory xmsf, Object document) throws Exception
182         {
183             dateUtils = new DateUtils(xmsf, document);
184             dateFormat = dateUtils.getFormat(NumberFormatIndex.DATE_SYS_DMMMYYYY);
185             numberFormat = dateUtils.getFormat(NumberFormatIndex.NUMBER_1000DEC2);
186         }
187 
formatCreated(DateTime date)188         public String formatCreated(DateTime date)
189         {
190             String sDate = dateUtils.format(dateFormat, date);
191             return JavaTools.replaceSubString(resources[CGSettings.RESOURCE_CREATED_TEMPLATE], sDate, "%DATE");
192         }
193 
formatUpdated(DateTime date)194         public String formatUpdated(DateTime date)
195         {
196             String sDate = dateUtils.format(dateFormat, date);
197             return JavaTools.replaceSubString(resources[CGSettings.RESOURCE_UPDATED_TEMPLATE], sDate, "%DATE");
198         }
199 
formatFileSize(int size)200         public String formatFileSize(int size)
201         {
202             float sizeInKB = ((float) size) / 1024f;
203             String sSize = dateUtils.getFormatter().convertNumberToString(numberFormat, sizeInKB);
204             return JavaTools.replaceSubString(resources[CGSettings.RESOURCE_SIZE_TEMPLATE], sSize, "%NUMBER");
205         }
206     }
207 }
208