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