]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/CSVExportPreferences.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / CSVExportPreferences.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.charts.ui;
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 Antti Villberg
25  */
26 public final class CSVExportPreferences {
27
28     public static final String  RECENT_LOCATIONS = "RECENT_CSV_EXPORT_LOCATIONS";
29     public static final String  EXPORT_OVERWRITE = "CSV_EXPORT_OVERWRITE";
30
31     private static final String TAG_PATH                = "path";
32     private static final String ATTR_NAME               = "name";
33
34     public static Deque<String> decodePaths(String recentPathsPref) {
35         Deque<String> result = new LinkedList<String>();
36         try {
37             StringMemento sm = new StringMemento(recentPathsPref);
38             for (IMemento m : sm.getChildren(TAG_PATH)) {
39                 String name = m.getString(ATTR_NAME);
40                 if (name != null && !name.isEmpty())
41                     result.add(name);
42             }
43         } catch (IllegalArgumentException e) {
44         }
45         return result;
46     }
47
48     public static String encodePaths(Deque<String> recentPaths) {
49         StringMemento sm = new StringMemento();
50         for (String path : recentPaths) {
51             IMemento m = sm.createChild(TAG_PATH);
52             m.putString(ATTR_NAME, path);
53         }
54         return sm.toString();
55     }
56
57     public static <T> void removeDuplicates(Iterable<String> iter) {
58         // Remove duplicates
59         Set<String> dups = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
60         for (Iterator<String> it = iter.iterator(); it.hasNext();) {
61             String path = it.next();
62             if (!dups.add(path)) {
63                 it.remove();
64             }
65         }
66     }
67
68 }