Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/bu/BuDesktop.java


1   /**
2    * @modification $Date: 2003/01/16 10:20:37 $
3    * @statut       unstable
4    * @file         BuDesktop.java
5    * @version      0.36
6    * @author       Guillaume Desnoix
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   */
11  
12  package com.memoire.bu;
13  
14  import com.memoire.bu.*;
15  import com.memoire.fu.*;
16  
17  import javax.swing.*;
18  import javax.swing.border.*;
19  import javax.swing.plaf.*;
20  
21  import java.awt.*;
22  import java.awt.event.*;
23  import java.beans.*;
24  
25  /**
26   * A DesktopPane with a few additional services.
27   * Auto-resizing, optionnaly tabbed, custom-friendly, icons management, ...
28   */
29  public class BuDesktop
30         extends JDesktopPane
31         implements MouseListener, ActionListener
32  {
33    public static final int LEFT_MARGIN=31;
34  
35    public static int BLOCK_LAYER=JLayeredPane.DEFAULT_LAYER.intValue()-100;
36  
37    public final boolean isPalette(JComponent _f)
38    {
39      /*
40        return (Boolean.TRUE.equals
41        (_f.getClientProperty("JInternalFrame.isPalette")));
42      */
43      return
44          (DEFAULT_LAYER.intValue()!=getLayer(_f))
45        &&Boolean.TRUE.equals
46        (_f.getClientProperty("JInternalFrame.isPalette"));
47    }
48  
49    // Anti-aliasing
50  
51    public void paint(Graphics _g)
52    {
53      BuLib.setAntialiasing(_g);
54      super.paint(_g);
55    }
56  
57    public final void snapXY(JComponent _f)
58    {
59      if(  BuPreferences.BU.getBooleanProperty("desktop.snap",false)
60         &&!isTabbed()
61         &&!isPalette(_f)
62         &&(_f instanceof JInternalFrame))
63      {
64        Point   p=_f.getLocation();
65        boolean b=false;
66        if(p.x%37!=0) { p.x=p.x-p.x%37; b=true; }
67        if(p.y%37!=0) { p.y=p.y-p.y%37; b=true; }
68        if(b) _f.setLocation(p);
69      }
70    }
71  
72    public final void snapWH(JComponent _f)
73    {
74      if(  BuPreferences.BU.getBooleanProperty("desktop.snap",false)
75         &&!isTabbed()
76         &&!isPalette(_f)
77         &&(_f instanceof JInternalFrame))
78      {
79        Dimension d=_f.getSize();
80        boolean   b=false;
81        if(((JInternalFrame)_f).isResizable())
82        {
83          if(d.width %37!=0) { d.width =d.width +37-d.width %37; b=true; }
84          if(d.height%37!=0) { d.height=d.height+37-d.height%37; b=true; }
85        }
86        if(b) _f.setSize(d);
87      }
88    }
89  
90    public BuDesktop()
91    {
92      super();
93  
94      if(BuPreferences.BU.getBooleanProperty("desktop.outline",true))
95      {
96        putClientProperty("JDesktopPane.dragMode","outline");
97        //setDragMode(OUTLINE_DRAG_MODE);
98      }
99      else
100     {
101       putClientProperty("JDesktopPane.dragMode","faster");
102       //setDragMode(LIVE_DRAG_MODE);
103     }
104 
105     setName("buDESKTOP");
106     setDesktopManager(createDesktopManager());
107 
108     BuBackgroundPainter bp=new BuBackgroundPainter();
109     setBackgroundPainter(bp);
110     if(isBlocked())
111     {
112       bp.setBar(true);
113       bp.setBarHeight
114   (BuLib.isMetal()
115    ? 8+BuResource.BU.getDefaultFrameSize()
116    : 0);
117     }
118 
119     addMouseListener(this);
120 
121     if(isTabbed())
122     {
123       setToolTipText("...");
124       setLayout(new Layout());
125     }
126   }
127 
128   protected DesktopManager createDesktopManager()
129   { return new BuDesktopManager(this); }
130 
131   public boolean isFocusCycleRoot()
132   { return true; }
133 
134   /*
135   public void moveToFront(JInternalFrame _f)
136   {
137     _f.moveToFront();
138 
139     JInternalFrame[] frames=getAllFrames();
140     int l=frames.length;
141     for(int i=0;i<l;i++)
142       if(isPalette(frames[i])&&frames[i].isVisible())
143   frames[i].moveToFront();
144   }
145   */
146 
147   public boolean isOpaque()
148   {
149     return Boolean.TRUE.equals(UIManager.get("Desktop.opaque"));
150   }
151 
152   private static final Border BORDER=new EmptyBorder(0,0,0,0);
153 
154   protected void addImpl(Component _c, Object _constraints, int _index)
155   {
156     if(_c instanceof JInternalFrame.JDesktopIcon)
157     {
158       _c.setCursor(new Cursor(Cursor.HAND_CURSOR));
159 
160       if(isBlocked())
161       {
162         JInternalFrame.JDesktopIcon i=(JInternalFrame.JDesktopIcon)_c;
163         setLayer(i,BLOCK_LAYER);
164 
165         if(BuLib.isMetal())
166         {
167           if(i.getComponentCount()==2)
168           {
169       i.remove(1);
170       i.setBorder(BORDER);
171       ((JButton)i.getComponent(0)).setHorizontalAlignment(JButton.LEFT);
172       ((JButton)i.getComponent(0)).setMargin(new Insets(1,1,1,1));
173     }
174   }
175   Dimension ps=i.getPreferredSize();
176        if(BuLib.isMotif()) ;
177         else if(BuLib.isMetal()) ps.width=37*4;
178         else                     ps.width=37*3;
179         //if(ps.width %37!=0) ps.width =ps.width +37-ps.width %37;
180         //if(ps.height%37!=0) ps.height=ps.height+37-ps.height%37;
181         i.setPreferredSize(ps);
182         i.setSize(ps);
183       }
184     }
185     
186     if(_c instanceof JComponent)
187     {
188       snapXY((JComponent)_c);
189       snapWH((JComponent)_c);
190     }
191 
192     super.addImpl(_c,_constraints,_index);
193 
194     if(  isBlocked()
195        &&(_c instanceof JInternalFrame.JDesktopIcon))
196       arrangeIcons0();
197   }
198 
199   public void remove(int _index)
200   {
201     boolean b=isBlocked()
202       &&(getComponent(_index) instanceof JInternalFrame.JDesktopIcon);
203 
204     super.remove(_index);
205     if(b) arrangeIcons0();
206   }
207 
208   // Paint
209 
210   public /*synchronized*/ void paintComponent(Graphics _g)
211   {
212     JInternalFrame[] frames=getAllFrames();
213 
214     if(!isTabbed()||(frames.length==0))
215     {
216       Rectangle clip=_g.getClipBounds();
217       Dimension dd  =getSize();
218 
219       BuBackgroundPainter bp=getBackgroundPainter();
220       if(bp!=null) bp.paintBackground(this,_g);
221 
222       if(logo_!=null)
223       {
224   Rectangle ri=new Rectangle(5,
225            // dd.width-5-logo_.getIconWidth(),
226            5,
227            logo_.getIconWidth(),
228            logo_.getIconHeight());
229   if(ri.intersects(clip))
230     logo_.paintIcon(this,_g,ri.x,ri.y);
231       }
232     }
233     else
234     {
235       Dimension dd=getSize();
236       Color pbg=UIManager.getColor("Panel.background");
237       Color dbg=getBackground();
238 
239       _g.setColor(pbg);
240       _g.fillRect(0,0,dd.width,dd.height); // LM+4
241 
242       // JInternalFrame[] frames=getAllFrames();
243       int y,l;
244 
245       sortFramesByTitle(frames);
246 
247       l=frames.length;
248       y=0;
249       for(int i=0; i<l; i++)
250       {
251   if( frames[i].isClosed() ) continue;
252   if(!frames[i].isVisible()) continue;
253 
254   // if(isPalette(frames[i])) continue;
255 
256   _g.setColor(pbg);
257   _g.draw3DRect(0,y,LEFT_MARGIN-1,LEFT_MARGIN-1,false);
258   
259        if(isPalette(frames[i]))   _g.setColor(pbg); 
260   else if(frames[i].isSelected()) _g.setColor(dbg);
261   else                            _g.setColor(dbg.brighter());
262   _g.fill3DRect(1,y+1,LEFT_MARGIN-2,LEFT_MARGIN-2,true);
263 
264   Icon icon=frames[i].getFrameIcon();
265   if(icon==null) icon=UIManager.getIcon("InternalFrame.icon");
266   if(icon!=null)
267   {
268     int w=icon.getIconWidth();
269     int h=icon.getIconHeight();
270     icon.paintIcon(this,_g,(LEFT_MARGIN-w)/2,y+(LEFT_MARGIN-h)/2);
271   }
272 
273   y+=LEFT_MARGIN;
274       }
275     }
276   }
277 
278   public void paintChildren(Graphics _g)
279   {
280     /*
281     if(isTabbed())
282     {
283       JInternalFrame[] frames=getAllFrames();
284     }
285     else
286     */
287 
288     super.paintChildren(_g);
289   }
290 
291   public String getToolTipText()
292   {
293     return null;
294   }
295 
296   public /*synchronized*/ String getToolTipText(MouseEvent _evt)
297   {
298     if(isTabbed())
299     {
300       int xe=_evt.getX();
301       int ye=_evt.getY();
302 
303       JInternalFrame[] frames=getAllFrames();
304       int l=frames.length;
305 
306       if((xe<LEFT_MARGIN)&&(ye<LEFT_MARGIN*l))
307       {
308   sortFramesByTitle(frames);
309   JInternalFrame f=frames[ye/LEFT_MARGIN];
310   String r=f.getTitle();
311   if("".equals(r)) r=null;
312   if(r==null) r=""+(1+ye/LEFT_MARGIN);
313   return r;
314       }
315     }
316 
317     return super.getToolTipText(_evt);
318   }
319 
320   public /*synchronized*/ Point getToolTipLocation(MouseEvent _evt)
321   {
322     if(isTabbed())
323     {
324       int xe=_evt.getX();
325       int ye=_evt.getY();
326 
327       JInternalFrame[] frames=getAllFrames();
328       int l=frames.length;
329 
330       if((xe<LEFT_MARGIN)&&(ye<LEFT_MARGIN*l))
331       {
332   sortFramesByTitle(frames);
333   // return new Point(0,(ye/LEFT_MARGIN+1)*LEFT_MARGIN);
334   return new Point(LEFT_MARGIN+2,(ye/LEFT_MARGIN)*LEFT_MARGIN+1);
335       }
336     }
337 
338     return super.getToolTipLocation(_evt);
339   }
340 
341   // Proprietes
342 
343   private boolean tabbed_=
344     BuPreferences.BU.getBooleanProperty("desktop.tabbed",false);
345   public boolean isTabbed() { return tabbed_; }
346   // public void setTabbed(boolean _tabbed) { tabbed_=_tabbed; }
347 
348   private boolean blocked_=
349     BuPreferences.BU.getBooleanProperty("desktop.blocked",true);
350   public boolean isBlocked() { return blocked_; }
351   // public void setBlocked(boolean _blocked) { blocked_=_blocked; }
352 
353   private Icon logo_;
354   public Icon getLogo() { return logo_; }
355   public void setLogo(Icon _logo)
356   {
357     logo_=_logo;
358     repaint();
359   }
360 
361   private BuBackgroundPainter bp_;
362   public BuBackgroundPainter getBackgroundPainter() { return bp_; }
363   public void setBackgroundPainter(BuBackgroundPainter _bp)
364   {
365     bp_=_bp;
366     repaint();
367   }
368 
369   // Filles
370 
371   public void addInternalFrame(JInternalFrame _f)
372   {
373     if(!SwingUtilities.isEventDispatchThread())
374       throw new RuntimeException
375   ("Not in swing thread. "+
376    "Use Implementation.addInternalFrame() instead");
377 
378     //System.err.println(_f.getName()+":"+_f.getDesktopPane());
379 
380     if(_f.getDesktopPane()==null)
381     {
382       if(_f.isSelected())
383       {
384   try { _f.setSelected(false); }
385   catch(PropertyVetoException ex) { }
386       }
387 
388       Point pf=_f.getLocation();
389       if((pf.x==0)&&(pf.y==0))
390       {
391   Container cp=getParent();
392   if(cp instanceof JViewport)
393   {
394     Rectangle vr=((JViewport)cp).getViewRect();
395     Dimension df=_f.getSize();
396 
397     pf.x=vr.x+(vr.width-df.width  )/2;
398     pf.y=vr.y+(vr.height-df.height)/2;
399     _f.setLocation(pf);
400   }
401       }
402 
403       //SwingUtilities.updateComponentTreeUI(_f);
404       add(_f);
405       //doLayout();
406       //validate();
407 
408       snapXY(_f);
409       snapWH(_f);
410       //repaint(); // @GDX ???
411     }
412     activateInternalFrame(_f);
413   }
414 
415   public void removeInternalFrame(JInternalFrame _f)
416   {
417     if(!SwingUtilities.isEventDispatchThread())
418       throw new RuntimeException
419   ("Not in swing thread. "+
420    "Use Implementation.removeInternalFrame() instead");
421 
422     if(_f.getDesktopPane()==this)
423     {
424       deactivateInternalFrame(_f);
425       remove(_f);
426       //doLayout();
427       //validate();
428       //repaint(); // @GDX ???
429     }
430 
431     adjustSize();
432   }
433 
434   public void checkInternalFrame(JInternalFrame _f)
435   {
436     if(!SwingUtilities.isEventDispatchThread())
437       throw new RuntimeException
438   ("Not in swing thread.");
439 
440     Dimension dd=getSize();
441     Point     pf=_f.getLocation();
442     Dimension df=_f.getSize();
443 
444     if(_f.isResizable())
445     {
446       if(df.width >dd.width ) df.width=dd.width;
447       if(df.height>dd.height) df.height=dd.height;
448       if(!df.equals(getSize())) _f.setSize(df);
449     }
450 
451     if(pf.x+df.width >dd.width ) pf.x=dd.width-df.width;
452     if(pf.y+df.height>dd.height) pf.y=dd.height-df.height;
453     if(pf.x<0) pf.x=0;
454     if(pf.y<0) pf.y=0;
455 
456     if(isTabbed()&&isPalette(_f)&&(pf.x<LEFT_MARGIN+4))
457       pf.x=LEFT_MARGIN+4;
458 
459     if(!pf.equals(getLocation())) _f.setLocation(pf);
460 
461     adjustSize();
462   }
463 
464   public void activateInternalFrame(JInternalFrame _f)
465   {
466     if(!SwingUtilities.isEventDispatchThread())
467       throw new RuntimeException
468   ("Not in swing thread. "+
469    "Use Implementation.activateInternalFrame() instead");
470 
471     if(!_f.isVisible())
472     {
473       _f.setVisible(true);
474     }
475 
476     if(_f.isClosed())
477     {
478       try { _f.setClosed(false); }
479       catch(PropertyVetoException ex) { }
480     }
481 
482     checkInternalFrame(_f);
483 
484     if(_f.isIcon())
485     {
486       try { _f.setIcon(false); }
487       catch(PropertyVetoException ex) { }
488     }
489 
490     //if(!isPalette(_f))
491     {
492       moveToFront(_f);
493       if(!_f.isSelected()&&!isPalette(_f))
494       {
495         try { _f.setSelected(true); }
496         catch(PropertyVetoException ex) { }
497       }
498     }
499   }
500 
501   public void deactivateInternalFrame(JInternalFrame _f)
502   {
503     if(!SwingUtilities.isEventDispatchThread())
504       throw new RuntimeException
505   ("Not in swing thread.");
506 
507     checkInternalFrame(_f);
508 
509     if(_f.isSelected())
510     {
511       try { _f.setSelected(false); }
512       catch(PropertyVetoException ex) { }
513     }
514   }
515 
516   public /*synchronized*/ JInternalFrame getCurrentInternalFrame()
517   {
518     JInternalFrame[] frames=getAllFrames();
519     JInternalFrame   r=null;
520 
521     for(int i=0; i<frames.length; i++)
522       if(frames[i].isSelected())
523   { r=frames[i]; break; }
524 
525     return r;
526   }
527 
528   public /*synchronized*/ JInternalFrame[] getNormalFrames()
529   {
530     JInternalFrame[] frames=getAllFrames();
531     int l,n,i,j;
532 
533     l=frames.length;
534     n=0;
535 
536     for(i=0; i<l; i++)
537       if(!frames[i].isIcon()&&!isPalette(frames[i])) n++;
538 
539     JInternalFrame[] r=new JInternalFrame[n];
540 
541     j=0;
542     for(i=0; i<l; i++)
543       if(!frames[i].isIcon()&&!isPalette(frames[i])) { r[j]=frames[i]; j++; }
544 
545     return r;
546   }
547 
548   public /*synchronized*/ JInternalFrame[] getNotIconifiedFrames()
549   {
550     JInternalFrame[] frames=getAllFrames();
551     int l,n,i,j;
552 
553     l=frames.length;
554     n=0;
555 
556     for(i=0; i<l; i++)
557       if(!frames[i].isIcon()) n++;
558 
559     JInternalFrame[] r=new JInternalFrame[n];
560 
561     j=0;
562     for(i=0; i<l; i++)
563       if(!frames[i].isIcon()) { r[j]=frames[i]; j++; }
564 
565     return r;
566   }
567 
568   public /*synchronized*/ JInternalFrame[] getIconifiedFrames()
569   {
570     JInternalFrame[] frames=getAllFrames();
571     int l,n,i,j;
572 
573     l=frames.length;
574     n=0;
575 
576     for(i=0; i<l; i++)
577       if(frames[i].isIcon()) n++;
578 
579     JInternalFrame[] r=new JInternalFrame[n];
580 
581     j=0;
582     for(i=0; i<l; i++)
583       if(frames[i].isIcon()) { r[j]=frames[i]; j++; }
584 
585     return r;
586   }
587 
588   public /*synchronized*/ JInternalFrame[] getPalettes()
589   {
590     JInternalFrame[] frames=getAllFrames();
591     int l,n,i,j;
592 
593     l=frames.length;
594     n=0;
595 
596     for(i=0; i<l; i++)
597       if(isPalette(frames[i])) n++;
598 
599     JInternalFrame[] r=new JInternalFrame[n];
600 
601     j=0;
602     for(i=0; i<l; i++)
603       if(isPalette(frames[i])) { r[j]=frames[i]; j++; }
604 
605     return r;
606   }
607 
608   public int getNormalFramesCount()
609   { return getNormalFrames().length; }
610 
611   public int getNotIconifiedFramesCount()
612   { return getNotIconifiedFrames().length; }
613 
614   public int getIconifiedFramesCount()
615   { return getIconifiedFrames().length; }
616 
617   public int getPalettesCount()
618   { return getPalettes().length; }
619 
620   // Reorganisation
621 
622   protected void sortFramesByTitle(JInternalFrame[] _frames)
623   {
624     JInternalFrame tmp;
625     int i,l;
626 
627     l=_frames.length;
628     for(i=0; i+1<l; i++)
629     {
630       String t1=_frames[i].getTitle();
631       String t2=_frames[i+1].getTitle();
632       if((t1!=null)&&(t2!=null)&&(t1.compareTo(t2)>0))
633       {
634   tmp         =_frames[i];
635   _frames[i]  =_frames[i+1];
636   _frames[i+1]=tmp;
637   i--; if(i>=0) i--;
638       }
639     }
640   }
641 
642   protected void sortFramesByHeight(JInternalFrame[] _frames)
643   {
644     sortFramesByTitle(_frames);
645 
646     JInternalFrame tmp;
647     int i,l;
648 
649     l=_frames.length;
650     for(i=0; i+1<l; i++)
651       if(_frames[i].getHeight()>_frames[i+1].getHeight())
652       {
653   tmp         =_frames[i];
654   _frames[i]  =_frames[i+1];
655   _frames[i+1]=tmp;
656   i--; if(i>=0) i--;
657       }
658   }
659 
660   protected void sortFramesByWidth(JInternalFrame[] _frames)
661   {
662     sortFramesByTitle(_frames);
663 
664     JInternalFrame tmp;
665     int i,l;
666 
667     l=_frames.length;
668     for(i=0; i+1<l; i++)
669       if(_frames[i].getWidth()<_frames[i+1].getWidth())
670       {
671   tmp         =_frames[i];
672   _frames[i]  =_frames[i+1];
673   _frames[i+1]=tmp;
674   i--; if(i>=0) i--;
675       }
676   }
677 
678   public /*synchronized*/ void waterfall()
679   {
680     if(isTabbed()) return;
681 
682     JInternalFrame[] frames=getNormalFrames();
683     int i,l,x,y;
684 
685     l=frames.length;
686     if(l>0)
687     {
688       sortFramesByTitle(frames);
689 
690       x=74;
691       y=0; //y=5;
692       for(i=l-1;i>=0;i--)
693       {
694   frames[i].setLocation(x,y);
695   moveToFront(frames[i]);
696   checkInternalFrame(frames[i]);
697   x+=37;//x+=25;
698   y+=37;//y+=25;
699       }
700     }
701   }
702 
703   public /*synchronized*/ void tile()
704   {
705     if(isTabbed()) return;
706 
707     JInternalFrame[] frames=getNormalFrames();
708     int i,l,x,y,h,wd,hd;
709 
710     l=frames.length;
711     if(l>0)
712     {
713       sortFramesByHeight(frames);
714 
715       wd=getWidth();  // getSize().width;
716       hd=getHeight(); // getSize().height;
717       wd=(wd<200 ? 200 : wd);
718       hd=(hd<200 ? 200 : hd-60);
719 
720       x=0;
721       y=0;
722       h=0;
723       for(i=0;i<l;i++)
724       {
725   int wf=frames[i].getWidth();  // Size().width;
726   int hf=frames[i].getHeight(); // Size().height;
727   if(x+wf>wd)
728     { x=0; y+=h; h=0; }
729   frames[i].setLocation(x,y);
730   x+=wf;
731   h=Math.max(h,hf);
732   moveToFront(frames[i]);
733   checkInternalFrame(frames[i]);
734       }
735     }
736   }
737 
738   public void arrangeIcons()
739   {
740     arrangeIcons0();
741     adjustSize();
742   }
743 
744   protected /*synchronized*/ final void arrangeIcons0()
745   {
746     if(isTabbed()) return;
747 
748     JInternalFrame[] frames;
749     int i,l,x,y,wd,hd,bh;
750 
751     wd=getWidth();  // getSize().width;
752     hd=getHeight(); // getSize().height;
753     bh=0;
754 
755     frames=getIconifiedFrames();
756     l=frames.length;
757 
758     if(l>0)
759     {
760       sortFramesByTitle(frames);
761 
762       x=0; y=hd;
763       for(i=0;i<l;i++)
764       {
765   JInternalFrame.JDesktopIcon dti=frames[i].getDesktopIcon();
766   Dimension cs;
767 
768   dti.setSize(dti.getPreferredSize());
769   cs=dti.getSize();
770 
771   if(x+cs.width<wd)
772   {
773     dti.setLocation(x,y-cs.height);
774     x+=cs.width;
775   }
776   else
777   {
778     x=0;
779     y-=cs.height;
780     dti.setLocation(x,y-cs.height);
781   }
782   bh=Math.max(bh,hd-y+cs.height);
783       }
784     }
785 
786     /*
787     frames=getNotIconifiedFrames();
788     l=frames.length;
789 
790     if(l>0)
791     {
792       sortFramesByWidth(frames);
793 
794       y=0;
795       for(i=l-1;i>=0;i--)
796       {
797   Dimension d=frames[i].getSize();
798   if(d.height<32)
799   {
800     frames[i].setLocation(0,y);
801     y+=d.height;
802   }
803       }
804     }
805     */
806 
807     if(isBlocked())
808     {
809       BuBackgroundPainter bp=getBackgroundPainter();
810       if((bp!=null)&&(bh!=bp.getBarHeight()))
811       {
812   if(bh>0)
813   {
814     bp.setBarHeight(bh);
815     repaint(0,hd-bh,wd,bh);
816   }
817       }
818     }
819   }
820 
821   public /*synchronized*/ void arrangePalettes()
822   {
823     // if(isTabbed()) return;
824 
825     JInternalFrame[] frames;
826     int i,l,x,y,wd,hd,wmax;
827 
828     wd=getWidth();  // getSize().width;
829     hd=getHeight(); // getSize().height;
830 
831     frames=getAllFrames();
832     l=frames.length;
833 
834     if(l>0)
835     {
836       sortFramesByHeight(frames);
837 
838       wmax=0;
839       x=(isTabbed() ? LEFT_MARGIN+1 : 0);
840       y=(isTabbed() ? 17 : 0);
841 
842       for(i=0;i<l;i++)
843       {
844   JInternalFrame dti=frames[i];
845   if(!isPalette(dti)) continue;
846 
847   // dti.setSize(dti.getPreferredSize());
848   Dimension cs=dti.getSize();
849 
850   if(y+cs.height>=hd)
851   {
852     x+=wmax;
853     y=(isTabbed() ? 17 : 0);
854     wmax=0;
855   }
856 
857   dti.setLocation(x,y);
858   y+=cs.height;
859   wmax=Math.max(wmax,cs.width);
860       }
861     }
862 
863     adjustSize();
864   }
865 
866   // Taille
867 
868   public /*synchronized*/ Dimension getPreferredSize()
869   {
870     Dimension r=super.getPreferredSize();
871 
872     // if(isTabbed()) return r;
873 
874     Point qmin=new Point(0,0);
875 
876     JInternalFrame[] frames=getAllFrames();
877     int i,l,n,x;
878 
879     l=frames.length;
880     for(i=l-1;i>=0;i--)
881     {
882       if(isTabbed()&&!isPalette(frames[i])) continue;
883 
884       Point q=null;
885       if(frames[i].isIcon()) q=frames[i].getDesktopIcon().getLocation();
886       else                   q=frames[i].getLocation();
887       if(qmin.x>q.x) qmin.x=q.x;
888       if(qmin.y>q.y) qmin.y=q.y;
889     }
890 
891     for(i=l-1;i>=0;i--)
892     {
893       if(isTabbed()&&!isPalette(frames[i])) continue;
894 
895       if(frames[i].isIcon())
896       {
897   if(!isBlocked())
898   {
899     JComponent di=frames[i].getDesktopIcon();
900     if(di.isVisible())
901     {
902       Point p=di.getLocation();
903       p.x-=qmin.x;
904       p.y-=qmin.y;
905       di.setLocation(p);
906 
907       Dimension d=di.getSize();
908       if(p.x+d.width >r.width ) r.width =p.x+d.width;
909       if(p.y+d.height>r.height) r.height=p.y+d.height;
910     }
911   }  
912       }
913       else
914       if(frames[i].isVisible())
915       {
916   Point p=frames[i].getLocation();
917   p.x-=qmin.x;
918   p.y-=qmin.y;
919   frames[i].setLocation(p);
920     
921   Dimension d=frames[i].getSize();
922   if(p.x+d.width >r.width ) r.width =p.x+d.width;
923   if(p.y+d.height>r.height) r.height=p.y+d.height;
924       }
925     }
926 
927     // System.err.println("PSIZE="+r);
928     return r;
929   }
930 
931   public void reshape(int _x, int _y, int _w, int _h)
932   {
933     int ow=getWidth();
934     int oh=getHeight();
935     super.reshape(_x,_y,_w,_h);
936 
937     if(isBlocked()&&((ow!=_w)||(oh!=_h))) arrangeIcons0();
938   }
939 
940   public void adjustSize()
941   {
942     SwingUtilities.invokeLater
943       (new Runnable()
944        {
945    public void run()
946    {
947      Container pt=getParent();
948 
949      /*
950      Point     vxy=null;
951      Dimension os =null;
952      if(pt instanceof JViewport)
953      {
954        vxy=((JViewport)pt).getViewPosition();
955        os =getSize();
956      }
957      */
958 
959      Dimension ns=getPreferredSize();
960 
961      if(pt!=null)
962      {
963        Dimension vs=pt.getSize();
964        if(ns.width <vs.width ) ns.width =vs.width;
965        if(ns.height<vs.height) ns.height=vs.height;
966      }
967 
968      setSize(ns);
969      revalidate();
970 
971      /*
972      getParent().doLayout();
973      getParent().validate();
974      doLayout();
975      validate();
976      */
977 
978      /*
979      if(pt instanceof JViewport)
980      {
981        Dimension vwh=((JViewport)pt).getExtentSize();
982        ((JViewport)pt).scrollRectToVisible
983          (new Rectangle(vxy.x-qmin.x-Math.max(0,ns.width -os.width ),
984             vxy.y-qmin.y-Math.max(0,ns.height-os.height),
985             vwh.width,vwh.height));
986      }
987      */
988 
989      /*
990      Container cp=getParent();
991      if(cp instanceof JViewport)
992      {
993        JInternalFrame sf=getCurrentInternalFrame();
994        if((sf!=null)&&sf.isShowing())
995          ((JViewport)cp).scrollRectToVisible(sf.getBounds());
996      }
997      */
998    }
999        });
1000  }
1001
1002  public void showFrame(final JInternalFrame _f)
1003  {
1004    Runnable runnable=new Runnable()
1005    {
1006      public void run()
1007      {
1008  Container cp=getParent();
1009  if(  (cp instanceof JViewport)
1010     &&(_f!=null)
1011     &&_f.isShowing())
1012  {
1013    JViewport vp=(JViewport)cp;
1014    Point     vo=vp.getViewPosition();
1015    Dimension es=vp.getExtentSize();
1016    Dimension vs=vp.getViewSize();
1017    Point     fo=_f.getLocation();
1018    Dimension fs=_f.getSize();
1019    
1020    if(fo.x<vo.x) vo.x=fo.x;
1021    if(fo.y<vo.y) vo.y=fo.y;
1022    if(fo.x+fs.width >vo.x+es.width )
1023      vo.x=Math.min(fo.x,vs.width -es.width );
1024    if(fo.y+fs.height>vo.y+es.height)
1025      vo.y=Math.min(fo.y,vs.height-es.height);
1026    vp.setViewPosition(vo);
1027  }
1028      }
1029    };
1030
1031    if(SwingUtilities.isEventDispatchThread())
1032      runnable.run();
1033    else
1034      SwingUtilities.invokeLater(runnable);
1035  }
1036
1037  // Layout
1038
1039  private final class Layout implements LayoutManager2
1040  {
1041    public void addLayoutComponent(String _s, Component _c)
1042    { addLayoutComponent(_c,_s); }
1043
1044    public void addLayoutComponent(Component _c, Object _o)
1045    {
1046      // System.out.println("ADD "+_c);
1047      if(  (_c instanceof JComponent)
1048   &&isPalette((JComponent)_c))
1049  return;
1050
1051      if(_c instanceof BuInternalFrame)
1052  ((BuInternalFrame)_c).simplifyTop();
1053      
1054      /*
1055      if(_c instanceof JComponent)
1056  ((JComponent)_c).setBorder(UIManager.getBorder("TitleBorder.border"));
1057      */
1058
1059      if(_c instanceof JInternalFrame)
1060      {
1061  JInternalFrame f=(JInternalFrame)_c;
1062  f.setIconifiable(false);
1063  f.setMaximizable(false);
1064  // f.setVisible(f.isSelected());
1065      }
1066    }
1067
1068    public void removeLayoutComponent(Component _c)
1069    { }
1070
1071    public float getLayoutAlignmentX(Container _p)
1072    { return 0.5f; }
1073    public float getLayoutAlignmentY(Container _p)
1074    { return 0.5f; }
1075    public Dimension minimumLayoutSize(Container _p)
1076    { return _p.getSize(); }
1077    public Dimension preferredLayoutSize(Container _p)
1078    { return _p.getSize(); }
1079    public Dimension maximumLayoutSize(Container _p)
1080    { return _p.getSize(); }
1081
1082    public void invalidateLayout(Container _p)
1083    {
1084      Dimension ds=_p.getSize();
1085      // int l=p.getComponentCount();
1086      repaint(0,0,LEFT_MARGIN,ds.height); // l*LEFT_MARGIN;
1087    }
1088
1089    public void layoutContainer(Container _p)
1090    {
1091      Dimension ds=_p.getSize();
1092      int l=_p.getComponentCount();
1093      for(int i=0;i<l;i++)
1094      {
1095  Component c=getComponent(i);
1096  /*
1097  if(  (c instanceof JComponent)
1098     &&isPalette((JComponent)c))
1099    ;
1100  else
1101    c.setBounds(LEFT_MARGIN,0,
1102          ds.width-LEFT_MARGIN,ds.height);
1103  */
1104  if(  (c instanceof JInternalFrame)
1105     &&!isPalette((JComponent)c))
1106  {
1107    c.setBounds(LEFT_MARGIN,0,
1108          ds.width-LEFT_MARGIN,ds.height);
1109  }
1110      }
1111
1112      //invalidate();
1113      repaint(0,0,LEFT_MARGIN,ds.height);
1114    }
1115  }
1116
1117  // Mouse
1118
1119  public void mouseDown(MouseEvent _evt)
1120  { }
1121
1122  public void mouseEntered(MouseEvent _evt)
1123  { }
1124
1125  public void mouseExited(MouseEvent _evt)
1126  { }
1127
1128  public void mousePressed(MouseEvent _evt)
1129  {
1130    if(  _evt.isPopupTrigger()
1131       ||((_evt.getModifiers()&MouseEvent.BUTTON3_MASK)!=0))
1132      popupMenu(_evt);
1133  }
1134
1135  public void mouseReleased(MouseEvent _evt)
1136  { }
1137
1138  public void mouseUp(MouseEvent _evt)
1139  { }
1140
1141  public void mouseClicked(MouseEvent _evt)
1142  {
1143    if(isTabbed())
1144    {
1145      int xe=_evt.getX();
1146      int ye=_evt.getY();
1147
1148      JInternalFrame[] frames=getAllFrames();
1149      int l=frames.length;
1150
1151      if((xe<LEFT_MARGIN)&&(ye<LEFT_MARGIN*l))
1152      {
1153  sortFramesByTitle(frames);
1154  activateInternalFrame(frames[ye/LEFT_MARGIN]);
1155      }
1156    }
1157  }
1158
1159  protected BuPopupMenu menu_=null;
1160
1161  protected /*synchronized*/ BuPopupMenu createPopupMenu()
1162  {
1163    if(menu_!=null) return menu_;
1164
1165    menu_=new BuPopupMenu(BuResource.BU.getString("Bureau"));
1166
1167    menu_.addCheckBox
1168      (BuResource.BU.getString("Grille"),"GRILLE",
1169       /*BuResource.BU.getMenuIcon("aucun"),*/true,true);
1170    menu_.addCheckBox
1171      (BuResource.BU.getString("Points"),"POINTS",
1172       /*BuResource.BU.getMenuIcon("aucun"),*/true,true);
1173    menu_.addCheckBox
1174      (BuResource.BU.getString("Magnétisme"),"MAGNETISME",
1175       /*BuResource.BU.getMenuIcon("aucun"),*/true,true);
1176    menu_.addSeparator();
1177    menu_.addMenuItem
1178  (BuResource.BU.getString("Uniforme"),
1179   "DESKTOP_UNIFORME",BuResource.BU.getMenuIcon("uniforme"),true);
1180    menu_.addMenuItem
1181  (BuResource.BU.getString("Dégradé"),
1182   "DESKTOP_DEGRADE" ,BuResource.BU.getMenuIcon("degrade"),true);
1183    menu_.addSeparator();
1184    
1185    BuMenu mnTextures=new BuMenu
1186  (BuResource.BU.getString("Textures"),"MENU_TEXTURES",
1187   new BuTextureIcon(BuResource.BU.getImage("background_desktop_1")));
1188    mnTextures.addMenuItem
1189  (BuResource.BU.getString("Texture")+" 1",
1190   "DESKTOP_TEXTURE1",new BuTextureIcon
1191       (BuResource.BU.getImage("background_desktop_1")),true);
1192    mnTextures.addMenuItem
1193  (BuResource.BU.getString("Texture")+" 2",
1194   "DESKTOP_TEXTURE2",new BuTextureIcon
1195       (BuResource.BU.getImage("background_desktop_2")),true);
1196    mnTextures.addMenuItem
1197  (BuResource.BU.getString("Texture")+" 3",
1198   "DESKTOP_TEXTURE3",new BuTextureIcon
1199       (BuResource.BU.getImage("background_desktop_3")),true);
1200    mnTextures.addMenuItem
1201  (BuResource.BU.getString("Texture")+" 4",
1202   "DESKTOP_TEXTURE4",new BuTextureIcon
1203       (new BuIcon
1204     (BuPreferences.BU.getStringProperty
1205      ("desktop.image")).getImage()),true);
1206
1207    BuMenu mnCouleurs=new BuMenu
1208  (BuResource.BU.getString("Couleurs"),"MENU_COULEURS",
1209   new BuColorIcon(new Color(128,160,255)));
1210    mnCouleurs.addMenuItem
1211  (BuResource.BU.getString("Rouge"),
1212   "DESKTOP_ROUGE"     ,new BuColorIcon(new Color(128, 64, 64)),true);
1213    mnCouleurs.addMenuItem
1214  (BuResource.BU.getString("Vert"),
1215   "DESKTOP_VERT"      ,new BuColorIcon(new Color( 64,129, 64)),true);
1216    mnCouleurs.addMenuItem
1217  (BuResource.BU.getString("Bleu"),
1218   "DESKTOP_BLEU"      ,new BuColorIcon(new Color( 64, 64,128)),true);
1219    mnCouleurs.addMenuItem
1220  (BuResource.BU.getString("Orange"),
1221   "DESKTOP_ORANGE"    ,new BuColorIcon(new Color(192,128, 96)),true);
1222    mnCouleurs.addMenuItem
1223  (BuResource.BU.getString("Similaire"),
1224   "DESKTOP_SIMILAIRE" ,new BuColorIcon(UIManager.getColor("Panel.background"  )),true);
1225    mnCouleurs.addMenuItem
1226  (BuResource.BU.getString("Défaut"),
1227   "DESKTOP_DEFAUT"    ,new BuColorIcon(UIManager.getColor("Desktop.background")),true);
1228
1229    menu_.addSubMenu(mnTextures,true);
1230    menu_.addSubMenu(mnCouleurs,true);
1231
1232    return menu_;
1233  }
1234
1235  public void popupMenu(MouseEvent _evt)
1236  {
1237    // System.err.println("EVT="+_evt);
1238
1239    int xe=_evt.getX();
1240    int ye=_evt.getY();
1241
1242    BuBackgroundPainter bp=getBackgroundPainter();
1243    BuPopupMenu         mn=createPopupMenu();
1244
1245    BuActionChecker.setCheckedForAction(mn,"GRILLE",bp.isGrid());
1246    BuActionChecker.setCheckedForAction(mn,"POINTS",bp.isDots());
1247    BuActionChecker.setCheckedForAction
1248  (mn,"MAGNETISME",
1249   BuPreferences.BU.getBooleanProperty("desktop.snap",false));
1250
1251    // xe=20-mn.getSize().width;
1252    // ye-=5;
1253    mn.show((JComponent)_evt.getSource(),xe,ye);
1254  }
1255
1256  public void actionPerformed(ActionEvent _evt)
1257  {
1258    String action=_evt.getActionCommand();
1259    // System.err.println("BuDesktop ACTION="+action); 
1260
1261    BuBackgroundPainter bp=getBackgroundPainter();
1262
1263    if(action.equals("GRILLE"))
1264    {
1265      bp.setGrid(!bp.isGrid());
1266    }
1267    else if(action.equals("POINTS"))
1268    {
1269      bp.setDots(!bp.isDots());
1270    }
1271    else if(action.equals("MAGNETISME"))
1272    {
1273      BuPreferences.BU.putBooleanProperty
1274  ("desktop.snap",
1275   !BuPreferences.BU.getBooleanProperty("desktop.snap",false));
1276    }
1277    else if(action.equals("DESKTOP_UNIFORME"))
1278    {
1279      bp.setIcon(null);
1280      bp.setGradient(false);
1281    }
1282    else if(action.equals("DESKTOP_DEGRADE"))
1283    {
1284      bp.setIcon(null);
1285      bp.setGradient(true);
1286    }
1287    else if(action.equals("DESKTOP_TEXTURE4"))
1288    {
1289      bp.setIcon(new BuIcon(BuPreferences.BU.getStringProperty("desktop.image")));
1290    }
1291    else if(action.startsWith("DESKTOP_TEXTURE"))
1292    {
1293      bp.setIcon(BuResource.BU.getIcon("background_desktop_"+action.substring(15)));
1294    }
1295    else if(action.equals("DESKTOP_ROUGE"))
1296    { 
1297      setBackground(new Color(128,64,64));
1298      bp.setIcon(null);
1299    }
1300    else if(action.equals("DESKTOP_VERT"))
1301    {
1302      setBackground(new Color(64,128,64));
1303      bp.setIcon(null);
1304    }
1305    else if(action.equals("DESKTOP_BLEU"))
1306    { 
1307      setBackground(new Color(64,64,128));
1308      bp.setIcon(null);
1309    }
1310    else if(action.equals("DESKTOP_ORANGE"))
1311    { 
1312      setBackground(new Color(192,128,96));
1313      bp.setIcon(null);
1314    }
1315    else if(action.equals("DESKTOP_SIMILAIRE"))
1316    { 
1317      setBackground(UIManager.getColor("Panel.background"));
1318      bp.setIcon(null);
1319    }
1320    else if(action.equals("DESKTOP_DEFAUT"))
1321    { 
1322      setBackground(UIManager.getColor("Desktop.background"));
1323      bp.setIcon(null);
1324    }
1325
1326    setBackgroundPainter(bp);
1327  }
1328
1329  public void updateUI()
1330  {
1331    if(isShowing()) setBackground(null);
1332    super.updateUI();
1333  }
1334}