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