71fdb5d7b64f20f34dd826ff425ef906c8f312e7
[jalview.git] / src / jalview / gui / RestInputParamEditDialog.java
1 package jalview.gui;
2
3 import java.util.ArrayList;
4 import java.util.Hashtable;
5
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;
11
12 import com.sun.tools.corba.se.idl.InvalidArgument;
13
14 import net.miginfocom.swing.MigLayout;
15
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;
23
24 public class RestInputParamEditDialog extends GRestInputParamEditDialog
25         implements OptsParametersContainerI
26 {
27   Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
28
29   Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
30
31   Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
32   
33   private InputType getTypeFor(String name)
34   {
35     try
36     {
37       return (InputType) (typeclass.get(name).getConstructor()
38               .newInstance(null));
39     } catch (Throwable x)
40     {
41       System.err
42               .println("Unexpected exception when instantiating rest input type.");
43       x.printStackTrace();
44     }
45     return null;
46   }
47
48   int reply;
49   JalviewDialog frame = new JalviewDialog()
50   {
51     
52     @Override
53     protected void raiseClosed()
54     {
55       
56     }
57     
58     @Override
59     protected void okPressed()
60     {
61       reply=JOptionPane.OK_OPTION; 
62     }
63     
64     @Override
65     protected void cancelPressed()
66     {
67       reply=JOptionPane.CANCEL_OPTION; 
68       
69     }
70   };
71   InputType old,current;
72   public RestInputParamEditDialog(
73           RestServiceEditorPane restServiceEditorPane,
74           RestServiceDescription currentservice, InputType toedit)
75   {
76     initFor(restServiceEditorPane, currentservice, toedit);
77     frame.waitForInput();
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
80   }
81   private void initFor(RestServiceEditorPane restServiceEditorPane,
82           RestServiceDescription currentservice, InputType toedit)
83   {
84     okcancel.add(frame.cancel);
85     okcancel.add(frame.ok);
86     frame.initDialogFrame(dpane, true, true, "Edit parameter for service "+currentservice.getName(), 600,800);
87     
88     initTypeLists();
89     reply=JOptionPane.CANCEL_OPTION;
90     old = toedit;
91     current = null;
92     if (old!=null) {
93       setStateFor(old);
94     }
95     updated = updated && reply==JOptionPane.OK_OPTION;
96     frame.validate();
97   }
98
99   public RestInputParamEditDialog(
100           RestServiceEditorPane restServiceEditorPane,
101           RestServiceDescription currentservice, String string)
102   {
103     initFor(restServiceEditorPane, currentservice,null);
104     tok.setText(string);
105     frame.waitForInput();
106   }
107
108   private void setStateFor(InputType current)
109   {
110     tok.setText(current.token);
111     OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
112     for (OptionI ops:current.getOptions())
113     {
114       if (ops instanceof ParameterI)
115       {
116         opanp.setParameter((ParameterI) ops);
117       } else {
118         if (ops.getValue()!=null && ops.getValue().length()>0) {
119           opanp.selectOption(ops, ops.getValue());
120         }
121       }
122     }
123     typeList.setSelectedValue(current.getURLtokenPrefix(), true);
124     type_SelectionChangedActionPerformed(null);
125   }
126   private void updateCurrentType()
127   {
128     if (typeList.getSelectedValue()!=null) {
129     InputType newType = getTypeFor((String)typeList.getSelectedValue());
130     if (newType!=null) {
131       newType.token = tok.getText().trim();
132       try {
133         newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
134         current=newType;
135         updated=true;
136       } catch (InvalidArgumentException ex) {
137         System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
138         ex.printStackTrace();
139       }
140     }
141     }
142     
143   }
144   private void initTypeLists()
145   {
146     ArrayList<String> types=new ArrayList<String>();
147     // populate type list
148     for (Class type : RestServiceDescription.getInputTypes())
149     {
150
151       InputType jtype = null;
152       try
153       {
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())
162         {
163
164           if (opt instanceof ParameterI)
165           {
166             prms.add(opanp.addParameter((ParameterI) opt));
167           }
168           else
169           {
170             opts.add(opanp.addOption(opt));
171           }
172         }
173         // then tag the params at the end of the options.
174         for (JPanel pnl : prms)
175         {
176           opts.add(pnl);
177         }
178         typeopts.put(jtype.getURLtokenPrefix(), opts);
179         types.add(jtype.getURLtokenPrefix());
180       } catch (Throwable x)
181       {
182         System.err
183                 .println("Unexpected exception when instantiating rest input type.");
184         x.printStackTrace();
185       }
186     }
187     typeList.setListData(types.toArray());
188     
189   }
190
191   @Override
192   protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
193   {
194     options.removeAll();
195     String typen = (String) typeList.getSelectedValue();
196     if (typeopts.get(typen) != null)
197     {
198       for (JPanel opt : typeopts.get(typen))
199       {
200         opt.setOpaque(true);
201         options.add(opt,"wrap");
202       }
203       options.invalidate();
204       optionsPanel.setVisible(true);
205     }
206     else
207     {
208       optionsPanel.setVisible(false);
209     }
210     dpane.revalidate();
211     updateCurrentType();
212   }
213
214   boolean updated=false;
215   public boolean wasUpdated()
216   {
217     return updated;
218   }
219
220   @Override
221   public void refreshParamLayout()
222   {
223     options.invalidate();
224     dpane.revalidate();
225   }
226
227   @Override
228   protected void tokChanged_actionPerformed()
229   {
230     if (tok.getText().trim().length()>0)
231     {
232       if (current!=null)
233       {
234         current.token = tok.getText().trim();
235         updated = true; 
236       } 
237     }
238   }
239   @Override
240   public void argSetModified(Object modifiedElement, boolean b)
241   {
242     updated = updated | b;
243     if (updated)
244     {
245       updateCurrentType();
246     }
247   }
248
249 }