1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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 com.sun.tools.corba.se.idl.InvalidArgument;
31 import net.miginfocom.swing.MigLayout;
33 import jalview.jbgui.GRestInputParamEditDialog;
34 import jalview.ws.params.ArgumentI;
35 import jalview.ws.params.InvalidArgumentException;
36 import jalview.ws.params.OptionI;
37 import jalview.ws.params.ParameterI;
38 import jalview.ws.rest.InputType;
39 import jalview.ws.rest.RestServiceDescription;
41 public class RestInputParamEditDialog extends GRestInputParamEditDialog
42 implements OptsParametersContainerI
44 Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
46 Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
48 Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
50 private InputType getTypeFor(String name)
54 return (InputType) (typeclass.get(name).getConstructor()
59 .println("Unexpected exception when instantiating rest input type.");
66 JalviewDialog frame = new JalviewDialog()
70 protected void raiseClosed()
76 protected void okPressed()
78 reply=JOptionPane.OK_OPTION;
82 protected void cancelPressed()
84 reply=JOptionPane.CANCEL_OPTION;
88 InputType old,current;
89 public RestInputParamEditDialog(
90 RestServiceEditorPane restServiceEditorPane,
91 RestServiceDescription currentservice, InputType toedit)
93 initFor(restServiceEditorPane, currentservice, toedit);
95 // TODO: warn user if they are about to overwrite an existing parameter 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
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 "+currentservice.getName(), 600,800);
106 reply=JOptionPane.CANCEL_OPTION;
112 updated = updated && reply==JOptionPane.OK_OPTION;
116 public RestInputParamEditDialog(
117 RestServiceEditorPane restServiceEditorPane,
118 RestServiceDescription currentservice, String string)
120 initFor(restServiceEditorPane, currentservice,null);
122 frame.waitForInput();
125 private void setStateFor(InputType current)
127 tok.setText(current.token);
128 OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
129 for (OptionI ops:current.getOptions())
131 if (ops instanceof ParameterI)
133 opanp.setParameter((ParameterI) ops);
135 if (ops.getValue()!=null && ops.getValue().length()>0) {
136 opanp.selectOption(ops, ops.getValue());
140 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
141 type_SelectionChangedActionPerformed(null);
143 private void updateCurrentType()
145 if (typeList.getSelectedValue()!=null) {
146 InputType newType = getTypeFor((String)typeList.getSelectedValue());
148 newType.token = tok.getText().trim();
150 newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
153 } catch (InvalidArgumentException ex) {
154 System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
155 ex.printStackTrace();
161 private void initTypeLists()
163 ArrayList<String> types=new ArrayList<String>();
164 // populate type list
165 for (Class type : RestServiceDescription.getInputTypes())
168 InputType jtype = null;
171 JPanel inopts = new JPanel(new MigLayout());
172 ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
173 jtype = (InputType) (type.getConstructor().newInstance(null));
174 typeclass.put(jtype.getURLtokenPrefix(), type);
175 // and populate parameters from this type
176 OptsAndParamsPage opanp = new OptsAndParamsPage(this,true);
177 opanps.put(jtype.getURLtokenPrefix(),opanp);
178 for (OptionI opt : jtype.getOptions())
181 if (opt instanceof ParameterI)
183 prms.add(opanp.addParameter((ParameterI) opt));
187 opts.add(opanp.addOption(opt));
190 // then tag the params at the end of the options.
191 for (JPanel pnl : prms)
195 typeopts.put(jtype.getURLtokenPrefix(), opts);
196 types.add(jtype.getURLtokenPrefix());
197 } catch (Throwable x)
200 .println("Unexpected exception when instantiating rest input type.");
204 typeList.setListData(types.toArray());
209 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
212 String typen = (String) typeList.getSelectedValue();
213 if (typeopts.get(typen) != null)
215 for (JPanel opt : typeopts.get(typen))
218 options.add(opt,"wrap");
220 options.invalidate();
221 optionsPanel.setVisible(true);
225 optionsPanel.setVisible(false);
231 boolean updated=false;
232 public boolean wasUpdated()
238 public void refreshParamLayout()
240 options.invalidate();
245 protected void tokChanged_actionPerformed()
247 if (tok.getText().trim().length()>0)
251 current.token = tok.getText().trim();
257 public void argSetModified(Object modifiedElement, boolean b)
259 updated = updated | b;