View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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 org.argeo.cms.ui.workbench.internal.jcr.commands;
17  
18  import java.util.Iterator;
19  
20  import org.argeo.cms.ui.jcr.JcrBrowserUtils;
21  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
22  import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
23  import org.argeo.eclipse.ui.TreeParent;
24  import org.eclipse.core.commands.AbstractHandler;
25  import org.eclipse.core.commands.ExecutionEvent;
26  import org.eclipse.core.commands.ExecutionException;
27  import org.eclipse.jface.viewers.ISelection;
28  import org.eclipse.jface.viewers.IStructuredSelection;
29  
30  /**
31   * Force the selected objects of the active view to be refreshed doing the
32   * following:
33   * <ol>
34   * <li>The model objects are recomputed</li>
35   * <li>the view is refreshed</li>
36   * </ol>
37   */
38  public class Refresh extends AbstractHandler {
39  
40  	public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".refresh";
41  
42  	public Object execute(ExecutionEvent event) throws ExecutionException {
43  
44  		JcrBrowserView view = (JcrBrowserView) WorkbenchUiPlugin.getDefault()
45  				.getWorkbench().getActiveWorkbenchWindow().getActivePage()
46  				.getActivePart();//
47  
48  		ISelection selection = WorkbenchUiPlugin.getDefault().getWorkbench()
49  				.getActiveWorkbenchWindow().getActivePage().getSelection();
50  
51  		if (selection != null && selection instanceof IStructuredSelection
52  				&& !selection.isEmpty()) {
53  			Iterator<?> it = ((IStructuredSelection) selection).iterator();
54  			while (it.hasNext()) {
55  				Object obj = it.next();
56  				if (obj instanceof TreeParent) {
57  					TreeParent tp = (TreeParent) obj;
58  					JcrBrowserUtils.forceRefreshIfNeeded(tp);
59  					view.refresh(obj);
60  				}
61  			}
62  		} else if (view instanceof JcrBrowserView)
63  			((JcrBrowserView) view).refresh(null); // force full refresh
64  
65  		return null;
66  	}
67  }