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
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 *******************************************************************************/
20 import java.util.ArrayList;
21 import java.util.Hashtable;
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;
29 import net.miginfocom.swing.MigLayout;
31 import jalview.jbgui.GRestInputParamEditDialog;
32 import jalview.ws.params.ArgumentI;
33 import jalview.ws.params.InvalidArgumentException;
34 import jalview.ws.params.OptionI;
35 import jalview.ws.params.ParameterI;
36 import jalview.ws.rest.InputType;
37 import jalview.ws.rest.RestServiceDescription;
39 public class RestInputParamEditDialog extends GRestInputParamEditDialog
40 implements OptsParametersContainerI
42 Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
44 Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
46 Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
48 private InputType getTypeFor(String name)
52 return (InputType) (typeclass.get(name).getConstructor()
57 .println("Unexpected exception when instantiating rest input type.");
64 JalviewDialog frame = new JalviewDialog()
68 protected void raiseClosed()
74 protected void okPressed()
76 reply=JOptionPane.OK_OPTION;
80 protected void cancelPressed()
82 reply=JOptionPane.CANCEL_OPTION;
86 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 because they have used the same name when editing a different parameter.
94 // TODO: make any press of the return key cause 'OK' to be pressed
96 private void initFor(RestServiceEditorPane restServiceEditorPane,
97 RestServiceDescription currentservice, InputType toedit)
99 okcancel.add(frame.cancel);
100 okcancel.add(frame.ok);
101 frame.initDialogFrame(dpane, true, true, "Edit parameter for service "+currentservice.getName(), 600,800);
104 reply=JOptionPane.CANCEL_OPTION;
110 updated = updated && reply==JOptionPane.OK_OPTION;
114 public RestInputParamEditDialog(
115 RestServiceEditorPane restServiceEditorPane,
116 RestServiceDescription currentservice, String string)
118 initFor(restServiceEditorPane, currentservice,null);
120 frame.waitForInput();
123 private void setStateFor(InputType current)
125 tok.setText(current.token);
126 OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
127 for (OptionI ops:current.getOptions())
129 if (ops instanceof ParameterI)
131 opanp.setParameter((ParameterI) ops);
133 if (ops.getValue()!=null && ops.getValue().length()>0) {
134 opanp.selectOption(ops, ops.getValue());
138 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
139 type_SelectionChangedActionPerformed(null);
141 private void updateCurrentType()
143 if (typeList.getSelectedValue()!=null) {
144 InputType newType = getTypeFor((String)typeList.getSelectedValue());
146 newType.token = tok.getText().trim();
148 newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
151 } catch (InvalidArgumentException ex) {
152 System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
153 ex.printStackTrace();
159 private void initTypeLists()
161 ArrayList<String> types=new ArrayList<String>();
162 // populate type list
163 for (Class type : RestServiceDescription.getInputTypes())
166 InputType jtype = null;
169 JPanel inopts = new JPanel(new MigLayout());
170 ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
171 jtype = (InputType) (type.getConstructor().newInstance(null));
172 typeclass.put(jtype.getURLtokenPrefix(), type);
173 // and populate parameters from this type
174 OptsAndParamsPage opanp = new OptsAndParamsPage(this,true);
175 opanps.put(jtype.getURLtokenPrefix(),opanp);
176 for (OptionI opt : jtype.getOptions())
179 if (opt instanceof ParameterI)
181 prms.add(opanp.addParameter((ParameterI) opt));
185 opts.add(opanp.addOption(opt));
188 // then tag the params at the end of the options.
189 for (JPanel pnl : prms)
193 typeopts.put(jtype.getURLtokenPrefix(), opts);
194 types.add(jtype.getURLtokenPrefix());
195 } catch (Throwable x)
198 .println("Unexpected exception when instantiating rest input type.");
202 typeList.setListData(types.toArray());
207 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
210 String typen = (String) typeList.getSelectedValue();
211 if (typeopts.get(typen) != null)
213 for (JPanel opt : typeopts.get(typen))
216 options.add(opt,"wrap");
218 options.invalidate();
219 optionsPanel.setVisible(true);
223 optionsPanel.setVisible(false);
229 boolean updated=false;
230 public boolean wasUpdated()
236 public void refreshParamLayout()
238 options.invalidate();
243 protected void tokChanged_actionPerformed()
245 if (tok.getText().trim().length()>0)
249 current.token = tok.getText().trim();
255 public void argSetModified(Object modifiedElement, boolean b)
257 updated = updated | b;