X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.team.ui%2Fsrc%2Forg%2Fsimantics%2Fteam%2FUtils.java;fp=bundles%2Forg.simantics.team.ui%2Fsrc%2Forg%2Fsimantics%2Fteam%2FUtils.java;h=59c9329548d9bf46be82a12a6091ca8a57f58ce5;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.team.ui/src/org/simantics/team/Utils.java b/bundles/org.simantics.team.ui/src/org/simantics/team/Utils.java new file mode 100644 index 000000000..59c932954 --- /dev/null +++ b/bundles/org.simantics.team.ui/src/org/simantics/team/Utils.java @@ -0,0 +1,87 @@ +package org.simantics.team; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CodingErrorAction; + +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.simantics.db.exception.DatabaseException; +import org.simantics.team.ui.Preferences; + +public class Utils { + public static final File getNewFolder(int index, String prefix, String suffix) { + File toFolder; + do { + toFolder = new File(prefix + index + suffix); + } while (toFolder.exists()); + return toFolder; + } + private static final Charset ASCII = Charset.forName("US-ASCII"); + protected static final Charset UTF8 = Charset.forName("UTF-8"); + public static final String bytesToStringASCII(byte[] bytes, int offset, int length) + throws IOException { + return bytesToString(bytes, offset, length, ASCII); + } + public static final String bytesToStringUTF8(byte[] bytes, int offset, int length) + throws IOException { + return bytesToString(bytes, offset, length, UTF8); + } + public static final String bytesToString(byte[] bytes, int offset, int length, Charset charset) + throws IOException { + if (bytes == null || offset < 0 || offset > length - 1 || length < 0 || length > bytes.length) + return null; + ByteBuffer bbuf = ByteBuffer.wrap(bytes, offset, length); + CharBuffer cbuf; + String s = null; + try { + cbuf = charset.newDecoder().decode(bbuf); + s = cbuf.toString(); + } catch (CharacterCodingException e) { + bbuf.rewind(); + try { + cbuf = charset.newDecoder() + .onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE) + .decode(bbuf); + s = cbuf.toString(); + } catch (CharacterCodingException e1) { + throw new IOException("String conversion error.", e1); + } + } + return s; + } + public static final File getTeamFolder() + throws DatabaseException { + IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + String currentTeamFolder = store.getString(Preferences.CURRENT_TEAM_FOLDER); + if (!currentTeamFolder.equals("")) + return new File(currentTeamFolder); + String name = System.getProperty("staging.team.folder"); + if (null != name && !name.equals("")) + return new File(name); + String folder = System.getProperty("java.io.tmpdir"); + if (null == folder || folder.equals("")) + throw new DatabaseException("Could not get team folder path."); + return null; +// IProduct product = Platform.getProduct(); +// if (null != product) +// name = "Team " + product.getName(); +// else +// name = "Default Team Folder"; +// return new File(folder, name); + } + public static final void setTeamFolder(File teamFolder) + throws DatabaseException { + IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + if (null == teamFolder) + store.setValue(Preferences.CURRENT_TEAM_FOLDER, ""); + else + store.setValue(Preferences.CURRENT_TEAM_FOLDER, teamFolder.getAbsolutePath()); + } +}