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.ApplicationSingletonProvider;
24 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
25 import jalview.bin.Cache;
26 import jalview.datamodel.AlignmentView;
27 import jalview.gui.AlignFrame;
28 import jalview.gui.AlignViewport;
29 import jalview.gui.AlignmentPanel;
30 import jalview.gui.Desktop;
31 import jalview.gui.JvOptionPane;
32 import jalview.gui.WebserviceInfo;
33 import jalview.util.MessageManager;
34 import jalview.ws.WSClient;
35 import jalview.ws.WSClientI;
36 import jalview.ws.WSMenuEntryProviderI;
37 import jalview.ws.rest.clientdefs.ShmrRestClient;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.util.Vector;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
52 public class RestClient extends WSClient
53 implements WSClientI, WSMenuEntryProviderI, ApplicationSingletonI
56 @SuppressWarnings("unused")
59 // accessed by ApplicationSingletonProvider
63 private static RestClient getInstance()
65 return (RestClient) ApplicationSingletonProvider.getInstance(RestClient.class);
68 public static final String RSBS_SERVICES = "RSBS_SERVICES";
71 protected Vector<String> services = null;
74 RestServiceDescription service;
76 public RestClient(RestServiceDescription rsd)
82 * parent alignframe for this job
87 * alignment view which provides data for job.
92 * get the alignFrame for the associated input data if it exists.
96 protected AlignFrame recoverAlignFrameForView()
98 return jalview.gui.Desktop.getAlignFrameFor(av);
101 public RestClient(RestServiceDescription service2, AlignFrame alignFrame)
103 this(service2, alignFrame, false);
106 boolean headless = false;
108 public RestClient(RestServiceDescription service2, AlignFrame alignFrame,
113 av = alignFrame.getViewport();
118 public void setWebserviceInfo(boolean headless)
120 WebServiceJobTitle = MessageManager
121 .formatMessage("label.webservice_job_title", new String[]
122 { service.details.getAction(), service.details.getName() });
123 WebServiceName = service.details.getName();
124 WebServiceReference = "No reference - go to url for more info";
125 if (service.details.getDescription() != null)
127 WebServiceReference = service.details.getDescription();
131 wsInfo = new WebserviceInfo(WebServiceJobTitle,
132 WebServiceName + "\n" + WebServiceReference, Desktop.FRAME_MAKE_VISIBLE);
133 wsInfo.setRenderAsHtml(true);
139 public boolean isCancellable()
141 // TODO define process for cancelling rsbws jobs
146 public boolean canMergeResults()
148 // TODO process service definition to identify if the results might be
150 // TODO: change comparison for annotation merge
155 public void cancelJob()
157 System.err.println("Cannot cancel this job type: " + service);
161 public void attachWSMenuEntry(final JMenu wsmenu,
162 final AlignFrame alignFrame)
164 JMenuItem submit = new JMenuItem(service.details.getName());
165 submit.setToolTipText(MessageManager
166 .formatMessage("label.rest_client_submit", new String[]
167 { service.details.getAction(), service.details.getName() }));
168 submit.addActionListener(new ActionListener()
172 public void actionPerformed(ActionEvent e)
174 new RestClient(service, alignFrame);
179 // TODO: menu listener should enable/disable entry depending upon selection
180 // state of the alignment
181 wsmenu.addMenuListener(new MenuListener()
185 public void menuSelected(MenuEvent e)
187 // TODO Auto-generated method stub
192 public void menuDeselected(MenuEvent e)
194 // TODO Auto-generated method stub
199 public void menuCanceled(MenuEvent e)
201 // TODO Auto-generated method stub
210 * record of initial undoredo hash for the alignFrame providing data for this
213 long[] undoredo = null;
216 * Compare the original input data to the data currently presented to the
217 * user. // LOGIC: compare undo/redo - if same, merge regardless (coping with
218 * any changes in hidden columns as normal) // if different undo/redo then
219 * compare region that was submitted // if same, then merge as before, if
220 * different then prompt user to open a new window.
224 protected boolean isAlignmentModified()
226 if (undoredo == null || av == null || av.getAlignment() == null)
228 // always return modified if we don't have access to live GUI elements
232 if (av.isUndoRedoHashModified(undoredo))
234 // alignment has been modified in some way.
237 // TODO: look deeper into modification of selection state, etc that may
238 // affect RestJobThread.realiseResults(boolean merge);
244 * TODO: combine to form a dataset+alignment+annotation context
246 AlignmentView _input;
251 jalview.io.packed.JalviewDataset jds;
254 * informative name for results
256 public String viewTitle;
258 protected void constructJob()
260 service.setInvolvesFlags();
261 // record all aspects of alignment view so we can merge back or recreate
263 undoredo = av.getUndoRedoHash();
265 * delete ? Vector sgs = av.getAlignment().getGroups(); if (sgs!=null) {
266 * _sgs = new SequenceGroup[sgs.size()]; sgs.copyInto(_sgs); } else { _sgs =
267 * new SequenceGroup[0]; }
269 boolean selExists = (av.getSelectionGroup() != null)
270 && (av.getSelectionGroup().getSize() > 1);
271 // TODO: JAL-715: refactor to alignViewport methods and revise to full
272 // focus+context+dataset input data staging model
275 if (service.partitiondata)
277 if (av.getAlignment().getGroups() != null
278 && av.getAlignment().getGroups().size() > 0)
280 // intersect groups with selected region
281 _input = new AlignmentView(av.getAlignment(),
282 av.getAlignment().getHiddenColumns(),
283 av.getSelectionGroup(), av.hasHiddenColumns(), true,
285 viewTitle = MessageManager.formatMessage(
286 "label.select_visible_region_of", new String[]
287 { (av.hasHiddenColumns()
288 ? MessageManager.getString("label.visible")
294 // use selected region to partition alignment
295 _input = new AlignmentView(av.getAlignment(),
296 av.getAlignment().getHiddenColumns(),
297 av.getSelectionGroup(), av.hasHiddenColumns(), false,
300 viewTitle = MessageManager.formatMessage(
301 "label.select_unselect_visible_regions_from", new String[]
302 { (av.hasHiddenColumns()
303 ? MessageManager.getString("label.visible")
309 // just take selected region intersection
310 _input = new AlignmentView(av.getAlignment(),
311 av.getAlignment().getHiddenColumns(),
312 av.getSelectionGroup(), av.hasHiddenColumns(), true, true);
313 viewTitle = MessageManager.formatMessage(
314 "label.select_visible_region_of", new String[]
315 { (av.hasHiddenColumns()
316 ? MessageManager.getString("label.visible")
323 // standard alignment view without selection present
324 _input = new AlignmentView(av.getAlignment(),
325 av.getAlignment().getHiddenColumns(), null,
326 av.hasHiddenColumns(), false, true);
328 + (av.hasHiddenColumns()
329 ? (new StringBuffer(" ")
330 .append(MessageManager
331 .getString("label.visible_region_of"))
337 RestJobThread jobsthread = new RestJobThread(this);
339 if (jobsthread.isValid())
341 setWebserviceInfo(headless);
344 wsInfo.setthisService(this);
345 jobsthread.setWebServiceInfo(wsInfo);
351 // TODO: try to tell the user why the job couldn't be started.
352 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
353 (jobsthread.hasWarnings() ? jobsthread.getWarnings()
354 : MessageManager.getString(
355 "label.job_couldnt_be_started_check_input")),
357 .getString("label.unable_start_web_service_analysis"),
358 JvOptionPane.WARNING_MESSAGE);
362 public AlignmentPanel recoverAlignPanelForView()
364 AlignmentPanel[] aps = Desktop
365 .getAlignmentPanels(av.getSequenceSetId());
366 for (AlignmentPanel alp : aps)
376 public boolean isShowResultsInNewView()
378 // TODO make this a property of the service
382 public static RestClient[] getRestClients()
384 return getInstance().getClients();
387 private RestClient[] getClients()
389 if (services == null)
391 services = new Vector<>();
394 for (RestServiceDescription descr : RestServiceDescription
395 .parseDescriptions(jalview.bin.Cache.getDefault(
397 ShmrRestClient.makeShmmrRestClient().service.toString())))
399 services.add(descr.toString());
401 } catch (Exception ex)
404 "Serious - RSBS descriptions in user preferences are corrupt!");
405 ex.printStackTrace();
409 RestClient[] lst = new RestClient[services.size()];
411 for (String svc : services)
413 lst[i++] = new RestClient(new RestServiceDescription(svc));
418 public String getAction()
420 return service.details.getAction();
423 public RestServiceDescription getRestDescription()
428 public static Vector<String> getRsbsDescriptions()
430 Vector<String> rsbsDescrs = new Vector<>();
431 for (RestClient rsbs : getRestClients())
433 rsbsDescrs.add(rsbs.getRestDescription().toString());
438 public static void setRsbsServices(Vector<String> rsbsUrls)
440 if (rsbsUrls != null)
443 // TODO: consider validating services ?
444 getInstance().services = new Vector<String>(rsbsUrls);
445 StringBuffer sprop = new StringBuffer();
446 for (String s : getInstance().services)
450 Cache.setProperty(RSBS_SERVICES, sprop.toString());
454 Cache.removeProperty(RSBS_SERVICES);