Back to index

wims  3.65+svn20090927
ItemEditor.java
Go to the documentation of this file.
00001 package rene.dialogs;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.FlowLayout;
00005 import java.awt.Frame;
00006 import java.awt.GridLayout;
00007 import java.awt.Panel;
00008 import java.awt.event.ItemEvent;
00009 import java.awt.event.ItemListener;
00010 import java.util.Vector;
00011 
00012 import rene.gui.ButtonAction;
00013 import rene.gui.CloseDialog;
00014 import rene.gui.Global;
00015 import rene.gui.MyLabel;
00016 import rene.gui.MyList;
00017 import rene.gui.MyPanel;
00018 import rene.gui.Panel3D;
00019 
00025 public class ItemEditor extends CloseDialog
00026        implements ItemListener
00027 {      Frame F;
00029        MyList L;
00031        boolean Aborted=true;
00033        Vector V;
00035        ItemPanel P;
00037        String Name;
00039        int Displayed=-1;
00041        public final static int NONE=0,SAVE=1,LOAD=2;
00043        int Action=NONE;
00044 
00050        public ItemEditor (Frame f, 
00051               ItemPanel p, Vector v, String name, String prompt)
00052        {      this(f,p,v,name,prompt,true,true,false,"");
00053        }
00054        public ItemEditor (Frame f, 
00055               ItemPanel p, Vector v, String name, String prompt,
00056               boolean allowChanges, boolean allowReorder, boolean allowSave,
00057               String extraButton)
00058        {      super(f,Global.name(name+".title"),true);
00059               Name=name; F=f; P=p;
00060               setLayout(new BorderLayout());
00061               
00062               // Title String:
00063               Panel title=new MyPanel();
00064               title.add(new MyLabel(prompt));
00065               add("North",title);
00066               
00067               // Center panel:
00068               Panel center=new MyPanel();
00069               center.setLayout(new BorderLayout(5,5));
00070               
00071               // Element List:
00072               center.add("West",L=new MyList(10));
00073               L.addItemListener(this);
00074               
00075               // Editor Panel:
00076               Panel cp=new MyPanel();
00077               cp.setLayout(new BorderLayout());
00078               cp.add("North",P);
00079               cp.add("Center",new MyPanel());
00080               center.add("Center",cp);
00081 
00082               add("Center",new Panel3D(center));
00083 
00084               // Buttons:
00085               Panel buttons=new MyPanel();
00086               buttons.setLayout(new GridLayout(0,1));
00087 
00088               if (allowChanges)
00089               {      Panel buttons1=new MyPanel();
00090                      buttons1.add(new ButtonAction(this,
00091                             Global.name("itemeditor.insert"),"Insert"));
00092                      buttons1.add(new ButtonAction(this,
00093                             Global.name("itemeditor.new"),"New"));
00094                      buttons1.add(new ButtonAction(this,
00095                             Global.name("itemeditor.delete"),"Delete"));
00096                      buttons.add(buttons1);
00097               }
00098               
00099               if (allowReorder)
00100               {      Panel buttons2=new MyPanel();
00101                      buttons2.add(new ButtonAction(this,
00102                             Global.name("itemeditor.down"),"Down"));
00103                      buttons2.add(new ButtonAction(this,
00104                             Global.name("itemeditor.up"),"Up"));
00105                      buttons.add(buttons2);
00106               }
00107               
00108               Panel buttons3=new MyPanel();
00109               buttons3.setLayout(new FlowLayout(FlowLayout.RIGHT));
00110               buttons3.add(new ButtonAction(this,
00111                      Global.name("OK"),"OK"));
00112               buttons3.add(new ButtonAction(this,
00113                      Global.name("abort"),"Close"));
00114               buttons.add(buttons3);
00115 
00116               if (allowSave)
00117               {      Panel buttons4=new MyPanel();
00118                      buttons4.setLayout(new FlowLayout(FlowLayout.RIGHT));
00119                      buttons4.add(new ButtonAction(this,
00120                             Global.name("save"),"Save"));
00121                      buttons4.add(new ButtonAction(this,
00122                             Global.name("load"),"Load"));
00123                      if (!extraButton.equals(""))
00124                      {      buttons4.add(new ButtonAction(this,
00125                                    extraButton,"Extra"));
00126                      }
00127                      buttons.add(buttons4);
00128               }
00129               
00130               add("South",new Panel3D(buttons));
00131 
00132               V=new Vector();
00133               for (int i=0; i<v.size(); i++) V.addElement(v.elementAt(i));
00134               init();
00135               
00136               pack();
00137        }
00138        
00142        public void init ()
00143        {      for (int i=0; i<V.size(); i++)
00144               {      ItemEditorElement e=(ItemEditorElement)V.elementAt(i);
00145                      L.add(e.getName());
00146               }
00147               if (V.size()>0)
00148               {      L.select(0);
00149                      select();
00150               }
00151        }
00152 
00156        public void itemStateChanged (ItemEvent e)
00157        {      if (e.getSource()==L)
00158               {      if (Displayed>=0) define(Displayed);
00159                      select();
00160               }
00161        }
00162 
00166        public void select ()       
00167        {      int i=L.getSelectedIndex();
00168               if (i<0) return;
00169               P.display((ItemEditorElement)V.elementAt(i));
00170               Displayed=i;
00171        }
00172 
00173        public void doAction (String o)
00174        {      if (o.equals("Delete"))
00175               {      delete();
00176               }
00177               else if (o.equals("Insert"))
00178               {      insert();
00179               }
00180               else if (o.equals("New"))
00181               {      P.newElement();
00182               }
00183               else if (o.equals("Up"))
00184               {      up();
00185               }
00186               else if (o.equals("Down"))
00187               {      down();
00188               }
00189               else if (o.equals("OK"))
00190               {      noteSize(Name);
00191                      define();
00192                      Aborted=false;
00193                      doclose();
00194               }
00195               else if (o.equals("Help"))
00196               {      P.help();
00197               }
00198               else if (o.equals("Save"))
00199               {      define();
00200                      Action=SAVE;
00201                      Aborted=false;
00202                      doclose();
00203               }
00204               else if (o.equals("Load"))
00205               {      define();
00206                      Action=LOAD;
00207                      Aborted=false;
00208                      doclose();
00209               }
00210               else if (o.equals("Extra"))
00211               {      if (P.extra(V))
00212                      {      Aborted=false;
00213                             doclose();
00214                      }
00215               }
00216               else super.doAction(o);
00217        }
00218        
00222        void insert ()
00223        {      String name=P.getName();
00224               int Selected=L.getSelectedIndex();
00225               if (Selected<0) Selected=0;
00226               while (find(name)) name=name+"*";
00227               P.setName(name);
00228               ItemEditorElement e=P.getElement();
00229               L.add(e.getName(),Selected);
00230               L.select(Selected);
00231               V.insertElementAt(e,Selected);
00232        }
00233 
00237        void define (int Selected)
00238        {      String name=P.getName();
00239               if (name.equals("")) return;
00240               if (!L.getItem(Selected).equals(name))
00241                      L.replaceItem(name,Selected);
00242               V.setElementAt(P.getElement(),Selected);
00243               P.notifyChange(V,Selected);
00244        }
00245        
00249        void define ()
00250        {      int Selected=L.getSelectedIndex();
00251               if (Selected<0) return;
00252               define(Selected);
00253               L.select(Selected);
00254        }
00255 
00260        boolean find (String name)
00261        {      int i;
00262               for (i=0; i<V.size(); i++)
00263               {      ItemEditorElement t=((ItemEditorElement)V.elementAt(i));
00264                      if (t.getName().equals(name)) return true;
00265               }
00266               return false;
00267        }
00268 
00272        void delete ()
00273        {      int Selected=L.getSelectedIndex();
00274               if (Selected<0) return;
00275               V.removeElementAt(Selected);
00276               L.remove(Selected);
00277               if (L.getItemCount()==0) return;
00278               if (Selected>=L.getItemCount()) Selected--;
00279               L.select(Selected);
00280               select();
00281        }
00282 
00286        void down ()
00287        {      define();
00288               int Selected=L.getSelectedIndex();
00289               if (Selected<0 || Selected+1>=V.size()) return;
00290               ItemEditorElement 
00291                      now=(ItemEditorElement)V.elementAt(Selected),
00292                      next=(ItemEditorElement)V.elementAt(Selected+1);
00293               V.setElementAt(next,Selected); 
00294               V.setElementAt(now,Selected+1);
00295               L.replaceItem(next.getName(),Selected); 
00296               L.replaceItem(now.getName(),Selected+1);
00297               Selected=Selected+1;
00298               L.select(Selected);
00299               select();
00300        }
00301 
00305        void up ()
00306        {      define();
00307               int Selected=L.getSelectedIndex();
00308               if (Selected<=0) return;
00309               ItemEditorElement 
00310                      now=(ItemEditorElement)V.elementAt(Selected),
00311                      prev=(ItemEditorElement)V.elementAt(Selected-1);
00312               V.setElementAt(prev,Selected); 
00313               V.setElementAt(now,Selected-1);
00314               L.replaceItem(prev.getName(),Selected); 
00315               L.replaceItem(now.getName(),Selected-1);
00316               Selected=Selected-1;
00317               L.select(Selected);
00318               select();
00319        }
00320 
00324        public boolean isAborted ()
00325        {      return Aborted;
00326        }
00327 
00331        public Vector getElements ()
00332        {      return V;
00333        }
00334        
00339        public int getAction ()
00340        {      return Action;
00341        }
00342 }