File: EpubMetadataDialog.java

package info (click to toggle)
writer2latex 1.6.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,456 kB
  • sloc: java: 33,474; xml: 4,710; javascript: 3,067; sh: 67; makefile: 29
file content (708 lines) | stat: -rw-r--r-- 24,841 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/************************************************************************
 *
 *  EpubMetadataDialog.java
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *  Copyright: 2002-2011 by Henrik Just
 *
 *  All Rights Reserved.
 * 
 *  Version 1.2 (2011-07-20)
 *
 */

package org.openoffice.da.comp.writer2xhtml;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openoffice.da.comp.w2lcommon.helper.DialogBase;
import org.openoffice.da.comp.w2lcommon.helper.SimpleDialog;

import writer2latex.util.CSVList;
import writer2latex.util.Misc;

import com.sun.star.awt.XDialog;
import com.sun.star.beans.IllegalTypeException;
import com.sun.star.beans.NotRemoveableException;
import com.sun.star.beans.Property;
import com.sun.star.beans.PropertyExistException;
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertyContainer;
import com.sun.star.beans.XPropertySet;
import com.sun.star.document.XDocumentProperties;
import com.sun.star.document.XDocumentPropertiesSupplier;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.ui.dialogs.ExecutableDialogResults;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.DateTime;

// TODO: Create the UNO helper class DocumentPropertiesAccess

/** This class provides a UNO component which implements a custom metadata editor UI for the EPUB export
 */
public class EpubMetadataDialog extends DialogBase {
	// Author data
	private class AuthorInfo {
		String sName = "";
		boolean isCreator = true;
		String sRole = "";
	}
	
	// Date data
	private class DateInfo {
		int nDate = 0;
		String sEvent = "";
	}
	
	// All the user defined properties we handle
	private static final String IDENTIFIER="Identifier";
	private static final String CREATOR="Creator";
	private static final String CONTRIBUTOR="Contributor";
	private static final String DATE="Date";
	private static final String PUBLISHER="Publisher";
	private static final String TYPE="Type";
	private static final String FORMAT="Format";
	private static final String SOURCE="Source";
	private static final String RELATION="Relation";
	private static final String COVERAGE="Coverage";
	private static final String RIGHTS="Rights";
	
	private static final String[] sRoles = {"", "adp", "ann", "arr", "art", "asn", "aut", "aqt", "aft", "aui", "ant", "bkp",
		"clb", "cmm", "dsr", "edt", "ill", "lyr", "mdc", "mus", "nrt", "oth", "pht", "prt", "red", "rev", "spn", "ths", "trc", "trl"};
	private static HashMap<String,Short> backRoles;
	static {
		backRoles = new HashMap<String,Short>();
		int nCount = sRoles.length;
		for (short i=0; i<nCount; i++) {
			backRoles.put(sRoles[i], i);
		}
	}
	
	// Access to the document properties
	private XDocumentProperties xDocumentProperties=null;
	private XPropertyContainer xUserProperties=null;
	private XPropertySet xUserPropertySet=null;
	
	// Author and date bookkeeping
	private Vector<AuthorInfo> authors = new Vector<AuthorInfo>();
	private Vector<DateInfo> dates = new Vector<DateInfo>();
	
	// Number formatter
    private NumberFormat formatter = new  DecimalFormat("00");
    
    // Pattern matcher for dates
	Pattern datePattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
    
    public EpubMetadataDialog(XComponentContext xContext) {
		super(xContext);
	}

	/** The component will be registered under this name.
     */
    public static String __serviceName = "org.openoffice.da.writer2xhtml.EpubMetadataDialog";

    /** The component should also have an implementation name.
     */
    public static String __implementationName = "org.openoffice.da.comp.writer2xhtml.EpubMetadataDialog";

    // --------------------------------------------------
    // Ensure that the super can find us :-)
    @Override public String getDialogLibraryName() {
		return "W2XDialogs2";
	}

	@Override public String getDialogName() {
		return "EpubMetadata";
	}
	
    // --------------------------------------------------
    // Implement the interface XDialogEventHandler
    @Override public boolean callHandlerMethod(XDialog xDialog, Object event, String sMethod) {
        if (sMethod.equals("UseCustomIdentifierChange")) {
        	return useCustomIdentifierChange();
        }
        else if (sMethod.equals("AuthorAddClick")) {
        	return authorAddclick();
        }
        else if (sMethod.equals("AuthorModifyClick")) {
        	return authorModifyclick();
        }
        else if (sMethod.equals("AuthorDeleteClick")) {
        	return authorDeleteclick();
        }
        else if (sMethod.equals("AuthorUpClick")) {
        	return authorUpclick();
        }
        else if (sMethod.equals("AuthorDownClick")) {
        	return authorDownclick();
        }
        else if (sMethod.equals("DateAddClick")) {
        	return dateAddClick();
        }
        else if (sMethod.equals("DateModifyClick")) {
        	return dateModifyClick();
        }
        else if (sMethod.equals("DateDeleteClick")) {
        	return dateDeleteClick();
        }
        else if (sMethod.equals("DateUpClick")) {
        	return dateUpClick();
        }
        else if (sMethod.equals("DateDownClick")) {
        	return dateDownClick();
        }
        return false;
    }
	
    @Override public String[] getSupportedMethodNames() {
        String[] sNames = { "UseCustomIdentifierChange",
        		"AuthorAddClick", "AuthorModifyClick", "AuthorDeleteClick", "AuthorUpClick", "AuthorDownClick",
        		"DataAddClick", "DateModifyClick", "DateDeleteClick", "DateUpClick", "DateDownClick"};
        return sNames;
    }
    
    private boolean useCustomIdentifierChange() {
    	boolean bEnabled = getCheckBoxStateAsBoolean("UseCustomIdentifier");
    	setControlEnabled("IdentifierLabel",bEnabled);
    	setControlEnabled("Identifier",bEnabled);
    	setControlEnabled("IdentifierTypeLabel",bEnabled);
    	setControlEnabled("IdentifierType",bEnabled);
    	return true;
    }
    
    private boolean authorAddclick() {
    	SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.AuthorDialog");
    	if (dialog.getDialog()!=null) {
    		dialog.getControls().setListBoxSelectedItem("Type", (short) 0);
    		dialog.getControls().setListBoxSelectedItem("Role", (short) 0); 		
    		if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    			AuthorInfo author = new AuthorInfo();
    			author.sName = dialog.getControls().getTextFieldText("Author");
    			author.sRole = sRoles[dialog.getControls().getListBoxSelectedItem("Role")];
    			author.isCreator = dialog.getControls().getListBoxSelectedItem("Type")==0;
    			authors.add(author);
    			updateAuthorList((short) (authors.size()-1));
    		}
    		dialog.getDialog().endExecute();
    	}
    	return true;
    }
    
    private boolean authorModifyclick() {
    	short nIndex = getListBoxSelectedItem("Authors");
    	AuthorInfo author = authors.get(nIndex);
    	SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.AuthorDialog");
    	if (dialog.getDialog()!=null) {
    		dialog.getControls().setTextFieldText("Author", author.sName);
    		dialog.getControls().setListBoxSelectedItem("Type", author.isCreator ? (short)0 : (short) 1);
    		dialog.getControls().setListBoxSelectedItem("Role", backRoles.containsKey(author.sRole)? backRoles.get(author.sRole) : (short)0); 		
    		if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    			author.sName = dialog.getControls().getTextFieldText("Author");
    			author.sRole = sRoles[dialog.getControls().getListBoxSelectedItem("Role")];
    			author.isCreator = dialog.getControls().getListBoxSelectedItem("Type")==0;
    			updateAuthorList(nIndex);
    		}
    		dialog.getDialog().endExecute();
    	}    	    	
    	return true;
    }
    
    private boolean authorDeleteclick() {
    	if (authors.size()>0) {
    		SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.DeleteDialog");
    		if (dialog.getDialog()!=null) {
    			short nIndex = getListBoxSelectedItem("Authors");
    			String sLabel = dialog.getControls().getLabelText("DeleteLabel");
    			sLabel = sLabel.replaceAll("%s", authors.get(nIndex).sName);
    			dialog.getControls().setLabelText("DeleteLabel", sLabel);
    			if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    				authors.remove(nIndex);
    				updateAuthorList(nIndex<authors.size() ? (short) nIndex : (short) (nIndex-1));
    			}
    		}
    	}
    	return true;
    }
    
    private boolean authorUpclick() {
		short nIndex = getListBoxSelectedItem("Authors");
		if (nIndex>0) {
			AuthorInfo author = authors.get(nIndex);
			authors.set(nIndex, authors.get(nIndex-1));
			authors.set(nIndex-1, author);
			updateAuthorList((short) (nIndex-1));
		}
    	return true;
    }
    
    private boolean authorDownclick() {
		short nIndex = getListBoxSelectedItem("Authors");
		if (nIndex+1<authors.size()) {
			AuthorInfo author = authors.get(nIndex);
			authors.set(nIndex, authors.get(nIndex+1));
			authors.set(nIndex+1, author);
			updateAuthorList((short) (nIndex+1));
		}
    	return true;
    }
    
    private boolean dateAddClick() {
    	SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.DateDialog");
    	if (dialog.getDialog()!=null) {
    		dialog.getControls().setDateFieldValue("Date", datetime2int(xDocumentProperties.getModificationDate()));
    		if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    			DateInfo date = new DateInfo();
    			date.nDate = dialog.getControls().getDateFieldValue("Date");
    			date.sEvent = dialog.getControls().getTextFieldText("Event").trim();
    			dates.add(date);
    			updateDateList((short) (dates.size()-1));
    		}
    		dialog.getDialog().endExecute();
    	}
    	return true;
    }
    
    private boolean dateModifyClick() {
    	short nIndex = getListBoxSelectedItem("Dates");
    	DateInfo date = dates.get(nIndex);
    	SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.DateDialog");
    	if (dialog.getDialog()!=null) {
    		dialog.getControls().setDateFieldValue("Date", date.nDate);
    		dialog.getControls().setTextFieldText("Event", date.sEvent);
    		if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    			date.nDate = dialog.getControls().getDateFieldValue("Date");
    			date.sEvent = dialog.getControls().getTextFieldText("Event").trim();
    			updateDateList(nIndex);
    		}
    		dialog.getDialog().endExecute();
    	}    	    	
    	return true;
    }
    
    private boolean dateDeleteClick() {
    	if (dates.size()>0) {
    		SimpleDialog dialog = new SimpleDialog(xContext,"W2XDialogs2.DeleteDialog");
    		if (dialog.getDialog()!=null) {
    			short nIndex = getListBoxSelectedItem("Dates");
    			String sLabel = dialog.getControls().getLabelText("DeleteLabel");
    			sLabel = sLabel.replaceAll("%s", formatDate(dates.get(nIndex).nDate));
    			dialog.getControls().setLabelText("DeleteLabel", sLabel);
    			if (dialog.getDialog().execute()==ExecutableDialogResults.OK) {
    				dates.remove(nIndex);
    				updateDateList(nIndex<dates.size() ? (short) nIndex : (short) (nIndex-1));
    			}
    		}
    	}
    	return true;
    }
    
    private boolean dateUpClick() {
		short nIndex = getListBoxSelectedItem("Dates");
		if (nIndex>0) {
			DateInfo date = dates.get(nIndex);
			dates.set(nIndex, dates.get(nIndex-1));
			dates.set(nIndex-1, date);
			updateDateList((short) (nIndex-1));
		}
    	return true;
    }
    
    private boolean dateDownClick() {
		short nIndex = getListBoxSelectedItem("Dates");
		if (nIndex+1<dates.size()) {
			DateInfo date = dates.get(nIndex);
			dates.set(nIndex, dates.get(nIndex+1));
			dates.set(nIndex+1, date);
			updateDateList((short) (nIndex+1));
		}
    	return true;
    }
    
    // --------------------------------------------------
    // Get and set properties from and to current document 
    
	@Override protected void initialize() {
		// Get the document properties
    	XDesktop xDesktop;
    	Object desktop;
		try {
			desktop = xContext.getServiceManager().createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
		} catch (Exception e) {
			// Failed to get desktop
			return;
		}
    	xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
		XComponent xComponent = xDesktop.getCurrentComponent();
		XDocumentPropertiesSupplier xSupplier = (XDocumentPropertiesSupplier) UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xComponent);
		
		// Get the document properties (we need several interfaces)
		xDocumentProperties = xSupplier.getDocumentProperties();
		xUserProperties= xDocumentProperties.getUserDefinedProperties();
		xUserPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xUserProperties);
		
		// Get the custom identifier and set the text fields
		String[] sIdentifiers = getProperties(IDENTIFIER,false);
		setCheckBoxStateAsBoolean("UseCustomIdentifier",sIdentifiers.length>0);
		useCustomIdentifierChange();
		if (sIdentifiers.length>0) { // Use the first if we have several...
			setTextFieldText("Identifier",getStringValue(sIdentifiers[0]));
			setTextFieldText("IdentifierType",getSuffix(sIdentifiers[0]));
		}
		
		// Get the authors and set the list box
		String[] sCreators = getProperties(CREATOR,false);
		for (String sCreator : sCreators) {
			AuthorInfo creator = new AuthorInfo();
			creator.sName = getStringValue(sCreator);
			creator.sRole = getSuffix(sCreator);
			creator.isCreator = true;
			authors.add(creator);
		}
		String[] sContributors = getProperties(CONTRIBUTOR,false);
		for (String sContributor : sContributors) {
			AuthorInfo contributor = new AuthorInfo();
			contributor.sName = getStringValue(sContributor);
			contributor.sRole = getSuffix(sContributor);
			contributor.isCreator = false;
			authors.add(contributor);
		}
		updateAuthorList((short) 0);
		
		// Get the dates and set the list box
		String[] sDates = getProperties(DATE,false);
		for (String sDate : sDates) {
			DateInfo date = new DateInfo();
			DateTime dt = getDateValue(sDate);
			if (dt!=null) { // We accept either a date
				date.nDate = datetime2int(dt);
			}
			else { // Or a string in the form yyyy-mm-dd
				date.nDate = parseDate(getStringValue(sDate));
			}
			date.sEvent = getSuffix(sDate);
			dates.add(date);
		}
		updateDateList((short) 0);
		
		// Get the standard properties and set the text fields
		setTextFieldText("Title",xDocumentProperties.getTitle());
		setTextFieldText("Subject",xDocumentProperties.getSubject());
		String[] sKeywords = xDocumentProperties.getKeywords();
		CSVList keywords = new CSVList(", ");
		for (String sKeyword : sKeywords) {
			keywords.addValue(sKeyword);
		}
		setTextFieldText("Keywords",keywords.toString());
		setTextFieldText("Description",xDocumentProperties.getDescription());
		
		// Get the simple user properties and set the text fields
		readSimpleProperty(PUBLISHER);
		readSimpleProperty(TYPE);
		readSimpleProperty(FORMAT);
		readSimpleProperty(SOURCE);
		readSimpleProperty(RELATION);
		readSimpleProperty(COVERAGE);
		readSimpleProperty(RIGHTS);

	}
		
	@Override protected void endDialog() {
		// Set the custom identifier from the text fields
		String[] sIdentifiers = getProperties(IDENTIFIER,false);
		for (String sIdentifier : sIdentifiers) { // Remove old identifier(s)
			removeProperty(sIdentifier);
		}
		if (getCheckBoxStateAsBoolean("UseCustomIdentifier")) {
			String sName = IDENTIFIER;
			if (getTextFieldText("IdentifierType").trim().length()>0) {
				sName+="."+getTextFieldText("IdentifierType").trim();
			}
			addProperty(sName);
			setValue(sName,getTextFieldText("Identifier"));
		}
		
		// Set the authors from the list box
		String[] sCreators = getProperties(CREATOR,false);
		for (String sCreator : sCreators) { // remove old creators
			removeProperty(sCreator);
		}
		String[] sContributors = getProperties(CONTRIBUTOR,false); 
		for (String sContributor : sContributors) { // remove old contributors
			removeProperty(sContributor);
		}
		int i=0;
		for (AuthorInfo author : authors) {
			String sName = (author.isCreator ? CREATOR : CONTRIBUTOR)+formatter.format(++i);
			if (author.sRole.length()>0) {
				sName+="."+author.sRole;
			}
			addProperty(sName);
			setValue(sName,author.sName);
		}
		
		// Set the dates from the list box
		String[] sDates = getProperties(DATE,false); 
		for (String sDate : sDates) { // remove old dates
			removeProperty(sDate);
		}
		i=0;
		for (DateInfo date : dates) {
			String sName = DATE+formatter.format(++i);
			if (date.sEvent.length()>0) {
				sName+="."+date.sEvent;
			}
			addProperty(sName);
			setValue(sName,formatDate(date.nDate));
			// Doesn't work (why not?)
			//setValue(sName,int2datetime(date.nDate));
		}
		
		// Set the standard properties from the text fields
		xDocumentProperties.setTitle(getTextFieldText("Title"));
		xDocumentProperties.setSubject(getTextFieldText("Subject"));
		String[] sKeywords = getTextFieldText("Keywords").split(",");
		for (int j=0; j<sKeywords.length; j++) {
			sKeywords[j] = sKeywords[j].trim();
		}
		xDocumentProperties.setKeywords(sKeywords);
		xDocumentProperties.setDescription(getTextFieldText("Description"));
		
		// Set the simple user properties from the text fields
		writeSimpleProperty(PUBLISHER);
		writeSimpleProperty(TYPE);
		writeSimpleProperty(FORMAT);
		writeSimpleProperty(SOURCE);
		writeSimpleProperty(RELATION);
		writeSimpleProperty(COVERAGE);
		writeSimpleProperty(RIGHTS);
	}
	
	// Get the suffix of a user defined property (portion after fist ., if any)
	private String getSuffix(String sPropertyName) {
		int nDot = sPropertyName.indexOf(".");
		return nDot>-1 ? sPropertyName.substring(nDot+1) : "";
	}

	// Get all currently defined user properties with a specific name or prefix
	private String[] getProperties(String sPrefix, boolean bComplete) {
		HashSet<String> names = new HashSet<String>();
		Property[] xProps = xUserPropertySet.getPropertySetInfo().getProperties();
		for (Property prop : xProps) {
			String sName = prop.Name;
			String sLCName = sName.toLowerCase();
			String sLCPrefix = sPrefix.toLowerCase();
			if ((bComplete && sLCName.equals(sLCPrefix)) || (!bComplete && sLCName.startsWith(sLCPrefix))) {
				names.add(sName);
			}
		}
		return Misc.sortStringSet(names);
	}
	
	// Add a user property
	private void addProperty(String sName) {
		try {
			xUserProperties.addProperty(sName, (short) 128, ""); // 128 means removeable, last parameter is default value
		} catch (PropertyExistException e) {
		} catch (IllegalTypeException e) {
		} catch (IllegalArgumentException e) {
		}
	}
	
	// Delete a user property
	private void removeProperty(String sName) {
		try {
			xUserProperties.removeProperty(sName);
		} catch (UnknownPropertyException e) {
		} catch (NotRemoveableException e) {
		}
	}
	
	// Set the value of a user property (failing silently if the property does not exist)
	private void setValue(String sName, Object value) {
		try {
			xUserPropertySet.setPropertyValue(sName, value);
		} catch (UnknownPropertyException e) {
		} catch (PropertyVetoException e) {
		} catch (IllegalArgumentException e) {
		} catch (WrappedTargetException e) {
		}
	}
	
	// Get the string value of a user property (returning null if the property does not exist)
	private String getStringValue(String sName) {
		Object value = getValue(sName);
		
		if (value!=null && AnyConverter.isString(value)) {
			try {
				return AnyConverter.toString(value);
			} catch (IllegalArgumentException e) {
				return null;
			}
		}
		return null;
	}
	
	private DateTime getDateValue(String sName) {
		Object value = getValue(sName);
		
		if (value!=null && value instanceof DateTime) {
			return (DateTime) value;
		}
		return null;
	}
	
	// Get the value of a user property (returning null if the property does not exist)
	private Object getValue(String sName) {
		try {
			return xUserPropertySet.getPropertyValue(sName);
		} catch (UnknownPropertyException e) {
			return null;
		} catch (WrappedTargetException e) {
			return null;
		}
	}
	
	private void updateAuthorList(short nItem) {
		int nCount = authors.size();
		if (nCount>0) {
			String[] sAuthors = new String[nCount];
			for (int i=0; i<nCount; i++) {
				AuthorInfo author = authors.get(i);
				sAuthors[i] = author.sName
				+" ("
				+(author.isCreator ? "creator":"contributor")
				+(author.sRole.length()>0 ? ", "+author.sRole : "")
				+")";
			}
			setListBoxStringItemList("Authors", sAuthors);
			setListBoxSelectedItem("Authors",nItem);
			setControlEnabled("Authors", true);
		}
		else { // Display the fall-back author
			String[] sAuthors = new String[1];
			//sAuthors[0] = xDocumentProperties.getAuthor()+" (default creator)";
			sAuthors[0] = xDocumentProperties.getModifiedBy()+" (default creator)";
			setListBoxStringItemList("Authors", sAuthors);
			setControlEnabled("Authors", false);
		}
		setControlEnabled("ModifyAuthorButton",nCount>0);
		setControlEnabled("DeleteAuthorButton",nCount>0);
		setControlEnabled("AuthorUpButton",nCount>1);
		setControlEnabled("AuthorDownButton",nCount>1);
	}
	
	private void updateDateList(short nItem) {
		int nCount = dates.size();
		if (nCount>0) {
			String[] sDates = new String[nCount];
			for (int i=0; i<nCount; i++) {
				DateInfo date = dates.get(i);
				sDates[i] = formatDate(date.nDate);
				if (date.sEvent.length()>0) {
					sDates[i]+=" (" + date.sEvent + ")";
				}
			}
			setListBoxStringItemList("Dates", sDates);
			setListBoxSelectedItem("Dates",nItem);
			setControlEnabled("Dates", true);
		}
		else { // Display the fall-back date
			String[] sDates = new String[1];
			sDates[0] = formatDate(datetime2int(xDocumentProperties.getModificationDate()))+" (default date)";
			setListBoxStringItemList("Dates", sDates);
			setControlEnabled("Dates", false);
		}
		setControlEnabled("ModifyDateButton",nCount>0);
		setControlEnabled("DeleteDateButton",nCount>0);
		setControlEnabled("DateUpButton",nCount>1);
		setControlEnabled("DateDownButton",nCount>1);
	}
	
	private void readSimpleProperty(String sName) {
		String[] sNames = getProperties(sName,true);
		if (sNames.length>0) {
			String sValue = getStringValue(sNames[0]);
			if (sValue!=null) {
				setTextFieldText(sName, sValue);
			}
		}
	}
	
	private void writeSimpleProperty(String sName) {
		String[] sOldNames = getProperties(sName,true);
		for (String sOldName : sOldNames) {
			removeProperty(sOldName);
		}
		String sValue = getTextFieldText(sName);
		if (sValue.length()>0) {
			addProperty(sName);
			setValue(sName,sValue);
		}
	}
	
	// Date fields uses integers for dates (format yyyymmdd)
	// Document properties uses com.sun.star.util.DateTime
	// Also dates should be formatted as yyyy-mm-dd as strings
	// Thus we need a few conversion methods

	// Format a integer date as yyyy-mm-dd
	private String formatDate(int nDate) {
		String sDate = Integer.toString(nDate);
		if (sDate.length()==8) {
			return sDate.substring(0,4)+"-"+sDate.substring(4, 6)+"-"+sDate.substring(6);
		}
		else {
			return "???";
		}
	}
	
	// Parse a string as a date in the format yyyy-mm-dd (returning 0 on failure)
	private int parseDate(String sDate) {
		Matcher matcher = datePattern.matcher(sDate);
		if (matcher.matches()) {
			return Misc.getPosInteger(matcher.group(1)+matcher.group(2)+matcher.group(3),0);
		}
		return 0;
	}
	
	// Convert an integer to com.sun.star.util.DateTime
	/*private DateTime int2datetime(int nDate) {
		DateTime date = new DateTime();
		date.Year = (short) (nDate/10000);
		date.Month = (short) ((nDate%10000)/100);
		date.Day = (short) (nDate%100);
		return date;
	}*/
	
	// Convert a com.sun.star.util.DateTime to integer
	private int datetime2int(DateTime date) {
		return 10000*date.Year+100*date.Month+date.Day;				
	}
    
}