2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.rest;
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignmentView;
25 import jalview.gui.AlignFrame;
26 import jalview.gui.AlignViewport;
27 import jalview.gui.AlignmentPanel;
28 import jalview.gui.Desktop;
29 import jalview.gui.JvOptionPane;
30 import jalview.gui.WebserviceInfo;
31 import jalview.util.MessageManager;
32 import jalview.ws.WSClient;
33 import jalview.ws.WSClientI;
34 import jalview.ws.WSMenuEntryProviderI;
35 import jalview.ws.rest.clientdefs.ShmrRestClient;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.Vector;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43 import javax.swing.event.MenuEvent;
44 import javax.swing.event.MenuListener;
50 public class RestClient extends WSClient
51 implements WSClientI, WSMenuEntryProviderI
53 RestServiceDescription service;
55 public RestClient(RestServiceDescription rsd)
61 * parent alignframe for this job
66 * alignment view which provides data for job.
71 * get the alignFrame for the associated input data if it exists.
75 protected AlignFrame recoverAlignFrameForView()
77 return jalview.gui.Desktop.getAlignFrameFor(av);
80 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
82 this(service2, alignFrame, false);
85 boolean headless = false;
87 public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
92 av = alignFrame.getViewport();
97 public void setWebserviceInfo(boolean headless)
99 WebServiceJobTitle = MessageManager
100 .formatMessage("label.webservice_job_title", new String[]
101 { service.details.getAction(), service.details.getName() });
102 WebServiceName = service.details.getName();
103 WebServiceReference = "No reference - go to url for more info";
104 if (service.details.getDescription() != null)
106 WebServiceReference = service.details.getDescription();
110 wsInfo = new WebserviceInfo(WebServiceJobTitle,
111 WebServiceName + "\n" + WebServiceReference, true);
112 wsInfo.setRenderAsHtml(true);
118 public boolean isCancellable()
120 // TODO define process for cancelling rsbws jobs
125 public boolean canMergeResults()
127 // TODO process service definition to identify if the results might be
129 // TODO: change comparison for annotation merge
134 public void cancelJob()
136 System.err.println("Cannot cancel this job type: " + service);
140 public void attachWSMenuEntry(final JMenu wsmenu,
141 final AlignFrame alignFrame)
143 JMenuItem submit = new JMenuItem(service.details.getName());
144 submit.setToolTipText(MessageManager
145 .formatMessage("label.rest_client_submit", new String[]
146 { service.details.getAction(), service.details.getName() }));
147 submit.addActionListener(new ActionListener()
151 public void actionPerformed(ActionEvent e)
153 new RestClient(service, alignFrame);
158 // TODO: menu listener should enable/disable entry depending upon selection
159 // state of the alignment
160 wsmenu.addMenuListener(new MenuListener()
164 public void menuSelected(MenuEvent e)
166 // TODO Auto-generated method stub
171 public void menuDeselected(MenuEvent e)
173 // TODO Auto-generated method stub
178 public void menuCanceled(MenuEvent e)
180 // TODO Auto-generated method stub
189 * record of initial undoredo hash for the alignFrame providing data for this
192 long[] undoredo = null;
195 * Compare the original input data to the data currently presented to the
196 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
197 * any changes in hidden columns as normal) // if different undo/redo then
198 * compare region that was submitted // if same, then merge as before, if
199 * different then prompt user to open a new window.
203 protected boolean isAlignmentModified()
205 if (undoredo == null || av == null || av.getAlignment() == null)
207 // always return modified if we don't have access to live GUI elements
211 if (av.isUndoRedoHashModified(undoredo))
213 // alignment has been modified in some way.
216 // TODO: look deeper into modification of selection state, etc that may
217 // affect RestJobThread.realiseResults(boolean merge);
223 * TODO: combine to form a dataset+alignment+annotation context
225 AlignmentView _input;
230 jalview.io.packed.JalviewDataset jds;
233 * informative name for results
235 public String viewTitle;
237 protected void constructJob()
239 service.setInvolvesFlags();
240 // record all aspects of alignment view so we can merge back or recreate
242 undoredo = av.getUndoRedoHash();
244 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
245 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
246 * new SequenceGroup[0]; }
248 boolean selExists = (av.getSelectionGroup() != null)
249 && (av.getSelectionGroup().getSize() > 1);
250 // TODO: JAL-715: refactor to alignViewport methods and revise to full
251 // focus+context+dataset input data staging model
254 if (service.partitiondata)
256 if (av.getAlignment().getGroups() != null
257 && av.getAlignment().getGroups().size() > 0)
259 // intersect groups with selected region
260 _input = new AlignmentView(av.getAlignment(),
261 av.getAlignment().getHiddenColumns(),
262 av.getSelectionGroup(), av.hasHiddenColumns(), true,
264 viewTitle = MessageManager.formatMessage(
265 "label.select_visible_region_of", new String[]
266 { (av.hasHiddenColumns()
267 ? MessageManager.getString("label.visible")
273 // use selected region to partition alignment
274 _input = new AlignmentView(av.getAlignment(),
275 av.getAlignment().getHiddenColumns(),
276 av.getSelectionGroup(), av.hasHiddenColumns(), false,
279 viewTitle = MessageManager.formatMessage(
280 "label.select_unselect_visible_regions_from", new String[]
281 { (av.hasHiddenColumns()
282 ? MessageManager.getString("label.visible")
288 // just take selected region intersection
289 _input = new AlignmentView(av.getAlignment(),
290 av.getAlignment().getHiddenColumns(),
291 av.getSelectionGroup(), av.hasHiddenColumns(), true, true);
292 viewTitle = MessageManager.formatMessage(
293 "label.select_visible_region_of", new String[]
294 { (av.hasHiddenColumns()
295 ? MessageManager.getString("label.visible")
302 // standard alignment view without selection present
303 _input = new AlignmentView(av.getAlignment(),
304 av.getAlignment().getHiddenColumns(), null,
305 av.hasHiddenColumns(), false, true);
307 + (av.hasHiddenColumns()
308 ? (new StringBuffer(" ")
309 .append(MessageManager
310 .getString("label.visible_region_of"))
316 RestJobThread jobsthread = new RestJobThread(this);
318 if (jobsthread.isValid())
320 setWebserviceInfo(headless);
323 wsInfo.setthisService(this);
324 jobsthread.setWebServiceInfo(wsInfo);
330 // TODO: try to tell the user why the job couldn't be started.
331 JvOptionPane.showMessageDialog(Desktop.desktop,
332 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
333 : MessageManager.getString(
334 "label.job_couldnt_be_started_check_input")),
336 .getString("label.unable_start_web_service_analysis"),
337 JvOptionPane.WARNING_MESSAGE);
341 public AlignmentPanel recoverAlignPanelForView()
343 AlignmentPanel[] aps = Desktop
344 .getAlignmentPanels(av.getSequenceSetId());
345 for (AlignmentPanel alp : aps)
355 public boolean isShowResultsInNewView()
357 // TODO make this a property of the service
361 protected static Vector<String> services = null;
363 public static final String RSBS_SERVICES = "RSBS_SERVICES";
365 public static RestClient[] getRestClients()
367 if (services == null)
369 services = new Vector<>();
372 for (RestServiceDescription descr : RestServiceDescription
373 .parseDescriptions(jalview.bin.Cache.getDefault(
375 ShmrRestClient.makeShmmrRestClient().service.toString())))
377 services.add(descr.toString());
379 } catch (Exception ex)
382 "Serious - RSBS descriptions in user preferences are corrupt!");
383 ex.printStackTrace();
387 RestClient[] lst = new RestClient[services.size()];
389 for (String svc : services)
391 lst[i++] = new RestClient(new RestServiceDescription(svc));
396 public String getAction()
398 return service.details.getAction();
401 public RestServiceDescription getRestDescription()
406 public static Vector<String> getRsbsDescriptions()
408 Vector<String> rsbsDescrs = new Vector<>();
409 for (RestClient rsbs : getRestClients())
411 rsbsDescrs.add(rsbs.getRestDescription().toString());
416 public static void setRsbsServices(Vector<String> rsbsUrls)
418 if (rsbsUrls != null)
420 // TODO: consider validating services ?
421 services = new Vector<>(rsbsUrls);
422 StringBuffer sprop = new StringBuffer();
423 for (String s : services)
427 Cache.setProperty(RSBS_SERVICES, sprop.toString());
431 Cache.removeProperty(RSBS_SERVICES);