summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/userspace/context/UserContext.java
blob: 064e6659a3c1684e93c17a739f91623c362b48c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Lo�c Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.  This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.  You should have received a copy of the 
 * GNU General Public License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA 02110-1301, USA.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Z�rich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Lo�c Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were entirely written by Marko Zivkovic
 */

package xlogo.kernel.userspace.context;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import xlogo.Logo;
import xlogo.kernel.userspace.files.LogoFile;
import xlogo.messages.MessageKeys;
import xlogo.messages.async.dialog.DialogMessenger;
import xlogo.storage.WSManager;
import xlogo.storage.global.GlobalConfig;
import xlogo.storage.user.UserConfig;

public class UserContext extends LogoContext
{

	/**
	 * Load and parse all the files in current user's source directory. <br>
	 * This happens only once, in the constructor. <br>
	 * Refresh is currently not planned, but it would be easy to add.
	 * 
	 * @param userDir
	 */
	public UserContext()
	{
		loadUserFiles();
	}
	
	private void loadUserFiles()
	{
		UserConfig userConfig = WSManager.getUserConfig();
		
		if (userConfig.isVirtual())
			return;
		
		File sourceDir = userConfig.getSourceDirectory();
		if (!sourceDir.exists())
			sourceDir.mkdirs();
		
		if (!sourceDir.isDirectory())
		{
			DialogMessenger.getInstance().dispatchMessage(Logo.messages.getString("ws.error.title"),
					Logo.messages.getString("ws.error.userdir.not.dir"));
			return;
		}
		
		StringBuilder ioErrors = new StringBuilder();
		
		for (String fileName : getLogoFileNamesFromDirectory(sourceDir))
		{
			String name = fileName.substring(0, fileName.length() - GlobalConfig.LOGO_FILE_EXTENSION.length());
			userConfig.addFile(name);
			try
			{
				LogoFile file = LogoFile.loadFile(name);
				getFilesTable().put(file.getPlainName(), file);
				
			}
			catch (IOException e)
			{
				ioErrors.append(e.toString());
				ioErrors.append("\n\n");
			}
		}
		
		// must remove files from fileOrder that could not be found anymore.
		for (String fileName : new ArrayList<String>(userConfig.getFileOrder()))
		{
			if (!getFilesTable().containsKey(fileName))
				userConfig.getFileOrder().remove(fileName);
		}
		
		if (ioErrors.length() > 0)
		{
			DialogMessenger.getInstance().dispatchMessage(Logo.messages.getString("ws.error.title"),
					Logo.messages.getString("ws.error.could.not.load.logo.files") + "\n" + ioErrors.toString());
		}
	}
	
	/**
	 * Caller must make sure that newName does not already exist.
	 */
	@Override
	public void renameFile(String oldName, String newName)
	{
		super.renameFile(oldName, newName);
		WSManager.getUserConfig().renameFile(oldName, newName);
	}
	
	@Override
	public void createFile(String fileName, String text) throws IOException
	{
		/*
		 * Eager creation of files in file order list in user config.
		 */		
		
		if (!WSManager.getUserConfig().isVirtual())
			super.createFile(fileName, text);
		else
		{
			LogoFile file = LogoFile.createNewVirtualFile(fileName);
			file.setText(text);
			getFilesTable().put(fileName, file);
			installListeners(file);
		}
		WSManager.getUserConfig().addFile(fileName);
	}
	
	@Override
	public void importFile(File path, String newFileName)
	{
		try
		{
			super.importFile(path, newFileName);
		}
		catch (IOException e)
		{
			DialogMessenger.getInstance().dispatchError(MessageKeys.GENERAL_ERROR_TITLE, "Could not import file : \n" + e.toString());
		}
		WSManager.getUserConfig().addFile(newFileName);
	}
	
	private String[] getLogoFileNamesFromDirectory(File dir)
	{
		return dir.list(new FilenameFilter(){
			public boolean accept(File file, String name)
			{
				return name.endsWith(".lgo");
			}
		});
	}
	
	@Override
	public String[] getFileOrder()
	{	
		/*
		 * Lazy deletion from file order list in user config.
		 */		
		ArrayList<String> list = new ArrayList<String>(WSManager.getUserConfig().getFileOrder());
		Map<String,LogoFile> filesTable = getFilesTable();
		
		if (filesTable.size() != list.size())
		{
			Iterator<String> iter = list.iterator();
			String current;
			
			while(iter.hasNext())
			{
				current = iter.next();
				if(!filesTable.containsKey(current))
					iter.remove();
			}
		}
				
		return list.toArray(new String[list.size()]); 
	}
	
	

}