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 package com.sun.star.wizards.form;
24 
25 import com.sun.star.awt.XListBox;
26 import com.sun.star.awt.XRadioButton;
27 import com.sun.star.beans.XPropertySet;
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.lang.EventObject;
30 import com.sun.star.lang.IllegalArgumentException;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.AnyConverter;
33 import com.sun.star.uno.Exception;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 import com.sun.star.wizards.common.Configuration;
37 import com.sun.star.wizards.common.Desktop;
38 import com.sun.star.wizards.common.FileAccess;
39 import com.sun.star.wizards.common.Helper;
40 import com.sun.star.wizards.common.JavaTools;
41 import com.sun.star.wizards.common.NoValidPathException;
42 import com.sun.star.wizards.common.PropertyNames;
43 import com.sun.star.wizards.document.Control;
44 import com.sun.star.wizards.document.DatabaseControl;
45 import com.sun.star.wizards.document.GridControl;
46 import com.sun.star.wizards.document.TimeStampControl;
47 import com.sun.star.wizards.text.TextStyleHandler;
48 import com.sun.star.wizards.ui.*;
49 import com.sun.star.wizards.ui.UIConsts;
50 import java.util.ArrayList;
51 
52 // TODO: Style Templates fuer OOo?
53 
54 public class StyleApplier
55 {
56 
57     private WizardDialog CurUnoDialog;
58     private XPropertySet xPageStylePropertySet;
59     private XMultiServiceFactory xMSF;
60     private short curtabindex;
61     private XRadioButton optNoBorder;
62     private XRadioButton opt3DLook;
63     private XRadioButton optFlat;
64     private XListBox lstStyles;
65     private Desktop.OfficePathRetriever curofficepath;//  String[][] sLayoutFiles;
66     private FormDocument curFormDocument;
67     private short iOldLayoutPos;
68     private int SOLAYOUTLST = 0;
69     private static final String SCHANGELAYOUT = "changeLayout";
70     private static final String SCHANGEBORDERTYPE = "changeBorderLayouts";
71     private String[] StyleNames;
72     private String[] StyleNodeNames;
73     private String[] FileNames;
74     // private String StylesPath;
75     private final static int SOBACKGROUNDCOLOR = 0;
76     private final static int SODBTEXTCOLOR = 1;
77     private final static int SOLABELTEXTCOLOR = 2;
78 //  final static int SODBCONTROLBACKGROUNDCOLOR = 3;
79     private final static int SOLABELBACKGROUNDCOLOR = 4;
80     private final static int SOBORDERCOLOR = 5;
81     private Short IBorderValue = new Short((short) 1);
82 
StyleApplier(WizardDialog _CurUnoDialog, FormDocument _curFormDocument)83     public StyleApplier(WizardDialog _CurUnoDialog, FormDocument _curFormDocument) throws NoValidPathException
84     {
85 //        try
86 //        {
87             this.curFormDocument = _curFormDocument;
88             xMSF = curFormDocument.xMSF;
89 
90             TextStyleHandler oTextStyleHandler = new TextStyleHandler(xMSF, curFormDocument.xTextDocument);
91             xPageStylePropertySet = oTextStyleHandler.getStyleByName("PageStyles", "Standard");
92             this.CurUnoDialog = _CurUnoDialog;
93             curtabindex = (short) (FormWizard.SOSTYLE_PAGE * 100);
94             Integer IStyleStep = new Integer(FormWizard.SOSTYLE_PAGE);
95             String sPageStyles = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 86);
96             String sNoBorder = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 29);
97             String s3DLook = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 30);
98             String sFlat = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 31);
99             String sFieldBorder = CurUnoDialog.m_oResource.getResText(UIConsts.RID_FORM + 28);
100 //            XInterface xUcbInterface = (XInterface) _curFormDocument.xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
101             setStyles();
102             short[] SelLayoutPos;
103             SelLayoutPos = new short[]
104                     {
105                         0
106                     };
107 
108             CurUnoDialog.insertLabel("lblStyles",
109                     new String[]
110                     {
111                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
112                     },
113                     new Object[]
114                     {
115                         UIConsts.INTEGERS[8], sPageStyles, 92, 25, IStyleStep, new Short(curtabindex++), 90
116                     });
117 
118             lstStyles = CurUnoDialog.insertListBox("lstStyles", null, SCHANGELAYOUT, this,
119                     new String[]
120                     {
121                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.SELECTED_ITEMS, PropertyNames.PROPERTY_STEP, PropertyNames.STRING_ITEM_LIST, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
122                     },
123                     new Object[]
124                     {
125                         143, "HID:WIZARDS_HID_DLGFORM_LSTSTYLES", 92, 35, SelLayoutPos, IStyleStep, this.StyleNames, new Short(curtabindex++), 90
126                     });
127 
128             optNoBorder = CurUnoDialog.insertRadioButton("otpNoBorder", SCHANGEBORDERTYPE, this,
129                     new String[]
130                     {
131                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Tag", PropertyNames.PROPERTY_WIDTH
132                     },
133                     new Object[]
134                     {
135                         UIConsts.INTEGERS[10], "HID:WIZARDS_HID_DLGFORM_CMDNOBORDER", sNoBorder, 196, 39, IStyleStep, new Short(curtabindex++), "0", 93
136                     });
137 
138             opt3DLook = CurUnoDialog.insertRadioButton("otp3DLook", SCHANGEBORDERTYPE, this,
139                     new String[]
140                     {
141                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STATE, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Tag", PropertyNames.PROPERTY_WIDTH
142                     },
143                     new Object[]
144                     {
145                         UIConsts.INTEGERS[10], "HID:WIZARDS_HID_DLGFORM_CMD3DBORDER", s3DLook, 196, 53, new Short((short) 1), IStyleStep, new Short(curtabindex++), "1", 93
146                     });
147 
148             optFlat = CurUnoDialog.insertRadioButton("otpFlat", SCHANGEBORDERTYPE, this,
149                     new String[]
150                     {
151                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Tag", PropertyNames.PROPERTY_WIDTH
152                     },
153                     new Object[]
154                     {
155                         UIConsts.INTEGERS[10], "HID:WIZARDS_HID_DLGFORM_CMDSIMPLEBORDER", sFlat, 196, 67, IStyleStep, new Short(curtabindex++), "2", 93
156                     });
157 
158             CurUnoDialog.insertFixedLine("lnFieldBorder",
159                     new String[]
160                     {
161                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
162                     },
163                     new Object[]
164                     {
165                         UIConsts.INTEGERS[8], sFieldBorder, 192, 25, IStyleStep, new Short(curtabindex++), 98
166                     });
167 //        }
168 //        catch (Exception e)
169 //        {
170 //            e.printStackTrace(System.out);
171 //        }
172     }
173 
174     /*  public void initialize(short _iStyleindex){
175     if (_iStyleindex < lstStyles.getItemCount()){
176     Helper.setUnoPropertyValue(UnoDialog.getModel(lstStyles), PropertyNames.SELECTED_ITEMS, new short[]{_iStyleindex});
177     applyStyle(true, false);
178     }
179     }
180      */
setStyles()181     private void setStyles()
182     {
183         try
184         {
185             Object oRootNode = Configuration.getConfigurationRoot(xMSF, "org.openoffice.Office.FormWizard/FormWizard/Styles", false);
186             XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, oRootNode);
187             StyleNodeNames = xNameAccess.getElementNames();
188             StyleNames = new String[StyleNodeNames.length];
189             FileNames = new String[StyleNodeNames.length];
190             for (int i = 0; i < StyleNodeNames.length; i++)
191             {
192                 Object oStyleNode = xNameAccess.getByName(StyleNodeNames[i]);
193                 StyleNames[i] = (String) Helper.getUnoPropertyValue(oStyleNode, PropertyNames.PROPERTY_NAME);
194                 FileNames[i] = (String) Helper.getUnoPropertyValue(oStyleNode, "CssHref");
195             }
196         }
197         catch (Exception e)
198         {
199             e.printStackTrace(System.out);
200         }
201     }
202 
getStyleIndex()203     private short getStyleIndex()
204     {
205         try
206         {
207             short[] SelFields = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(lstStyles), PropertyNames.SELECTED_ITEMS));
208             if (SelFields != null)
209             {
210                 return SelFields[0];
211             }
212         }
213         catch (IllegalArgumentException e)
214         {
215             e.printStackTrace(System.out);
216         }
217         return (short) -1;
218     }
219 
applyStyle(boolean _bmodifyBackground, boolean _bapplyalways)220     public void applyStyle(boolean _bmodifyBackground, boolean _bapplyalways)
221     {
222         short iStyle = getStyleIndex();
223         if ((iStyle != iOldLayoutPos) || _bapplyalways)
224         {
225             if (iStyle > -1)
226             {
227                 iOldLayoutPos = iStyle;
228                 String sFileName = FileNames[iStyle]; //Style =  lstStyles.getSelectedItem();
229                 int[] iStyles = getStyleColors(sFileName);
230                 applyDBControlProperties(iStyles);
231             }
232         }
233     }
234 
changeLayout()235     public void changeLayout()
236     {
237         short iPos = lstStyles.getSelectedItemPos();
238         if (iPos != iOldLayoutPos)
239         {
240             iOldLayoutPos = iPos;
241             String sFileName = FileNames[iPos]; //Style =  lstStyles.getSelectedItem();
242             int[] iStyles = getStyleColors(sFileName);
243             applyDBControlProperties(iStyles);
244         }
245         curFormDocument.unlockallControllers();
246     }
247 
248     /*  public void changeLayout(){
249     /       curFormDocument.xTextDocument.lockControllers();
250     applyStyle(true, false);
251     curFormDocument.unlockallControllers();
252     }
253      */
getBorderType()254     public Short getBorderType()
255     {
256         return IBorderValue;
257     }
258 
changeBorderLayouts()259     public void changeBorderLayouts()
260     {
261         try
262         {
263             curFormDocument.xTextDocument.lockControllers();
264 
265             if (optNoBorder.getState())
266             {
267                 IBorderValue = new Short((short) 0);
268             }
269             else if (opt3DLook.getState())
270             {
271                 IBorderValue = new Short((short) 1);
272             }
273             else
274             {
275                 IBorderValue = new Short((short) 2);
276             }
277             for (int m = 0; m < curFormDocument.oControlForms.size(); m++)
278             {
279                 FormDocument.ControlForm curControlForm = ((FormDocument.ControlForm) curFormDocument.oControlForms.get(m));
280                 if (curControlForm.getArrangemode() == FormWizard.AS_GRID)
281                 {
282                     GridControl oGridControl = curControlForm.getGridControl();
283                     oGridControl.xPropertySet.setPropertyValue(PropertyNames.PROPERTY_BORDER, IBorderValue);
284                 }
285                 else
286                 {
287                     DatabaseControl[] DBControls = curControlForm.getDatabaseControls();
288                     for (int n = 0; n < DBControls.length; n++)
289                     {
290                         if (DBControls[n].xServiceInfo.supportsService("com.sun.star.drawing.ShapeCollection"))
291                         {
292                             TimeStampControl oTimeStampControl = (TimeStampControl) DBControls[n];
293                             for (int i = 0; i < 2; i++)
294                             {
295                                 XPropertySet xPropertySet = oTimeStampControl.getControlofGroupShapeByIndex(i);
296                                 if (xPropertySet.getPropertySetInfo().hasPropertyByName(PropertyNames.PROPERTY_BORDER))
297                                 {
298                                     xPropertySet.setPropertyValue(PropertyNames.PROPERTY_BORDER, IBorderValue);
299                                 }
300                             }
301                         }
302                         else
303                         {
304                             if (DBControls[n].xPropertySet.getPropertySetInfo().hasPropertyByName(PropertyNames.PROPERTY_BORDER))
305                             {
306                                 DBControls[n].xPropertySet.setPropertyValue(PropertyNames.PROPERTY_BORDER, IBorderValue);
307                             }
308                         }
309                     }
310                 }
311             }
312         }
313         catch (Exception e)
314         {
315             e.printStackTrace(System.out);
316         }
317         curFormDocument.unlockallControllers();
318     }
319 
disposing(EventObject eventObject)320     public void disposing(EventObject eventObject)
321     {
322     }
323 
getStyleColor(String[] _sDataList, String _sHeader, String _sPropertyDescription)324     private int getStyleColor(String[] _sDataList, String _sHeader, String _sPropertyDescription)
325     {
326         int iColor = -1;
327         int index = JavaTools.FieldInList(_sDataList, _sHeader);
328         if (index > -1)
329         {
330             String sPropName = PropertyNames.EMPTY_STRING;
331             int iStyleColor;
332             while (((sPropName.indexOf("}") < 0) && (index < _sDataList.length - 1)))
333             {
334                 String scurline = _sDataList[index++];
335                 if ((scurline.indexOf(_sPropertyDescription)) > 0)
336                 {
337                     if (scurline.indexOf(":") > 0)
338                     {
339                         String[] sPropList = JavaTools.ArrayoutofString(scurline, ":");
340                         String sPropValue = sPropList[1];
341                         sPropValue = sPropValue.trim();
342                         if (sPropValue.indexOf("#") > -1)
343                         {
344                             sPropValue = JavaTools.replaceSubString(sPropValue, PropertyNames.EMPTY_STRING, PropertyNames.SEMI_COLON);
345                             sPropValue = JavaTools.replaceSubString(sPropValue, PropertyNames.EMPTY_STRING, PropertyNames.SPACE);
346                             return Integer.decode(sPropValue).intValue();
347                         }
348                     }
349                 }
350             }
351         }
352         return -1;
353     }
354 
getMSF()355     private XMultiServiceFactory getMSF()
356     {
357         return xMSF;
358     }
359 
getStylePaths()360     private ArrayList<String> getStylePaths()
361     {
362         ArrayList<String> aStylePaths = new ArrayList<String>();
363         try
364         {
365             // TODO: check different languages in header layouts
366             aStylePaths = FileAccess.getOfficePaths(getMSF(), "Config", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING);
367             FileAccess.combinePaths(getMSF(), aStylePaths, "/wizard/form/styles");
368 
369             String[][] LayoutFiles = FileAccess.getFolderTitles(getMSF(), null, aStylePaths, ".css");
370 
371         }
372         catch (com.sun.star.wizards.common.NoValidPathException e)
373         {
374             // if there are problems, don't show anything is a little bit hard.
375             aStylePaths.add("default");
376         }
377         return aStylePaths;
378     }
379 
getStylePath()380     private String getStylePath()
381         {
382 // TODO: umstellen auf mehrere Pfade
383             String StylesPath = PropertyNames.EMPTY_STRING;
384             try
385             {
386                 StylesPath = FileAccess.getOfficePath(xMSF, "Config", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING);
387                 StylesPath = FileAccess.combinePaths(xMSF, StylesPath, "/wizard/form/styles");
388             }
389             catch (NoValidPathException e)
390             {
391             }
392             return StylesPath;
393         }
394 
getStyleColors(String _filename)395     private int[] getStyleColors(String _filename)
396     {
397         String sFilePath = getStylePath() + "/" + _filename;
398         int[] oStylePropList = new int[6];
399         String[] sData = FileAccess.getDataFromTextFile(xMSF, sFilePath);
400         oStylePropList[SOBACKGROUNDCOLOR] = getStyleColor(sData, ".toctitle {", "background-color:");
401         oStylePropList[SODBTEXTCOLOR] = getStyleColor(sData, ".doctitle {", "color:");
402         oStylePropList[SOLABELTEXTCOLOR] = getStyleColor(sData, ".toctitle {", "color:");
403 //      oStylePropList[SODBCONTROLBACKGROUNDCOLOR] = getStyleColor(sData, "body {", "background-color:");
404 //      oStylePropList[SOLABELBACKGROUNDCOLOR] = getStyleColor(sData, ".toctitle {", "background-color:");
405         oStylePropList[SOBORDERCOLOR] = getStyleColor(sData, ".tcolor {", "border-color:");
406         return oStylePropList;
407     }
408 
setDBControlColors(XPropertySet xPropertySet, int[] _iStyleColors)409     private void setDBControlColors(XPropertySet xPropertySet, int[] _iStyleColors)
410     {
411         try
412         {
413             if (xPropertySet.getPropertySetInfo().hasPropertyByName("TextColor"))
414             {
415                 if (_iStyleColors[SODBTEXTCOLOR] > -1)
416                 {
417                     xPropertySet.setPropertyValue("TextColor", Integer.decode("#00000"));
418                 }
419             }
420             if (xPropertySet.getPropertySetInfo().hasPropertyByName("BackgroundColor"))
421             {
422                 xPropertySet.setPropertyValue("BackgroundColor", Integer.decode("#DDDDDD"));
423             }
424         }
425         catch (Exception e)
426         {
427             e.printStackTrace(System.out);
428         }
429     }
430 
applyDBControlProperties(int[] _iStyleColors)431     public void applyDBControlProperties(int[] _iStyleColors)
432     {
433         try
434         {
435             for (int m = 0; m < curFormDocument.oControlForms.size(); m++)
436             {
437                 FormDocument.ControlForm curControlForm = ((FormDocument.ControlForm) curFormDocument.oControlForms.get(m));
438                 if (curControlForm.getArrangemode() == FormWizard.AS_GRID)
439                 {
440                     if (_iStyleColors[SOLABELTEXTCOLOR] > -1)
441                     {
442                         curControlForm.oGridControl.xPropertySet.setPropertyValue("TextColor", new Integer(_iStyleColors[SODBTEXTCOLOR]));
443                     }
444                     curControlForm.oGridControl.xPropertySet.setPropertyValue("BackgroundColor", Integer.decode("#DDDDDD"));
445                 }
446                 else
447                 {
448                     DatabaseControl[] DBControls = curControlForm.getDatabaseControls();
449                     for (int n = 0; n < DBControls.length; n++)
450                     {
451                         if (_iStyleColors[SODBTEXTCOLOR] > -1)
452                         {
453                             DatabaseControl aDBControl = DBControls[n];
454                             if (aDBControl != null)
455                             {
456                                 if (aDBControl.xServiceInfo.supportsService("com.sun.star.drawing.ShapeCollection"))
457                             {
458                                     TimeStampControl oTimeStampControl = (TimeStampControl) aDBControl;
459                                 for (int i = 0; i < 2; i++)
460                                 {
461                                     XPropertySet xPropertySet = oTimeStampControl.getControlofGroupShapeByIndex(i);
462                                     setDBControlColors(xPropertySet, _iStyleColors);
463                                 }
464                             }
465                             else
466                             {
467                                     setDBControlColors(aDBControl.xPropertySet, _iStyleColors);
468                                 }
469                             }
470                         }
471                     }
472                     Control[] LabelControls = curControlForm.getLabelControls();
473                     for (int n = 0; n < LabelControls.length; n++)
474                     {
475                         if (_iStyleColors[SOLABELTEXTCOLOR] > -1)
476                         {
477                             LabelControls[n].xPropertySet.setPropertyValue("TextColor", new Integer(_iStyleColors[SOLABELTEXTCOLOR]));
478 //                  if (_iStyleColors[SOCONTROLBACKGROUNDCOLOR] > -1)
479 //                      LabelControls[n].xPropertySet.setPropertyValue("BackgroundColor", new Integer(_iStyleColors[SOCONTROLBACKGROUNDCOLOR]));
480                         }
481                     }
482                 }
483             }
484             xPageStylePropertySet.setPropertyValue("BackColor", new Integer(_iStyleColors[SOBACKGROUNDCOLOR]));
485         }
486         catch (Exception e)
487         {
488             e.printStackTrace(System.out);
489         }
490     }
491 }
492