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.ui;
24 
25 import com.sun.star.wizards.common.Properties;
26 import com.sun.star.wizards.common.*;
27 import com.sun.star.beans.*;
28 import com.sun.star.awt.Size;
29 import com.sun.star.awt.XTextComponent;
30 import com.sun.star.awt.XTextListener;
31 
32 import java.util.*;
33 
34 public class TitlesComponent extends ControlScroller
35 {
36 
37     int iLabelPosX;
38     final int iRelLabelPosXDist = 6;
39     public String[] fieldnames;
40     public Map fieldtitleset;
41     public String[] fieldtitles;
42     public XTextListener xTextListener;
43     final String SOLABELPREFIX = "lblColumnName_";
44     final String SOTITLEPREFIX = "lblTitleName_";
45 
TitlesComponent(WizardDialog _CurUnoDialog, int _iStep, int _iCompPosX, int _iCompPosY, int _iCompWidth, int _uitextfieldcount, String _slblColumnNames, String _slblColumnTitles, int _firsthelpindex)46     public TitlesComponent(WizardDialog _CurUnoDialog, int _iStep, int _iCompPosX, int _iCompPosY, int _iCompWidth, int _uitextfieldcount, String _slblColumnNames, String _slblColumnTitles, int _firsthelpindex)
47     {
48         super(_CurUnoDialog, null, _iStep, _iCompPosX, _iCompPosY, _iCompWidth, _uitextfieldcount, 18, _firsthelpindex);
49         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblColumnNames", new String[]
50                 {
51                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
52                 }, new Object[]
53                 {
54                     8, _slblColumnNames, new Integer(iLabelPosX), new Integer(iCompPosY - 10), IStep, 60
55                 });
56         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblColumnTitles", new String[]
57                 {
58                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_WIDTH
59                 }, new Object[]
60                 {
61                     8, _slblColumnTitles, 90, new Integer(iCompPosY - 10), IStep, 152
62                 });
63     }
64 
insertControlGroup(int i, int ypos)65     protected void insertControlGroup(int i, int ypos)
66     {
67         iLabelPosX = iCompPosX + iRelLabelPosXDist;
68         ControlRow oControlRow = new ControlRow(i, ypos);
69         ControlGroupVector.addElement(oControlRow);
70 
71     }
72 
setControlGroupVisible(int _index, boolean _bIsVisible)73     protected void setControlGroupVisible(int _index, boolean _bIsVisible)
74     {
75         CurUnoDialog.setControlVisible(getColumnName(_index), _bIsVisible);
76         CurUnoDialog.setControlVisible(getTitleName(_index), _bIsVisible);
77     }
78 
addTextListener(XTextListener _xTextListener)79     public void addTextListener(XTextListener _xTextListener)
80     {
81         for (int i = 0; i < super.ControlGroupVector.size(); i++)
82         {
83             ControlRow curControlRow = (ControlRow) ControlGroupVector.elementAt(i);
84             XTextComponent xTextBox = curControlRow.xTextComponent;
85             if ((xTextBox != null) && (_xTextListener != null))
86             {
87                 xTextBox.addTextListener(_xTextListener);
88             }
89         }
90     }
91 
92     class ControlRow
93     {
94 
95         PropertyValue[] regproperties;
96         XTextComponent xTextComponent;
97 
ControlRow(int _index, int _iCompPosY)98         public ControlRow(int _index, int _iCompPosY)
99         {
100             String slabelname = getColumnName(_index);
101             String stextfieldname = getTitleName(_index);
102 
103             CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", slabelname,
104                     new String[]
105                     {
106                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_MULTILINE, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
107                     },
108                     new Object[]
109                     {
110                         16, Boolean.TRUE, new Integer(iLabelPosX), new Integer(_iCompPosY + 1), UIConsts.INVISIBLESTEP, new Short(curtabindex++), 30
111                     });
112 
113             xTextComponent = CurUnoDialog.insertTextField(stextfieldname, 0, null,
114                     new String[]
115                     {
116                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
117                     },
118                     new Object[]
119                     {
120                         12, HelpIds.getHelpIdString(curHelpIndex++), new Integer(iLabelPosX + 30), new Integer(_iCompPosY), UIConsts.INVISIBLESTEP, new Short(curtabindex++), new Integer(iCompWidth - 90 - 20)
121                     });
122         }
123     }
124 
getColumnName(int _index)125     private String getColumnName(int _index)
126     {
127         return SOLABELPREFIX + Integer.toString(_index + 1);
128     }
129 
getTitleName(int _index)130     private String getTitleName(int _index)
131     {
132         return SOTITLEPREFIX + Integer.toString(_index + 1);
133     }
134 
initializeScrollFields()135     protected void initializeScrollFields()
136     {
137         for (int i = 0; i < fieldnames.length; i++)
138         {
139             PropertyValue[] currowproperties = new PropertyValue[2];
140             currowproperties[0] = Properties.createProperty(getColumnName(i), fieldnames[i], i);
141             currowproperties[1] = Properties.createProperty(getTitleName(i), fieldtitles[i], i); //getTitleName(i)
142             registerControlGroup(currowproperties, i);
143         }
144     }
145 
adjustPosSizes()146     private void adjustPosSizes()
147     {
148         String sLongestFieldName = JavaTools.getlongestArrayItem(fieldnames);
149         Size aSize = CurUnoDialog.getpreferredLabelSize(getColumnName(0), sLongestFieldName);
150         double dblMAPConversion = CurUnoDialog.getMAPConversionFactor(getColumnName(0));
151         int iFieldNameWidth = getFieldNameWidth(aSize.Width, dblMAPConversion) + 10;
152         Integer FieldNameWidth = new Integer(iFieldNameWidth);
153         Integer TitlePosX = new Integer(iLabelPosX + iFieldNameWidth + 2);
154         Integer TitleWidth = new Integer(iCompPosX + iCompWidth - TitlePosX.intValue() - iScrollBarWidth - 6);
155         for (short i = 0; i <= ncurfieldcount; i++)
156         {
157             CurUnoDialog.setControlProperty(getColumnName(i), PropertyNames.PROPERTY_WIDTH, FieldNameWidth);
158             CurUnoDialog.setControlProperties(getTitleName(i), new String[]
159                     {
160                         PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_WIDTH
161                     }, new Object[]
162                     {
163                         TitlePosX, TitleWidth
164                     });
165         }
166         CurUnoDialog.setControlProperty("lblColumnNames", PropertyNames.PROPERTY_WIDTH, FieldNameWidth);
167         CurUnoDialog.setControlProperty("lblColumnTitles", PropertyNames.PROPERTY_POSITION_X, TitlePosX);
168     }
169 
initialize(String[] _fieldnames, Map _fieldtitleset)170     public void initialize(String[] _fieldnames, Map _fieldtitleset)
171     {
172         this.fieldnames = _fieldnames;
173         this.fieldtitleset = _fieldtitleset;
174         setFieldTitles();
175         initializeScrollFields();
176         super.initialize(fieldnames.length);
177         adjustPosSizes();
178 //      setComponentMouseTransparent();
179     }
180 
setFieldTitles()181     private void setFieldTitles()
182     {
183         fieldtitles = new String[fieldnames.length];
184         for (int i = 0; i < fieldnames.length; i++)
185         {
186             if (this.fieldtitleset.containsKey(fieldnames[i]))
187             {
188                 Object curvalue = fieldtitleset.get(fieldnames[i]);
189                 if (curvalue != null)
190                 {
191                     fieldtitles[i] = (String) curvalue;
192                 }
193                 else
194                 {
195                     fieldtitles[i] = fieldnames[i];
196                 }
197             }
198             else
199             {
200                 fieldtitles[i] = fieldnames[i];
201             }
202         }
203     }
204 
getFieldNameByTitleControl(Object _fieldtitlemodel)205     public String getFieldNameByTitleControl(Object _fieldtitlemodel)
206     {
207         String sTitleModelName = (String) Helper.getUnoPropertyValue(_fieldtitlemodel, PropertyNames.PROPERTY_NAME);
208         String sindex = JavaTools.getSuffixNumber(sTitleModelName);
209         return (String) CurUnoDialog.getControlProperty(this.SOLABELPREFIX + sindex, PropertyNames.PROPERTY_LABEL);
210     }
211 
getFieldTitles()212     public String[] getFieldTitles()
213     {
214         PropertyValue[][] titlelist = this.getScrollFieldValues();
215         PropertyValue[] currowproperties;
216         PropertyValue curtitleproperty;
217         for (int i = 0; i < titlelist.length; i++)
218         {
219             currowproperties = titlelist[i];
220             curtitleproperty = currowproperties[1];
221             fieldtitles[i] = (String) curtitleproperty.Value;
222         }
223         refreshtitleset();
224         return fieldtitles;
225     }
226 
refreshtitleset()227     private void refreshtitleset()
228     {
229         for (int i = 0; i < fieldnames.length; i++)
230         {
231             fieldtitleset.put(fieldnames[i], fieldtitles[i]);
232         }
233     }
234 
getFieldNameWidth(int iMAPControlWidth, double dblMAPConversion)235     private int getFieldNameWidth(int iMAPControlWidth, double dblMAPConversion)
236     {
237         int iFieldNameWidth = (int) (1.15 * ((double) (iMAPControlWidth)) / dblMAPConversion);
238         double dblAvailableWidth = (double) (iCompWidth - iScrollBarWidth - iRelLabelPosXDist);
239         if (iFieldNameWidth > (0.5 * (dblAvailableWidth)))
240         {
241             iFieldNameWidth = (int) (0.5 * (dblAvailableWidth));
242         }
243         return iFieldNameWidth;
244     }
245 }
246