]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/Preferences.java
Improved usability of shared library export wizard
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / Preferences.java
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     public static final String  EXPORT_INCLUDE_DEPENDENCIES = "EXPORT_INCLUDE_DEPENDENCIES";
32     public static final String  IMPORT_INCLUDE_DEPENDENCIES = "IMPORT_INCLUDE_DEPENDENCIES";
33
34     private static final String TAG_PATH                = "path";
35     private static final String ATTR_NAME               = "name";
36
37     public static Deque<String> decodePaths(String recentPathsPref) {
38         Deque<String> result = new LinkedList<String>();
39         try {
40             StringMemento sm = new StringMemento(recentPathsPref);
41             for (IMemento m : sm.getChildren(TAG_PATH)) {
42                 String name = m.getString(ATTR_NAME);
43                 if (name != null && !name.isEmpty())
44                     result.add(name);
45             }
46         } catch (IllegalArgumentException e) {
47         }
48         return result;
49     }
50
51     public static String encodePaths(Deque<String> recentPaths) {
52         StringMemento sm = new StringMemento();
53         for (String path : recentPaths) {
54             IMemento m = sm.createChild(TAG_PATH);
55             m.putString(ATTR_NAME, path);
56         }
57         return sm.toString();
58     }
59
60     public static <T> void removeDuplicates(Iterable<String> iter) {
61         // Remove duplicates
62         Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
63         for (Iterator<String> it = iter.iterator(); it.hasNext();) {
64             String path = it.next();
65             if (!dups.add(path)) {
66                 it.remove();
67             }
68         }
69     }
70
71 }