Update ooo320-m1
[ooovba.git] / odk / examples / java / ToDo / ToDo.java
blob88d02da9eeaab66038198f827461814f7a0b66ae
1 /*************************************************************************
3 * $RCSfile: ToDo.java,v $
5 * $Revision: 1.9 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:19:47 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com.sun.star.lib.uno.helper.Factory;
42 import com.sun.star.lang.XInitialization;
43 import com.sun.star.lang.XMultiComponentFactory;
44 import com.sun.star.lang.XSingleComponentFactory;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.lang.XServiceInfo;
47 import com.sun.star.lang.XTypeProvider;
48 import com.sun.star.lib.uno.helper.WeakBase;
49 import com.sun.star.registry.XRegistryKey;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 import com.sun.star.uno.XWeak;
54 import com.sun.star.uno.XComponentContext;
55 import org.openoffice.*;
57 // addintional interfaces used by the implementation
58 import com.sun.star.sheet.XSpreadsheetDocument;
59 import com.sun.star.sheet.XSpreadsheet;
60 import com.sun.star.sheet.XCellRangeMovement;
61 import com.sun.star.sheet.XFunctionAccess;
62 import com.sun.star.container.XIndexAccess;
63 import com.sun.star.table.XCellRange;
64 import com.sun.star.table.XCell;
65 import com.sun.star.table.CellAddress;
66 import com.sun.star.table.CellRangeAddress;
67 import com.sun.star.table.XColumnRowRange;
68 import com.sun.star.table.XTableRows;
69 import com.sun.star.beans.XPropertySet;
70 import com.sun.star.text.XTextRange;
71 import com.sun.star.text.XSimpleText;
72 import com.sun.star.text.XTextCursor;
73 import com.sun.star.text.XText;
74 import com.sun.star.text.XTextField;
76 import java.util.GregorianCalendar;
77 import java.util.Calendar;
78 import java.util.Vector;
79 import java.util.Arrays;
81 /** This class capsulates the class, that implements the minimal component, a
82 * factory for creating the service (<CODE>__getServiceFactory</CODE>) and a
83 * method, that writes the information into the given registry key
84 * (<CODE>__writeRegistryServiceInfo</CODE>).
86 public class ToDo {
88 /** This class implements the component. At least the interfaces
89 * XInterface, XTypeProvider, and XWeak implemented by the helper class
90 * WeakBase and XServiceInfo should be provided by the service.
92 public static class ToDoImpl extends WeakBase implements XServiceInfo, XToDo {
94 /** The service name, that must be used to get an instance of this service.
96 private static final String __serviceName = "org.openoffice.ToDo";
98 /** The initial component contextr, that gives access to
99 * the service manager, supported singletons, ...
100 * It's often later used
102 private XComponentContext m_cmpCtx;
104 /** The service manager, that gives access to all registered services.
105 * It's often later used
107 private XMultiComponentFactory m_xMCF;
109 // Implementation helper variables
110 static private final int INT_COLUMN_FEATURE = 0;
111 static private final int INT_COLUMN_COMMENT = 1;
112 static private final int INT_COLUMN_NEEDEDDAYS = 2;
113 static private final int INT_COLUMN_STARTDATE = 3;
114 static private final int INT_COLUMN_START_DAY_OF_WEEK = 4;
115 static private final int INT_COLUMN_ENDDATE = 5;
116 static private final int INT_COLUMN_END_DAY_OF_WEEK = 6;
117 static private final int INT_COLUMN_DUEDATE = 7;
118 static private final int INT_COLUMN_STATUS = 8;
120 static private final int INT_ROW_FROM = 14; // 8
122 static private final int INT_ROW_HOLIDAYS_START = 4;
123 static private final int INT_COLUMN_HOLIDAYS_START = 7; // 10
125 static private final String STRING_SEPARATOR = "/";
128 /** The constructor of the inner class has a XComponenContext parameter.
129 * @param xCompContext the initial component context
131 public ToDoImpl(XComponentContext xCompContext) {
132 try {
133 m_cmpCtx = xCompContext;
134 m_xMCF = m_cmpCtx.getServiceManager();
136 catch( Exception e ) {
137 e.printStackTrace(System.err);
141 /** This method returns an array of all supported service names.
142 * @return Array of supported service names.
144 public String[] getSupportedServiceNames() {
145 return getServiceNames();
148 public static String[] getServiceNames() {
149 String[] sSupportedServiceNames = { __serviceName };
150 return sSupportedServiceNames;
153 /** This method returns true, if the given service will be
154 * supported by the component.
155 * @param sService Service name.
156 * @return True, if the given service name will be supported.
158 public boolean supportsService(String sServiceName) {
159 return sServiceName.equals( __serviceName );
162 /** Return the class name of the component.
163 * @return Class name of the component.
165 public String getImplementationName() {
166 return ToDoImpl.class.getName();
169 /** For every bug/feature listed in a spreadsheet document this method
170 * calculates the start date, day of week of the start date, the end date
171 * and the day of week of the end date. All calculations are dependent
172 * on the values of "Needed Days", "Due Date" and "Status". The columns
173 * "Needed Days" and "Status" are mandatory. The first feature/bug should
174 * be placed in row nine. The date to start the calculation should be
175 * placed in cell C6. The private holidays should be placed in cell K4/K5
176 * and below. All rows will be calculated up to the first empty cell in
177 * the first column. If a cell in the column "Due Date" will be colored
178 * red, you should take a look at your entries.
179 * @param aInstance Spreadsheet document.
180 * @throws com.sun.star.uno.RuntimeException This exception could occur
181 * at every interface method.
183 public void recalc( java.lang.Object aInstance )
184 throws com.sun.star.uno.RuntimeException {
185 try {
186 // Querying for the interface XSpreadsheetDocument
187 XSpreadsheetDocument xspreadsheetdocument =
188 ( XSpreadsheetDocument ) UnoRuntime.queryInterface(
189 XSpreadsheetDocument.class, aInstance );
191 // Querying for the interface XIndexAccess
192 XIndexAccess xindexaccess = ( XIndexAccess )
193 UnoRuntime.queryInterface( XIndexAccess.class,
194 xspreadsheetdocument.getSheets() );
196 // Getting the first XSpreadsheet
197 XSpreadsheet xspreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
198 XSpreadsheet.class, xindexaccess.getByIndex( 0 ));
200 // Querying for the interface XCellRange on the XSpeadsheet
201 XCellRange xcellrange = ( XCellRange )
202 UnoRuntime.queryInterface( XCellRange.class, xspreadsheet );
204 /* Getting the gregorian calendar with the date on which to start
205 the calculation */
206 GregorianCalendar gregCalAbsoluteStartDate =
207 this.getGregorianCalendarFromString(this.getStringFromCell(
208 xcellrange, 5, 2 ) );
209 gregCalAbsoluteStartDate.add( Calendar.DATE, -1 );
211 // Set the start date with the absolute start date
212 GregorianCalendar gregCalStartDate =
213 (GregorianCalendar) gregCalAbsoluteStartDate.clone();
215 /* Creating the service FunctionAccess, which allows generic
216 access to all spreadsheet functions */
217 Object objectFunctionAccess =
218 m_xMCF.createInstanceWithContext(
219 "com.sun.star.sheet.FunctionAccess", m_cmpCtx );
221 // Querying for the interface XFunctionAccess on service
222 // FunctionAccess
223 XFunctionAccess xfunctionaccess = (XFunctionAccess)
224 UnoRuntime.queryInterface(XFunctionAccess.class,
225 objectFunctionAccess );
227 // Creating vector for holidays
228 Vector vectorHolidays = new Vector();
230 // Get the Official Holidays
231 this.getOfficialHolidays( vectorHolidays, xcellrange,
232 xfunctionaccess,
233 gregCalStartDate.get(
234 Calendar.YEAR ) );
236 // Get the private holidays
237 this.getPrivateHolidays(vectorHolidays, xcellrange,
238 xfunctionaccess);
240 // Getting the object array of holidays
241 Object[] objectSortedHolidays = vectorHolidays.toArray();
243 // Sorting the holidays
244 Arrays.sort( objectSortedHolidays );
246 // Collect the Official Holidays and the private holidays
247 Object [][]objectHolidays =
248 new Object[][] { objectSortedHolidays };
250 // Row index
251 int intRowTo = this.INT_ROW_FROM - 1;
253 // Getting the feature of the first cell
254 String sFeature = this.getStringFromCell(xcellrange,
255 intRowTo + 1,
256 this.INT_COLUMN_FEATURE);
258 // Determine the last row with an entry in the first column
259 while ( ( sFeature != null ) &&
260 ( !sFeature.equals( "" ) ) ) {
261 intRowTo++;
262 sFeature = this.getStringFromCell( xcellrange,
263 intRowTo + 1, this.INT_COLUMN_FEATURE );
266 // Setting the last row to be calculated
267 final int INT_ROW_TO = intRowTo + 1;
269 // Deleting cells which will be recalculated
270 for ( int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO + 5;
271 intRow++ ) {
272 for ( int intColumn = this.INT_COLUMN_STARTDATE;
273 intColumn <= this.INT_COLUMN_END_DAY_OF_WEEK;
274 intColumn++ ) {
275 this.setStringToCell(xcellrange, intRow, intColumn, "");
279 /* Clearing the background color of the due date cells and setting
280 the hyperlink to the bugtracker */
281 for (int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++)
283 // Querying for the interface XPropertySet for the cell
284 // providing the due date
285 XPropertySet xpropertyset = ( XPropertySet )
286 UnoRuntime.queryInterface(XPropertySet.class,
287 xcellrange.getCellByPosition(
288 this.INT_COLUMN_DUEDATE,
289 intRow ));
291 // Changing the background color of the cell to white
292 xpropertyset.setPropertyValue( "CellBackColor",
293 new Integer( 16777215 ) );
295 // Getting the cell of the bug id
296 XCell xcell = xcellrange.getCellByPosition(
297 this.INT_COLUMN_FEATURE, intRow );
299 // Querying for the interface XSimpleText
300 XSimpleText xsimpletext = ( XSimpleText )
301 UnoRuntime.queryInterface( XSimpleText.class, xcell );
303 // Getting the text cursor
304 XTextCursor xtextcursor = xsimpletext.createTextCursor();
306 // Querying for the interface XTextRange
307 XTextRange xtextrange = ( XTextRange )
308 UnoRuntime.queryInterface( XTextRange.class, xtextcursor );
310 // Getting the bug ID from the cell
311 String sBugID = xtextrange.getString();
312 if ( !sBugID.startsWith(
313 "http://www.openoffice.org/issues/show_bug.cgi?id=") ) {
314 String sBugIDLink =
315 "http://www.openoffice.org/issues/show_bug.cgi?id=" + sBugID;
317 // Querying for the interface XMultiServiceFactory
318 XMultiServiceFactory xMSFTextField =
319 (XMultiServiceFactory)UnoRuntime.queryInterface(
320 XMultiServiceFactory.class, aInstance );
322 // Creating an instance of the text field URL
323 Object objectTextField =
324 xMSFTextField.createInstance(
325 "com.sun.star.text.TextField.URL" );
327 // Querying for the interface XTextField
328 XTextField xtextfield = ( XTextField )
329 UnoRuntime.queryInterface( XTextField.class,
330 objectTextField );
332 // Querying for the interface XPropertySet
333 XPropertySet xpropertysetTextField = ( XPropertySet )
334 UnoRuntime.queryInterface( XPropertySet.class,
335 xtextfield );
337 // Setting the URL
338 xpropertysetTextField.setPropertyValue( "URL",
339 sBugIDLink );
341 // Setting the representation of the URL
342 xpropertysetTextField.setPropertyValue( "Representation",
343 sBugID );
345 // Querying for the interface XText
346 XText xtext = ( XText )UnoRuntime.queryInterface(
347 XText.class, xcell );
349 // Delete cell content
350 xtextrange.setString( "" );
352 // Inserting the text field URL to the cell
353 xtext.insertTextContent( xtextrange, xtextfield, false );
357 // Processing all features/bugs in the table
358 for (int intRow = this.INT_ROW_FROM; intRow < INT_ROW_TO; intRow++)
360 // Getting the cell of the column "Needed Days" in the
361 // current row
362 XCell xcell = xcellrange.getCellByPosition(
363 INT_COLUMN_NEEDEDDAYS, intRow );
365 // Getting the number of needed days to perform the feature
366 int intNeededDays = (int) Math.round( xcell.getValue() );
368 // Getting the content of a specified cell
369 String sStatus = this.getStringFromCell( xcellrange,
370 intRow, this.INT_COLUMN_STATUS );
372 /* Testing if the number of needed days is greater than
373 zero and if
374 the status is not "done" */
375 if ( ( intNeededDays > 0 )
376 && !( sStatus.toLowerCase().trim().equals("done")) ) {
377 // Getting the start date after a specified number of
378 // workdays
379 gregCalStartDate = this.getWorkday(
380 gregCalStartDate, 1, objectHolidays,
381 xfunctionaccess );
383 // Getting a string with the date format jjjj-mm-dd from
384 // the gregorian calendar
385 String sDate = this.getStringFromGregorianCalendar(
386 gregCalStartDate );
388 // Set the start date in the specified cell of the table
389 this.setStringToCell(xcellrange, intRow,
390 this.INT_COLUMN_STARTDATE, sDate);
392 // For the start day set the day of week in the specified
393 // cell of the table
394 this.setDayOfWeek( gregCalStartDate,
395 xcellrange, intRow,
396 this.INT_COLUMN_START_DAY_OF_WEEK );
398 // Getting the end date after a specified number of workdays
399 GregorianCalendar gregCalEndDate =
400 this.getWorkday( gregCalStartDate,
401 intNeededDays - 1,
402 objectHolidays, xfunctionaccess );
404 // Creating a string with the date format jjjj-mm-dd
405 sDate = this.getStringFromGregorianCalendar(
406 gregCalEndDate );
408 // Set the end date in the specified cell of the table
409 this.setStringToCell( xcellrange, intRow,
410 this.INT_COLUMN_ENDDATE, sDate );
412 // For the end day set the day of week in the specified
413 // cell of the table
414 this.setDayOfWeek(gregCalEndDate, xcellrange,
415 intRow, this.INT_COLUMN_END_DAY_OF_WEEK);
417 // Set the initial date for the next loop
418 gregCalStartDate = ( GregorianCalendar )
419 gregCalEndDate.clone();
421 // Get the due date from the table
422 String sDueDate = this.getStringFromCell(
423 xcellrange, intRow, this.INT_COLUMN_DUEDATE );
425 // Testing if the due date is not empty
426 if ( !sDueDate.equals( "" ) ) {
427 GregorianCalendar gregCalDueDate =
428 this.getGregorianCalendarFromString(sDueDate);
430 // Testing if the due date is before the calculated
431 // end date
432 if ( gregCalDueDate.before(
433 gregCalEndDate ) ) {
434 /* Getting the date when the processing of the
435 feature/bug should
436 be started at the latest */
437 GregorianCalendar gregCalLatestDateToStart =
438 this.getWorkday(gregCalDueDate,
439 -( intNeededDays - 1 ),
440 objectHolidays,
441 xfunctionaccess);
443 // Begin with the current row
444 int intRowToInsert = intRow;
446 // Get the start date for the feature/bug in the
447 // current row
448 GregorianCalendar gregCalPreviousStartDate =
449 this.getGregorianCalendarFromString(
450 this.getStringFromCell(
451 xcellrange, intRowToInsert,
452 this.INT_COLUMN_STARTDATE ) );
454 // Testing if we have to search for an earlier date
455 // to begin
456 while ((gregCalLatestDateToStart.before(
457 gregCalPreviousStartDate)) &&
458 (INT_ROW_FROM != intRowToInsert)) {
459 // Decrease the row
460 intRowToInsert--;
462 // Get the start date for the feature/bug in
463 // the current row
464 String sStartDate = this.getStringFromCell(
465 xcellrange, intRowToInsert,
466 this.INT_COLUMN_STARTDATE );
468 // Search until a valid start date is found
469 while ( sStartDate.equals( "" ) ) {
470 // Decrease the row
471 intRowToInsert--;
473 // Get the start date for the feature/bug
474 // in the current row
475 sStartDate = this.getStringFromCell(
476 xcellrange, intRowToInsert,
477 this.INT_COLUMN_STARTDATE );
480 // Get the GregorianCalender format for the
481 // start date
482 gregCalPreviousStartDate =
483 this.getGregorianCalendarFromString(
484 sStartDate );
487 // Getting the cell of the column "Needed Days"
488 // in the row where to insert
489 XCell xcellNeededDaysWhereToInsert =
490 xcellrange.getCellByPosition(
491 INT_COLUMN_NEEDEDDAYS, intRowToInsert );
492 // Getting the number of needed days to perform
493 // the feature
494 int intNeededDaysWhereToInsert = (int)
495 Math.round(
496 xcellNeededDaysWhereToInsert.getValue());
498 GregorianCalendar gregCalPreviousNewEndDate =
499 this.getWorkday(gregCalPreviousStartDate,
500 intNeededDays - 1 +
501 intNeededDaysWhereToInsert,
502 objectHolidays,
503 xfunctionaccess);
504 String sPreviousDueDate = this.getStringFromCell(
505 xcellrange, intRowToInsert,
506 this.INT_COLUMN_DUEDATE );
508 GregorianCalendar gregCalPreviousDueDate = null;
510 if ( !sPreviousDueDate.equals( "" ) ) {
511 gregCalPreviousDueDate =
512 this.getGregorianCalendarFromString(
513 sPreviousDueDate );
516 if ( ( intRowToInsert == intRow ) ||
517 ( gregCalPreviousNewEndDate.after(
518 gregCalPreviousDueDate ) ) ) {
519 // Querying for the interface XPropertySet for
520 // the cell providing the due date
521 XPropertySet xpropertyset = ( XPropertySet )
522 UnoRuntime.queryInterface(
523 XPropertySet.class,
524 xcellrange.getCellByPosition(
525 this.INT_COLUMN_DUEDATE,
526 intRow ) );
528 // Changing the background color of the cell
529 // to red
530 xpropertyset.setPropertyValue(
531 "CellBackColor", new Integer( 16711680 ) );
532 } else {
533 // Querying for the interface XColumnRowRange
534 // on the XCellRange
535 XColumnRowRange xcolumnrowrange =
536 ( XColumnRowRange)UnoRuntime.queryInterface(
537 XColumnRowRange.class, xcellrange );
538 // Inserting one row to the table
539 XTableRows xTableRows =
540 xcolumnrowrange.getRows();
541 xTableRows.insertByIndex( intRowToInsert, 1 );
543 // Querying for the interface
544 // XCellRangeMovement on XCellRange
545 XCellRangeMovement xcellrangemovement =
546 (XCellRangeMovement)UnoRuntime.queryInterface(
547 XCellRangeMovement.class, xcellrange );
549 // Creating the cell address of the destination
550 CellAddress celladdress = new CellAddress();
551 celladdress.Sheet = 0;
552 celladdress.Column = 0;
553 celladdress.Row = intRowToInsert;
555 // Creating the cell range of the source
556 CellRangeAddress cellrangeaddress =
557 new CellRangeAddress();
558 cellrangeaddress.Sheet = 0;
559 cellrangeaddress.StartColumn = 0;
560 cellrangeaddress.StartRow = intRow + 1;
561 cellrangeaddress.EndColumn = 8;
562 cellrangeaddress.EndRow = intRow + 1;
564 // Moves the cell range to another position in
565 // the document
566 xcellrangemovement.moveRange(celladdress,
567 cellrangeaddress);
569 // Removing the row not needed anymore
570 xcolumnrowrange.getRows().removeByIndex(intRow
571 + 1, 1);
573 // Set the current row, because we want to
574 // recalculate all rows below
575 intRow = intRowToInsert - 1;
577 // Tests at which line we want to insert
578 if ( intRow >= this.INT_ROW_FROM ) {
579 // Get the start date
580 gregCalStartDate =
581 this.getGregorianCalendarFromString(
582 this.getStringFromCell( xcellrange,
583 intRow,this.INT_COLUMN_ENDDATE));
585 else {
586 // Set the start date with the absolute s
587 // tart date
588 gregCalStartDate = (GregorianCalendar)
589 gregCalAbsoluteStartDate.clone();
597 catch( Exception exception ) {
598 showExceptionMessage( exception );
602 /** Getting a string from a gregorian calendar.
603 * @param gregCal Date to be converted.
604 * @return string (converted gregorian calendar).
606 public String getStringFromGregorianCalendar( GregorianCalendar gregCal ) {
607 String sDate = ( gregCal.get( Calendar.MONTH ) + 1 )
608 + STRING_SEPARATOR + gregCal.get( Calendar.DATE )
609 // + STRING_SEPARATOR + ( gregCal.get( Calendar.MONTH ) + 1 )
610 + STRING_SEPARATOR + gregCal.get( Calendar.YEAR );
612 return sDate;
615 /** Getting a GregorianCalendar from a string.
616 * @param sDate String to be converted.
617 * @return The result of the converting of the string.
619 public GregorianCalendar getGregorianCalendarFromString( String sDate ) {
620 int []intDateValue = this.getDateValuesFromString( sDate );
622 return( new GregorianCalendar( intDateValue[ 2 ], intDateValue[ 0 ],
623 intDateValue[ 1 ] ) );
626 /** Getting the day, month and year from a string.
627 * @param sDate String to be parsed.
628 * @return Returns an array of integer variables.
630 public int[] getDateValuesFromString( String sDate) {
631 int[] intDateValues = new int[ 3 ];
633 int intPositionFirstTag = sDate.indexOf( STRING_SEPARATOR );
634 int intPositionSecondTag = sDate.indexOf(STRING_SEPARATOR,
635 intPositionFirstTag + 1);
637 // Getting the value of the month
638 intDateValues[ 0 ] = Integer.parseInt(
639 sDate.substring(0, intPositionFirstTag)) - 1;
640 // Getting the value of the day
641 intDateValues[ 1 ] = Integer.parseInt(
642 sDate.substring(intPositionFirstTag + 1, intPositionSecondTag));
643 // Getting the value of the year
644 intDateValues[ 2 ] = Integer.parseInt(
645 sDate.substring(intPositionSecondTag + 1, sDate.length()));
647 return intDateValues;
650 /** Getting a content from a specified cell.
651 * @param xcellrange Providing access to cells.
652 * @param intRow Number of row.
653 * @param intColumn Number of column.
654 * @return String from the specified cell.
656 public String getStringFromCell( XCellRange xcellrange, int intRow,
657 int intColumn ) {
658 XTextRange xtextrangeStartDate = null;
660 try {
661 // Getting the cell holding the information about the start date
662 XCell xcellStartDate = xcellrange.getCellByPosition(intColumn,
663 intRow);
664 // Querying for the interface XTextRange on the XCell
665 xtextrangeStartDate = (XTextRange)
666 UnoRuntime.queryInterface(XTextRange.class, xcellStartDate);
668 catch( Exception exception ) {
669 this.showExceptionMessage( exception );
672 // Getting the start date
673 return xtextrangeStartDate.getString().trim();
676 /** Writing a specified string to a specified cell.
677 * @param xcellrange Providing access to the cells.
678 * @param intRow Number of row.
679 * @param intColumn Number of column.
680 * @param sDate Date to write to the cell.
682 public void setStringToCell( XCellRange xcellrange, int intRow,
683 int intColumn, String sDate ) {
684 try {
685 // Getting the cell holding the information on the day to start
686 XCell xcellStartDate = xcellrange.getCellByPosition(intColumn,
687 intRow);
688 // Querying for the interface XTextRange on the XCell
689 XTextRange xtextrange = (XTextRange)
690 UnoRuntime.queryInterface(XTextRange.class, xcellStartDate);
691 // Setting the new start date
692 xtextrange.setString( sDate );
694 catch( Exception exception ) {
695 this.showExceptionMessage( exception );
699 /** Calculates the week of day and calls the method "setStringToCell".
700 * @param gregCal Day to be written to the cell.
701 * @param xcellrange Providing access to the cells.
702 * @param intRow Number of row.
703 * @param intColumn Number of column.
705 public void setDayOfWeek( GregorianCalendar gregCal,
706 XCellRange xcellrange, int intRow,
707 int intColumn) {
708 int intDayOfWeek = gregCal.get( Calendar.DAY_OF_WEEK );
709 String sDayOfWeek = "";
710 if ( intDayOfWeek == Calendar.MONDAY ) {
711 sDayOfWeek = "MON";
712 } else if ( intDayOfWeek == Calendar.TUESDAY ) {
713 sDayOfWeek = "TUE";
714 } else if ( intDayOfWeek == Calendar.WEDNESDAY ) {
715 sDayOfWeek = "WED";
716 } else if ( intDayOfWeek == Calendar.THURSDAY ) {
717 sDayOfWeek = "THU";
718 } else if ( intDayOfWeek == Calendar.FRIDAY ) {
719 sDayOfWeek = "FRI";
722 this.setStringToCell( xcellrange, intRow, intColumn,
723 sDayOfWeek );
726 /** Calculates the dates of the official holidays with help of Calc
727 * functions.
728 * @param vectorHolidays Holding all holidays.
729 * @param xcellrange Providing the cells.
730 * @param xfunctionaccess Provides access to functions of the Calc.
731 * @param intYear Year to calculate the official holidays.
733 public void getOfficialHolidays(
734 Vector vectorHolidays,
735 XCellRange xcellrange,
736 XFunctionAccess xfunctionaccess,
737 int intYear ) {
738 try {
739 // Official Holidays for how many years?
740 final int intHowManyYears = 2;
742 // Get the Official Holiday for two years
743 for ( int intNumberOfYear = 0;
744 intNumberOfYear <= ( intHowManyYears - 1 );
745 intNumberOfYear++ ) {
746 intYear += intNumberOfYear;
748 // Getting the Easter sunday
749 Double dEasterSunday = ( Double )
750 xfunctionaccess.callFunction(
751 "EASTERSUNDAY", new Object[] { new Integer(intYear) });
753 int intEasterSunday = (int)Math.round(
754 dEasterSunday.doubleValue());
756 // New-year
757 vectorHolidays.addElement( xfunctionaccess.callFunction(
758 "DATE",
759 new Object[] {
760 new Integer( intYear ),
761 new Integer( 1 ),
762 new Integer( 1 ) } ));
764 // Good Friday
765 vectorHolidays.addElement(
766 new Double( intEasterSunday - 2 ) );
768 // Easter monday
769 vectorHolidays.addElement(
770 new Double( intEasterSunday + 1 ) );
772 // Labour Day
773 vectorHolidays.addElement( xfunctionaccess.callFunction(
774 "DATE",
775 new Object[] {
776 new Integer( intYear ),
777 new Integer( 5 ),
778 new Integer( 1 ) } ));
780 // Ascension Day
781 vectorHolidays.addElement(new Double(intEasterSunday + 39 ));
783 // Pentecost monday
784 vectorHolidays.addElement(new Double(intEasterSunday + 50 ));
786 // German Unification
787 vectorHolidays.addElement( xfunctionaccess.callFunction(
788 "DATE",
789 new Object[] {
790 new Integer( intYear ),
791 new Integer( 10 ),
792 new Integer( 3 ) } ));
794 // Christmas Day First
795 vectorHolidays.addElement( xfunctionaccess.callFunction(
796 "DATE",
797 new Object[] {
798 new Integer( intYear ),
799 new Integer( 12 ),
800 new Integer( 25 ) } ));
802 // Christmas Day Second
803 vectorHolidays.addElement( xfunctionaccess.callFunction(
804 "DATE",
805 new Object[] {
806 new Integer( intYear ),
807 new Integer( 12 ),
808 new Integer( 26 ) } ));
811 catch( Exception exception ) {
812 this.showExceptionMessage( exception );
816 /** Returns the serial number of the date before or after a specified
817 * number of workdays.
818 * @param gregCalStartDate Date to start with the calculation.
819 * @param intDays Number of workdays (e.g. 5 or -3).
820 * @param objectHolidays Private and public holidays to take into account.
821 * @param xfunctionaccess Allows to call functions from the Calc.
822 * @return The gregorian date before or after a specified number of
823 * workdays.
825 public GregorianCalendar getWorkday(
826 GregorianCalendar gregCalStartDate,
827 int intDays, Object[][] objectHolidays,
828 XFunctionAccess xfunctionaccess ) {
829 GregorianCalendar gregCalWorkday = null;
831 try {
832 // Getting the value of the start date
833 Double dDate = ( Double ) xfunctionaccess.callFunction(
834 "DATE",
835 new Object[] {
836 new Integer( gregCalStartDate.get( Calendar.YEAR ) ),
837 new Integer( gregCalStartDate.get( Calendar.MONTH ) + 1 ),
838 new Integer( gregCalStartDate.get( Calendar.DATE ) )
839 } );
841 Double dWorkday = ( Double ) xfunctionaccess.callFunction(
842 "com.sun.star.sheet.addin.Analysis.getWorkday",
843 new Object[] { dDate, new Integer( intDays ), objectHolidays } );
845 Double dYear = ( Double ) xfunctionaccess.callFunction(
846 "YEAR", new Object[] { dWorkday } );
847 Double dMonth = ( Double ) xfunctionaccess.callFunction(
848 "MONTH", new Object[] { dWorkday } );
849 Double dDay = ( Double ) xfunctionaccess.callFunction(
850 "DAY", new Object[] { dWorkday } );
852 gregCalWorkday = new GregorianCalendar(
853 dYear.intValue(),
854 dMonth.intValue() - 1,
855 dDay.intValue() );
857 catch( Exception exception ) {
858 this.showExceptionMessage( exception );
861 return gregCalWorkday;
864 /** Getting the holidays from the spreadsheet.
865 * @param vectorHolidays Holding all holidays.
866 * @param xcellrange Providing the cells.
867 * @param xfunctionaccess Provides the access to functions of the Calc.
869 public void getPrivateHolidays( Vector vectorHolidays,
870 XCellRange xcellrange,
871 XFunctionAccess xfunctionaccess ) {
872 try {
873 int intRow = this.INT_ROW_HOLIDAYS_START;
874 int intColumn = this.INT_COLUMN_HOLIDAYS_START;
876 double dHolidayStart = xcellrange.getCellByPosition(
877 intColumn, intRow ).getValue();
879 double dHolidayEnd = xcellrange.getCellByPosition(
880 intColumn + 1, intRow ).getValue();
882 while ( dHolidayStart != 0 ) {
883 if ( dHolidayEnd == 0 ) {
884 vectorHolidays.addElement(
885 new Integer( (int) Math.round(
886 dHolidayStart ) ) );
888 else {
889 for ( int intHoliday = (int) Math.round(
890 dHolidayStart );
891 intHoliday <= (int) Math.round( dHolidayEnd );
892 intHoliday++ ) {
893 vectorHolidays.addElement( new Double( intHoliday ) );
897 intRow++;
898 dHolidayStart = xcellrange.getCellByPosition(
899 intColumn, intRow).getValue();
900 dHolidayEnd = xcellrange.getCellByPosition(
901 intColumn + 1, intRow).getValue();
904 catch( Exception exception ) {
905 this.showExceptionMessage( exception );
909 /** Showing the stack trace in a JOptionPane.
910 * @param sMessage The message to show.
912 public void showMessage( String sMessage ) {
913 javax.swing.JFrame jframe = new javax.swing.JFrame();
914 jframe.setLocation(100, 100);
915 jframe.setSize(300, 200);
916 jframe.setVisible(true);
917 javax.swing.JOptionPane.showMessageDialog(
918 jframe, sMessage, "Debugging information",
919 javax.swing.JOptionPane.INFORMATION_MESSAGE);
920 jframe.dispose();
923 /** Writing the stack trace from an exception to a string and calling
924 * the method showMessage() with this string.
925 * @param exception The occured exception.
926 * @see showMessage
928 public void showExceptionMessage( Exception exception ) {
929 java.io.StringWriter swriter = new java.io.StringWriter();
930 java.io.PrintWriter printwriter =
931 new java.io.PrintWriter( swriter );
932 exception.printStackTrace( printwriter);
933 System.err.println( exception );
934 this.showMessage( swriter.getBuffer().substring(0) );
939 * Gives a factory for creating the service.
940 * This method is called by the <code>JavaLoader</code>
941 * <p>
942 * @return returns a <code>XSingleComponentFactory</code> for creating
943 * the component
944 * @param sImplName the name of the implementation for which a
945 * service is desired
946 * @see com.sun.star.comp.loader.JavaLoader
948 public static XSingleComponentFactory __getComponentFactory(String sImplName) {
949 XSingleComponentFactory xFactory = null;
951 if ( sImplName.equals( ToDoImpl.class.getName() ) )
952 xFactory = Factory.createComponentFactory(ToDoImpl.class,
953 ToDoImpl.getServiceNames());
955 return xFactory;
959 * Writes the service information into the given registry key.
960 * This method is called by the <code>JavaLoader</code>
961 * <p>
962 * @return returns true if the operation succeeded
963 * @param regKey the registryKey
964 * @see com.sun.star.comp.loader.JavaLoader
966 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
967 return Factory.writeRegistryServiceInfo(ToDoImpl.class.getName(),
968 ToDoImpl.getServiceNames(), regKey);