4166d3eb07b427a79b9254293b62b4c5082fb8bd
[jalview.git] / src / jalview / ws / jws2 / Jws2Client.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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  */
18 package jalview.ws.jws2;
19
20 import java.util.List;
21
22 import javax.swing.JMenu;
23
24 import compbio.metadata.Argument;
25
26 import jalview.gui.AlignFrame;
27 import jalview.gui.Desktop;
28 import jalview.gui.WebserviceInfo;
29 import jalview.gui.WsJobParameters;
30 import jalview.ws.jws2.dm.JabaWsParamSet;
31 import jalview.ws.jws2.jabaws2.Jws2Instance;
32 import jalview.ws.params.WsParamSetI;
33
34 /**
35  * provides metadata for a jabaws2 service instance - resolves names, etc.
36  * 
37  * @author JimP
38  * 
39  */
40 public abstract class Jws2Client extends jalview.ws.WSClient
41 {
42   protected AlignFrame alignFrame;
43
44   protected WsParamSetI preset;
45
46   protected List<Argument> paramset;
47
48   public Jws2Client(AlignFrame _alignFrame, WsParamSetI preset,
49           List<Argument> arguments)
50   {
51     alignFrame = _alignFrame;
52     this.preset = preset;
53     if (preset != null)
54     {
55       if (!((preset instanceof JabaPreset) || preset instanceof JabaWsParamSet))
56       {
57         /*
58          * { this.preset = ((JabaPreset) preset).p; } else if (preset instanceof
59          * JabaWsParamSet) { List<Argument> newargs = new ArrayList<Argument>();
60          * JabaWsParamSet pset = ((JabaWsParamSet) preset); for (Option opt :
61          * pset.getjabaArguments()) { newargs.add(opt); } if (arguments != null
62          * && arguments.size() > 0) { // merge arguments with preset's own
63          * arguments. for (Argument opt : arguments) { newargs.add(opt); } }
64          * paramset = newargs; } else {
65          */
66         throw new Error(
67                 "Implementation error: Can only instantiate Jaba parameter sets.");
68       }
69     }
70     else
71     {
72       // just provided with a bunch of arguments
73       this.paramset = arguments;
74     }
75   }
76
77   boolean processParams(Jws2Instance sh, boolean editParams)
78   {
79     return processParams(sh, editParams, false);
80   }
81
82   protected boolean processParams(Jws2Instance sh, boolean editParams,
83           boolean adjustingExisting)
84   {
85
86     if (editParams)
87     {
88       if (sh.paramStore == null)
89       {
90         sh.paramStore = new JabaParamStore(sh,
91                 Desktop.getUserParameterStore());
92       }
93       WsJobParameters jobParams = (preset == null && paramset != null && paramset
94               .size() > 0) ? new WsJobParameters(null, sh, null, paramset)
95               : new WsJobParameters(sh, preset);
96       if (adjustingExisting)
97       {
98         jobParams.setName("Adjusting parameters for existing Calculation");
99       }
100       if (!jobParams.showRunDialog())
101       {
102         return false;
103       }
104       WsParamSetI prset = jobParams.getPreset();
105       if (prset == null)
106       {
107         paramset = jobParams.isServiceDefaults() ? null : JabaParamStore
108                 .getJabafromJwsArgs(jobParams.getJobParams());
109         this.preset = null;
110       }
111       else
112       {
113         this.preset = prset; // ((JabaPreset) prset).p;
114         paramset = null; // no user supplied parameters.
115       }
116     }
117     return true;
118
119   }
120
121   public Jws2Client()
122   {
123     // anonymous constructor - used for headless method calls only
124   }
125
126   protected WebserviceInfo setWebService(Jws2Instance serv, boolean b)
127   {
128     // serviceHandle = serv;
129     String serviceInstance = serv.action; // serv.service.getClass().getName();
130     WebServiceName = serv.serviceType;
131     WebServiceJobTitle = serv.getActionText();
132     WsURL = serv.hosturl;
133     if (!b)
134     {
135       return new WebserviceInfo(WebServiceJobTitle, WebServiceJobTitle
136               + " using service hosted at " + serv.hosturl + "\n"
137               + (serv.description != null ? serv.description : ""));
138     }
139     return null;
140   }
141
142   /*
143    * Jws2Instance serviceHandle; (non-Javadoc)
144    * 
145    * @see jalview.ws.WSMenuEntryProviderI#attachWSMenuEntry(javax.swing.JMenu,
146    * jalview.gui.AlignFrame)
147    * 
148    * @Override public void attachWSMenuEntry(JMenu wsmenu, AlignFrame
149    * alignFrame) { if (serviceHandle==null) { throw new
150    * Error("Implementation error: No service handle for this Jws2 service."); }
151    * attachWSMenuEntry(wsmenu, serviceHandle, alignFrame); }
152    */
153   /**
154    * add the menu item for a particular jws2 service instance
155    * 
156    * @param wsmenu
157    * @param service
158    * @param alignFrame
159    */
160   abstract void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
161           final AlignFrame alignFrame);
162
163 }