11package com .ss .editor .ui .component .asset ;
22
33import com .ss .editor .config .EditorConfig ;
4+ import com .ss .editor .manager .ExecutorManager ;
5+ import com .ss .editor .manager .WorkspaceManager ;
6+ import com .ss .editor .model .workspace .Workspace ;
47import com .ss .editor .ui .component .ScreenComponent ;
58import com .ss .editor .ui .component .asset .tree .ResourceTree ;
9+ import com .ss .editor .ui .component .asset .tree .resource .FolderElement ;
610import com .ss .editor .ui .component .asset .tree .resource .ResourceElement ;
711import com .ss .editor .ui .component .asset .tree .resource .ResourceElementFactory ;
812import com .ss .editor .ui .css .CSSIds ;
@@ -36,6 +40,7 @@ public class AssetComponent extends VBox implements ScreenComponent {
3640
3741 private static final Insets TREE_OFFSET = new Insets (6 , 3 , 0 , 0 );
3842
43+ private static final ExecutorManager EXECUTOR_MANAGER = ExecutorManager .getInstance ();
3944 private static final FXEventManager FX_EVENT_MANAGER = FXEventManager .getInstance ();
4045
4146 /**
@@ -53,6 +58,11 @@ public class AssetComponent extends VBox implements ScreenComponent {
5358 */
5459 private ResourceTree resourceTree ;
5560
61+ /**
62+ * The flag for ignoring expand changes.
63+ */
64+ private boolean ignoreExpanded ;
65+
5666 public AssetComponent () {
5767 this .waitedFilesToSelect = ArrayFactory .newArray (Path .class );
5868 setId (CSSIds .ASSET_COMPONENT );
@@ -102,6 +112,11 @@ private void processEvent(final MovedFileEvent event) {
102112
103113 final ResourceTree resourceTree = getResourceTree ();
104114 resourceTree .notifyMoved (prevFile , newFile );
115+
116+ final WorkspaceManager workspaceManager = WorkspaceManager .getInstance ();
117+ final Workspace workspace = workspaceManager .getCurrentWorkspace ();
118+ if (workspace == null ) return ;
119+ workspace .updateEditorState (prevFile , newFile );
105120 }
106121
107122 /**
@@ -114,6 +129,11 @@ private void processEvent(final RenamedFileEvent event) {
114129
115130 final ResourceTree resourceTree = getResourceTree ();
116131 resourceTree .notifyRenamed (prevFile , newFile );
132+
133+ final WorkspaceManager workspaceManager = WorkspaceManager .getInstance ();
134+ final Workspace workspace = workspaceManager .getCurrentWorkspace ();
135+ if (workspace == null ) return ;
136+ workspace .updateEditorState (prevFile , newFile );
117137 }
118138
119139 /**
@@ -142,6 +162,11 @@ private void processEvent(final DeletedFileEvent event) {
142162
143163 final ResourceTree resourceTree = getResourceTree ();
144164 resourceTree .notifyDeleted (file );
165+
166+ final WorkspaceManager workspaceManager = WorkspaceManager .getInstance ();
167+ final Workspace workspace = workspaceManager .getCurrentWorkspace ();
168+ if (workspace == null ) return ;
169+ workspace .removeEditorState (file );
145170 }
146171
147172 /**
@@ -169,9 +194,12 @@ private void processRefresh() {
169194 * Create components.
170195 */
171196 private void createComponents () {
197+ setIgnoreExpanded (true );
172198
173199 this .barComponent = new AssetBarComponent ();
174200 this .resourceTree = new ResourceTree (false );
201+ this .resourceTree .setExpandHandler (this ::updateExpanded );
202+ this .resourceTree .setOnLoadHandler (this ::handleTreeLoading );
175203
176204 //FIXME пока он не нужен
177205 //FXUtils.addToPane(barComponent, this);
@@ -182,6 +210,62 @@ private void createComponents() {
182210 VBox .setMargin (resourceTree , TREE_OFFSET );
183211 }
184212
213+ /**
214+ * Handle changing loading state of the tree.
215+ */
216+ private void handleTreeLoading (final Boolean finished ) {
217+
218+ final WorkspaceManager workspaceManager = WorkspaceManager .getInstance ();
219+ final Workspace workspace = workspaceManager .getCurrentWorkspace ();
220+
221+ if (finished && workspace != null ) {
222+ final Array <Path > expandedFolders = workspace .getExpandedFolders ();
223+ expandedFolders .forEach (path -> resourceTree .expandTo (path , false ));
224+ }
225+
226+ if (finished ) {
227+ EXECUTOR_MANAGER .addFXTask (() -> setIgnoreExpanded (false ));
228+ } else {
229+ setIgnoreExpanded (true );
230+ }
231+ }
232+
233+ /**
234+ * @return true if the expand listener is ignored.
235+ */
236+ public boolean isIgnoreExpanded () {
237+ return ignoreExpanded ;
238+ }
239+
240+ /**
241+ * @param ignoreExpanded the flag for ignoring expand changes.
242+ */
243+ public void setIgnoreExpanded (final boolean ignoreExpanded ) {
244+ this .ignoreExpanded = ignoreExpanded ;
245+ }
246+
247+ /**
248+ * Handle changes count of expanded folders.
249+ */
250+ private void updateExpanded (final int count , final ResourceTree tree ) {
251+ if (isIgnoreExpanded ()) return ;
252+
253+ final WorkspaceManager workspaceManager = WorkspaceManager .getInstance ();
254+ final Workspace workspace = workspaceManager .getCurrentWorkspace ();
255+ if (workspace == null ) return ;
256+
257+ final Array <Path > expanded = ArrayFactory .newArray (Path .class );
258+ final Array <TreeItem <ResourceElement >> allItems = UIUtils .getAllItems (tree );
259+ allItems .stream ().filter (TreeItem ::isExpanded )
260+ .filter (treeItem -> !treeItem .isLeaf ())
261+ .map (TreeItem ::getValue )
262+ .filter (element -> element instanceof FolderElement )
263+ .map (ResourceElement ::getFile )
264+ .forEach (expanded ::add );
265+
266+ workspace .updateExpandedFolders (expanded );
267+ }
268+
185269 /**
186270 * @return the toolbar of this component.
187271 */
0 commit comments