KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mountainminds > eclemma > internal > ui > wizards > ComboHistory


1 /*******************************************************************************
2  * Copyright (c) 2006 Mountainminds GmbH & Co. KG
3  * This software is provided under the terms of the Eclipse Public License v1.0
4  * See http://www.eclipse.org/legal/epl-v10.html.
5  *
6  * $Id: ComboHistory.java 74 2006-09-12 21:11:40Z mho $
7  ******************************************************************************/

8 package com.mountainminds.eclemma.internal.ui.wizards;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.eclipse.jface.dialogs.IDialogSettings;
15 import org.eclipse.swt.widgets.Combo;
16
17 /**
18  * Utility class for saving/restoring the history of entered text strings in a
19  * combo box widget.
20  *
21  * @author Marc R. Hoffmann
22  * @version $Revision: 74 $
23  */

24 public class ComboHistory {
25
26   /**
27    * Maximum number of history items.
28    */

29   public static final int HISTORY_LIMIT = 10;
30
31   /**
32    * Restores the items of a combo box.
33    *
34    * @param settings
35    * dialog setting used to persist the history
36    * @param key
37    * key used for this combo box
38    * @param combo
39    * the combo box
40    */

41   public static void restore(IDialogSettings settings, String JavaDoc key, Combo combo) {
42     String JavaDoc[] destinations = settings.getArray(key);
43     if (destinations != null) {
44       combo.setItems(destinations);
45       if (destinations.length > 0) {
46         combo.setText(destinations[0]);
47       }
48     }
49   }
50
51   /**
52    * Saves the items of the given combo box as its history. The current text
53    * value is added as the most recent history item. The numer of history items
54    * is limited.
55    *
56    * @param settings
57    * dialog setting used to persist the history
58    * @param key
59    * key used for this combo box
60    * @param combo
61    * the combo box
62    */

63   public static void save(IDialogSettings settings, String JavaDoc key, Combo combo) {
64     List JavaDoc history = new ArrayList JavaDoc(Arrays.asList(combo.getItems()));
65     history.remove(combo.getText());
66     history.add(0, combo.getText());
67     if (history.size() > HISTORY_LIMIT) {
68       history = history.subList(0, HISTORY_LIMIT);
69     }
70     settings.put(key, (String JavaDoc[]) history.toArray(new String JavaDoc[0]));
71   }
72
73 }
74
Popular Tags