Back to index

wims  3.65+svn20090927
IconBar.java
Go to the documentation of this file.
00001 package rene.gui;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import java.io.*;
00006 import java.util.*;
00007 
00008 class SaveColor extends Color
00009 {      public SaveColor (int red, int green, int blue)
00010        {      super(red>0?red:0,green>0?green:0,blue>0?blue:0);
00011        }
00012 }
00013 
00018 interface IconBarElement
00019 {      public int width ();
00020        public void setPosition (int x, int y);
00021        public Point getPosition ();
00022        public void setEnabled (boolean flag);
00023        public String getName ();
00024 }
00025 
00030 class Separator extends Panel 
00031        implements IconBarElement
00032 {      final int Size=6;
00033        public Separator (IconBar bar)
00034        {      if (bar.Vertical)
00035                      setSize(BasicIcon.Size,Size);
00036               else
00037                      setSize(Size,BasicIcon.Size);      
00038        }
00039        public int width ()
00040        {      return Size;
00041        }
00042        public void setPosition (int x, int y)
00043        {      setLocation(x,y);
00044        }
00045        public Point getPosition () { return new Point(0,0); }
00046        public void setEnabled (boolean flag) {}
00047        public String getName () { return ""; }
00048        public void paint (Graphics g)
00049        {      g.setColor(getBackground());
00050               if (Global.getParameter("iconbar.showseparators",false))
00051                      g.fill3DRect(1,1,getSize().width-1,getSize().height-1,false);
00052               else
00053                      g.fillRect(1,1,getSize().width-1,getSize().height-1);
00054        }
00055 }
00056 
00062 class BasicIcon extends Panel
00063        implements MouseListener,IconBarElement,Runnable
00064 {      IconBar Bar;
00065        String Name;
00066        boolean Enabled; // Icon cannot be changed by user action.
00067        boolean On; // On or off are the basic stated of icons.
00068        boolean Focus=false;
00069        public static int Size=22; // the size of icons
00070        boolean MouseOver,MouseDown; // for display states during mouse action
00071        boolean Unset; // Unknown State!
00072 
00073        public BasicIcon (IconBar bar, String name)
00074        {      Bar=bar; Name=name; Enabled=true; On=false;
00075               addMouseListener(this);
00076               enableEvents(AWTEvent.KEY_EVENT_MASK);
00077               setSize(Size,Size);
00078        }
00079        
00080        public void processKeyEvent (KeyEvent e)
00081        {      Bar.getKey(e);
00082        }
00083 
00087        public void paint (Graphics g)
00088        {      if (MouseDown)
00089               {      g.setColor(getBackground());
00090                      g.fill3DRect(0,0,Size,Size,false);
00091               }
00092               else
00093               {      if (MouseOver)
00094                      {      if (On)
00095                             {      Color c=getBackground();
00096                                    g.setColor(
00097                                           new SaveColor(c.getRed()-30,c.getGreen()-30,c.getBlue()));
00098                             }
00099                             else g.setColor(getBackground());
00100                             g.fill3DRect(0,0,Size,Size,true);
00101                      }
00102                      else
00103                      {      if (On)
00104                             {      Color c=getBackground();
00105                                    g.setColor(c);
00106                                    g.fillRect(0,0,Size,Size);
00107                                    g.setColor(
00108                                           new SaveColor(c.getRed()-100,c.getGreen()-100,c.getBlue()));
00109                                    g.fillRect(3,3,Size-2,Size-2);
00110                                    g.setColor(
00111                                           new SaveColor(c.getRed()-50,c.getGreen()-50,c.getBlue()));
00112                                    g.fillRect(1,1,Size-2,Size-2);
00113                             }
00114                             else
00115                             {      g.setColor(getBackground());
00116                                    g.fillRect(0,0,Size,Size);
00117                             }
00118                      }
00119               }
00120               dopaint(g);   
00121               if (Unset)
00122               {      Color c=getBackground();
00123                      g.setColor(
00124                             new SaveColor(c.getRed()-100,c.getGreen(),c.getBlue()));
00125                      g.drawLine(0,0,Size,Size);
00126               }      
00127               if (Focus) showFocus(g);
00128        }
00129        
00130        public void showFocus (Graphics g)
00131        {      g.setColor(Color.white);
00132               g.drawRect(4,4,1,1);
00133               g.drawRect(Size-5,4,1,1);
00134               g.drawRect(4,Size-5,1,1);
00135               g.drawRect(Size-5,Size-5,1,1);
00136        }
00137        
00138        public void dopaint (Graphics g)
00139        {
00140        }
00141        
00142        public void update (Graphics g)
00143        {      paint(g);
00144        }
00145        
00149        public void mousePressed (MouseEvent e)
00150        {      if (!Enabled) return;
00151               MouseDown=true; repaint();
00152        }
00153 
00157        public void mouseReleased (MouseEvent e)
00158        {      if (!Enabled) return;
00159               MouseDown=false;
00160               Dimension d=getSize();
00161               if (e.getX()<0 || e.getX()>d.width ||
00162                      e.getY()<0 || e.getY()>d.height) 
00163               {      repaint(); return;
00164               }
00165               Unset=false;
00166               pressed(e); // call method for children to change states etc.
00167               repaint();
00168               T=null; // stop icon help thread
00169               // Notify Iconbar about activation:
00170               long time=System.currentTimeMillis();
00171               Bar.iconPressed(Name,e.isShiftDown(),e.isControlDown());
00172               // Necessary, since Java 1.4 does not report
00173               // MouseExited, if a modal dialog is active:
00174               time=System.currentTimeMillis()-time;
00175               if (MouseOver && time>1000) 
00176               {      MouseOver=false;
00177                      repaint();
00178               }
00179        }
00180        
00185        public void pressed (MouseEvent e)
00186        {
00187        }
00188        
00189        public void mouseClicked (MouseEvent e) {}
00190        
00191        Thread T;
00192        boolean Control;
00193        
00198        public synchronized void mouseEntered (MouseEvent e)
00199        {      if (T!=null) return;
00200               if (Enabled) MouseOver=true;
00201               repaint();
00202               if (!Global.getParameter("iconbar.showtips",true)) return;
00203               Control=e.isControlDown();
00204               T=new Thread(this);
00205               T.start();
00206        }
00207        
00211        public void run ()
00212        {      try
00213               {      Thread.sleep(1000);
00214               }
00215               catch (Exception e) {}
00216               if (T!=null)
00217               {      synchronized(this)
00218                      {      try
00219                             {      Point P=getLocationOnScreen();
00220                                    String help=Global.name("iconhelp."+Name,"");
00221                                    if (help.equals("") && Name.length()>1)
00222                                    {      help=Global.name("iconhelp."+
00223                                                  Name.substring(0,Name.length()-1)+"?","");
00224                                    }
00225                                    if (help.equals(""))
00226                                           help=Bar.getHelp(Name);
00227                                    if (help.equals(""))
00228                                           help=Global.name("iconhelp.nohelp","No help available");
00229                                    if (Control)
00230                                    {      String hc=Global.name("iconhelp.control."+Name,"");
00231                                           if (!hc.equals("")) help=hc;
00232                                    }
00233                                    Bar.displayHelp(this,help);
00234                             }
00235                             catch (Exception e) {}
00236                      }
00237                      try
00238                      {      Thread.sleep(5000);
00239                      }
00240                      catch (Exception e) {}
00241                      if (T!=null) Bar.removeHelp();
00242                      T=null;
00243               }
00244        }
00245 
00250        public synchronized void mouseExited (MouseEvent e)
00251        {      T=null;
00252               MouseOver=false;
00253               repaint();
00254               Bar.removeHelp();
00255        }
00256        
00257        // for the IconBarElement interface
00258        
00259        public int width ()
00260        {      return Size;
00261        }
00262        
00263        public void setPosition (int x, int y)
00264        {      setLocation(x,y);
00265        }
00266        
00267        public Point getPosition ()
00268        {      return getLocationOnScreen();
00269        }
00270        
00271        public void setEnabled (boolean flag) 
00272        {      if (Enabled==flag) return;
00273               Enabled=flag;
00274               repaint();
00275        }
00276        
00277        public String getName ()
00278        {      return Name;
00279        }
00280 
00281        public boolean hasFocus () { return Focus; }
00282        public void setFocus (boolean flag) { Focus=flag; repaint(); } 
00283        
00284        // needs to be removed:
00285        
00286        public boolean isSet ()
00287        {      return !Unset;
00288        }      
00289        public void unset (boolean flag)
00290        {      Unset=flag;
00291        }
00292        public void unset ()
00293        {      unset(true);
00294        }
00295 }
00296 
00301 class IconWithGif extends BasicIcon
00302 {      Image I;
00303        Color C;
00304        int W,H,X,Y;
00305        
00310        public IconWithGif (IconBar bar, String file)
00311        {      super(bar,file);
00312               String iconfile=getDisplay(file);
00313               if (!iconfile.equals("")) file=iconfile;
00314               try
00315               {      InputStream in=getClass().getResourceAsStream(
00316                             Bar.Resource+file+"."+Global.getParameter("icontype","gif"));
00317                      int pos=0;
00318                      int n=in.available();
00319                      byte b[]=new byte[20000];
00320                      while (n>0)
00321                      {      int k=in.read(b,pos,n);
00322                             if (k<0) break;
00323                             pos+=k;
00324                             n=in.available();
00325                      }
00326                      in.close();
00327                      I=Toolkit.getDefaultToolkit().createImage(b,0,pos);
00328                      MediaTracker T=new MediaTracker(bar);
00329                      T.addImage(I,0);
00330                      T.waitForAll();
00331               }
00332               catch (Exception e)
00333               {      try
00334                      {      I=getToolkit().getImage(file+"."+Global.getParameter("icontype","gif")); 
00335                             MediaTracker mt=new MediaTracker(this); 
00336                             mt.addImage(I,0); 
00337                             mt.waitForID(0); 
00338                             if (!(mt.checkID(0) && !mt.isErrorAny()))
00339                                    throw new Exception("");
00340                      }
00341                      catch (Exception ex) { I=null; return;}
00342               }
00343               W=I.getWidth(this);
00344               H=I.getHeight(this);
00345               X=Size/2-W/2;
00346               Y=Size/2-H/2;
00347        }
00348        
00349        public String getDisplay (String name)
00350        {      if (!name.endsWith(")")) return "";
00351               int n=name.lastIndexOf('(');
00352               if (n<0) return "";
00353               return name.substring(n+1,name.length()-1);
00354        }
00355        
00356        public IconWithGif (IconBar bar, String name, Color color)
00357        {      super(bar,name);
00358               C=color;
00359        }
00360        
00361        public void dopaint (Graphics g)   
00362        {      if (I!=null)
00363               {      if (W>getSize().width) g.drawImage(I,1,1,Size-2,Size-2,this);
00364                      else g.drawImage(I,X,Y,this);
00365               }
00366               else if (C!=null)
00367               {      g.setColor(C);
00368                      g.fillRect(3,3,Size-6,Size-6);
00369               }
00370               else
00371               {      g.setFont(new Font("Courier",Font.BOLD,Size/3));
00372                      FontMetrics fm=getFontMetrics(getFont());
00373                      String s=getDisplay(Name);
00374                      if (s.length()>3) s=s.substring(0,3);
00375                      int w=fm.stringWidth(s);
00376                      int h=fm.getHeight();
00377                      g.setColor(this.getForeground());
00378                      Graphics2D G=(Graphics2D)g;
00379                      G.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
00380                                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
00381                      G.drawString(s,Size/2-w/2,Size/2-h/2+fm.getAscent());
00382               }
00383        }
00384        
00385 }
00386 
00391 class MultipleIcon extends BasicIcon
00392 {      int N;
00393        Image I[];
00394        int Selected;
00395        int X[],Y[],W[],H[];
00396        
00397        public MultipleIcon (IconBar bar, String name, int number)
00398        {      super(bar,name);
00399               N=number;
00400               I=new Image[N];
00401               X=new int[N];
00402               Y=new int[N];
00403               W=new int[N];
00404               H=new int[N];
00405               MediaTracker T=new MediaTracker(bar);
00406               try
00407               {      for (int i=0; i<N; i++)
00408                      {      try
00409                             {      InputStream in=getClass().getResourceAsStream(
00410                                           Bar.Resource+name+i+"."+Global.getParameter("icontype","gif"));
00411                                    int pos=0;
00412                                    int n=in.available();
00413                                    byte b[]=new byte[20000];
00414                                    while (n>0)
00415                                    {      int k=in.read(b,pos,n);
00416                                           if (k<0) break;
00417                                           pos+=k;
00418                                           n=in.available();
00419                                    }
00420                                    in.close();
00421                                    I[i]=Toolkit.getDefaultToolkit().createImage(b,0,pos);
00422                                    T.addImage(I[i],i);
00423                             }
00424                             catch (Exception e)
00425                             {      I[i]=null;
00426                             }
00427                      }
00428                      T.waitForAll();
00429                      for (int i=0; i<N; i++)
00430                      {      W[i]=I[i].getWidth(this);
00431                             H[i]=I[i].getHeight(this);
00432                             X[i]=Size/2-W[i]/2;
00433                             Y[i]=Size/2-H[i]/2;
00434                      }
00435               }
00436               catch (Exception e)
00437               {      for (int i=0; i<N; i++) I[i]=null;
00438               }
00439        }
00440        
00441        public MultipleIcon (IconBar bar, String name)
00442        {      super(bar,name);
00443               Selected=0;
00444        }
00445 
00449        public void dopaint (Graphics g)
00450        {      if (I[Selected]!=null)
00451               {      if (W[Selected]>getSize().width) g.drawImage(I[Selected],1,1,Size-2,Size-2,this);
00452                      else g.drawImage(I[Selected],X[Selected],Y[Selected],this);
00453               }
00454        }
00455        
00459        public void pressed (MouseEvent e)
00460        {      if (e.isMetaDown())
00461               {      Selected--; 
00462                      if (Selected<0) Selected=N-1;
00463               }
00464               else
00465               {      Selected++; 
00466                      if (Selected>=N) Selected=0;
00467               }
00468        }
00469 
00470        public void setSelected (int s)
00471        {      if (Selected==s) return;
00472               Selected=s;
00473               repaint();
00474        }
00475        
00476        public int getSelected ()
00477        {      return Selected;
00478        }
00479 }
00480 
00485 class MultipleToggleIcon extends MultipleIcon
00486 {      public MultipleToggleIcon (IconBar bar, String name, int number)
00487        {      super(bar,name,number);
00488        }
00489        public void setState (boolean flag)
00490        {      On=flag;
00491               repaint();
00492        }
00493 }
00494 
00499 class ColorIcon extends MultipleIcon
00500 {      Color Colors[];
00501        public ColorIcon (IconBar bar, String name, Color colors[])
00502        {      super(bar,name);
00503               N=colors.length;
00504               Colors=colors;
00505        }
00506        public void dopaint (Graphics g)
00507        {      g.setColor(Colors[Selected]);
00508               g.fill3DRect(5,5,Size-10,Size-10,true);
00509        }      
00510 }
00511 
00516 class MultipleStringIcon extends MultipleIcon
00517 {      String S[];
00518        
00519        public MultipleStringIcon (IconBar bar, String name, String s[])
00520        {      super(bar,name);
00521               S=s;
00522               N=S.length;
00523        }
00524        
00525        public void dopaint (Graphics g)
00526        {      g.setColor(getForeground());
00527               Font font=new Font("Dialog",Font.PLAIN,Size*2/3);
00528               g.setFont(font);
00529               FontMetrics fm=getFontMetrics(font);
00530               int w=fm.stringWidth(S[Selected]);
00531               g.drawString(S[Selected],(Size-w)/2,Size-fm.getDescent());
00532        }
00533        
00534 }
00535 
00539 class OverflowButton extends Panel
00540 {      IconBar IB;
00541        boolean Left=true;
00542        
00543        public OverflowButton (IconBar ib, boolean left)
00544        {      IB=ib; Left=left;
00545               addMouseListener(new MouseAdapter()
00546                             {      public void mouseClicked (MouseEvent e)
00547                                    {      IB.setShifted(!Left);       
00548                                    }
00549                             }
00550                      );
00551        }
00552 
00553        public void paint (Graphics g)
00554        {      int size=BasicIcon.Size;
00555               g.setColor(getBackground());
00556               g.fill3DRect(0,0,10,size,true);
00557               g.setColor(getForeground());
00558               int x[]=new int[3],y[]=new int[3];
00559               if (Left)
00560               {      x[0]=2; x[1]=x[2]=8;
00561                      y[0]=size/2; y[1]=y[0]-6; y[2]=y[0]+6;
00562               }
00563               else
00564               {      x[0]=8; x[1]=x[2]=2;
00565                      y[0]=size/2; y[1]=y[0]-6; y[2]=y[0]+6;
00566               }
00567               g.fillPolygon(x,y,3);
00568        }
00569 }
00570 
00571 class PopupIcon extends BasicIcon
00572 {      
00573        public PopupIcon (IconBar bar, String name[])
00574        {      super(bar,name[0]);
00575        }
00576 }
00577 
00578 
00583 class ToggleIcon extends IconWithGif
00584 {      boolean State;
00585        private IconGroup G;
00586 
00587        public ToggleIcon (IconBar bar, String file, IconGroup g)
00588        {      super(bar,file);
00589               State=false; G=g;
00590        }
00591        
00592        public ToggleIcon (IconBar bar, String file, Color c, IconGroup g)
00593        {      super(bar,file,c);
00594               State=false; G=g;
00595        }
00596        
00597        public ToggleIcon (IconBar bar, String file)
00598        {      this(bar,file,null);
00599        }
00600        
00601        public void pressed (MouseEvent e)
00602        {      setState(!On);
00603        }
00604 
00605        public boolean getState () { return State; }
00606        
00607        public void setState (boolean state)
00608        {      if (G!=null) G.toggle(this);
00609               else
00610               {      if (On==state) { State=state; return; }
00611                      On=State=state;
00612                      repaint();
00613               }
00614        }
00615        
00616        public void unselect ()
00617        {      if (G!=null) G.unselect();
00618        }
00619        
00620        public void setStateInGroup (boolean state)
00621        {      if (On==state) { State=state; return; }
00622               On=State=state;
00623               repaint();
00624        }
00625        
00626        public int countPeers ()
00627        {      if (G==null) return 0;
00628               return G.getN();
00629        }
00630        
00631        public void unset ()
00632        {      if (G!=null) G.unset(true);
00633               else super.unset();
00634        }      
00635        public void dounset (boolean flag)
00636        {      super.unset(flag);
00637        }
00638        public void set ()
00639        {      if (G!=null) G.unset(false);
00640               else super.unset(false);
00641        }      
00642        public void doset ()
00643        {      super.unset(false);
00644        }
00645 }
00646 
00651 class OnOffIcon extends ToggleIcon
00652 {      static int LampSize=4;
00653        public OnOffIcon (IconBar bar, String file)
00654        {      super(bar,file,null);
00655        }
00656        public void pressed (MouseEvent e)
00657        {      State=On=!On;
00658        }
00659 }
00660 
00666 class IconGroup
00667 {      String Files[],Breaks[];
00668        IconBar Bar;
00669        int N;
00670        ToggleIcon Icons[];
00671        public IconGroup (IconBar bar, String files[], String breaks[])
00672        {      Files=files; Breaks=breaks; Bar=bar;
00673               init();
00674        }
00675        public IconGroup (IconBar bar, String files[])
00676        {      this(bar,files,files);
00677        }
00678        public void init ()
00679        {      N=0;
00680               for (int i=0; i<Files.length; i++)
00681                      if (!Files[i].equals("")) N++;
00682               Icons=new ToggleIcon[N];
00683               int k=0;
00684               for (int i=0; i<Files.length; i++)
00685               {      if (!Files[i].equals(""))
00686                      {      Icons[k++]=new ToggleIcon(Bar,Files[i],this);
00687                      }
00688               }             
00689        }
00690        public IconGroup (IconBar bar, String name, int n)
00691        {      Breaks=Files=new String[n]; 
00692               for (int i=0; i<n; i++)
00693               {      Files[i]=name+i;
00694               }
00695               Bar=bar;
00696               init();
00697        }
00698        public IconGroup (IconBar bar, String name, Color colors[])
00699        {      N=colors.length;
00700               Breaks=Files=new String[N];
00701               for (int i=0; i<N; i++)
00702               {      Files[i]=name+i;
00703               }
00704               Bar=bar;
00705               Icons=new ToggleIcon[N];
00706               for (int i=0; i<N; i++)
00707               {      Icons[i]=new ToggleIcon(Bar,Files[i],colors[i],this);
00708               }             
00709        }
00710        public void addLeft ()
00711        {      int i=0;
00712               for (int k=0; k<Files.length; k++)
00713                      if (Files[k].equals("")) Bar.addSeparatorLeft();
00714                      else
00715                      {      if (Breaks[k].startsWith("!")) Bar.addSeparatorLeft();
00716                             Bar.addLeft(Icons[i++]);
00717                      }
00718        }
00719        public void addRight ()
00720        {      int i=0;
00721               for (int k=0; k<Files.length; k++)
00722                      if (Files[k].equals("")) Bar.addSeparatorRight();
00723                      else 
00724                      {      if (Breaks[k].startsWith("!")) Bar.addSeparatorRight();
00725                             Bar.addRight(Icons[i++]);
00726                      }
00727        }
00728        public void toggle (ToggleIcon icon)
00729        {      for (int i=0; i<N; i++)
00730               {      if (Icons[i]==icon) icon.setStateInGroup(true);
00731                      else Icons[i].setStateInGroup(false);
00732               }
00733        }
00734        public void unselect ()
00735        {      for (int i=0; i<N; i++)
00736               {      Icons[i].setStateInGroup(false);
00737               }
00738        }
00739        public int getN () { return N; }
00740        public void unset (boolean flag)
00741        {      for (int i=0; i<N; i++)
00742               {      Icons[i].dounset(flag);
00743               }
00744        }
00745 }
00746 
00752 class StateDisplay extends BasicIcon
00753 {      Image IOn,IOff;
00754        int W,H,X,Y;
00755        
00759        public StateDisplay (IconBar bar, String file)
00760        {      super(bar,file);
00761               try
00762               {      InputStream in=getClass().getResourceAsStream(
00763                             Bar.Resource+file+"on"+"."+Global.getParameter("icontype","gif"));
00764                      int pos=0;
00765                      int n=in.available();
00766                      byte b[]=new byte[20000];
00767                      while (n>0)
00768                      {      int k=in.read(b,pos,n);
00769                             if (k<0) break;
00770                             pos+=k;
00771                             n=in.available();
00772                      }
00773                      in.close();
00774                      IOn=Toolkit.getDefaultToolkit().createImage(b,0,pos);
00775                      MediaTracker T=new MediaTracker(bar);
00776                      T.addImage(IOn,0);
00777                      in=getClass().getResourceAsStream(
00778                             Bar.Resource+file+"off"+"."+Global.getParameter("icontype","gif"));
00779                      pos=0;
00780                      n=in.available();
00781                      byte b1[]=new byte[20000];
00782                      while (n>0)
00783                      {      int k=in.read(b1,pos,n);
00784                             if (k<0) break;
00785                             pos+=k;
00786                             n=in.available();
00787                      }
00788                      in.close();
00789                      IOff=Toolkit.getDefaultToolkit().createImage(b1,0,pos);
00790                      T.addImage(IOff,1);
00791                      T.waitForAll();
00792                      W=IOn.getWidth(this);
00793                      H=IOn.getHeight(this);
00794                      if (Bar.Vertical) X=Size/2-W/2;
00795                      else X=0;
00796                      Y=Size/2-H/2;
00797               }
00798               catch (Exception e)
00799               {      IOn=IOff=null;
00800               }
00801        }
00802        
00806        public void paint (Graphics g)
00807        {      if (Enabled && IOn!=null)
00808               {      if (W>getSize().width) g.drawImage(IOn,1,1,Size-2,Size-2,this);
00809                      else g.drawImage(IOn,X,Y,this);
00810               }
00811               else if (!Enabled && IOff!=null)
00812               {      if (W>getSize().width) g.drawImage(IOff,1,1,Size-2,Size-2,this);
00813                      else g.drawImage(IOff,X,Y,this);
00814               }
00815        }
00816        
00817        public void mousePressed (MouseEvent e) {}
00818        public void mouseReleased (MouseEvent e) { T=null; }
00819        public void mouseClicked (MouseEvent e) {}       
00820 }
00821 
00827 public class IconBar extends Panel
00828        implements KeyListener, FocusListener, IconBarListener
00829 {      Vector Left=new Vector(),Right=new Vector();
00830        int W;
00831        Frame F;
00832        public final int Offset=2;
00833        public String Resource="/";
00834        int Focus=0;
00835        public boolean TraverseFocus=true;
00836        public boolean UseSize=true;
00837        public boolean Vertical=false;
00838        
00839        public IconBar (Frame f, boolean traversefocus)
00840        {      F=f; TraverseFocus=traversefocus;
00841               if (Global.ControlBackground!=null)
00842                      setBackground(Global.ControlBackground);
00843               else setBackground(SystemColor.menu);
00844               Resource=Global.getParameter("iconpath","");
00845               BasicIcon.Size=Global.getParameter("iconsize",20);
00846               setLayout(null);
00847               W=Offset*2;
00848               addKeyListener(this);
00849               if (TraverseFocus) addFocusListener(this);
00850        }
00851        public IconBar (Frame f)
00852        {      this(f,true);
00853        }      
00854        
00859        public void forceRepaint ()
00860        {      super.repaint();
00861               Enumeration e=Left.elements();
00862               while (e.hasMoreElements())
00863               {      BasicIcon i=(BasicIcon)e.nextElement();
00864                      i.repaint();
00865               }
00866               e=Right.elements();
00867               while (e.hasMoreElements())
00868               {      BasicIcon i=(BasicIcon)e.nextElement();
00869                      i.repaint();
00870               }
00871        }
00872 
00873        public void keyPressed (KeyEvent e) {}
00874        public void keyReleased (KeyEvent e)
00875        {      switch (e.getKeyCode())
00876               {      case KeyEvent.VK_RIGHT :
00877                             setFocus(Focus,false);
00878                             Focus++;
00879                             if (Focus>=Left.size()+Right.size()) Focus=0;
00880                             while (!(getIcon(Focus) instanceof BasicIcon))
00881                             {      Focus++;
00882                                    if (Focus>=Left.size()+Right.size())
00883                                    {      Focus=0; break;
00884                                    }
00885                             }
00886                             setFocus(Focus,true);
00887                             break;
00888                      case KeyEvent.VK_LEFT :
00889                             setFocus(Focus,false);
00890                             Focus--;
00891                             if (Focus<0) Focus=Left.size()+Right.size()-1;
00892                             while (!(getIcon(Focus) instanceof BasicIcon))
00893                             {      Focus--;
00894                                    if (Focus<0)
00895                                    {      Focus=Left.size()+Right.size()-1;
00896                                           break;
00897                                    }
00898                             }
00899                             setFocus(Focus,true);
00900                             break;
00901                      case KeyEvent.VK_SPACE :
00902                             try
00903                             {      BasicIcon icon=(BasicIcon)getIcon(Focus);
00904                                    icon.mouseReleased(new MouseEvent(this,
00905                                           MouseEvent.MOUSE_RELEASED,0,0,0,0,1,false));
00906                             }
00907                             catch (Exception ex) {}
00908                             break;
00909               }
00910        }
00911        public void keyTyped (KeyEvent e) {}
00912        
00913        /*
00914        public boolean isFocusTraversable ()
00915        {      return TraverseFocus;
00916        }
00917        */
00918        
00919        public Object getIcon (int n)
00920        {      if (n<Left.size()) return Left.elementAt(n);
00921               else return Right.elementAt(Right.size()-1-(n-Left.size()));
00922        }
00923        
00924        public void focusGained (FocusEvent e)
00925        {      if (TraverseFocus) setFocus(Focus,true);
00926        }
00927        public void focusLost (FocusEvent e)
00928        {      if (TraverseFocus) setFocus(Focus,false);
00929        }
00930               
00931        public void setFocus (int n, boolean flag)
00932        {      if (!TraverseFocus) return;
00933               try
00934               {      if (n<Left.size())
00935                      {      BasicIcon icon=(BasicIcon)Left.elementAt(n);
00936                             icon.setFocus(flag);
00937                      }
00938                      else
00939                      {      BasicIcon icon=(BasicIcon)Right.elementAt(
00940                                    Right.size()-1-(n-Left.size()));
00941                             icon.setFocus(flag);
00942                      }
00943               }
00944               catch (Exception e) {}
00945        }
00946        
00950        public void addLeft (String name)
00951        {      addLeft(new IconWithGif(this,name));
00952        }
00953        public void addLeft (BasicIcon i)
00954        {      Left.addElement(i);
00955               add(i);
00956               W+=i.width()+Offset;
00957        }
00958 
00962        public void addRight (String name)
00963        {      addRight(new IconWithGif(this,name));
00964        }
00965        public void addRight (BasicIcon i)
00966        {      Right.addElement(i);
00967               add(i);
00968               W+=i.width()+Offset;
00969        }
00970 
00974        public void addToggleLeft (String name)
00975        {      addLeft(new ToggleIcon(this,name));
00976        }
00977        public void addToggleRight (String name)
00978        {      addRight(new ToggleIcon(this,name));
00979        }
00980        
00984        public void addOnOffLeft (String name)
00985        {      addLeft(new OnOffIcon(this,name));
00986        }
00987        public void addOnOffRight (String name)
00988        {      addRight(new OnOffIcon(this,name));
00989        }
00990        
00994        public void addToggleGroupLeft (String names[], String breaks[])
00995        {      IconGroup g=new IconGroup(this,names,breaks);
00996               g.addLeft();
00997        }
00998        public void addToggleGroupRight (String names[], String breaks[])
00999        {      IconGroup g=new IconGroup(this,names,breaks);
01000               g.addRight();
01001        }
01002        public void addToggleGroupLeft (String names[])
01003        {      addToggleGroupLeft(names,names);
01004        }
01005        public void addToggleGroupRight (String names[])
01006        {      addToggleGroupRight(names,names);
01007        }
01008        public void addToggleGroupLeft (String name, int n)
01009        {      IconGroup g=new IconGroup(this,name,n);
01010               g.addLeft();
01011        }
01012        public void addToggleGroupRight (String name, int n)
01013        {      IconGroup g=new IconGroup(this,name,n);
01014               g.addRight();
01015        }
01016        public void addToggleGroupLeft (String name, Color colors[])
01017        {      IconGroup g=new IconGroup(this,name,colors);
01018               g.addLeft();
01019        }
01020        public void addToggleGroupRight (String name, Color colors[])
01021        {      IconGroup g=new IconGroup(this,name,colors);
01022               g.addRight();
01023        }
01024 
01028        public void addSeparatorLeft ()
01029        {      if (Left.size()==0) return;
01030               if (Left.lastElement() instanceof Separator) return;
01031               Separator s=new Separator(this);
01032               Left.addElement(s);
01033               add(s);
01034               W+=s.width()+Offset;
01035        }
01036        public void addSeparatorRight ()
01037        {      if (Right.size()==0) return;
01038               if (Right.lastElement() instanceof Separator) return;
01039               Separator s=new Separator(this);
01040               Right.addElement(s);
01041               add(s);
01042               W+=s.width()+Offset;
01043        }
01044 
01048        public void addMultipleIconLeft (String name, int number)
01049        {      addLeft(new MultipleIcon(this,name,number));
01050        }
01051        public void addMultipleIconRight (String name, int number)
01052        {      addRight(new MultipleIcon(this,name,number));
01053        }
01054        
01058        public void addMultipleStringIconLeft (String name, String s[])
01059        {      addLeft(new MultipleStringIcon(this,name,s));
01060        }
01061        public void addMultipleStringIconRight (String name, String s[])
01062        {      addRight(new MultipleStringIcon(this,name,s));
01063        }
01064        
01068        public void addMultipleToggleIconLeft (String name, int number)
01069        {      addLeft(new MultipleToggleIcon(this,name,number));
01070        }
01071        public void addMultipleToggleIconRight (String name, int number)
01072        {      addRight(new MultipleToggleIcon(this,name,number));
01073        }
01074        
01078        public void addColorIconLeft (String name, Color colors[])
01079        {      addLeft(new ColorIcon(this,name,colors));
01080        }
01081        public void addColorIconRight (String name, Color colors[])
01082        {      addRight(new ColorIcon(this,name,colors));
01083        }
01084 
01088        public void addStateLeft (String name)
01089        {      addLeft(new StateDisplay(this,name));
01090        }
01091        public void addStateRight (String name)
01092        {      addRight(new StateDisplay(this,name));
01093        }
01094        
01095        boolean Overflow=false,Shifted=false;
01096        OverflowButton OB;
01097        int OverflowX;
01098        
01103        public void doLayout ()
01104        {      if (OB!=null)
01105               {      remove(OB); OB=null;
01106               }
01107               if (Vertical) 
01108               {      int x;
01109                      x=getSize().height;
01110                      for (int k=0; k<Right.size(); k++)
01111                      {      IconBarElement i=(IconBarElement)Right.elementAt(k);
01112                             x-=i.width();
01113                             i.setPosition(2,x);
01114                             x-=Offset;
01115                      }
01116                      int xmax=x;
01117                      x=0;
01118                      for (int k=0; k<Left.size(); k++)
01119                      {      IconBarElement i=(IconBarElement)Left.elementAt(k);
01120                             i.setPosition(2,x);
01121                             x+=i.width();
01122                             x+=Offset;
01123                             if (x+IconWithGif.Size>xmax) x=-1000;
01124                      }
01125               }
01126               else
01127               {      int x;
01128                      x=getSize().width;
01129                      for (int k=0; k<Right.size(); k++)
01130                      {      IconBarElement i=(IconBarElement)Right.elementAt(k);
01131                             x-=i.width();
01132                             i.setPosition(x,2);
01133                             x-=Offset;
01134                      }
01135                      int xmax=x;
01136                      x=0;
01137                      for (int k=0; k<Left.size(); k++)
01138                      {      IconBarElement i=(IconBarElement)Left.elementAt(k);
01139                             i.setPosition(x,2);
01140                             x+=i.width();
01141                             x+=Offset;
01142                             if (x+IconWithGif.Size>xmax-10 && k<Left.size()-1)
01143                             {      Overflow=true; OverflowX=x;
01144                                    OB=new OverflowButton(this,Shifted);
01145                                    add(OB);
01146                                    OB.setSize(10,BasicIcon.Size);
01147                                    OB.setLocation(xmax-10-Offset,2);
01148                                    if (!Shifted)
01149                                    {      x=-1000;      
01150                                    }
01151                                    else
01152                                    {      x=xmax-10-2*Offset;
01153                                           for (int l=Left.size()-1; l>=0; l--)
01154                                           {      i=(IconBarElement)Left.elementAt(l);
01155                                                  x-=i.width();
01156                                                  i.setPosition(x,2);
01157                                                  x-=Offset;
01158                                                  if (x-IconWithGif.Size<0) x-=1000;
01159                                           }
01160                                           break;
01161                                    }
01162                             }
01163                      }
01164               }
01165        }
01166        
01167        public void setShifted (boolean flag)
01168        {      Shifted=flag;
01169               doLayout();
01170        }
01171        
01175        public Dimension getPreferredSize ()
01176        {      if (Vertical)
01177               {      if (!UseSize) return new Dimension(IconWithGif.Size+4,10);
01178                      return new Dimension(IconWithGif.Size+4,W+10);
01179               }
01180               else
01181               {      if (!UseSize) return new Dimension(10,IconWithGif.Size+4);
01182                      return new Dimension(W+10,IconWithGif.Size+4);
01183               }
01184        }
01185        public Dimension getMinimumSize ()
01186        {      return getPreferredSize();
01187        }
01188        
01189        // The IconBar can notify one IconBarListener on icon
01190        // clicks.
01191        
01192        IconBarListener L=null;
01193        
01194        public void setIconBarListener (IconBarListener l)
01195        {      L=l;
01196        }
01197        
01198        public void removeIconBarListener (IconBarListener l)
01199        {      L=null;
01200        }
01201        
01202        boolean Shift,Control;
01203        
01204        public void iconPressed (String name, boolean shift, boolean control)
01205        {      Shift=shift; Control=control;
01206               removeHelp();
01207               if (L!=null) L.iconPressed(name);
01208        }
01209        
01210        public boolean isShiftPressed () { return Shift; }
01211        public boolean isControlPressed () { return Control; }
01212        public void clearShiftControl () { Shift=Control=false; }
01213        
01214        // The tool tip help, initiated by the icons.
01215        
01216        Window WHelp=null;
01217        
01218        public synchronized void displayHelp (IconBarElement i, String text)
01219        {      if (F==null || WHelp!=null) return;
01220               Point P=i.getPosition();
01221               WHelp=new Window(F);
01222               Panel p=new Panel();
01223               StringTokenizer t=new StringTokenizer(text,"+");
01224               p.setLayout(new GridLayout(0,1));
01225               while (t.hasMoreTokens())
01226               {      p.add(new MyLabel(t.nextToken()));
01227               }
01228               WHelp.add("Center",p);
01229               WHelp.pack();
01230               Dimension d=WHelp.getSize();
01231               Dimension ds=getToolkit().getScreenSize();
01232               int x=P.x,y=P.y+i.width()+10;
01233               if (x+d.width>ds.width) x=ds.width-d.width;
01234               if (y+d.height>ds.height) y=P.y-i.width()-d.height;
01235               WHelp.setLocation(x,y);
01236               WHelp.setBackground(new Color(255,255,220));
01237               WHelp.setForeground(Color.black);
01238               WHelp.setVisible(true);
01239        }
01240        
01241        public synchronized void removeHelp ()
01242        {      if (WHelp==null) return;
01243               WHelp.setVisible(false);
01244               WHelp.dispose();
01245               WHelp=null;
01246        }
01247        
01248        private BasicIcon find (String name)
01249        {      int k;
01250               for (k=0; k<Left.size(); k++)
01251               {      try
01252                      {      BasicIcon i=(BasicIcon)Left.elementAt(k);
01253                             if (i.getName().equals(name)) return i;
01254                      }
01255                      catch (Exception e) {}
01256               }
01257               for (k=0; k<Right.size(); k++)
01258               {      try
01259                      {      BasicIcon i=(BasicIcon)Right.elementAt(k);
01260                             if (i.getName().equals(name)) return i;
01261                      }
01262                      catch (Exception e) {}
01263               }
01264               return null;
01265        }
01266 
01270        public void setEnabled (String name, boolean flag)
01271        {      BasicIcon icon=find(name);
01272               if (icon==null) return;
01273               icon.setEnabled(flag);
01274        }
01275 
01279        public void toggle (String name)
01280        {      BasicIcon icon=find(name);
01281               if (icon==null || !(icon instanceof ToggleIcon)) return;
01282               ((ToggleIcon)icon).setState(true);
01283        }
01284        
01288        public boolean have (String name)
01289        {      return find(name)!=null;
01290        }
01291        
01295        public void unselect (String name)
01296        {      BasicIcon icon=find(name);
01297               if (icon==null || !(icon instanceof ToggleIcon)) return;
01298               ((ToggleIcon)icon).unselect();
01299        }
01300        
01304        public void toggle (String name, int n)
01305        {      toggle(name+n);
01306        }
01307        
01311        public void setState (String name, boolean flag)
01312        {      BasicIcon icon=find(name);
01313               if (icon!=null && (icon instanceof ToggleIcon))
01314                      ((ToggleIcon)icon).setState(flag);
01315               if (icon!=null && (icon instanceof MultipleToggleIcon))
01316                      ((MultipleToggleIcon)icon).setState(flag);
01317        }
01318 
01322        public boolean getState (String name)
01323        {      BasicIcon icon=find(name);
01324               if (icon==null || !(icon instanceof ToggleIcon)) return false;
01325               return ((ToggleIcon)icon).getState();
01326        }
01327        
01331        public int getToggleState (String name)
01332        {      BasicIcon icon=find(name+0);
01333               if (icon==null || !(icon instanceof ToggleIcon)) return -1;
01334               int n=((ToggleIcon)icon).countPeers();
01335               for (int i=0; i<n; i++)
01336               {      if (getState(name+i)) return i;
01337               }
01338               return -1;
01339        }
01340 
01344        public int getMultipleState (String name)
01345        {      int k;
01346               for (k=0; k<Left.size(); k++)
01347               {      IconBarElement i=(IconBarElement)Left.elementAt(k);
01348                      if (i.getName().equals(name) && i instanceof MultipleIcon)
01349                      {      return ((MultipleIcon)i).getSelected();
01350                      }
01351               }
01352               for (k=0; k<Right.size(); k++)
01353               {      IconBarElement i=(IconBarElement)Right.elementAt(k);
01354                      if (i.getName().equals(name) && i instanceof MultipleIcon)
01355                      {      return ((MultipleIcon)i).getSelected();
01356                      }
01357               }
01358               return -1;
01359        }
01360 
01364        public void setMultipleState (String name, int state)
01365        {      int k;
01366               for (k=0; k<Left.size(); k++)
01367               {      IconBarElement i=(IconBarElement)Left.elementAt(k);
01368                      if (i.getName().equals(name) && i instanceof MultipleIcon)
01369                      {      ((MultipleIcon)i).setSelected(state);
01370                      }
01371               }
01372               for (k=0; k<Right.size(); k++)
01373               {      IconBarElement i=(IconBarElement)Right.elementAt(k);
01374                      if (i.getName().equals(name) && i instanceof MultipleIcon)
01375                      {      ((MultipleIcon)i).setSelected(state);
01376                      }
01377               }
01378        }
01379 
01383        public boolean isSet (String name)
01384        {      BasicIcon icon=find(name);
01385               if (icon==null) return false;
01386               return icon.isSet();
01387        }
01388        
01392        public void unset (String name)
01393        {      BasicIcon icon=find(name);
01394               if (icon!=null) icon.unset();
01395        }
01396        
01397        public void getKey (KeyEvent e)
01398        {      processKeyEvent(e);
01399        }
01400        
01401        public void setSize (int size)
01402        {      BasicIcon.Size=size;
01403        }
01404        
01405        public void removeAll ()
01406        {      Enumeration e=Left.elements();
01407               while (e.hasMoreElements())
01408               {      remove((BasicIcon)e.nextElement());
01409               }
01410               e=Right.elements();
01411               while (e.hasMoreElements())
01412               {      remove((BasicIcon)e.nextElement());
01413               }
01414               Left.removeAllElements();
01415               Right.removeAllElements();
01416        }
01417        
01423        public String getHelp (String name)
01424        {      return "";
01425        }
01426        
01427        public static void main (String args[])
01428        {      CloseFrame f=new CloseFrame("Test");
01429               IconBar IA=new IconBar(f);
01430               IA.Vertical=true;
01431               IA.setSize(30);
01432               IA.Resource="/icons/"; 
01433               IA.addLeft("back"); 
01434               IA.addLeft("undo"); 
01435               IA.addSeparatorLeft(); 
01436               IA.addOnOffLeft("grid"); 
01437               IA.addSeparatorLeft(); 
01438               IA.addToggleLeft("delete"); 
01439               IA.addSeparatorLeft();
01440               String tg[]={"zoom","draw","","rename","edit"}; 
01441               IA.addToggleGroupLeft(tg); 
01442               IA.addSeparatorLeft(); 
01443               IA.addMultipleToggleIconLeft("macro",3); 
01444               IA.addSeparatorLeft(); 
01445               String tga[]={"zoom","draw","rename","edit"}; 
01446               IA.addLeft(new PopupIcon(IA,tga));
01447               IA.addSeparatorLeft(); 
01448               String st[]={"A","B","C","D"}; 
01449               IA.addMultipleStringIconLeft("strings",st); 
01450               Color col[]={Color.BLACK,Color.RED,Color.GREEN};
01451               IA.addStateLeft("needsave");
01452               IA.addColorIconLeft("color",col); 
01453               f.add("Center",new IconBarPanel(
01454                      new Panel3D(IA),new Panel3D(new Panel())));
01455               f.pack();     
01456               f.center();
01457               IA.setIconBarListener(IA);
01458               f.setVisible(true);
01459        }
01460 
01461        public void iconPressed(String name)
01462        {      System.out.println(name);
01463        }
01464 
01465 }