]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/Utils.java
59c9329548d9bf46be82a12a6091ca8a57f58ce5
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / Utils.java
1 package org.simantics.team;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 import java.nio.ByteBuffer;\r
6 import java.nio.CharBuffer;\r
7 import java.nio.charset.CharacterCodingException;\r
8 import java.nio.charset.Charset;\r
9 import java.nio.charset.CodingErrorAction;\r
10 \r
11 import org.eclipse.core.runtime.preferences.InstanceScope;\r
12 import org.eclipse.jface.preference.IPreferenceStore;\r
13 import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.team.ui.Preferences;\r
16 \r
17 public class Utils {\r
18     public static final File getNewFolder(int index, String prefix, String suffix) {\r
19         File toFolder;\r
20         do {\r
21             toFolder = new File(prefix + index + suffix);\r
22         } while (toFolder.exists());\r
23         return toFolder;\r
24     }\r
25     private static final Charset ASCII = Charset.forName("US-ASCII");\r
26     protected static final Charset UTF8 = Charset.forName("UTF-8");\r
27     public static final String bytesToStringASCII(byte[] bytes, int offset, int length)\r
28     throws IOException {\r
29         return bytesToString(bytes, offset, length, ASCII);\r
30     }\r
31     public static final String bytesToStringUTF8(byte[] bytes, int offset, int length)\r
32     throws IOException {\r
33         return bytesToString(bytes, offset, length, UTF8);\r
34     }\r
35     public static final String bytesToString(byte[] bytes, int offset, int length, Charset charset)\r
36     throws IOException {\r
37         if (bytes == null || offset < 0 || offset > length - 1 || length < 0 || length > bytes.length)\r
38             return null;\r
39         ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length);\r
40         CharBuffer cbuf;\r
41         String s = null;\r
42         try {\r
43             cbuf =  charset.newDecoder().decode(bbuf);\r
44             s = cbuf.toString();\r
45         } catch (CharacterCodingException e) {\r
46             bbuf.rewind();\r
47             try {\r
48                 cbuf = charset.newDecoder()\r
49                 .onMalformedInput(CodingErrorAction.REPLACE)\r
50                 .onUnmappableCharacter(CodingErrorAction.REPLACE)\r
51                 .decode(bbuf);\r
52                 s = cbuf.toString();\r
53             } catch (CharacterCodingException e1) {\r
54                 throw new IOException("String conversion error.", e1);\r
55             }\r
56         }\r
57         return s;\r
58     }\r
59     public static final File getTeamFolder() \r
60     throws DatabaseException {\r
61         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
62         String currentTeamFolder = store.getString(Preferences.CURRENT_TEAM_FOLDER);\r
63         if (!currentTeamFolder.equals(""))\r
64             return new File(currentTeamFolder);\r
65         String name = System.getProperty("staging.team.folder");\r
66         if (null != name && !name.equals(""))\r
67             return new File(name);\r
68         String folder = System.getProperty("java.io.tmpdir");\r
69         if (null == folder || folder.equals(""))\r
70             throw new DatabaseException("Could not get team folder path.");\r
71         return null;\r
72 //        IProduct product = Platform.getProduct();\r
73 //        if (null != product)\r
74 //            name = "Team " + product.getName();\r
75 //        else\r
76 //          name = "Default Team Folder";\r
77 //        return new File(folder, name);\r
78     }\r
79     public static final void setTeamFolder(File teamFolder) \r
80     throws DatabaseException {\r
81         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
82         if (null == teamFolder)\r
83             store.setValue(Preferences.CURRENT_TEAM_FOLDER, "");\r
84         else\r
85             store.setValue(Preferences.CURRENT_TEAM_FOLDER, teamFolder.getAbsolutePath());\r
86     }\r
87 }\r