]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench.search.impl/src/org/simantics/workbench/search/impl/BrowserView.java
Add missing influxdb-client-core to target platform
[simantics/platform.git] / bundles / org.simantics.workbench.search.impl / src / org / simantics / workbench / search / impl / 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.search.impl;
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.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;
34
35 /**
36  * A very non-configurable Web browser view for search trim.
37  * 
38  * @author Tuukka Lehtonen
39  */
40 public class BrowserView extends ViewPart {
41
42     /**
43      * ID of this view's plug-in extension.
44      */
45     public static final String ID = "org.simantics.workbench.search.browser";
46
47     /**
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.
53      */
54     public static final String LAST_QUERY_PROPERTY = "org.simantics.workbench.search.browser.lastQuery";
55
56     private Browser              browser;
57     private boolean              disposed;
58     private ActiveModelsListener currentListener;
59
60     @Override
61     public void createPartControl(Composite parent) {
62         browser = new org.eclipse.swt.browser.Browser(parent, SWT.NONE);
63
64         // Ensure the view receives some browser content even when no searches
65         // have been performed yet.
66         scheduleInitializeViewContent(false);
67
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
71         // will find nothing.
72         Simantics.getSessionContextProvider().addContextChangedListener(sessionListener);
73         trackActiveModels(Simantics.peekSession());
74     }
75
76     private ISessionContextChangedListener sessionListener = new ISessionContextChangedListener() {
77         @Override
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() {
83                 @Override
84                 public void run() {
85                     if (!disposed)
86                         trackActiveModels(newSession);
87                 }
88             });
89         }
90     };
91
92     private void trackActiveModels(Session session) {
93         if (currentListener != null) {
94             currentListener.dispose();
95             currentListener = null;
96         }
97         if (session != null) {
98                 
99                         LifecycleSupport lfs = session.peekService(LifecycleSupport.class);
100                         if (lfs == null)
101                                 return;
102                         if (lfs.isClosed())
103                                 return;
104                 
105             ActiveModelsListener listener = new ActiveModelsListener(new Runnable() {
106                 @Override
107                 public void run() {
108                     scheduleInitializeViewContent(true);
109                 }
110             });
111                         
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;
116         }
117     }
118
119     protected void scheduleInitializeViewContent(final boolean force) {
120         SWTUtils.asyncExec(browser, new Runnable() {
121             @Override
122             public void run() {
123                 if (disposed)
124                     return;
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();
130             }
131         });
132     }
133
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);
141     }
142
143     @Override
144     public void dispose() {
145         disposed = true;
146         super.dispose();
147     }
148
149     public boolean isDisposed() {
150         return disposed;
151     }
152
153     @Override
154     public void setFocus() {
155         browser.setFocus();
156     }
157
158     public org.eclipse.swt.browser.Browser getBrowser() {
159         return browser;
160     }
161
162     public void setUrl(URL url) {
163         getBrowser().setUrl(url.toString());
164     }
165
166     @Override
167     public void setContentDescription(String description) {
168         super.setContentDescription(description);
169     }
170
171 }