JAL-975 null any existing preset if user has supplied custom args
[jalview.git] / src / jalview / ws / jws2 / Jws2Client.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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,
49           WsParamSetI preset, 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   protected boolean processParams(Jws2Instance sh, boolean editParams,
82           boolean adjustingExisting)
83   {
84
85     if (editParams)
86     {
87       if (sh.paramStore == null)
88       {
89         sh.paramStore = new JabaParamStore(sh,
90                 Desktop.getUserParameterStore());
91       }
92       WsJobParameters jobParams = (preset==null && paramset!=null && paramset.size()>0) 
93               ?  new WsJobParameters(null, sh, null, paramset) 
94         : new WsJobParameters(sh, preset);
95       if (adjustingExisting)
96       {
97         jobParams.setName("Adjusting parameters for existing Calculation");
98       }
99       if (!jobParams.showRunDialog())
100       {
101         return false;
102       }
103       WsParamSetI prset = jobParams.getPreset();
104       if (prset == null)
105       {
106         paramset = jobParams.isServiceDefaults() ? null : JabaParamStore.getJabafromJwsArgs(jobParams
107                 .getJobParams());
108         this.preset=null;
109       }
110       else
111       {
112         this.preset = prset; // ((JabaPreset) prset).p;
113         paramset = null; // no user supplied parameters.
114       }
115     }
116     return true;
117
118   }
119
120   public Jws2Client()
121   {
122     // anonymous constructor - used for headless method calls only
123   }
124
125   protected WebserviceInfo setWebService(Jws2Instance serv, boolean b)
126   {
127     // serviceHandle = serv;
128     String serviceInstance = serv.action; // serv.service.getClass().getName();
129     WebServiceName = serv.serviceType;
130     WebServiceJobTitle = serv.getActionText();
131     WsURL = serv.hosturl;
132     if (!b)
133     {
134       return new WebserviceInfo(WebServiceJobTitle, WebServiceJobTitle
135               + " using service hosted at " + serv.hosturl + "\n"
136               + (serv.description != null ? serv.description : ""));
137     }
138     return null;
139   }
140
141   /*
142    * Jws2Instance serviceHandle; (non-Javadoc)
143    * 
144    * @see jalview.ws.WSMenuEntryProviderI#attachWSMenuEntry(javax.swing.JMenu,
145    * jalview.gui.AlignFrame)
146    * 
147    * @Override public void attachWSMenuEntry(JMenu wsmenu, AlignFrame
148    * alignFrame) { if (serviceHandle==null) { throw new
149    * Error("Implementation error: No service handle for this Jws2 service."); }
150    * attachWSMenuEntry(wsmenu, serviceHandle, alignFrame); }
151    */
152   /**
153    * add the menu item for a particular jws2 service instance
154    * 
155    * @param wsmenu
156    * @param service
157    * @param alignFrame
158    */
159   abstract void attachWSMenuEntry(JMenu wsmenu, final Jws2Instance service,
160           final AlignFrame alignFrame);
161
162 }