2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import java.util.ArrayList;
22 import java.util.Hashtable;
24 import javax.swing.JOptionPane;
25 import javax.swing.JPanel;
26 import javax.swing.event.ListSelectionEvent;
28 import net.miginfocom.swing.MigLayout;
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;
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 .println("Unexpected exception when instantiating rest input type.");
63 JalviewDialog frame = new JalviewDialog()
67 protected void raiseClosed()
73 protected void okPressed()
75 reply = JOptionPane.OK_OPTION;
79 protected void cancelPressed()
81 reply = JOptionPane.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, "Edit parameter for service "
105 + currentservice.getName(), 600, 800);
108 reply = JOptionPane.CANCEL_OPTION;
115 updated = updated && reply == JOptionPane.OK_OPTION;
119 public RestInputParamEditDialog(
120 RestServiceEditorPane restServiceEditorPane,
121 RestServiceDescription currentservice, String string)
123 initFor(restServiceEditorPane, currentservice, null);
125 frame.waitForInput();
128 private void setStateFor(InputType current)
130 tok.setText(current.token);
131 OptsAndParamsPage opanp = opanps.get(current.getURLtokenPrefix());
132 for (OptionI ops : current.getOptions())
134 if (ops instanceof ParameterI)
136 opanp.setParameter((ParameterI) ops);
140 if (ops.getValue() != null && ops.getValue().length() > 0)
142 opanp.selectOption(ops, ops.getValue());
146 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
147 type_SelectionChangedActionPerformed(null);
150 private void updateCurrentType()
152 if (typeList.getSelectedValue() != null)
154 InputType newType = getTypeFor((String) typeList.getSelectedValue());
157 newType.token = tok.getText().trim();
160 newType.configureFromArgumentI(opanps.get(
161 newType.getURLtokenPrefix()).getCurrentSettings());
164 } catch (InvalidArgumentException ex)
167 .println("IMPLEMENTATION ERROR: Invalid argument for type : "
168 + typeList.getSelectedValue() + "\n");
169 ex.printStackTrace();
176 private void initTypeLists()
178 ArrayList<String> types = new ArrayList<String>();
179 // populate type list
180 for (Class type : RestServiceDescription.getInputTypes())
183 InputType jtype = null;
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())
196 if (opt instanceof ParameterI)
198 prms.add(opanp.addParameter((ParameterI) opt));
202 opts.add(opanp.addOption(opt));
205 // then tag the params at the end of the options.
206 for (JPanel pnl : prms)
210 typeopts.put(jtype.getURLtokenPrefix(), opts);
211 types.add(jtype.getURLtokenPrefix());
212 } catch (Throwable x)
215 .println("Unexpected exception when instantiating rest input type.");
219 typeList.setListData(types.toArray());
224 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
227 String typen = (String) typeList.getSelectedValue();
228 if (typeopts.get(typen) != null)
230 for (JPanel opt : typeopts.get(typen))
233 options.add(opt, "wrap");
235 options.invalidate();
236 optionsPanel.setVisible(true);
240 optionsPanel.setVisible(false);
246 boolean updated = false;
248 public boolean wasUpdated()
254 public void refreshParamLayout()
256 options.invalidate();
261 protected void tokChanged_actionPerformed()
263 if (tok.getText().trim().length() > 0)
267 current.token = tok.getText().trim();
274 public void argSetModified(Object modifiedElement, boolean b)
276 updated = updated | b;