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