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