--- /dev/null
+package org.simantics.team;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.nio.ByteBuffer;\r
+import java.nio.CharBuffer;\r
+import java.nio.charset.CharacterCodingException;\r
+import java.nio.charset.Charset;\r
+import java.nio.charset.CodingErrorAction;\r
+\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.preference.IPreferenceStore;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.team.ui.Preferences;\r
+\r
+public class Utils {\r
+ public static final File getNewFolder(int index, String prefix, String suffix) {\r
+ File toFolder;\r
+ do {\r
+ toFolder = new File(prefix + index + suffix);\r
+ } while (toFolder.exists());\r
+ return toFolder;\r
+ }\r
+ private static final Charset ASCII = Charset.forName("US-ASCII");\r
+ protected static final Charset UTF8 = Charset.forName("UTF-8");\r
+ public static final String bytesToStringASCII(byte[] bytes, int offset, int length)\r
+ throws IOException {\r
+ return bytesToString(bytes, offset, length, ASCII);\r
+ }\r
+ public static final String bytesToStringUTF8(byte[] bytes, int offset, int length)\r
+ throws IOException {\r
+ return bytesToString(bytes, offset, length, UTF8);\r
+ }\r
+ public static final String bytesToString(byte[] bytes, int offset, int length, Charset charset)\r
+ throws IOException {\r
+ if (bytes == null || offset < 0 || offset > length - 1 || length < 0 || length > bytes.length)\r
+ return null;\r
+ ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length);\r
+ CharBuffer cbuf;\r
+ String s = null;\r
+ try {\r
+ cbuf = charset.newDecoder().decode(bbuf);\r
+ s = cbuf.toString();\r
+ } catch (CharacterCodingException e) {\r
+ bbuf.rewind();\r
+ try {\r
+ cbuf = charset.newDecoder()\r
+ .onMalformedInput(CodingErrorAction.REPLACE)\r
+ .onUnmappableCharacter(CodingErrorAction.REPLACE)\r
+ .decode(bbuf);\r
+ s = cbuf.toString();\r
+ } catch (CharacterCodingException e1) {\r
+ throw new IOException("String conversion error.", e1);\r
+ }\r
+ }\r
+ return s;\r
+ }\r
+ public static final File getTeamFolder() \r
+ throws DatabaseException {\r
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+ String currentTeamFolder = store.getString(Preferences.CURRENT_TEAM_FOLDER);\r
+ if (!currentTeamFolder.equals(""))\r
+ return new File(currentTeamFolder);\r
+ String name = System.getProperty("staging.team.folder");\r
+ if (null != name && !name.equals(""))\r
+ return new File(name);\r
+ String folder = System.getProperty("java.io.tmpdir");\r
+ if (null == folder || folder.equals(""))\r
+ throw new DatabaseException("Could not get team folder path.");\r
+ return null;\r
+// IProduct product = Platform.getProduct();\r
+// if (null != product)\r
+// name = "Team " + product.getName();\r
+// else\r
+// name = "Default Team Folder";\r
+// return new File(folder, name);\r
+ }\r
+ public static final void setTeamFolder(File teamFolder) \r
+ throws DatabaseException {\r
+ IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+ if (null == teamFolder)\r
+ store.setValue(Preferences.CURRENT_TEAM_FOLDER, "");\r
+ else\r
+ store.setValue(Preferences.CURRENT_TEAM_FOLDER, teamFolder.getAbsolutePath());\r
+ }\r
+}\r