package greenfoot.platforms.ide;
import greenfoot.GreenfootImage;
import greenfoot.UserInfo;
import greenfoot.UserInfoVisitor;
import greenfoot.core.GreenfootMain;
import greenfoot.platforms.GreenfootUtilDelegate;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import bluej.Config;
import bluej.runtime.ExecServer;
import bluej.utility.Debug;
import threadchecker.OnThread;
import threadchecker.Tag;
| GreenfootUtilDelegate implementation for the Greenfoot IDE.
|
@OnThread(Tag.Simulation)
public class GreenfootUtilDelegateIDE
implements GreenfootUtilDelegate{
@OnThread(Tag.Any)
private static GreenfootUtilDelegateIDE instance;
static {
instance = new GreenfootUtilDelegateIDE();
}
| Get the GreenfootUtilDelegateIDE instance.
|
@OnThread(Tag.Any)
public static GreenfootUtilDelegateIDE getInstance()
{
return instance;
}
private GreenfootUtilDelegateIDE()
{
}
@Override
@OnThread(Tag.Any)
public URL getResource(String path)
{
return ExecServer.getCurrentClassLoader().getResource(path);
}
@Override
@OnThread(Tag.Any)
public Iterable getSoundFiles()
{
ArrayList<String> files = new ArrayList<>();
try
{
URL url = getResource("sounds");
if (url != null && "file".equals(url.getProtocol()))
{
for (String file : new File(url.toURI()).list())
{
files.add(file);
}
}
}
catch (URISyntaxException e)
{
Debug.reportError("Bad URI in getResources", e);
}
return files;
}
| Returns the path to a small version of the greenfoot logo.
|
@Override
@OnThread(Tag.Any)
public String getGreenfootLogoPath()
{
File libDir = Config.getGreenfootLibDir();
return libDir.getAbsolutePath() + "/imagelib/other/greenfoot.png";
}
@Override
public boolean isStorageSupported()
{
return getUserName() != null && !getUserName().isEmpty();
}
public String getUserName()
{
return GreenfootMain.getPropString("greenfoot.player.name", "Player1");
}
@Override
public UserInfo getCurrentUserInfo()
{
if (getUserName() == null || getUserName().isEmpty())
return null;
ArrayList<UserInfo> all = getAllDataSorted(true);
if (all == null)
return null;
for (int i = 0; i < all.size(); i++)
{
if (getUserName().equals(all.get(i).getUserName()))
{
return all.get(i);
}
}
return UserInfoVisitor.allocate(getUserName(), -1, getUserName());
}
private UserInfo makeStorage(String[] line, int rank, boolean useSingleton)
{
UserInfo r = null;
try
{
int column = 0;
r = UserInfoVisitor.allocate(line[column++], rank, useSingleton ? getUserName() : null);
r.setScore(Integer.parseInt(line[column++]));
for (int i = 0; i < UserInfo.NUM_INTS; i++)
{
r.setInt(i, Integer.parseInt(line[column++]));
}
for (int i = 0; i < UserInfo.NUM_STRINGS; i++)
{
r.setString(i, line[column++]);
}
}
catch (IndexOutOfBoundsException e)
{
}
return r;
}
private String[] makeLine(String userName, UserInfo data)
{
String[] line = new String[1 + 1 + UserInfo.NUM_INTS + UserInfo.NUM_STRINGS];
int column = 0;
line[column++] = userName;
line[column++] = Integer.toString(data.getScore());
try
{
for (int i = 0; i < UserInfo.NUM_INTS; i++)
{
line[column++] = Integer.toString(data.getInt(i));
}
for (int i = 0; i < UserInfo.NUM_STRINGS; i++)
{
line[column++] = data.getString(i);
}
}
catch (IndexOutOfBoundsException e)
{
}
return line;
}
@Override
public boolean storeCurrentUserInfo(UserInfo data)
{
if (getUserName() == null || getUserName().isEmpty())
return false;
List<String[]> all;
try
{
CSVReader csv = new CSVReader(new InputStreamReader(new FileInputStream("storage.csv"), "UTF-8"));
all = csv.readAll();
csv.close();
}
catch (FileNotFoundException e)
{
all = new ArrayList<>();
}
catch (IOException e)
{
Debug.message("Error reading user data: " + e.getMessage());
return false;
}
Iterator<String[]> lineIt = all.iterator();
while (lineIt.hasNext())
{
String[] line = lineIt.next();
if (line.length > 1 && getUserName().equals(line[0]))
{
lineIt.remove();
break;
}
}
if (data != null)
{
all.add(makeLine(getUserName(), data));
}
try
{
CSVWriter csvOut = new CSVWriter(new OutputStreamWriter(new FileOutputStream("storage.csv"), "UTF-8"));
csvOut.writeAll(all);
csvOut.close();
return true;
}
catch (IOException e)
{
Debug.message("Error storing user data: " + e.getMessage());
return false;
}
}
private ArrayList getAllDataSorted(boolean useSingleton)
{
try
{
ArrayList<UserInfo> ret = new ArrayList<>();
CSVReader csv = new CSVReader(new InputStreamReader(new FileInputStream("storage.csv"), "UTF-8"));
List<String[]> all = csv.readAll();
Collections.sort(all, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2)
{
return -(Integer.parseInt(o1[1]) - Integer.parseInt(o2[1]));
}
});
int rank = 1;
for (String[] line : all)
{
ret.add(makeStorage(line, rank, useSingleton));
rank++;
}
csv.close();
return ret;
}
catch (FileNotFoundException e)
{
return new ArrayList<>();
}
catch (IOException e)
{
Debug.message("Error reading user data: " + e.getMessage());
return null;
}
}
@Override
public List getTopUserInfo(int limit)
{
ArrayList<UserInfo> ret = getAllDataSorted(false);
if (ret == null)
return null;
else if (ret.size() <= limit || limit <= 0)
return ret;
else{ return ret.subList(0, limit);
}
}
@Override
public GreenfootImage getUserImage(String userName)
{
return null;
}
@Override
public List getNearbyUserInfo(int maxAmount)
{
if (getUserName() == null || getUserName().isEmpty())
return null;
ArrayList<UserInfo> all = getAllDataSorted(false);
if (all == null)
return null;
int index = -1;
for (int i = 0; i < all.size();i++)
{
if (getUserName() != null && getUserName().equals(all.get(i).getUserName()))
{
index = i;
break;
}
}
if (index == -1 || maxAmount == 0)
return new ArrayList<>();
int availableBefore = index;
int availableAfter = all.size() - 1 - index;
int desiredBefore = maxAmount / 2;
int desiredAfter = Math.max(0, maxAmount - 1) / 2;
if (availableAfter + availableBefore + 1 <= maxAmount)
{
return all;
}
else if (availableBefore <= desiredBefore)
{
return all.subList(index - availableBefore, index - availableBefore + maxAmount + 1);
}
else if (availableAfter <= desiredAfter)
{
return all.subList(index + availableAfter - maxAmount, index + availableAfter + 1);
}
else
{
return all.subList(index - desiredBefore, index + desiredAfter + 1);
}
}
}
top,
use,
map,
class GreenfootUtilDelegateIDE
. getInstance
. GreenfootUtilDelegateIDE
. getResource
. getSoundFiles
. getGreenfootLogoPath
. isStorageSupported
. getUserName
. getCurrentUserInfo
. makeStorage
. makeLine
. storeCurrentUserInfo
. getAllDataSorted
. compare
. getTopUserInfo
. getUserImage
. getNearbyUserInfo
477 neLoCode
+ 3 LoComm