update author list in license for (JAL-826)
[jalview.git] / src / jalview / gui / RestInputParamEditDialog.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.gui;
19
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22
23 import javax.swing.JDialog;
24 import javax.swing.JInternalFrame;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.event.ListSelectionEvent;
28
29 import com.sun.tools.corba.se.idl.InvalidArgument;
30
31 import net.miginfocom.swing.MigLayout;
32
33 import jalview.jbgui.GRestInputParamEditDialog;
34 import jalview.ws.params.ArgumentI;
35 import jalview.ws.params.InvalidArgumentException;
36 import jalview.ws.params.OptionI;
37 import jalview.ws.params.ParameterI;
38 import jalview.ws.rest.InputType;
39 import jalview.ws.rest.RestServiceDescription;
40
41 public class RestInputParamEditDialog extends GRestInputParamEditDialog
42         implements OptsParametersContainerI
43 {
44   Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
45
46   Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
47
48   Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
49   
50   private InputType getTypeFor(String name)
51   {
52     try
53     {
54       return (InputType) (typeclass.get(name).getConstructor()
55               .newInstance(null));
56     } catch (Throwable x)
57     {
58       System.err
59               .println("Unexpected exception when instantiating rest input type.");
60       x.printStackTrace();
61     }
62     return null;
63   }
64
65   int reply;
66   JalviewDialog frame = new JalviewDialog()
67   {
68     
69     @Override
70     protected void raiseClosed()
71     {
72       
73     }
74     
75     @Override
76     protected void okPressed()
77     {
78       reply=JOptionPane.OK_OPTION; 
79     }
80     
81     @Override
82     protected void cancelPressed()
83     {
84       reply=JOptionPane.CANCEL_OPTION; 
85       
86     }
87   };
88   InputType old,current;
89   public RestInputParamEditDialog(
90           RestServiceEditorPane restServiceEditorPane,
91           RestServiceDescription currentservice, InputType toedit)
92   {
93     initFor(restServiceEditorPane, currentservice, toedit);
94     frame.waitForInput();
95     // TODO: warn user if they are about to overwrite an existing parameter because they have used the same name when editing a different parameter.
96     // TODO: make any press of the return key cause 'OK' to be pressed
97   }
98   private void initFor(RestServiceEditorPane restServiceEditorPane,
99           RestServiceDescription currentservice, InputType toedit)
100   {
101     okcancel.add(frame.cancel);
102     okcancel.add(frame.ok);
103     frame.initDialogFrame(dpane, true, true, "Edit parameter for service "+currentservice.getName(), 600,800);
104     
105     initTypeLists();
106     reply=JOptionPane.CANCEL_OPTION;
107     old = toedit;
108     current = null;
109     if (old!=null) {
110       setStateFor(old);
111     }
112     updated = updated && reply==JOptionPane.OK_OPTION;
113     frame.validate();
114   }
115
116   public RestInputParamEditDialog(
117           RestServiceEditorPane restServiceEditorPane,
118           RestServiceDescription currentservice, String string)
119   {
120     initFor(restServiceEditorPane, currentservice,null);
121     tok.setText(string);
122     frame.waitForInput();
123   }
124
125   private void setStateFor(InputType current)
126   {
127     tok.setText(current.token);
128     OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
129     for (OptionI ops:current.getOptions())
130     {
131       if (ops instanceof ParameterI)
132       {
133         opanp.setParameter((ParameterI) ops);
134       } else {
135         if (ops.getValue()!=null && ops.getValue().length()>0) {
136           opanp.selectOption(ops, ops.getValue());
137         }
138       }
139     }
140     typeList.setSelectedValue(current.getURLtokenPrefix(), true);
141     type_SelectionChangedActionPerformed(null);
142   }
143   private void updateCurrentType()
144   {
145     if (typeList.getSelectedValue()!=null) {
146     InputType newType = getTypeFor((String)typeList.getSelectedValue());
147     if (newType!=null) {
148       newType.token = tok.getText().trim();
149       try {
150         newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
151         current=newType;
152         updated=true;
153       } catch (InvalidArgumentException ex) {
154         System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
155         ex.printStackTrace();
156       }
157     }
158     }
159     
160   }
161   private void initTypeLists()
162   {
163     ArrayList<String> types=new ArrayList<String>();
164     // populate type list
165     for (Class type : RestServiceDescription.getInputTypes())
166     {
167
168       InputType jtype = null;
169       try
170       {
171         JPanel inopts = new JPanel(new MigLayout());
172         ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
173         jtype = (InputType) (type.getConstructor().newInstance(null));
174         typeclass.put(jtype.getURLtokenPrefix(), type);
175         // and populate parameters from this type
176         OptsAndParamsPage opanp = new OptsAndParamsPage(this,true);
177         opanps.put(jtype.getURLtokenPrefix(),opanp);
178         for (OptionI opt : jtype.getOptions())
179         {
180
181           if (opt instanceof ParameterI)
182           {
183             prms.add(opanp.addParameter((ParameterI) opt));
184           }
185           else
186           {
187             opts.add(opanp.addOption(opt));
188           }
189         }
190         // then tag the params at the end of the options.
191         for (JPanel pnl : prms)
192         {
193           opts.add(pnl);
194         }
195         typeopts.put(jtype.getURLtokenPrefix(), opts);
196         types.add(jtype.getURLtokenPrefix());
197       } catch (Throwable x)
198       {
199         System.err
200                 .println("Unexpected exception when instantiating rest input type.");
201         x.printStackTrace();
202       }
203     }
204     typeList.setListData(types.toArray());
205     
206   }
207
208   @Override
209   protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
210   {
211     options.removeAll();
212     String typen = (String) typeList.getSelectedValue();
213     if (typeopts.get(typen) != null)
214     {
215       for (JPanel opt : typeopts.get(typen))
216       {
217         opt.setOpaque(true);
218         options.add(opt,"wrap");
219       }
220       options.invalidate();
221       optionsPanel.setVisible(true);
222     }
223     else
224     {
225       optionsPanel.setVisible(false);
226     }
227     dpane.revalidate();
228     updateCurrentType();
229   }
230
231   boolean updated=false;
232   public boolean wasUpdated()
233   {
234     return updated;
235   }
236
237   @Override
238   public void refreshParamLayout()
239   {
240     options.invalidate();
241     dpane.revalidate();
242   }
243
244   @Override
245   protected void tokChanged_actionPerformed()
246   {
247     if (tok.getText().trim().length()>0)
248     {
249       if (current!=null)
250       {
251         current.token = tok.getText().trim();
252         updated = true; 
253       } 
254     }
255   }
256   @Override
257   public void argSetModified(Object modifiedElement, boolean b)
258   {
259     updated = updated | b;
260     if (updated)
261     {
262       updateCurrentType();
263     }
264   }
265
266 }