KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > HistoryUtils


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.browser.common.widgets;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
30
31
32 /**
33  * The HistoryUtils are used to save and load the history of input fields.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev$, $Date$
37  */

38 public class HistoryUtils
39 {
40
41     /**
42      * Saves the the given value under the given key in the dialog settings.
43      *
44      * @param key the key
45      * @param value the value
46      */

47     public static void save( String JavaDoc key, String JavaDoc value )
48     {
49         // get current history
50
String JavaDoc[] history = load( key );
51         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>( Arrays.asList( history ) );
52
53         // add new value or move to first position
54
if ( list.contains( value ) )
55         {
56             list.remove( value );
57         }
58         list.add( 0, value );
59
60         // check history size
61
while ( list.size() > BrowserCommonConstants.HISTORYSIZE )
62         {
63             list.remove( list.size() - 1 );
64         }
65
66         // save
67
history = list.toArray( new String JavaDoc[list.size()] );
68         BrowserCommonActivator.getDefault().getDialogSettings().put( key, history );
69
70     }
71
72
73     /**
74      * Loads the value of the given key from the dialog settings
75      *
76      * @param key the key
77      * @return the value
78      */

79     public static String JavaDoc[] load( String JavaDoc key )
80     {
81         String JavaDoc[] history = BrowserCommonActivator.getDefault().getDialogSettings().getArray( key );
82         if ( history == null )
83         {
84             history = new String JavaDoc[0];
85         }
86         return history;
87     }
88
89 }
90
Popular Tags