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.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;
35 * A very non-configurable Web browser view for search trim.
37 * @author Tuukka Lehtonen
39 public class BrowserView extends ViewPart {
42 * ID of this view's plug-in extension.
44 public static final String ID = "org.simantics.workbench.search.browser";
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.
53 public static final String LAST_QUERY_PROPERTY = "org.simantics.workbench.search.browser.lastQuery";
55 private Browser browser;
56 private boolean disposed;
57 private ActiveModelsListener currentListener;
60 public void createPartControl(Composite parent) {
61 browser = new org.eclipse.swt.browser.Browser(parent, SWT.NONE);
63 // Ensure the view receives some browser content even when no searches
64 // have been performed yet.
65 scheduleInitializeViewContent(false);
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
71 Simantics.getSessionContextProvider().addContextChangedListener(sessionListener);
72 trackActiveModels(Simantics.peekSession());
75 private ISessionContextChangedListener sessionListener = new ISessionContextChangedListener() {
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() {
85 trackActiveModels(newSession);
91 private void trackActiveModels(Session session) {
92 if (currentListener != null) {
93 currentListener.dispose();
94 currentListener = null;
96 if (session != null) {
98 LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
104 ActiveModelsListener listener = new ActiveModelsListener(new Runnable() {
107 scheduleInitializeViewContent(true);
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;
118 protected void scheduleInitializeViewContent(final boolean force) {
119 SWTUtils.asyncExec(browser, new Runnable() {
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();
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);
143 public void dispose() {
148 public boolean isDisposed() {
153 public void setFocus() {
157 public org.eclipse.swt.browser.Browser getBrowser() {
161 public void setUrl(URL url) {
162 getBrowser().setUrl(url.toString());
166 public void setContentDescription(String description) {
167 super.setContentDescription(description);