1 package org.simantics.team;
\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
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
17 public class Utils {
\r
18 public static final File getNewFolder(int index, String prefix, String suffix) {
\r
21 toFolder = new File(prefix + index + suffix);
\r
22 } while (toFolder.exists());
\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
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
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
39 ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length);
\r
43 cbuf = charset.newDecoder().decode(bbuf);
\r
44 s = cbuf.toString();
\r
45 } catch (CharacterCodingException e) {
\r
48 cbuf = charset.newDecoder()
\r
49 .onMalformedInput(CodingErrorAction.REPLACE)
\r
50 .onUnmappableCharacter(CodingErrorAction.REPLACE)
\r
52 s = cbuf.toString();
\r
53 } catch (CharacterCodingException e1) {
\r
54 throw new IOException("String conversion error.", e1);
\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
72 // IProduct product = Platform.getProduct();
\r
73 // if (null != product)
\r
74 // name = "Team " + product.getName();
\r
76 // name = "Default Team Folder";
\r
77 // return new File(folder, name);
\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
85 store.setValue(Preferences.CURRENT_TEAM_FOLDER, teamFolder.getAbsolutePath());
\r