home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / Viewers / Src / SplitViewerPanel.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  5.6 KB  |  205 lines

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4. import java.awt.Event;
  5. import java.awt.Insets;
  6. import java.awt.Point;
  7. import java.awt.Rectangle;
  8. import java.awt.Panel;
  9. import java.awt.GridLayout;
  10. import com.ms.ui.*;
  11. import com.ms.fx.*;
  12. import com.ms.object.*;
  13. import com.ms.object.dragdrop.*;
  14.  
  15. public class SplitViewerPanel extends UIPanel implements SDKConsts, IFxGraphicsConstants
  16. {
  17.     static public Class clrClass;
  18.     static { try { clrClass = Class.forName("com.ms.fx.FxColor"); } catch (Exception e) {} }
  19.  
  20.     public SplitViewerPanel()
  21.     {
  22.         DropPanel pnl1, pnl2, pnl3, pnl4, pnl5, pnl6, pnl7, pnl8;
  23.         UISplitViewer spv1, spv2, spv3, spv4, spv5, spv6, spv7, spv8;
  24.  
  25.         setLayout(new UIBorderLayout(5,10));
  26.         add(new UIText("Create your own Mondrian"), "North");
  27.  
  28.         pnl1 = new DropPanel(PURPLE);
  29.         pnl2 = new DropPanel(DEEP_BLUE);
  30.         pnl3 = new DropPanel(BR_RED);
  31.         pnl4 = new DropPanel(TEAL);
  32.         pnl5 = new DropPanel(KHAKI);
  33.         pnl6 = new DropPanel(WHITE);
  34.         pnl7 = new DropPanel(LIME);
  35.         pnl8 = new DropPanel(FOREST_GRN);
  36.  
  37.         spv1 = new UISplitViewer(null, null, 0, 100);
  38.         spv2 = new UISplitViewer(null, null, UISplitViewer.HORIZONTAL, 150);
  39.         spv3 = new UISplitViewer(null, null, 0, 60);
  40.         spv4 = new UISplitViewer(null, null, 0, 95);
  41.         spv5 = new UISplitViewer(null, null, UISplitViewer.HORIZONTAL, 40);
  42.         spv6 = new UISplitViewer(null, null, UISplitViewer.HORIZONTAL, 100);
  43.         spv7 = new UISplitViewer(null, null, 0, 60);
  44.  
  45.         spv1.add(pnl1, "nw"); spv1.add(spv2, "se");
  46.         spv2.add(spv3, "nw"); spv2.add(spv4, "se");
  47.         spv3.add(spv5, "nw"); spv3.add(spv6, "se");
  48.         spv4.add(pnl2, "nw"); spv4.add(pnl3, "se");
  49.         spv5.add(pnl4, "nw"); spv5.add(pnl5, "se");
  50.         spv6.add(spv7, "nw"); spv6.add(pnl6, "se");
  51.         spv7.add(pnl7, "nw"); spv7.add(pnl8, "se");
  52.  
  53.         add(spv1, "center");
  54.         spv1.setEdge(EDGE_SUNKEN);
  55.         spv1.setBackground(new FxColor(0,0,0));
  56.         add(new UIAwtHost(new ColorGrid()), "east");
  57.     }
  58.  
  59.     public Insets getInsets() { return new Insets(10,5,5,5); }
  60. }
  61.  
  62. class DropPanel extends UIPanel implements SDKConsts, DragHandler
  63. {
  64.     FxColor normal, hot;
  65.     boolean dohot = false;
  66.  
  67.     public DropPanel(int idx)
  68.     {
  69.         super();
  70.         normal = new FxColor(CLRS[idx][0], CLRS[idx][1], CLRS[idx][2]);
  71.         setFxColor(normal);
  72.     }
  73.  
  74.     private void setFxColor(FxColor c)
  75.     {
  76.         hot = HotColor.make(c);
  77.         normal = c;
  78.         hotTrack(dohot);
  79.     }
  80.  
  81.     private void hotTrack(boolean set)
  82.     {
  83.         dohot = set;
  84.         setBackground(dohot ? hot : normal);
  85.         if (isVisible())
  86.             repaint();
  87.     }
  88.  
  89.     public int dragOver(DragSession session, int x, int y)
  90.     {
  91.         MetaObject data = session.getTransferData();
  92.         if ((data != null) && data.hasObject(null, SplitViewerPanel.clrClass))
  93.             return session.getAvailableEffects() & Transfer.COPY;
  94.         return Transfer.NONE;
  95.     }
  96.  
  97.     public void drop(DragSession session, int x, int y)
  98.     {
  99.         MetaObject data = session.getTransferData();
  100.         if (data != null) {
  101.             Object object = data.getObject(null, SplitViewerPanel.clrClass);
  102.             if (object instanceof FxColor)
  103.                 setFxColor(HotColor.unmake((FxColor)object));
  104.         }
  105.     }
  106.  
  107.     // hot tracking serves to indicate drop target
  108.     public void dragEnter(DragSession session) { hotTrack(true); }
  109.     public void dragLeave() { hotTrack(false); }
  110. }
  111.  
  112. class ColorGrid extends Panel implements SDKConsts, DragHandler
  113. {
  114.     public ColorGrid()
  115.     {
  116.         setLayout(new GridLayout(0,2, 2,2));
  117.  
  118.         for ( int i = 0; i < NUM_COLORS; i++)
  119.             add(new AwtUIHost(new ColorPanel(i)));
  120.     }
  121.  
  122.     //
  123.     // Need to have an AWT component and have it be addNotify'd for DragnDrop to work
  124.     //  that's why this panel is a java.awt.Panel and not a UIPanel, and that the 
  125.     //  DragHandler methods below are NOPs.
  126.     //
  127.     public int dragOver(DragSession session, int x, int y) { return Transfer.NONE; }
  128.     public void drop(DragSession session, int x, int y) { }
  129.     public void dragEnter(DragSession session) { }
  130.     public void dragLeave() { }
  131. }
  132.  
  133. class ColorPanel extends UIPanel implements IFxGraphicsConstants
  134. {
  135.     public ColorPanel(int color)
  136.     {
  137.         setEdge(EDGE_SUNKEN);
  138.         setLayout(new UIBorderLayout());
  139.         add(new DragColor(color), "center");
  140.     }
  141.  
  142.     public Insets getInsets() { return new Insets(3,4,3,3); }
  143. }
  144.  
  145. class DragColor extends UIPanel implements SDKConsts
  146. {
  147.     FxColor normal, hot;
  148.     boolean dohot = false;
  149.  
  150.     public DragColor(int idx)
  151.     {
  152.         super();
  153.         normal = new FxColor(CLRS[idx][0], CLRS[idx][1], CLRS[idx][2]);
  154.         setFxColor(normal);
  155.     }
  156.  
  157.     private void setFxColor(FxColor c)
  158.     {
  159.         hot = HotColor.make(c);
  160.         normal = c;
  161.         hotTrack(dohot);
  162.     }
  163.  
  164.     private void hotTrack(boolean set)
  165.     {
  166.         dohot = set;
  167.         setBackground(dohot ? hot : normal);
  168.         if (isVisible())
  169.             repaint();
  170.     }
  171.  
  172.     public boolean mouseDown(Event e, int x, int y)
  173.     {
  174.         ObjectBag data = new ObjectBag();
  175.         data.addObject(null, null, hot);
  176.         UIDragDrop.beginDrag(data, Transfer.COPY | Transfer.LINK);
  177.         return true;
  178.     }
  179.  
  180.     // hot tracking serves to indicate drag target
  181.     public boolean mouseEnter(Event e, int x, int y) { hotTrack(true); return true; }
  182.     public boolean mouseExit(Event e, int x, int y) { hotTrack(false); return true; }
  183.  
  184.     public Insets getInsets() { return new Insets(8,8,8,8); }
  185. }
  186.  
  187. class HotColor
  188. {
  189.     public static FxColor make(FxColor c)
  190.     {
  191.         int r = c.getRed() - 25;    if ( r < 0 ) r = 0;
  192.         int g = c.getGreen() - 25;    if ( g < 0 ) g = 0;
  193.         int b = c.getBlue() - 25;    if ( b < 0 ) b = 0;
  194.         return new FxColor(r,g,b);
  195.     }
  196.  
  197.     public static FxColor unmake(FxColor c)
  198.     {
  199.         int r = c.getRed() + 25;    if ( r == 25 ) r = 0;
  200.         int g = c.getGreen() + 25;    if ( g == 25 ) g = 0;
  201.         int b = c.getBlue() + 25;    if ( b == 25 ) b = 0;
  202.         return new FxColor(r,g,b);
  203.     }
  204. }
  205.