0a3db0dac0c8eee3b24e2d65f844019400aac10f
[jalview.git] / src / jalview / ws / jws2 / Jws2Client.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.jws2;
20
21 import java.util.List;
22
23 import javax.swing.JMenu;
24
25 import compbio.metadata.Argument;
26
27 import jalview.gui.AlignFrame;
28 import jalview.gui.Desktop;
29 import jalview.gui.WebserviceInfo;
30 import jalview.gui.WsJobParameters;
31 import jalview.util.MessageManager;
32 import jalview.ws.jws2.dm.JabaWsParamSet;
33 import jalview.ws.jws2.jabaws2.Jws2Instance;
34 import jalview.ws.params.WsParamSetI;
35
36 /**
37  * provides metadata for a jabaws2 service instance - resolves names, etc.
38  * 
39  * @author JimP
40  * 
41  */
42 public abstract class Jws2Client extends jalview.ws.WSClient
43 {
44   protected AlignFrame alignFrame;
45
46   protected WsParamSetI preset;
47
48   protected List<Argument> paramset;
49
50   public Jws2Client(AlignFrame _alignFrame, WsParamSetI preset,
51           List<Argument> arguments)
52   {
53     alignFrame = _alignFrame;
54     this.preset = preset;
55     if (preset != null)
56     {
57       if (!((preset instanceof JabaPreset) || preset instanceof JabaWsParamSet))
58       {
59         /*
60          * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
61          * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
62          * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
63          * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
64          * && arguments.size() > 0) { // merge arguments with preset's own
65          * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
66          * paramset = newargs; } else {
67          */
68         throw new Error(
69                 "Implementation error: Can only instantiate Jaba parameter sets.");
70       }
71     }
72     else
73     {
74       // just provided with a bunch of arguments
75       this.paramset = arguments;
76     }
77   }
78
79   boolean processParams(Jws2Instance sh, boolean editParams)
80   {
81     return processParams(sh, editParams, false);
82   }
83
84   protected boolean processParams(Jws2Instance sh, boolean editParams,
85           boolean adjustingExisting)
86   {
87
88     if (editParams)
89     {
90       if (sh.paramStore == null)
91       {
92         sh.paramStore = new JabaParamStore(sh,
93                 Desktop.getUserParameterStore());
94       }
95       WsJobParameters jobParams = (preset == null && paramset != null && paramset
96               .size() > 0) ? new WsJobParameters(null, sh, null, paramset)
97               : new WsJobParameters(sh, preset);
98       if (adjustingExisting)
99       {
100         jobParams.setName(MessageManager.getString("label.adjusting_parameters_for_calculation"));
101       }
102       if (!jobParams.showRunDialog())
103       {
104         return false;
105       }
106       WsParamSetI prset = jobParams.getPreset();
107       if (prset == null)
108       {
109         paramset = jobParams.isServiceDefaults() ? null : JabaParamStore
110                 .getJabafromJwsArgs(jobParams.getJobParams());
111         this.preset = null;
112       }
113       else
114       {
115         this.preset = prset; // ((JabaPreset) prset).p;
116         paramset = null; // no user supplied parameters.
117       }
118     }
119     return true;
120
121   }
122
123   public Jws2Client()
124   {
125     // anonymous constructor - used for headless method calls only
126   }
127
128   protected WebserviceInfo setWebService(Jws2Instance serv, boolean b)
129   {
130     // serviceHandle = serv;
131     String serviceInstance = serv.action; // serv.service.getClass().getName();
132     WebServiceName = serv.serviceType;
133     WebServiceJobTitle = serv.getActionText();
134     WsURL = serv.hosturl;
135     if (!b)
136     {
137       return new WebserviceInfo(WebServiceJobTitle, WebServiceJobTitle
138               + " using service hosted at " + serv.hosturl + "\n"
139               + (serv.description != null ? serv.description : ""));
140     }
141     return null;
142   }
143
144   /*
145    * Jws2Instance serviceHandle; (non-Javadoc)
146    * 
147    * @see jalview.ws.WSMenuEntryProviderI#attachWSMenuEntry(javax.swing.JMenu,
148    * jalview.gui.AlignFrame)
149    * 
150    * @Override public void attachWSMenuEntry(JMenu wsmenu, AlignFrame
151    * alignFrame) { if (serviceHandle==null) { throw new
152    * Error("Implementation error: No service handle for this Jws2 service."); }
153    * attachWSMenuEntry(wsmenu, serviceHandle, alignFrame); }
154    */
155   /**
156    * add the menu item for a particular jws2 service instance
157    * 
158    * @param wsmenu
159    * @param service
160    * @param alignFrame
161    */
162   abstract void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
163           final AlignFrame alignFrame);
164
165 }