2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
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.
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.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import java.util.ArrayList;
21 import java.util.Hashtable;
23 import javax.swing.JOptionPane;
24 import javax.swing.JPanel;
25 import javax.swing.event.ListSelectionEvent;
27 import net.miginfocom.swing.MigLayout;
29 import jalview.jbgui.GRestInputParamEditDialog;
30 import jalview.ws.params.InvalidArgumentException;
31 import jalview.ws.params.OptionI;
32 import jalview.ws.params.ParameterI;
33 import jalview.ws.rest.InputType;
34 import jalview.ws.rest.RestServiceDescription;
36 public class RestInputParamEditDialog extends GRestInputParamEditDialog
37 implements OptsParametersContainerI
39 Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
41 Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
43 Hashtable<String, OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
45 private InputType getTypeFor(String name)
49 return (InputType) (typeclass.get(name).getConstructor()
54 .println("Unexpected exception when instantiating rest input type.");
62 JalviewDialog frame = new JalviewDialog()
66 protected void raiseClosed()
72 protected void okPressed()
74 reply = JOptionPane.OK_OPTION;
78 protected void cancelPressed()
80 reply = JOptionPane.CANCEL_OPTION;
85 InputType old, current;
87 public RestInputParamEditDialog(
88 RestServiceEditorPane restServiceEditorPane,
89 RestServiceDescription currentservice, InputType toedit)
91 initFor(restServiceEditorPane, currentservice, toedit);
93 // TODO: warn user if they are about to overwrite an existing parameter
94 // because they have used the same name when editing a different parameter.
95 // TODO: make any press of the return key cause 'OK' to be pressed
98 private void initFor(RestServiceEditorPane restServiceEditorPane,
99 RestServiceDescription currentservice, InputType toedit)
101 okcancel.add(frame.cancel);
102 okcancel.add(frame.ok);
103 frame.initDialogFrame(dpane, true, true, "Edit parameter for service "
104 + currentservice.getName(), 600, 800);
107 reply = JOptionPane.CANCEL_OPTION;
114 updated = updated && reply == JOptionPane.OK_OPTION;
118 public RestInputParamEditDialog(
119 RestServiceEditorPane restServiceEditorPane,
120 RestServiceDescription currentservice, String string)
122 initFor(restServiceEditorPane, currentservice, null);
124 frame.waitForInput();
127 private void setStateFor(InputType current)
129 tok.setText(current.token);
130 OptsAndParamsPage opanp = opanps.get(current.getURLtokenPrefix());
131 for (OptionI ops : current.getOptions())
133 if (ops instanceof ParameterI)
135 opanp.setParameter((ParameterI) ops);
139 if (ops.getValue() != null && ops.getValue().length() > 0)
141 opanp.selectOption(ops, ops.getValue());
145 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
146 type_SelectionChangedActionPerformed(null);
149 private void updateCurrentType()
151 if (typeList.getSelectedValue() != null)
153 InputType newType = getTypeFor((String) typeList.getSelectedValue());
156 newType.token = tok.getText().trim();
159 newType.configureFromArgumentI(opanps.get(
160 newType.getURLtokenPrefix()).getCurrentSettings());
163 } catch (InvalidArgumentException ex)
166 .println("IMPLEMENTATION ERROR: Invalid argument for type : "
167 + typeList.getSelectedValue() + "\n");
168 ex.printStackTrace();
175 private void initTypeLists()
177 ArrayList<String> types = new ArrayList<String>();
178 // populate type list
179 for (Class type : RestServiceDescription.getInputTypes())
182 InputType jtype = null;
185 JPanel inopts = new JPanel(new MigLayout());
186 ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
187 jtype = (InputType) (type.getConstructor().newInstance(null));
188 typeclass.put(jtype.getURLtokenPrefix(), type);
189 // and populate parameters from this type
190 OptsAndParamsPage opanp = new OptsAndParamsPage(this, true);
191 opanps.put(jtype.getURLtokenPrefix(), opanp);
192 for (OptionI opt : jtype.getOptions())
195 if (opt instanceof ParameterI)
197 prms.add(opanp.addParameter((ParameterI) opt));
201 opts.add(opanp.addOption(opt));
204 // then tag the params at the end of the options.
205 for (JPanel pnl : prms)
209 typeopts.put(jtype.getURLtokenPrefix(), opts);
210 types.add(jtype.getURLtokenPrefix());
211 } catch (Throwable x)
214 .println("Unexpected exception when instantiating rest input type.");
218 typeList.setListData(types.toArray());
223 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
226 String typen = (String) typeList.getSelectedValue();
227 if (typeopts.get(typen) != null)
229 for (JPanel opt : typeopts.get(typen))
232 options.add(opt, "wrap");
234 options.invalidate();
235 optionsPanel.setVisible(true);
239 optionsPanel.setVisible(false);
245 boolean updated = false;
247 public boolean wasUpdated()
253 public void refreshParamLayout()
255 options.invalidate();
260 protected void tokChanged_actionPerformed()
262 if (tok.getText().trim().length() > 0)
266 current.token = tok.getText().trim();
273 public void argSetModified(Object modifiedElement, boolean b)
275 updated = updated | b;