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