Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.motorolamobility.studio.android.db.core.ui;
     17 
     18 import java.util.List;
     19 
     20 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
     21 import org.eclipse.core.runtime.jobs.Job;
     22 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
     23 import org.eclipse.jface.viewers.ILazyTreeContentProvider;
     24 import org.eclipse.jface.viewers.TreeViewer;
     25 import org.eclipse.swt.widgets.Display;
     26 
     27 import com.motorola.studio.android.common.utilities.PluginUtils;
     28 import com.motorolamobility.studio.android.db.core.DbCoreActivator;
     29 import com.motorolamobility.studio.android.db.core.ui.view.MOTODEVDatabaseExplorerView;
     30 
     31 /**
     32  * This class is responsible to set the loading attribute on a tree node when a loadingJob is scheduled and set it to false
     33  *  before updating the treeView after the loadingJob finished execution.
     34  * The node child count is update on job done. TreeView contentProvider will take care of the rest.
     35  */
     36 public final class LoadingJobListener extends JobChangeAdapter
     37 {
     38     /* (non-Javadoc)
     39      * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#scheduled(org.eclipse.core.runtime.jobs.IJobChangeEvent)
     40      */
     41     @Override
     42     public void scheduled(IJobChangeEvent event)
     43     {
     44         Job job = event.getJob();
     45         if (job instanceof AbstractLoadingNodeJob)
     46         {
     47             AbstractLoadingNodeJob loadingNodeJob = (AbstractLoadingNodeJob) job;
     48             ITreeNode node = loadingNodeJob.getNode();
     49 
     50             //Since load job has been scheduled, the loading flag must be set to true (This will help the content provider to show the loading node).
     51             node.setLoading(true);
     52         }
     53         super.scheduled(event);
     54     }
     55 
     56     /* (non-Javadoc)
     57      * @see org.eclipse.core.runtime.jobs.JobChangeAdapter#done(org.eclipse.core.runtime.jobs.IJobChangeEvent)
     58      */
     59     @Override
     60     public void done(IJobChangeEvent event)
     61     {
     62         MOTODEVDatabaseExplorerView view = DbCoreActivator.getMOTODEVDatabaseExplorerView();
     63         if (view != null)
     64         {
     65             final TreeViewer treeViewer = view.getTreeViewer();
     66             Job job = event.getJob();
     67             if (job instanceof AbstractLoadingNodeJob)
     68             {
     69                 AbstractLoadingNodeJob loadingNodeJob = (AbstractLoadingNodeJob) job;
     70                 ITreeNode node = loadingNodeJob.getNode();
     71 
     72                 //Job is done, so we can set the loading flag to false.
     73                 node.setLoading(false);
     74                 final ITreeNode[] treeNodeContainer =
     75                 {
     76                     node
     77                 };
     78 
     79                 //TreeViewer operations must be executed on the UI Thread.
     80                 Display.getDefault().syncExec(new Runnable()
     81                 {
     82                     public void run()
     83                     {
     84                         ITreeNode node = treeNodeContainer[0];
     85                         List<ITreeNode> children = node.getChildren();
     86                         if (PluginUtils.getOS() != PluginUtils.OS_LINUX)
     87                         {
     88                             treeViewer.setChildCount(node, 0);
     89                         }
     90                         int size = children.size();
     91                         if (size > 0)
     92                         {
     93                             ((ILazyTreeContentProvider) treeViewer.getContentProvider())
     94                                     .updateElement(node, 0); //Force removal of loading node.
     95                         }
     96                         //updating the child count is sufficient to allow the tree to call the content provider and retrieve the new nodes.
     97                         treeViewer.setChildCount(node, size);
     98                         //updating the node so if needed the label/icon will be updated
     99                         treeViewer.update(node, null);
    100                     }
    101                 });
    102             }
    103             super.done(event);
    104         }
    105     }
    106 }