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