home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / COMMON / TOOLS / VB / CABINETS / MSDAO350.CAB / icontrols / DataList / DataCombo.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-01-08  |  15.1 KB  |  526 lines

  1. package icontrols.DataList;
  2.  
  3. import com.ms.ado.DataSource;
  4. import com.ms.dll.DllLib;
  5. import com.ms.wd.app.SystemInformation;
  6. import com.ms.wd.core.Event;
  7. import com.ms.wd.core.EventHandler;
  8. import com.ms.wd.ui.Bitmap;
  9. import com.ms.wd.ui.Brush;
  10. import com.ms.wd.ui.Color;
  11. import com.ms.wd.ui.Control;
  12. import com.ms.wd.ui.Font;
  13. import com.ms.wd.ui.Graphics;
  14. import com.ms.wd.ui.MouseEvent;
  15. import com.ms.wd.ui.MouseEventHandler;
  16. import com.ms.wd.ui.PaintEvent;
  17. import com.ms.wd.ui.Pen;
  18. import com.ms.wd.ui.Point;
  19. import com.ms.wd.ui.Rectangle;
  20. import com.ms.wd.ui.Screen;
  21. import com.ms.wd.util.Root;
  22. import com.ms.wd.win32.MOUSEHOOKSTRUCT;
  23. import com.ms.wd.win32.TEXTMETRIC;
  24. import com.ms.wd.win32.Windows;
  25. import icontrols.BorderDisplayer;
  26. import icontrols.StringDisplayer;
  27. import icontrols.Data.RecordsetProvider;
  28.  
  29. public class DataCombo extends Control {
  30.    private RecordsetProvider recordsetProvider = new RecordsetProvider();
  31.    private ComboButton dropdownButton = new ComboButton();
  32.    private ComboList comboList = new ComboList();
  33.    private ComboEdit comboEdit = new ComboEdit();
  34.    private ComboDropDown dropDown = new ComboDropDown();
  35.    private StringDisplayer stringDisplayer = new StringDisplayer();
  36.    private BorderDisplayer borderDisplayer = new BorderDisplayer();
  37.    private int comboStyle = -1;
  38.    private boolean comboEditDisplayed = false;
  39.    private boolean comboListDisplayed = false;
  40.    private boolean dropdownButtonDisplayed = false;
  41.    private Rectangle clientRect = null;
  42.    private boolean dropDownVisible = false;
  43.    private String displayString = "";
  44.    private boolean autoComplete = true;
  45.    private Object currentSelection = null;
  46.    private int mouseHookRoot = 0;
  47.    private int mouseHookHandle = 0;
  48.    // $FF: synthetic field
  49.    private static Class class$com$ms$wd$win32$MOUSEHOOKSTRUCT;
  50.  
  51.    public void setListColumn(String column) {
  52.       this.comboList.setListColumn(column);
  53.    }
  54.  
  55.    public DataSource getRowSource() {
  56.       return this.recordsetProvider.getDataSource();
  57.    }
  58.  
  59.    private void setDisplayFromList() {
  60.       String data = this.comboList.getDisplayData(this.comboList.getValue());
  61.       this.displayString = data;
  62.       if (this.comboStyle != 1) {
  63.          this.comboEdit.setText(data);
  64.       } else {
  65.          ((Control)this).invalidate();
  66.       }
  67.  
  68.       this.currentSelection = null;
  69.    }
  70.  
  71.    public void setRowSource(DataSource dataSource) {
  72.       this.comboList.setRowSource(dataSource);
  73.       this.recordsetProvider.setDataSource(dataSource);
  74.    }
  75.  
  76.    private void setupDropDownList() {
  77.       this.dropDown.setComponent(this.comboList);
  78.    }
  79.  
  80.    private void doDropdownListLayout() {
  81.       Rectangle client = this.getCachedClientRect();
  82.       int borderHeight = this.borderDisplayer.getBorderHeight();
  83.       int borderWidth = this.borderDisplayer.getBorderWidth();
  84.       ComboButton var10000 = this.dropdownButton;
  85.       int var10001 = client.x + client.width - borderWidth - this.dropdownButton.getBounds().width;
  86.       int var10002 = client.y + borderHeight;
  87.       int var10004 = client.height - borderHeight * 2;
  88.       ((Control)var10000).setBounds(var10001, var10002, this.dropdownButton.getBounds().width, var10004);
  89.    }
  90.  
  91.    public int getStyle() {
  92.       return this.comboStyle;
  93.    }
  94.  
  95.    private void internalSetStyle(int style) {
  96.       if (this.comboStyle != style) {
  97.          boolean editDisplayed = false;
  98.          boolean listDisplayed = false;
  99.          boolean buttonDisplayed = false;
  100.          switch (style) {
  101.             case 0:
  102.                editDisplayed = true;
  103.                listDisplayed = false;
  104.                buttonDisplayed = true;
  105.                break;
  106.             case 1:
  107.                editDisplayed = false;
  108.                listDisplayed = false;
  109.                buttonDisplayed = true;
  110.                break;
  111.             case 2:
  112.                editDisplayed = true;
  113.                listDisplayed = true;
  114.                buttonDisplayed = false;
  115.          }
  116.  
  117.          if (this.comboEditDisplayed && !editDisplayed) {
  118.             ((Control)this).remove(this.comboEdit);
  119.          } else if (!this.comboEditDisplayed && editDisplayed) {
  120.             ((Control)this).add(this.comboEdit);
  121.          }
  122.  
  123.          if (this.comboListDisplayed && !listDisplayed) {
  124.             this.comboList.setBorderStyle(0);
  125.             this.setupDropDownList();
  126.             ((Control)this).remove(this.comboList);
  127.          } else if (!this.comboListDisplayed && listDisplayed) {
  128.             this.comboList.setBorderStyle(2);
  129.             this.clearDropDownList();
  130.             ((Control)this).add(this.comboList);
  131.          }
  132.  
  133.          if (this.dropdownButtonDisplayed && !buttonDisplayed) {
  134.             this.dropdownButton.setVisible(false);
  135.             ((Control)this).remove(this.dropdownButton);
  136.          } else if (!this.dropdownButtonDisplayed && buttonDisplayed) {
  137.             ((Control)this).add(this.dropdownButton);
  138.             this.dropdownButton.setVisible(true);
  139.          }
  140.  
  141.          this.comboStyle = style;
  142.          this.dropdownButtonDisplayed = buttonDisplayed;
  143.          this.comboListDisplayed = listDisplayed;
  144.          this.comboEditDisplayed = editDisplayed;
  145.          this.onResize(Event.EMPTY);
  146.       }
  147.    }
  148.  
  149.    public void setStyle(int style) {
  150.       if (this.comboStyle != style) {
  151.          this.internalSetStyle(style);
  152.       }
  153.  
  154.    }
  155.  
  156.    private synchronized void hookMouseMessages() {
  157.       this.mouseHookRoot = Root.alloc(new MouseHookProc(this));
  158.       int[] i = new int[1];
  159.       this.mouseHookHandle = Windows.SetWindowsHookEx(7, DllLib.addrOf(this.mouseHookRoot), Windows.GetModuleHandle((String)null), Windows.GetWindowThreadProcessId(((Control)this).getHandle(), i));
  160.    }
  161.  
  162.    private void clearDropDownList() {
  163.       this.hideDropdown();
  164.       this.dropDown.setComponent((Control)null);
  165.    }
  166.  
  167.    public void onParentChange(Object sender, Event event) {
  168.       this.hideDropdown();
  169.    }
  170.  
  171.    private void doSimpleComboLayout() {
  172.       Rectangle client = this.getCachedClientRect();
  173.       int borderHeight = this.borderDisplayer.getBorderHeight();
  174.       int borderWidth = this.borderDisplayer.getBorderWidth();
  175.       int height = this.computeEditHeight();
  176.       int listHeight = -1;
  177.       if (height > client.height - borderHeight * 2) {
  178.          height = client.height - borderHeight * 2;
  179.       } else {
  180.          listHeight = client.height - height - 2;
  181.          if (listHeight < 4) {
  182.             height = client.height - borderHeight * 2;
  183.             listHeight = -1;
  184.          }
  185.       }
  186.  
  187.       this.comboEdit.setBounds(client.x + borderWidth, client.y + borderHeight, client.width - borderWidth * 2, height - borderWidth * 2);
  188.       if (listHeight >= 0) {
  189.          this.comboList.setBounds(client.x, client.y + height + 2, client.width, listHeight);
  190.       }
  191.  
  192.    }
  193.  
  194.    private int computeEditHeight() {
  195.       Graphics g = ((Control)this).createGraphics();
  196.       g.setFont(((Control)this).getFont());
  197.       TEXTMETRIC tm = new TEXTMETRIC();
  198.       Windows.GetTextMetrics(g.getHandle(), tm);
  199.       int height = tm.tmHeight + 4 + 4;
  200.       g.dispose();
  201.       return height;
  202.    }
  203.  
  204.    public String getBoundColumn() {
  205.       return this.comboList.getBoundColumn();
  206.    }
  207.  
  208.    protected void onResize(Event event) {
  209.       this.hideDropdown();
  210.       this.clientRect = null;
  211.       this.doLayout();
  212.       if (this.comboStyle != 2) {
  213.          ((Control)this).invalidate();
  214.       } else {
  215.          int height = this.computeEditHeight();
  216.          int borderHeight = this.borderDisplayer.getBorderHeight();
  217.          if (height > this.getCachedClientRect().height - borderHeight * 2) {
  218.             height = this.getCachedClientRect().height - borderHeight * 2;
  219.          } else {
  220.             height += 2;
  221.          }
  222.  
  223.          ((Control)this).invalidateRect(new Rectangle(this.getCachedClientRect().x, this.getCachedClientRect().y, this.getCachedClientRect().width, height));
  224.       }
  225.  
  226.       this.dropdownButton.invalidate();
  227.    }
  228.  
  229.    public void setBoundColumn(String column) {
  230.       this.comboList.setBoundColumn(column);
  231.    }
  232.  
  233.    public void onButtonClick(Object sender, Event event) {
  234.       if (this.dropDownVisible) {
  235.          this.hideDropdown();
  236.       } else {
  237.          this.showDropdown();
  238.       }
  239.  
  240.    }
  241.  
  242.    private void doLayout() {
  243.       switch (this.comboStyle) {
  244.          case 0:
  245.             this.doDropdownComboLayout();
  246.             break;
  247.          case 1:
  248.             this.doDropdownListLayout();
  249.             break;
  250.          case 2:
  251.             this.doSimpleComboLayout();
  252.       }
  253.  
  254.    }
  255.  
  256.    private void hideDropdown() {
  257.       if (this.dropDown != null && this.comboStyle != 2 && this.dropDownVisible) {
  258.          Windows.SetWindowPos(this.dropDown.getHandle(), 0, 0, 0, 0, 0, 407);
  259.          this.dropDownVisible = false;
  260.          ((Control)this).getParent().removeOnMove(new EventHandler(this, "onParentChange"));
  261.          ((Control)this).getParent().removeOnResize(new EventHandler(this, "onParentChange"));
  262.          this.comboList.setTrackSelection(false);
  263.          if (this.comboStyle == 0) {
  264.             this.comboEdit.removeOnLostFocus(new EventHandler(this, "onEditLostFocus"));
  265.          }
  266.  
  267.          this.comboList.removeOnMouseUp(new MouseEventHandler(this, "onListMouseUp"));
  268.          if (this.currentSelection != null) {
  269.             this.recordsetProvider.getRecordset().move(0, this.currentSelection);
  270.          }
  271.  
  272.          this.unhookMouseMessages();
  273.       }
  274.  
  275.    }
  276.  
  277.    int mouseHookProc(int nCode, int wParam, int lParam) {
  278.       if (nCode == 0) {
  279.          int swap = Windows.GetSystemMetrics(23);
  280.          int mouseWatch = 1;
  281.          if (swap != 0) {
  282.             mouseWatch = 2;
  283.          }
  284.  
  285.          if ((Windows.GetAsyncKeyState(mouseWatch) & 'ΦÇÇ') != 0) {
  286.             MOUSEHOOKSTRUCT mhs = (MOUSEHOOKSTRUCT)DllLib.ptrToStruct(class$com$ms$wd$win32$MOUSEHOOKSTRUCT != null ? class$com$ms$wd$win32$MOUSEHOOKSTRUCT : (class$com$ms$wd$win32$MOUSEHOOKSTRUCT = class$("com.ms.wd.win32.MOUSEHOOKSTRUCT")), lParam);
  287.             if (mhs.hwnd != ((Control)this).getHandle() && mhs.hwnd != this.comboEdit.getHandle() && mhs.hwnd != this.dropdownButton.getHandle() && !this.comboList.isHandleOwnedByList(mhs.hwnd)) {
  288.                this.hideDropdown();
  289.             }
  290.          }
  291.       }
  292.  
  293.       return Windows.CallNextHookEx(this.mouseHookHandle, nCode, wParam, lParam);
  294.    }
  295.  
  296.    public void onEditLostFocus(Object sender, Event event) {
  297.       this.hideDropdown();
  298.    }
  299.  
  300.    protected void onLostFocus(Event event) {
  301.       this.hideDropdown();
  302.    }
  303.  
  304.    private synchronized void unhookMouseMessages() {
  305.       Windows.UnhookWindowsHookEx(this.mouseHookHandle);
  306.       Root.free(this.mouseHookRoot);
  307.    }
  308.  
  309.    public String getValue() {
  310.       return this.comboList.getValue();
  311.    }
  312.  
  313.    public void setValue(String value) {
  314.       this.comboList.setValue(value);
  315.    }
  316.  
  317.    public DataCombo() {
  318.       ((Control)this).setBackColor(Color.WINDOW);
  319.       this.borderDisplayer.setBorderStyle(18);
  320.       this.stringDisplayer.setFont((Font)null);
  321.       this.stringDisplayer.setForeColor((Color)null);
  322.       this.stringDisplayer.setBackColor((Color)null);
  323.       this.stringDisplayer.setAlignment(5);
  324.       this.comboList.setBorderStyle(0);
  325.       this.comboList.addValueChangedHandler(new EventHandler(this, "onListValueChanged"));
  326.       this.comboEdit.setBorder(false);
  327.       this.dropdownButton.addOnClick(new EventHandler(this, "onButtonClick"));
  328.       this.dropdownButton.setBackColor(Color.CONTROL);
  329.       this.buildDropDownArrow();
  330.       this.setupDropDownList();
  331.       this.internalSetStyle(0);
  332.       ((Control)this).setBounds(0, 0, 121, 21);
  333.    }
  334.  
  335.    public boolean getAutoComplete() {
  336.       return this.autoComplete;
  337.    }
  338.  
  339.    public void setAutoComplete(boolean autoComplete) {
  340.    }
  341.  
  342.    public void onListMouseUp(Object sender, MouseEvent event) {
  343.       this.hideDropdown();
  344.       this.setDisplayFromList();
  345.    }
  346.  
  347.    public String getRowMember() {
  348.       return this.recordsetProvider.getDataMember();
  349.    }
  350.  
  351.    protected void processEditChar(int key) {
  352.       String pressed = new String(new char[]{(char)key});
  353.       String newSearchString = null;
  354.       int originalSelStart = this.comboEdit.getSelectionStart();
  355.       String text = this.comboEdit.getText();
  356.       if (text.length() > 0) {
  357.          String selText = text.substring(this.comboEdit.getSelectionStart(), this.comboEdit.getSelectionEnd());
  358.          String unselectedText = text.substring(0, originalSelStart);
  359.          if (selText.length() > 1 && pressed.equalsIgnoreCase(selText.substring(0, 0))) {
  360.             this.comboEdit.setSelectionStart(this.comboEdit.getSelectionStart() + 1);
  361.             return;
  362.          }
  363.  
  364.          newSearchString = unselectedText + pressed;
  365.       } else {
  366.          newSearchString = pressed;
  367.       }
  368.  
  369.       String selected = this.comboList.selectByMatch(newSearchString);
  370.       if (this.getAutoComplete() && selected != null) {
  371.          this.comboEdit.setText(selected);
  372.          this.comboEdit.setSelectionEnd(selected.length());
  373.          if (originalSelStart < 1) {
  374.             this.comboEdit.setSelectionStart(1);
  375.          } else {
  376.             this.comboEdit.setSelectionStart(newSearchString.length());
  377.          }
  378.       } else {
  379.          this.comboEdit.setText(newSearchString);
  380.          this.comboEdit.setSelectionEnd(newSearchString.length());
  381.          this.comboEdit.setSelectionStart(newSearchString.length());
  382.       }
  383.  
  384.    }
  385.  
  386.    public void setRowMember(String dataMember) {
  387.       this.comboList.setRowMember(dataMember);
  388.       this.recordsetProvider.setDataMember(dataMember);
  389.    }
  390.  
  391.    private void buildDropDownArrow() {
  392.       Bitmap bmp = new Bitmap(8, 8);
  393.       Graphics g = bmp.getGraphics();
  394.       g.setBrush(new Brush(Color.CONTROL));
  395.       g.setPen((Pen)null);
  396.       g.drawRect(new Rectangle(0, 0, 8, 8));
  397.       g.setPen(new Pen(Color.CONTROLTEXT));
  398.       int x = 3;
  399.       int y = 5;
  400.       g.setPixel(x, y);
  401.  
  402.       for(int i = 1; i < 4; ++i) {
  403.          g.drawLine(x - i, y - i, x + i + 1, y - i);
  404.       }
  405.  
  406.       g.dispose();
  407.       this.dropdownButton.setStretch(false);
  408.       this.dropdownButton.setImage(bmp);
  409.       this.dropdownButton.setBounds(new Rectangle(0, 0, SystemInformation.getVertScrollBarWidth(), 1));
  410.    }
  411.  
  412.    private void doDropdownComboLayout() {
  413.       Rectangle client = this.getCachedClientRect();
  414.       int borderHeight = this.borderDisplayer.getBorderHeight();
  415.       int borderWidth = this.borderDisplayer.getBorderWidth();
  416.       this.comboEdit.setBounds(client.x + borderWidth, client.y + borderHeight, client.width - borderWidth * 2 - this.dropdownButton.getBounds().width, client.height - borderHeight * 2);
  417.       ComboButton var10000 = this.dropdownButton;
  418.       int var10001 = client.x + client.width - borderWidth - this.dropdownButton.getBounds().width;
  419.       int var10002 = client.y + borderHeight;
  420.       int var10004 = client.height - borderHeight * 2;
  421.       ((Control)var10000).setBounds(var10001, var10002, this.dropdownButton.getBounds().width, var10004);
  422.    }
  423.  
  424.    public void onListValueChanged(Object sender, Event event) {
  425.       this.setDisplayFromList();
  426.       this.hideDropdown();
  427.    }
  428.  
  429.    // $FF: synthetic method
  430.    private static Class class$(String s) {
  431.       try {
  432.          return Class.forName(s);
  433.       } catch (ClassNotFoundException e) {
  434.          throw new NoClassDefFoundError(((Throwable)e).getMessage());
  435.       }
  436.    }
  437.  
  438.    private Rectangle getCachedClientRect() {
  439.       if (this.clientRect == null) {
  440.          this.clientRect = ((Control)this).getClientRect();
  441.       }
  442.  
  443.       return this.clientRect;
  444.    }
  445.  
  446.    protected void onMouseDown(MouseEvent event) {
  447.       if (this.comboStyle == 1) {
  448.          this.onButtonClick(this, event);
  449.       }
  450.  
  451.    }
  452.  
  453.    private void showDropdown() {
  454.       if (this.dropDown != null && this.comboStyle != 2) {
  455.          Rectangle client = new Rectangle(this.getCachedClientRect());
  456.          Rectangle screen = Screen.fromControl(this).workingArea;
  457.          int comboHeight = client.height;
  458.          Point loc = ((Control)this).pointToScreen(new Point(0, 0));
  459.          client.y += loc.y;
  460.          client.x += loc.x;
  461.          client.y += comboHeight;
  462.          client.height *= 4;
  463.          if (client.y + client.height > screen.y + screen.height) {
  464.             client.y -= client.height + comboHeight;
  465.          }
  466.  
  467.          this.dropDown.setFont(((Control)this).getFont());
  468.          this.dropDown.setBackColor(((Control)this).getBackColor());
  469.          this.dropDown.setForeColor(((Control)this).getForeColor());
  470.          Windows.SetWindowPos(this.dropDown.getHandle(), -1, client.x, client.y, client.width, client.height, 336);
  471.          this.dropDown.setVisible(true);
  472.          this.comboList.setTrackSelection(true);
  473.          ((Control)this).getParent().addOnMove(new EventHandler(this, "onParentChange"));
  474.          ((Control)this).getParent().addOnResize(new EventHandler(this, "onParentChange"));
  475.          if (this.comboStyle == 0) {
  476.             this.comboEdit.focus();
  477.             this.comboEdit.addOnLostFocus(new EventHandler(this, "onEditLostFocus"));
  478.          }
  479.  
  480.          this.comboList.addOnMouseUp(new MouseEventHandler(this, "onListMouseUp"));
  481.          if (this.recordsetProvider.getRecordset() != null) {
  482.             this.currentSelection = this.recordsetProvider.getRecordset().getBookmark();
  483.          }
  484.  
  485.          this.hookMouseMessages();
  486.          this.dropDownVisible = true;
  487.       }
  488.  
  489.    }
  490.  
  491.    protected void onPaint(PaintEvent pe) {
  492.       int borderHeight = this.borderDisplayer.getBorderHeight();
  493.       int borderWidth = this.borderDisplayer.getBorderWidth();
  494.       if (this.comboStyle != 2) {
  495.          this.borderDisplayer.paintIn(pe.graphics, this.getCachedClientRect(), pe.clipRect);
  496.       } else {
  497.          Rectangle top = new Rectangle(this.getCachedClientRect());
  498.          int height = this.computeEditHeight();
  499.          if (height > this.getCachedClientRect().height - borderHeight * 2) {
  500.             height = this.getCachedClientRect().height - borderHeight * 2;
  501.          } else {
  502.             top.y += height;
  503.             top.height = 2;
  504.             pe.graphics.setBrush(new Brush(Color.CONTROL));
  505.             pe.graphics.setPen((Pen)null);
  506.             pe.graphics.drawRect(top);
  507.             top.y -= height;
  508.          }
  509.  
  510.          top.height = height;
  511.          this.borderDisplayer.paintIn(pe.graphics, top, pe.clipRect);
  512.       }
  513.  
  514.       if (this.comboStyle == 1) {
  515.          Rectangle bounds = new Rectangle(this.getCachedClientRect());
  516.          bounds.inflateRect(-1 * (borderWidth + 1), -1 * borderHeight);
  517.          this.stringDisplayer.paintIn(pe.graphics, bounds, pe.clipRect, this.displayString);
  518.       }
  519.  
  520.    }
  521.  
  522.    public String getListColumn() {
  523.       return this.comboList.getListColumn();
  524.    }
  525. }
  526.