1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.workbench.internal.contributions.search;
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.genericrelation.Dependencies;
25 import org.simantics.db.layer0.request.ActiveModels;
26 import org.simantics.db.management.ISessionContext;
27 import org.simantics.db.management.ISessionContextChangedListener;
28 import org.simantics.db.management.SessionContextChangedEvent;
29 import org.simantics.db.service.LifecycleSupport;
30 import org.simantics.utils.ui.SWTUtils;
31 import org.simantics.workbench.search.ISearchService;
32 import org.simantics.workbench.search.ISearchService.ResultBrowser;
33 import org.simantics.workbench.search.SearchQuery;
36 * A very non-configurable Web browser view for search trim.
38 * @author Tuukka Lehtonen
40 public class BrowserView extends ViewPart {
43 * ID of this view's plug-in extension.
45 public static final String ID = "org.simantics.workbench.search.browser";
48 * Used as a property of this view to keep track of the last search query
49 * performed with this view. This information is needed to know whether or
50 * not to initialize the view with default contents after it has been
51 * created. This depends on whether the creation was caused by a simple
52 * activation of the view or an actual search being performed.
54 public static final String LAST_QUERY_PROPERTY = "org.simantics.workbench.search.browser.lastQuery";
56 private Browser browser;
57 private boolean disposed;
58 private ActiveModelsListener currentListener;
61 public void createPartControl(Composite parent) {
62 browser = new org.eclipse.swt.browser.Browser(parent, SWT.NONE);
64 // Ensure the view receives some browser content even when no searches
65 // have been performed yet.
66 scheduleInitializeViewContent(false);
68 // Make sure that the contents of the view are reset every time the set
69 // of active models changes. This must be because what is searchable depends
70 // completely on the active models. When there is no active model, the search
72 Simantics.getSessionContextProvider().addContextChangedListener(sessionListener);
73 trackActiveModels(Simantics.peekSession());
76 private ISessionContextChangedListener sessionListener = new ISessionContextChangedListener() {
78 public void sessionContextChanged(SessionContextChangedEvent event) {
79 ISessionContext ctx = event.getNewValue();
80 final Session newSession = ctx != null ? ctx.peekSession() : null;
81 // Synchronize session changes to the UI thread to keep threading more predictable.
82 SWTUtils.asyncExec(browser, new Runnable() {
86 trackActiveModels(newSession);
92 private void trackActiveModels(Session session) {
93 if (currentListener != null) {
94 currentListener.dispose();
95 currentListener = null;
97 if (session != null) {
99 LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
105 ActiveModelsListener listener = new ActiveModelsListener(new Runnable() {
108 scheduleInitializeViewContent(true);
112 // Make sure that the listener is disposed if the session is disposed.
113 session.registerService(ActiveModelsListener.class, listener);
114 session.asyncRequest(new ActiveModels(Simantics.getProjectResource()), listener);
115 this.currentListener = listener;
119 protected void scheduleInitializeViewContent(final boolean force) {
120 SWTUtils.asyncExec(browser, new Runnable() {
125 // If SearchServiceImpl hasn't performed any queries yet,
126 // then we shall initialize the view contents at this point.
127 String lastQuery = getPartProperty(LAST_QUERY_PROPERTY);
128 if (force || lastQuery == null)
129 initializeViewContent();
134 protected void initializeViewContent() {
135 SearchQuery query = new SearchQuery("");
136 query.setSearchFlag(Dependencies.FIELD_NAME_SEARCH, "on");
137 query.setSearchFlag(Dependencies.FIELD_TYPES_SEARCH, "on");
138 ISearchService searchService = (ISearchService) PlatformUI.getWorkbench().getService(ISearchService.class);
139 if (searchService != null)
140 searchService.performQuery(query, ResultBrowser.VIEW, false);
144 public void dispose() {
149 public boolean isDisposed() {
154 public void setFocus() {
158 public org.eclipse.swt.browser.Browser getBrowser() {
162 public void setUrl(URL url) {
163 getBrowser().setUrl(url.toString());
167 public void setContentDescription(String description) {
168 super.setContentDescription(description);