]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/contributions/search/BrowserView.java
aa96ed13bc6daabd535c908dfa94f1dab07ce379
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / contributions / search / BrowserView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2014 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy
12  *******************************************************************************/
13 package org.simantics.workbench.internal.contributions.search;
14
15 import java.net.URL;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.browser.Browser;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.part.ViewPart;
22 import org.simantics.Simantics;
23 import org.simantics.db.Session;
24 import org.simantics.db.layer0.request.ActiveModels;
25 import org.simantics.db.management.ISessionContext;
26 import org.simantics.db.management.ISessionContextChangedListener;
27 import org.simantics.db.management.SessionContextChangedEvent;
28 import org.simantics.db.service.LifecycleSupport;
29 import org.simantics.utils.ui.SWTUtils;
30 import org.simantics.workbench.search.ISearchService;
31 import org.simantics.workbench.search.ISearchService.ResultBrowser;
32 import org.simantics.workbench.search.SearchQuery;
33
34 /**
35  * A very non-configurable Web browser view for search trim.
36  * 
37  * @author Tuukka Lehtonen
38  */
39 public class BrowserView extends ViewPart {
40
41     /**
42      * ID of this view's plug-in extension.
43      */
44     public static final String ID = "org.simantics.workbench.search.browser";
45
46     /**
47      * Used as a property of this view to keep track of the last search query
48      * performed with this view. This information is needed to know whether or
49      * not to initialize the view with default contents after it has been
50      * created. This depends on whether the creation was caused by a simple
51      * activation of the view or an actual search being performed.
52      */
53     public static final String LAST_QUERY_PROPERTY = "org.simantics.workbench.search.browser.lastQuery";
54
55     private Browser              browser;
56     private boolean              disposed;
57     private ActiveModelsListener currentListener;
58
59     @Override
60     public void createPartControl(Composite parent) {
61         browser = new org.eclipse.swt.browser.Browser(parent, SWT.NONE);
62
63         // Ensure the view receives some browser content even when no searches
64         // have been performed yet.
65         scheduleInitializeViewContent(false);
66
67         // Make sure that the contents of the view are reset every time the set
68         // of active models changes. This must be because what is searchable depends
69         // completely on the active models. When there is no active model, the search
70         // will find nothing.
71         Simantics.getSessionContextProvider().addContextChangedListener(sessionListener);
72         trackActiveModels(Simantics.peekSession());
73     }
74
75     private ISessionContextChangedListener sessionListener = new ISessionContextChangedListener() {
76         @Override
77         public void sessionContextChanged(SessionContextChangedEvent event) {
78             ISessionContext ctx = event.getNewValue();
79             final Session newSession = ctx != null ? ctx.peekSession() : null;
80             // Synchronize session changes to the UI thread to keep threading more predictable.
81             SWTUtils.asyncExec(browser, new Runnable() {
82                 @Override
83                 public void run() {
84                     if (!disposed)
85                         trackActiveModels(newSession);
86                 }
87             });
88         }
89     };
90
91     private void trackActiveModels(Session session) {
92         if (currentListener != null) {
93             currentListener.dispose();
94             currentListener = null;
95         }
96         if (session != null) {
97                 
98                         LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
99                         if (lfs == null)
100                                 return;
101                         if (lfs.isClosed())
102                                 return;
103                 
104             ActiveModelsListener listener = new ActiveModelsListener(new Runnable() {
105                 @Override
106                 public void run() {
107                     scheduleInitializeViewContent(true);
108                 }
109             });
110                         
111             // Make sure that the listener is disposed if the session is disposed.
112             session.registerService(ActiveModelsListener.class, listener);
113             session.asyncRequest(new ActiveModels(Simantics.getProjectResource()), listener);
114             this.currentListener = listener;
115         }
116     }
117
118     protected void scheduleInitializeViewContent(final boolean force) {
119         SWTUtils.asyncExec(browser, new Runnable() {
120             @Override
121             public void run() {
122                 if (disposed)
123                     return;
124                 // If SearchServiceImpl hasn't performed any queries yet,
125                 // then we shall initialize the view contents at this point.
126                 String lastQuery = getPartProperty(LAST_QUERY_PROPERTY);
127                 if (force || lastQuery == null)
128                     initializeViewContent();
129             }
130         });
131     }
132
133     protected void initializeViewContent() {
134         SearchQuery query = new SearchQuery("");
135         query.setSearchFlag("Name", "on");
136         query.setSearchFlag("Types", "on");
137         ISearchService searchService = (ISearchService) PlatformUI.getWorkbench().getService(ISearchService.class);
138         if (searchService != null)
139             searchService.performQuery(query, ResultBrowser.VIEW, false);
140     }
141
142     @Override
143     public void dispose() {
144         disposed = true;
145         super.dispose();
146     }
147
148     public boolean isDisposed() {
149         return disposed;
150     }
151
152     @Override
153     public void setFocus() {
154         browser.setFocus();
155     }
156
157     public org.eclipse.swt.browser.Browser getBrowser() {
158         return browser;
159     }
160
161     public void setUrl(URL url) {
162         getBrowser().setUrl(url.toString());
163     }
164
165     @Override
166     public void setContentDescription(String description) {
167         super.setContentDescription(description);
168     }
169
170 }