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