KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > time > InPlaceGeneralizedTimeValueEditor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.valueeditors.time;
22
23
24 import java.text.DateFormat JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.IValue;
30 import org.apache.directory.ldapstudio.valueeditors.AbstractInPlaceStringValueEditor;
31
32
33 /**
34  * Implementation of IValueEditor for syntax 1.3.6.1.4.1.1466.115.121.1.24
35  * (Generalized Time).
36  *
37  * Currently only the getDisplayXXX() methods are implemented.
38  * For modification the raw string must be edited.
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  * @version $Rev$, $Date$
42  */

43 public class InPlaceGeneralizedTimeValueEditor extends AbstractInPlaceStringValueEditor
44 {
45
46     /**
47      * {@inheritDoc}
48      *
49      * Returns the proper formatted date and time, Timezone is
50      * convertet to the default locale.
51      *
52      * Can handle
53      * <ul>
54      * <li>default LDAP format: yyyyMMddHHmmssZ
55      * <li>Active Directory format: yyyyMMddHHmmss.SSSZ
56      * </ul>
57      */

58     public String JavaDoc getDisplayValue( IValue value )
59     {
60         String JavaDoc displayValue = super.getDisplayValue( value );
61
62         if ( !showRawValues() )
63         {
64             DateFormat JavaDoc ldapFormat = new SimpleDateFormat JavaDoc( "yyyyMMddHHmmssZ" );
65             DateFormat JavaDoc activeDirectoryFormat = new SimpleDateFormat JavaDoc( "yyyyMMddHHmmss'.'SSSZ" );
66             DateFormat JavaDoc targetFormat = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.LONG );
67
68             String JavaDoc s = displayValue;
69             if ( s.matches( "[\\.0-9]+Z" ) )
70             {
71                 s = s.replaceAll( "Z", "GMT" );
72             }
73
74             try
75             {
76                 Date JavaDoc date = ldapFormat.parse( s );
77                 displayValue = targetFormat.format( date ) + " (" + displayValue + ")";
78             }
79             catch ( ParseException JavaDoc e1 )
80             {
81                 try
82                 {
83                     Date JavaDoc date = activeDirectoryFormat.parse( s );
84                     displayValue = targetFormat.format( date ) + " (" + displayValue + ")";
85                 }
86                 catch ( ParseException JavaDoc e2 )
87                 {
88                 }
89             }
90         }
91
92         return displayValue;
93     }
94
95 }
96
Popular Tags