2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
23 import java.util.ArrayList;
24 import java.util.Hashtable;
26 import javax.swing.JPanel;
27 import javax.swing.event.ListSelectionEvent;
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;
35 import net.miginfocom.swing.MigLayout;
37 public class RestInputParamEditDialog extends GRestInputParamEditDialog
38 implements OptsParametersContainerI
40 Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
42 Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
44 Hashtable<String, OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
46 private InputType getTypeFor(String name)
50 return (InputType) (typeclass.get(name).getConstructor()
55 "Unexpected exception when instantiating rest input type.");
63 JalviewDialog frame = new JalviewDialog()
67 protected void raiseClosed()
73 protected void okPressed()
75 reply = JvOptionPane.OK_OPTION;
79 protected void cancelPressed()
81 reply = JvOptionPane.CANCEL_OPTION;
86 InputType old, current;
88 public RestInputParamEditDialog(
89 RestServiceEditorPane restServiceEditorPane,
90 RestServiceDescription currentservice, InputType toedit)
92 initFor(restServiceEditorPane, currentservice, toedit);
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
99 private void initFor(RestServiceEditorPane restServiceEditorPane,
100 RestServiceDescription currentservice, InputType toedit)
102 okcancel.add(frame.cancel);
103 okcancel.add(frame.ok);
104 frame.initDialogFrame(dpane, true, true,
105 "Edit parameter for service " + currentservice.getName(), 600,
109 reply = JvOptionPane.CANCEL_OPTION;
116 updated = updated && reply == JvOptionPane.OK_OPTION;
120 public RestInputParamEditDialog(
121 RestServiceEditorPane restServiceEditorPane,
122 RestServiceDescription currentservice, String string)
124 initFor(restServiceEditorPane, currentservice, null);
126 frame.waitForInput();
129 private void setStateFor(InputType current)
131 tok.setText(current.token);
132 OptsAndParamsPage opanp = opanps.get(current.getURLtokenPrefix());
133 for (OptionI ops : current.getOptions())
135 if (ops instanceof ParameterI)
137 opanp.setParameter((ParameterI) ops);
141 if (ops.getValue() != null && ops.getValue().length() > 0)
143 opanp.selectOption(ops, ops.getValue());
147 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
148 type_SelectionChangedActionPerformed(null);
151 private void updateCurrentType()
153 if (typeList.getSelectedValue() != null)
155 InputType newType = getTypeFor((String) typeList.getSelectedValue());
158 newType.token = tok.getText().trim();
161 newType.configureFromArgumentI(opanps
162 .get(newType.getURLtokenPrefix()).getCurrentSettings());
165 } catch (InvalidArgumentException ex)
168 "IMPLEMENTATION ERROR: Invalid argument for type : "
169 + typeList.getSelectedValue() + "\n");
170 ex.printStackTrace();
177 private void initTypeLists()
179 ArrayList<String> types = new ArrayList<String>();
180 // populate type list
181 for (Class type : RestServiceDescription.getInputTypes())
184 InputType jtype = null;
187 JPanel inopts = new JPanel(new MigLayout());
188 ArrayList<JPanel> opts = new ArrayList<JPanel>(),
189 prms = new ArrayList<JPanel>();
190 jtype = (InputType) (type.getConstructor().newInstance());
191 typeclass.put(jtype.getURLtokenPrefix(), type);
192 // and populate parameters from this type
193 OptsAndParamsPage opanp = new OptsAndParamsPage(this, true);
194 opanps.put(jtype.getURLtokenPrefix(), opanp);
195 for (OptionI opt : jtype.getOptions())
198 if (opt instanceof ParameterI)
200 prms.add(opanp.addParameter((ParameterI) opt));
204 opts.add(opanp.addOption(opt));
207 // then tag the params at the end of the options.
208 for (JPanel pnl : prms)
212 typeopts.put(jtype.getURLtokenPrefix(), opts);
213 types.add(jtype.getURLtokenPrefix());
214 } catch (Throwable x)
217 "Unexpected exception when instantiating rest input type.");
221 typeList.setListData(types.toArray());
226 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
229 String typen = (String) typeList.getSelectedValue();
230 if (typeopts.get(typen) != null)
232 for (JPanel opt : typeopts.get(typen))
235 options.add(opt, "wrap");
237 options.invalidate();
238 optionsPanel.setVisible(true);
242 optionsPanel.setVisible(false);
248 boolean updated = false;
250 public boolean wasUpdated()
256 public void refreshParamLayout()
258 options.invalidate();
263 protected void tokChanged_actionPerformed()
265 if (tok.getText().trim().length() > 0)
269 current.token = tok.getText().trim();
276 public void argSetModified(Object modifiedElement, boolean b)
278 updated = updated | b;