3 import java.util.ArrayList;
4 import java.util.Hashtable;
6 import javax.swing.JDialog;
7 import javax.swing.JInternalFrame;
8 import javax.swing.JOptionPane;
9 import javax.swing.JPanel;
10 import javax.swing.event.ListSelectionEvent;
12 import com.sun.tools.corba.se.idl.InvalidArgument;
14 import net.miginfocom.swing.MigLayout;
16 import jalview.jbgui.GRestInputParamEditDialog;
17 import jalview.ws.params.ArgumentI;
18 import jalview.ws.params.InvalidArgumentException;
19 import jalview.ws.params.OptionI;
20 import jalview.ws.params.ParameterI;
21 import jalview.ws.rest.InputType;
22 import jalview.ws.rest.RestServiceDescription;
24 public class RestInputParamEditDialog extends GRestInputParamEditDialog
25 implements OptsParametersContainerI
27 Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
29 Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
31 Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
33 private InputType getTypeFor(String name)
37 return (InputType) (typeclass.get(name).getConstructor()
42 .println("Unexpected exception when instantiating rest input type.");
49 JalviewDialog frame = new JalviewDialog()
53 protected void raiseClosed()
59 protected void okPressed()
61 reply=JOptionPane.OK_OPTION;
65 protected void cancelPressed()
67 reply=JOptionPane.CANCEL_OPTION;
71 InputType old,current;
72 public RestInputParamEditDialog(
73 RestServiceEditorPane restServiceEditorPane,
74 RestServiceDescription currentservice, InputType toedit)
76 initFor(restServiceEditorPane, currentservice, toedit);
78 // TODO: warn user if they are about to overwrite an existing parameter because they have used the same name when editing a different parameter.
79 // TODO: make any press of the return key cause 'OK' to be pressed
81 private void initFor(RestServiceEditorPane restServiceEditorPane,
82 RestServiceDescription currentservice, InputType toedit)
84 okcancel.add(frame.cancel);
85 okcancel.add(frame.ok);
86 frame.initDialogFrame(dpane, true, true, "Edit parameter for service "+currentservice.getName(), 600,800);
89 reply=JOptionPane.CANCEL_OPTION;
95 updated = updated && reply==JOptionPane.OK_OPTION;
99 public RestInputParamEditDialog(
100 RestServiceEditorPane restServiceEditorPane,
101 RestServiceDescription currentservice, String string)
103 initFor(restServiceEditorPane, currentservice,null);
105 frame.waitForInput();
108 private void setStateFor(InputType current)
110 tok.setText(current.token);
111 OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
112 for (OptionI ops:current.getOptions())
114 if (ops instanceof ParameterI)
116 opanp.setParameter((ParameterI) ops);
118 if (ops.getValue()!=null && ops.getValue().length()>0) {
119 opanp.selectOption(ops, ops.getValue());
123 typeList.setSelectedValue(current.getURLtokenPrefix(), true);
124 type_SelectionChangedActionPerformed(null);
126 private void updateCurrentType()
128 if (typeList.getSelectedValue()!=null) {
129 InputType newType = getTypeFor((String)typeList.getSelectedValue());
131 newType.token = tok.getText().trim();
133 newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
136 } catch (InvalidArgumentException ex) {
137 System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
138 ex.printStackTrace();
144 private void initTypeLists()
146 ArrayList<String> types=new ArrayList<String>();
147 // populate type list
148 for (Class type : RestServiceDescription.getInputTypes())
151 InputType jtype = null;
154 JPanel inopts = new JPanel(new MigLayout());
155 ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
156 jtype = (InputType) (type.getConstructor().newInstance(null));
157 typeclass.put(jtype.getURLtokenPrefix(), type);
158 // and populate parameters from this type
159 OptsAndParamsPage opanp = new OptsAndParamsPage(this,true);
160 opanps.put(jtype.getURLtokenPrefix(),opanp);
161 for (OptionI opt : jtype.getOptions())
164 if (opt instanceof ParameterI)
166 prms.add(opanp.addParameter((ParameterI) opt));
170 opts.add(opanp.addOption(opt));
173 // then tag the params at the end of the options.
174 for (JPanel pnl : prms)
178 typeopts.put(jtype.getURLtokenPrefix(), opts);
179 types.add(jtype.getURLtokenPrefix());
180 } catch (Throwable x)
183 .println("Unexpected exception when instantiating rest input type.");
187 typeList.setListData(types.toArray());
192 protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
195 String typen = (String) typeList.getSelectedValue();
196 if (typeopts.get(typen) != null)
198 for (JPanel opt : typeopts.get(typen))
201 options.add(opt,"wrap");
203 options.invalidate();
204 optionsPanel.setVisible(true);
208 optionsPanel.setVisible(false);
214 boolean updated=false;
215 public boolean wasUpdated()
221 public void refreshParamLayout()
223 options.invalidate();
228 protected void tokChanged_actionPerformed()
230 if (tok.getText().trim().length()>0)
234 current.token = tok.getText().trim();
240 public void argSetModified(Object modifiedElement, boolean b)
242 updated = updated | b;