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.report;
24 //import com.sun.star.ucb.CommandAbortedException;
25 import com.sun.star.ucb.XSimpleFileAccess;
26 import com.sun.star.uno.Exception;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.uno.XInterface;
29 import com.sun.star.wizards.common.Desktop;
30 import com.sun.star.wizards.common.*;
31 import com.sun.star.wizards.ui.*;
32 import com.sun.star.awt.VclWindowPeerAttribute;
33 import com.sun.star.awt.XTextComponent;
34 //import com.sun.star.container.XHierarchicalNameAccess;
35 //import com.sun.star.container.XNameAccess;
36 //import com.sun.star.lang.EventObject;
37 //import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.wizards.db.RecordParser;
40 //import com.sun.star.wizards.document.OfficeDocument;
41 public class ReportFinalizer
42 {
43 
44     WizardDialog CurUnoDialog;
45     XTextComponent xTitleTextBox;
46     XTextComponent[] xSaveTextBox = new XTextComponent[2];
47     Object chkTemplate;
48     String CHANGEREPORTTITLE_FUNCNAME = "changeReportTitle";
49     String TOGGLESUBTEMPLATECONTROLS_FUNCNAME = "toggleSubTemplateControls";
50 //    String slblHowProceed;
51 //    String slblChooseReportKind;
52     String TemplatePath;
53     String StoreName;
54     boolean bfinalaskbeforeOverwrite;
55     String DefaultName;
56     String OldDefaultName;
57     // ReportTextDocument CurReportDocument;
58     IReportDocument CurReportDocument;
59     // Desktop.OfficePathRetriever curofficepath;
60 //    short curtabindex;
61 //    String sMsgReportDocumentNameDuplicate;
62     public static final int SOCREATEDOCUMENT = 1;
63     public static final int SOCREATETEMPLATE = 2;
64     public static final int SOUSETEMPLATE = 3;
65     private XMultiServiceFactory m_xMSF;
66     // public Finalizer(ReportTextDocument _CurReportDocument, WizardDialog _CurUnoDialog) {
ReportFinalizer(XMultiServiceFactory _xMSF, IReportDocument _CurReportDocument, WizardDialog _CurUnoDialog)67     public ReportFinalizer(XMultiServiceFactory _xMSF, IReportDocument _CurReportDocument, WizardDialog _CurUnoDialog)
68     {
69         m_xMSF = _xMSF;
70 
71         this.CurUnoDialog = _CurUnoDialog;
72         this.CurReportDocument = _CurReportDocument;
73         short curtabindex = (short) (ReportWizard.SOSTOREPAGE * 100);
74         Desktop odesktop = new Desktop();
75         // curofficepath = odesktop.new OfficePathRetriever(m_xMSF);
76 
77         String sSaveAsTemplate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 40);
78         String sUseTemplate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 41);
79         String sEditTemplate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 42);
80         String sSaveAsDocument = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 43);
81 // String            sSaveAs = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 44);
82         String sReportTitle = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 33);
83         String slblHowProceed = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 78);
84         String slblChooseReportKind = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 79);
85 
86         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblTitle",
87                 new String[]
88                 {
89                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
90                 },
91                 new Object[]
92                 {
93                     8, sReportTitle, 95, 27, new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 68
94                 });
95 
96         xTitleTextBox = CurUnoDialog.insertTextField("txtTitle", CHANGEREPORTTITLE_FUNCNAME, this,
97                 new String[]
98                 {
99                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
100                 },
101                 new Object[]
102                 {
103                     12, "HID:WIZARDS_HID_DLGREPORT_4_TITLE", 95, 37, new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 209
104                 });
105 
106         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblChooseReportKind",
107                 new String[]
108                 {
109                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
110                 },
111                 new Object[]
112                 {
113                     8, slblChooseReportKind, 95, 57, new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 209
114                 });
115 
116         CurUnoDialog.insertRadioButton("optCreateDocument", TOGGLESUBTEMPLATECONTROLS_FUNCNAME, this,
117                 new String[]
118                 {
119                     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, PropertyNames.PROPERTY_WIDTH
120                 },
121                 new Object[]
122                 {
123                     10, "HID:WIZARDS_HID_DLGREPORT_5_OPTSTATDOCUMENT", sSaveAsDocument, 95, 69, new Short((short) 0), new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 138
124                 });
125 
126         CurUnoDialog.insertRadioButton("optCreateReportTemplate", TOGGLESUBTEMPLATECONTROLS_FUNCNAME, this,
127                 new String[]
128                 {
129                     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, PropertyNames.PROPERTY_WIDTH
130                 },
131                 new Object[]
132                 {
133                     8, "HID:WIZARDS_HID_DLGREPORT_5_OPTDYNTEMPLATE", sSaveAsTemplate, 95, 81, new Short((short) 1), new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 209
134                 });
135 
136 
137         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblHowProceed",
138                 new String[]
139                 {
140                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
141                 },
142                 new Object[]
143                 {
144                     8, slblHowProceed, 105, 93, new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 209
145                 });
146 
147 
148         CurUnoDialog.insertRadioButton("optEditTemplate", TOGGLESUBTEMPLATECONTROLS_FUNCNAME, 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, PropertyNames.PROPERTY_WIDTH
152                 },
153                 new Object[]
154                 {
155                     10, "HID:WIZARDS_HID_DLGREPORT_5_OPTEDITTEMPLATE", sEditTemplate, 111, 105, 6, new Short(curtabindex++), 138
156                 });
157 
158         CurUnoDialog.insertRadioButton("optUseTemplate", TOGGLESUBTEMPLATECONTROLS_FUNCNAME, this,
159                 new String[]
160                 {
161                     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, PropertyNames.PROPERTY_WIDTH
162                 },
163                 new Object[]
164                 {
165                     10, "HID:WIZARDS_HID_DLGREPORT_5_OPTUSETEMPLATE", sUseTemplate, 111, 115, new Short((short) 1), new Integer(ReportWizard.SOSTOREPAGE), new Short(curtabindex++), 138
166                 });
167     }
168 
169 
170     /*
171      * This function is called if one of the radio buttons is pressed
172      */
toggleSubTemplateControls()173     public void toggleSubTemplateControls()
174     {
175         // String sStorePath = PropertyNames.EMPTY_STRING;
176         Short iState = (Short) CurUnoDialog.getControlProperty("optCreateReportTemplate", PropertyNames.PROPERTY_STATE);
177         boolean bDoTemplateEnable = iState.shortValue() == 1;
178         CurUnoDialog.setControlProperty("optEditTemplate", PropertyNames.PROPERTY_ENABLED, bDoTemplateEnable);
179         CurUnoDialog.setControlProperty("optUseTemplate", PropertyNames.PROPERTY_ENABLED, bDoTemplateEnable);
180         CurUnoDialog.setControlProperty("lblHowProceed", PropertyNames.PROPERTY_ENABLED, bDoTemplateEnable);
181 
182         String sTitle = xTitleTextBox.getText();
183         boolean bDoEnable = sTitle.equals(PropertyNames.EMPTY_STRING);
184         CurUnoDialog.enableFinishButton(!bDoEnable);
185     }
186 //  private boolean fileexists(XMultiServiceFactory _xMSF, String _spath){
187 //  try {
188 //      XInterface xUcbInterface = (XInterface) _xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
189 //      XSimpleFileAccess xSimpleFileAccess = (XSimpleFileAccess) com.sun.star.uno.UnoRuntime.queryInterface(XSimpleFileAccess.class, xUcbInterface);
190 //      return xSimpleFileAccess.exists(_spath);
191 //  } catch (Exception exception) {
192 //      exception.printStackTrace(System.out);
193 //      return false;
194 //  }}
initialize(RecordParser _CurDBMetaData)195     public void initialize(RecordParser _CurDBMetaData)
196     {
197         String FirstCommandName = (_CurDBMetaData.getIncludedCommandNames())[0];
198         DefaultName = Desktop.getUniqueName(_CurDBMetaData.getReportDocuments(), FirstCommandName);
199         if (!DefaultName.equals(OldDefaultName))
200         {
201             OldDefaultName = DefaultName;
202         }
203         xTitleTextBox.setText(DefaultName);
204     }
205 
getStoreName()206     public String getStoreName()
207     {
208         if (CurUnoDialog != null)
209         {
210             String LocStoreName = xTitleTextBox.getText();
211             if (!LocStoreName.equals(PropertyNames.EMPTY_STRING))
212             {
213                 StoreName = LocStoreName;
214             }
215         }
216         return (StoreName);
217     }
218 
getStorePath()219     public String getStorePath()
220     {
221         try
222         {
223             StoreName = getStoreName();
224             String StorePath;
225             XInterface xInterface = (XInterface) m_xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
226             XSimpleFileAccess xSimpleFileAccess = UnoRuntime.queryInterface(XSimpleFileAccess.class, xInterface);
227             StorePath = FileAccess.getOfficePath(m_xMSF, "Temp", xSimpleFileAccess) + "/" + StoreName;
228             return StorePath;
229         }
230         catch (Exception e)
231         {
232             e.printStackTrace(System.out);
233             return PropertyNames.EMPTY_STRING;
234         }
235     }
236 
changeReportTitle()237     public void changeReportTitle()
238     {
239         final String TitleName = xTitleTextBox.getText();
240         CurReportDocument.liveupdate_updateReportTitle(TitleName);
241         CurUnoDialog.enableFinishButton(!PropertyNames.EMPTY_STRING.equals(TitleName));
242     }
243 
getReportOpenMode()244     public int getReportOpenMode()
245     {
246         int ReportMode = SOCREATEDOCUMENT;
247         boolean bcreateTemplate = ((Short) CurUnoDialog.getControlProperty("optCreateReportTemplate", PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
248         if (bcreateTemplate)
249         {
250             ReportMode = SOCREATETEMPLATE;
251         }
252         boolean buseTemplate = ((Short) CurUnoDialog.getControlProperty("optUseTemplate", PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
253         if (buseTemplate)
254         {
255             ReportMode = SOUSETEMPLATE;
256         }
257         boolean buseDocument = ((Short) CurUnoDialog.getControlProperty("optCreateDocument", PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
258         if (buseDocument)
259         {
260             ReportMode = SOCREATEDOCUMENT;
261         }
262         return ReportMode;
263     }
264 
finish()265     public boolean finish()
266     {
267         StoreName = getStoreName();
268         if (!CurReportDocument.getRecordParser().getReportDocuments().hasByHierarchicalName(StoreName))
269         {
270             try
271             {
272                 CurReportDocument.store(StoreName, getReportOpenMode());
273                 ReportWizard.bCloseDocument = false;
274                 return true;
275             }
276             catch(Exception e)
277             {
278                 CurUnoDialog.showMessageBox("ErrorBox", VclWindowPeerAttribute.OK,e.getLocalizedMessage() );
279                 CurUnoDialog.enableFinishButton(false);
280                 return false;
281             }
282         }
283         String sMsgReportDocumentNameDuplicate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 76);
284         String sShowMsgReportNameisDuplicate = JavaTools.replaceSubString(sMsgReportDocumentNameDuplicate, StoreName, "%REPORTNAME");
285         /* int iMsg = */ CurUnoDialog.showMessageBox("ErrorBox", VclWindowPeerAttribute.OK, sShowMsgReportNameisDuplicate);
286         CurUnoDialog.enableFinishButton(false);
287         return false;
288     }
289 }
290