]> gerrit.simantics Code Review - simantics/platform.git/blob
723f56df96ec961d1d3bc2b2c92c01769c0b0adb
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
13
14 import java.util.Deque;
15 import java.util.Iterator;
16 import java.util.LinkedList;
17 import java.util.Set;
18 import java.util.TreeSet;
19
20 import org.eclipse.ui.IMemento;
21 import org.simantics.utils.ui.workbench.StringMemento;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public final class Preferences {
27
28     public static final String  RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS = "RECENT_SHARED_LIBRARY_IMPORT_LOCATIONS";
29     public static final String  RECENT_SHARED_LIBRARY_EXPORT_LOCATIONS = "RECENT_SHARED_LIBRARY_EXPORT_LOCATIONS";
30     public static final String  SHARED_LIBRARY_EXPORT_OVERWRITE = "SHARED_LIBRARY_EXPORT_OVERWRITE";
31
32     private static final String TAG_PATH                = "path";
33     private static final String ATTR_NAME               = "name";
34
35     public static Deque<String> decodePaths(String recentPathsPref) {
36         Deque<String> result = new LinkedList<String>();
37         try {
38             StringMemento sm = new StringMemento(recentPathsPref);
39             for (IMemento m : sm.getChildren(TAG_PATH)) {
40                 String name = m.getString(ATTR_NAME);
41                 if (name != null && !name.isEmpty())
42                     result.add(name);
43             }
44         } catch (IllegalArgumentException e) {
45         }
46         return result;
47     }
48
49     public static String encodePaths(Deque<String> recentPaths) {
50         StringMemento sm = new StringMemento();
51         for (String path : recentPaths) {
52             IMemento m = sm.createChild(TAG_PATH);
53             m.putString(ATTR_NAME, path);
54         }
55         return sm.toString();
56     }
57
58     public static <T> void removeDuplicates(Iterable<String> iter) {
59         // Remove duplicates
60         Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
61         for (Iterator<String> it = iter.iterator(); it.hasNext();) {
62             String path = it.next();
63             if (!dups.add(path)) {
64                 it.remove();
65             }
66         }
67     }
68
69 }