-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetFilesUI.m
executable file
·343 lines (285 loc) · 12.6 KB
/
GetFilesUI.m
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function varargout = GetFilesUI(varargin)
%GETFILESUI M-file for GetFilesUI.fig
% GETFILESUI, by itself, creates a new GETFILESUI or raises the existing
% singleton*.
%
% H = GETFILESUI returns the handle to a new GETFILESUI or the handle to
% the existing singleton*.
%
% GETFILESUI('Property','Value',...) creates a new GETFILESUI using the
% given property value pairs. Unrecognized properties are passed via
% varargin to GetFilesUI_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% GETFILESUI('CALLBACK') and GETFILESUI('CALLBACK',hObject,...) call the
% local function named CALLBACK in GETFILESUI.M with the given input
% arguments.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
%
% Author: Jenny Owen
% Copyright 2015 The MathWorks Inc.
% Edit the above text to modify the response to help GetFilesUI
% Last Modified by GUIDE v2.5 24-Apr-2015 16:55:11
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GetFilesUI_OpeningFcn, ...
'gui_OutputFcn', @GetFilesUI_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GetFilesUI is made visible.
function GetFilesUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for GetFilesUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% set current folder as last folder viewed by ui
setappdata(handles.mainFig, 'LastFolderViewed', pwd);
% specify what data formats are supported
setappdata(handles.mainFig, 'SupportedFormats', {'*.txt'; '*.pdf'; '*.doc'; '*.docx'});
% enable multiple select on the list box
handles.file_listbox.Max = 2;
% disable semantic surface until there are 10 files to parse
handles.generate_surface_btn.Enable = 'off';
% check if the statistics toolbox is installed.
hasStatsTbx = WordCloud.checkForStatisticsToolbox();
setappdata(handles.mainFig, 'hasStatsToolbox', hasStatsTbx);
% set tooltip string if statistics toolbox is unlicensed.
if ~hasStatsTbx
handles.generate_surface_btn.TooltipString = ...
'Semantic surface functionality is only available with the statistics toolbox.';
end
% UIWAIT makes GetFilesUI wait for user response (see UIRESUME)
% uiwait(handles.mainFig);
% --- Outputs from this function are returned to the command line.
function varargout = GetFilesUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in file_listbox.
function file_listbox_Callback(hObject, eventdata, handles)
% hObject handle to file_listbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns file_listbox contents as cell array
% contents{get(hObject,'Value')} returns selected item from file_listbox
% --- Executes during object creation, after setting all properties.
function file_listbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to file_listbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
handles.file_listbox.Value = [];
% --- Executes during object creation, after setting all properties.
function projectname_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to projectname_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function projectname_edit_Callback(hObject, eventdata, handles)
% hObject handle to add_manualpath_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in add_file_btn.
function add_file_btn_Callback(hObject, eventdata, handles)
% hObject handle to add_file_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% open UI to select a file
startfolder = getappdata(handles.mainFig, 'LastFolderViewed');
formats = getappdata(handles.mainFig, 'SupportedFormats');
[f,p] = uigetfile(formats, 'Select File to add to Word Cloud', startfolder, 'MultiSelect', 'on');
if ~isequal(f,0) % if the user actually selected a file instead of cancelling
% if they selected more than 1 file...
if iscell(f)
filelist = fullfile(p,f)';
else
filelist = fullfile(p,f);
end
% add file to file list
appendToFileList(handles, filelist);
% tell next thing to open in the selected folder.
setappdata(handles.mainFig, 'LastFolderViewed', p);
end
% --- Executes on button press in add_folder_btn.
function add_folder_btn_Callback(hObject, eventdata, handles)
% hObject handle to add_folder_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
startfolder = getappdata(handles.mainFig, 'LastFolderViewed');
% open UI to select a folder
folder = uigetdir(startfolder, 'Select Folder to add to Word Cloud');
if ~isequal(folder,0) % if the user actually selected a folder instead of cancelling
addFolderToFileList(handles, folder);
% tell next thing to open in the selected folder.
setappdata(handles.mainFig, 'LastFolderViewed', folder);
end
% --- Executes on button press in add_plaintext_btn.
function add_plaintext_btn_Callback(hObject, eventdata, handles)
% hObject handle to add_plaintext_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% create input dialogue to get plain text info
inputBoxSizes = [1, 53; 10, 50];
dialogueOutput = {'';''};
% loop box checks until some valid input is found.
% valid inputs are :
% - cancel input dlg
% - nothing (in which case cancel)
% - both fields are filled.
while isempty(dialogueOutput{1}) || isempty(dialogueOutput{2})
dialogueOutput = inputdlg({'Title', 'Text'}, 'Enter Plain Text', inputBoxSizes, dialogueOutput);
% check for valid inputs
if isempty(dialogueOutput) % user cancelled
return;
elseif isempty(dialogueOutput{1}) && isempty(dialogueOutput{2}) % no data entered.
return;
elseif isempty(dialogueOutput{1}) % no title
uiwait(msgbox('No title entered!', 'No title warning'));
elseif isempty(dialogueOutput{2}) % no text
uiwait(msgbox('No text entered!', 'No text warning'));
end
end
% getting here means that the user entered a title and some text.
% save this as a txt file
savefolder = getappdata(handles.mainFig, 'LastFolderViewed');
filename = fullfile(savefolder, [dialogueOutput{1}, '.txt']);
fid = fopen(filename, 'w');
% write text to file row by row.
for row = 1:size(dialogueOutput{2}, 1)
fprintf(fid, dialogueOutput{2}(row,:) );
fprintf(fid, '\n');
end
fclose(fid);
% add new file to file list
appendToFileList(handles, filename);
% --- Executes on button press in remove_btn.
function remove_btn_Callback(hObject, eventdata, handles)
% hObject handle to remove_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
flist = getappdata(handles.file_listbox, 'FileList');
selected = handles.file_listbox.Value;
% default "selected" value is 1, even if there are no files so it errors
% if you click the remove button without files in the list box.
% so only perform the delete if the "selected" variables are all valid.
if all(selected <= numel(flist))
% delete selected files
flist(selected) = [];
% clear all values, otherwise this can cause a value to be larger than
% number of strings in the listbox, causing an error.
handles.file_listbox.Value = [];
% update file list internally and in the listbox display
setappdata(handles.file_listbox, 'FileList', flist);
setListBoxString(handles, flist);
end
% --- Executes on button press in generate_cloud_btn.
function generate_cloud_btn_Callback(hObject, eventdata, handles)
% hObject handle to generate_cloud_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get the file list
flist = getappdata(handles.file_listbox, 'FileList');
% get the project name
name = handles.projectname_edit.String;
if isempty(flist)
% if there are no files in the project, do nothing!
uiwait(warndlg('No files to parse!'));
elseif isempty(name)
uiwait(warndlg('Please enter a project name.'));
else
docparser = ParseFiles(flist, name);
docparser.parse();
WordCloudEditor('parser', docparser);
end
% --- Executes on button press in generate_surface_btn.
function generate_surface_btn_Callback(hObject, eventdata, handles)
% hObject handle to generate_surface_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get the file list
flist = getappdata(handles.file_listbox, 'FileList');
% get the project name
name = handles.projectname_edit.String;
if isempty(flist)
% if there are no files in the project, do nothing!
uiwait(warndlg('No files to parse!'));
elseif isempty(name)
uiwait(warndlg('Please enter a project name.'));
else
docparser = ParseFiles(flist, name);
docparser.parse();
generateSemanticSurface(docparser, 50);
end
function addFolderToFileList(handles, folder)
formats = getappdata(handles.mainFig, 'SupportedFormats');
% get list of all files with the supported file extensions
files = {};
for ext = formats'
% use DIR to get list of files with supported file extension
files = [dir( fullfile(folder, ext{1})); files]; %#ok<AGROW>
end
files = {files.name}';
% add these files to the list of files we saved
appendToFileList(handles, fullfile(folder, files) );
function appendToFileList(handles, newfiles)
flist = getappdata(handles.file_listbox, 'FileList');
if isempty(flist)
if iscell(newfiles)
flist = newfiles;
else
flist = {newfiles};
end
else
flist = [flist; newfiles];
end
setappdata(handles.file_listbox, 'FileList', flist);
setListBoxString(handles, flist);
function setListBoxString(handles, fileList)
% update list box with new file list, excluding full path name
[~,names,exts] = cellfun(@fileparts, fileList, 'UniformOutput', false);
handles.file_listbox.String = strcat(names, exts);
% update list box with new file list INCLUDING full path name
% handles.file_listbox.String = fileList;
setGenerateSurfaceButtonEnableState(handles, numel(names));
function setGenerateSurfaceButtonEnableState(handles, numFiles)
% if there are enough files to parse and the statistics toolbox is installed
hasStatsTbx = getappdata(handles.mainFig, 'hasStatsToolbox');
if (numFiles > 4) && hasStatsTbx
% then enable the generate surface button
handles.generate_surface_btn.Enable = 'on';
else
% then disable the generate surface button
handles.generate_surface_btn.Enable = 'off';
end