Advertisement
Guest User

Peter Severin

a guest
Jun 5th, 2009
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.64 KB | None | 0 0
  1. package com.wireframesketcher.spy;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. import java.util.Arrays;
  6. import java.util.regex.Pattern;
  7.  
  8. import org.eclipse.jface.action.Action;
  9. import org.eclipse.swt.SWT;
  10. import org.eclipse.swt.custom.CCombo;
  11. import org.eclipse.swt.custom.CLabel;
  12. import org.eclipse.swt.custom.CTabFolder;
  13. import org.eclipse.swt.custom.CTabItem;
  14. import org.eclipse.swt.custom.StyledText;
  15. import org.eclipse.swt.graphics.Font;
  16. import org.eclipse.swt.graphics.GC;
  17. import org.eclipse.swt.graphics.Point;
  18. import org.eclipse.swt.graphics.Rectangle;
  19. import org.eclipse.swt.widgets.Button;
  20. import org.eclipse.swt.widgets.Canvas;
  21. import org.eclipse.swt.widgets.Combo;
  22. import org.eclipse.swt.widgets.Composite;
  23. import org.eclipse.swt.widgets.Control;
  24. import org.eclipse.swt.widgets.CoolBar;
  25. import org.eclipse.swt.widgets.CoolItem;
  26. import org.eclipse.swt.widgets.DateTime;
  27. import org.eclipse.swt.widgets.Display;
  28. import org.eclipse.swt.widgets.Group;
  29. import org.eclipse.swt.widgets.Label;
  30. import org.eclipse.swt.widgets.Link;
  31. import org.eclipse.swt.widgets.List;
  32. import org.eclipse.swt.widgets.Menu;
  33. import org.eclipse.swt.widgets.MenuItem;
  34. import org.eclipse.swt.widgets.ProgressBar;
  35. import org.eclipse.swt.widgets.Sash;
  36. import org.eclipse.swt.widgets.Scale;
  37. import org.eclipse.swt.widgets.Shell;
  38. import org.eclipse.swt.widgets.Slider;
  39. import org.eclipse.swt.widgets.Spinner;
  40. import org.eclipse.swt.widgets.TabFolder;
  41. import org.eclipse.swt.widgets.TabItem;
  42. import org.eclipse.swt.widgets.Table;
  43. import org.eclipse.swt.widgets.TableColumn;
  44. import org.eclipse.swt.widgets.TableItem;
  45. import org.eclipse.swt.widgets.Text;
  46. import org.eclipse.swt.widgets.ToolBar;
  47. import org.eclipse.swt.widgets.ToolItem;
  48. import org.eclipse.swt.widgets.Tree;
  49. import org.eclipse.swt.widgets.TreeColumn;
  50. import org.eclipse.swt.widgets.TreeItem;
  51.  
  52. import com.wireframesketcher.model.Checkbox;
  53. import com.wireframesketcher.model.FontSupport;
  54. import com.wireframesketcher.model.Icon;
  55. import com.wireframesketcher.model.IconDesc;
  56. import com.wireframesketcher.model.IconSize;
  57. import com.wireframesketcher.model.ModelFactory;
  58. import com.wireframesketcher.model.Panel;
  59. import com.wireframesketcher.model.RadioButton;
  60. import com.wireframesketcher.model.ResizeMode;
  61. import com.wireframesketcher.model.Screen;
  62. import com.wireframesketcher.model.SearchField;
  63. import com.wireframesketcher.model.Tabs;
  64. import com.wireframesketcher.model.TextAlignment;
  65. import com.wireframesketcher.model.TextAlignmentSupport;
  66. import com.wireframesketcher.model.TextArea;
  67. import com.wireframesketcher.model.TextField;
  68. import com.wireframesketcher.model.Widget;
  69. import com.wireframesketcher.model.Window;
  70.  
  71. public class Converter {
  72.     private final Shell shell;
  73.  
  74.     private final ModelFactory factory = ModelFactory.eINSTANCE;
  75.  
  76.     private final Screen screen;
  77.  
  78.     private final Point fontExtents;
  79.  
  80.     private Point origin = new Point(25, 25);
  81.  
  82.     public Converter(Shell shell) {
  83.         this.shell = shell;
  84.         this.screen = factory.createScreen();
  85.         this.fontExtents = computeFontExtents(shell.getDisplay(), shell
  86.                 .getDisplay().getSystemFont());
  87.         convert(shell);
  88.     }
  89.  
  90.     private static Point computeFontExtents(Display display, Font font) {
  91.         GC gc = new GC(display);
  92.         try {
  93.             return gc.textExtent("");
  94.         } finally {
  95.             gc.dispose();
  96.         }
  97.     }
  98.  
  99.     private void convert(Control control) {
  100.         if (control == null)
  101.             return;
  102.  
  103.         if (!control.isVisible())
  104.             return;
  105.  
  106.         Point size = control.getSize();
  107.         if (size.x == 0 || size.y == 0)
  108.             return;
  109.  
  110.         String methodName = "convert" + control.getClass().getSimpleName();
  111.         Method method = null;
  112.         try {
  113.             method = getClass().getMethod(methodName,
  114.                     new Class[] { control.getClass() });
  115.         } catch (SecurityException e) {
  116.             throw new RuntimeException(e);
  117.         } catch (NoSuchMethodException e) {
  118.             // ignore
  119.             System.out.println("Unhandled widget type: "
  120.                     + control.getClass().getName());
  121.         }
  122.  
  123.         if (method != null) {
  124.             try {
  125.                 method.invoke(this, new Object[] { control });
  126.             } catch (IllegalArgumentException e) {
  127.                 throw new RuntimeException(e);
  128.             } catch (IllegalAccessException e) {
  129.                 throw new RuntimeException(e);
  130.             } catch (InvocationTargetException e) {
  131.                 throw new RuntimeException(e);
  132.             }
  133.         } else if (control instanceof Canvas) {
  134.             if (size.x > 30 && size.y > 30) {
  135.                 if (size.x > 50 && size.y > 50)
  136.                     add(factory.createPlaceholder(), control);
  137.                 else
  138.                     add(factory.createPanel(), control);
  139.             }
  140.         }
  141.  
  142.         if (control instanceof Composite && !isWidgetComposite(control)) {
  143.             Composite composite = (Composite) control;
  144.             Control[] children = composite.getChildren();
  145.             for (int i = 0; i < children.length; i++) {
  146.                 convert(children[i]);
  147.             }
  148.         }
  149.     }
  150.  
  151.     private boolean isWidgetComposite(Control control) {
  152.         if (control instanceof DateTime)
  153.             return true;
  154.  
  155.         return false;
  156.     }
  157.  
  158.     private void add(Widget widget, int x, int y, int width, int height) {
  159.         widget.setX(origin.x + x);
  160.         widget.setY(origin.y + y);
  161.         if ((widget.getDescriptor().getResizeMode().getValue() & ResizeMode.HORIZONTAL) != 0
  162.                 && !(widget instanceof com.wireframesketcher.model.Label))
  163.             widget.setWidth(width);
  164.         if ((widget.getDescriptor().getResizeMode().getValue() & ResizeMode.VERTICAL) != 0)
  165.             widget.setHeight(height);
  166.  
  167.         screen.getWidgets().add(widget);
  168.     }
  169.  
  170.     private void add(Widget widget, Control control) {
  171.         add(widget, control, 0, 0);
  172.     }
  173.  
  174.     private void add(Widget widget, Control control, int dx, int dy) {
  175.         Rectangle bounds = control.getBounds();
  176.         int x = bounds.x, y = bounds.y;
  177.         if (control.getParent() != null && !(control instanceof Shell)) {
  178.             Point location = control.getParent().toDisplay(x, y);
  179.             x = location.x;
  180.             y = location.y;
  181.         }
  182.         x -= shell.getBounds().x;
  183.         y -= shell.getBounds().y;
  184.  
  185.         x += dx;
  186.         y += dy;
  187.  
  188.         add(widget, x, y, bounds.width - dx, bounds.height - dy);
  189.     }
  190.  
  191.     private void add(Widget widget, Composite parent, Rectangle bounds, int dx,
  192.             int dy) {
  193.         Point location = parent.toDisplay(bounds.x, bounds.y);
  194.         int x = location.x;
  195.         int y = location.y;
  196.         x -= shell.getBounds().x;
  197.         y -= shell.getBounds().y;
  198.  
  199.         add(widget, x + dx, y + dy, bounds.width - dx, bounds.height - dy);
  200.     }
  201.  
  202.     public void convertShell(Shell control) {
  203.         Window window = factory.createWindow();
  204.         window.setText(control.getText());
  205.         add(window, control);
  206.  
  207.         if (control.getMenuBar() != null) {
  208.             Menu menuWidget = control.getMenuBar();
  209.             com.wireframesketcher.model.Menu menu = factory.createMenu();
  210.             MenuItem[] items = menuWidget.getItems();
  211.             StringBuilder content = new StringBuilder();
  212.             for (int i = 0; i < items.length; i++) {
  213.                 content.append(Action.removeMnemonics(items[i].getText()));
  214.                 if (i < items.length - 1)
  215.                     content.append(',');
  216.             }
  217.             menu.setText(content.toString());
  218.             Rectangle bounds = control.getBounds();
  219.             add(menu, 0, 19, bounds.width, -1);
  220.         }
  221.     }
  222.  
  223.     public void convertGroup(Group control) {
  224.         com.wireframesketcher.model.Group group = factory.createGroup();
  225.         group.setText(Action.removeMnemonics(control.getText()));
  226.         add(group, control);
  227.     }
  228.  
  229.     public void convertButton(Button control) {
  230.         Widget widget = null;
  231.  
  232.         if ((control.getStyle() & SWT.CHECK) != 0) {
  233.             Checkbox checkbox = factory.createCheckbox();
  234.             checkbox.setSelected(control.getSelection());
  235.             widget = checkbox;
  236.         } else if ((control.getStyle() & SWT.RADIO) != 0) {
  237.             RadioButton radioButton = factory.createRadioButton();
  238.             radioButton.setSelected(control.getSelection());
  239.             widget = radioButton;
  240.         } else if ((control.getStyle() & SWT.PUSH) != 0) {
  241.             com.wireframesketcher.model.Button button = factory.createButton();
  242.             widget = button;
  243.         } else if ((control.getStyle() & SWT.ARROW) != 0) {
  244.             com.wireframesketcher.model.Button button = factory.createButton();
  245.             String iconName;
  246.  
  247.             if ((control.getStyle() & SWT.UP) != 0)
  248.                 iconName = "empty-arrow-up";
  249.             else if ((control.getStyle() & SWT.LEFT) != 0)
  250.                 iconName = "empty-arrow-left";
  251.             else if ((control.getStyle() & SWT.RIGHT) != 0)
  252.                 iconName = "empty-arrow-right";
  253.             else
  254.                 iconName = "empty-arrow-down";
  255.  
  256.             button.setIcon(new IconDesc(iconName, IconSize.SMALL_LITERAL));
  257.  
  258.             widget = button;
  259.         }
  260.  
  261.         if (widget != null)
  262.             widget.setText(Action.removeMnemonics(control.getText()));
  263.  
  264.         if (widget != null)
  265.             add(widget, control);
  266.     }
  267.  
  268.     public void convertLabel(Label control) {
  269.         if ((control.getStyle() & SWT.SEPARATOR) != 0) {
  270.             add(factory.createHLine(), control);
  271.         } else {
  272.             if (control.getText().trim().length() > 0) {
  273.                 if (isMultiLine(control)) {
  274.                     com.wireframesketcher.model.Text text = factory
  275.                             .createText();
  276.                     text.setText(Action.removeMnemonics(control.getText()));
  277.                     applyAlignment(text, control);
  278.                     applyFont(text, control);
  279.                     add(text, control);
  280.                 } else {
  281.                     com.wireframesketcher.model.Label label = factory
  282.                             .createLabel();
  283.                     label.setText(Action.removeMnemonics(control.getText()));
  284.                     applyAlignment(label, control);
  285.                     applyFont(label, control);
  286.                     add(label, control);
  287.                 }
  288.             } else if (control.getImage() != null) {
  289.                 add(factory.createPlaceholder(), control);
  290.             }
  291.         }
  292.     }
  293.  
  294.     public void convertCLabel(CLabel control) {
  295.         if (control.getText() != null && control.getText().trim().length() > 0) {
  296.             if (isMultiLine(control)) {
  297.                 com.wireframesketcher.model.Text text = factory.createText();
  298.                 text.setText(Action.removeMnemonics(control.getText()));
  299.                 applyAlignment(text, control);
  300.                 applyFont(text, control);
  301.                 add(text, control);
  302.             } else {
  303.                 com.wireframesketcher.model.Label label = factory.createLabel();
  304.                 label.setText(Action.removeMnemonics(control.getText()));
  305.                 applyAlignment(label, control);
  306.                 applyFont(label, control);
  307.                 add(label, control);
  308.             }
  309.         } else if (control.getImage() != null) {
  310.             add(factory.createPlaceholder(), control);
  311.         }
  312.     }
  313.  
  314.     public void convertText(Text control) {
  315.         if ((control.getStyle() & SWT.SEARCH) != 0) {
  316.             SearchField searchField = factory.createSearchField();
  317.             searchField.setText(control.getText());
  318.             add(searchField, control);
  319.         } else if (isUsedAsLabel(control)) {
  320.             if (isMultiLine(control)) {
  321.                 com.wireframesketcher.model.Text text = factory.createText();
  322.                 text.setText(control.getText());
  323.                 applyAlignment(text, control);
  324.                 applyFont(text, control);
  325.                 add(text, control);
  326.             } else {
  327.                 com.wireframesketcher.model.Label label = factory.createLabel();
  328.                 label.setText(control.getText());
  329.                 applyAlignment(label, control);
  330.                 applyFont(label, control);
  331.                 add(label, control);
  332.             }
  333.         } else {
  334.             if (isMultiLine(control)) {
  335.                 TextArea textArea = factory.createTextArea();
  336.                 textArea.setText(control.getText());
  337.                 add(textArea, control);
  338.             } else {
  339.                 TextField textField = factory.createTextField();
  340.                 if ((control.getStyle() & SWT.PASSWORD) != 0) {
  341.                     textField.setText(createPasswordText(control.getText()));
  342.                 } else {
  343.                     textField.setText(control.getText());
  344.                 }
  345.  
  346.                 add(textField, control);
  347.             }
  348.         }
  349.     }
  350.  
  351.     public void convertCombo(Combo control) {
  352.         com.wireframesketcher.model.Combo combo = factory.createCombo();
  353.         combo.setText(control.getText());
  354.         add(combo, control);
  355.  
  356.         if ((control.getStyle() & SWT.SIMPLE) != 0) {
  357.             com.wireframesketcher.model.List list = factory.createList();
  358.             String[] items = control.getItems();
  359.             StringBuilder content = new StringBuilder();
  360.             for (int i = 0; i < items.length; i++) {
  361.                 content.append(items[i]);
  362.                 content.append('\n');
  363.             }
  364.             list.setText(content.toString());
  365.             list.setSelection(Integer.toString(control.getSelectionIndex()));
  366.             add(list, control, 0, 22);
  367.             // force auto-size
  368.             list.setWidth(-1);
  369.             list.setHeight(-1);
  370.         }
  371.     }
  372.  
  373.     public void convertCCombo(CCombo control) {
  374.         com.wireframesketcher.model.Combo combo = factory.createCombo();
  375.         combo.setText(control.getText());
  376.         add(combo, control);
  377.     }
  378.  
  379.     public void convertLink(Link control) {
  380.         com.wireframesketcher.model.Link link = factory.createLink();
  381.         link.setText(removeLinkTags(control.getText()));
  382.         add(link, control);
  383.     }
  384.  
  385.     public void convertTree(Tree control) {
  386.         TreeColumn[] columns = control.getColumns();
  387.         if (columns.length > 1) {
  388.             com.wireframesketcher.model.Table table = factory.createTable();
  389.             StringBuilder content = new StringBuilder();
  390.             if (columns.length > 0) {
  391.                 for (int i = 0; i < columns.length; i++) {
  392.                     content.append(columns[i].getText());
  393.                     if (i < columns.length - 1)
  394.                         content.append(',');
  395.                 }
  396.                 content.append('\n');
  397.             }
  398.  
  399.             TreeItem[] items = control.getItems();
  400.             for (int i = 0; i < items.length; i++) {
  401.                 TreeItem item = items[i];
  402.                 if (isItemVisible(item)) {
  403.                     for (int j = 0; j < columns.length; j++) {
  404.                         content.append(item.getText(j));
  405.                         if (j < columns.length - 1)
  406.                             content.append(',');
  407.                     }
  408.                     content.append('\n');
  409.                 }
  410.             }
  411.  
  412.             table.setText(content.toString());
  413.             add(table, control);
  414.         } else {
  415.             com.wireframesketcher.model.Tree tree = factory.createTree();
  416.             TreeItem[] items = control.getItems();
  417.             StringBuilder content = new StringBuilder();
  418.             for (int i = 0; i < items.length; i++)
  419.                 convertTreeItem(items[i], content, 0);
  420.             tree.setText(content.toString());
  421.             add(tree, control);
  422.         }
  423.     }
  424.  
  425.     private void convertTreeItem(TreeItem item, StringBuilder content,
  426.             int indent) {
  427.         String text = item.getText();
  428.         if (text != null && text.length() > 0) {
  429.             for (int i = 0; i < indent; i++)
  430.                 content.append('-');
  431.             content.append(text);
  432.             content.append('\n');
  433.             TreeItem[] items = item.getItems();
  434.             if (items != null)
  435.                 for (int i = 0; i < items.length; i++)
  436.                     convertTreeItem(items[i], content, indent + 1);
  437.         }
  438.     }
  439.  
  440.     public void convertTable(Table control) {
  441.         com.wireframesketcher.model.Table table = factory.createTable();
  442.         StringBuilder content = new StringBuilder();
  443.         TableColumn[] columns = control.getColumns();
  444.         if (columns.length > 0) {
  445.             for (int i = 0; i < columns.length; i++) {
  446.                 content.append(columns[i].getText());
  447.                 if (i < columns.length - 1)
  448.                     content.append(',');
  449.             }
  450.             content.append('\n');
  451.         }
  452.  
  453.         TableItem[] items = control.getItems();
  454.         for (int i = 0; i < items.length; i++) {
  455.             TableItem item = items[i];
  456.  
  457.             if (isItemVisible(item)) {
  458.                 if (columns.length > 0) {
  459.                     for (int j = 0; j < columns.length; j++) {
  460.                         content.append(item.getText(j));
  461.                         if (j < columns.length - 1)
  462.                             content.append(',');
  463.                     }
  464.                 } else {
  465.                     content.append(item.getText());
  466.                 }
  467.                 content.append('\n');
  468.             }
  469.         }
  470.  
  471.         table.setText(content.toString());
  472.  
  473.         if (control.getSelectionIndex() != -1)
  474.             table.setSelection(Integer.toString(control.getSelectionIndex()));
  475.  
  476.         add(table, control);
  477.     }
  478.  
  479.     public void convertSlider(Slider control) {
  480.         if ((control.getStyle() & SWT.VERTICAL) != 0)
  481.             add(factory.createVScrollbar(), control);
  482.         else
  483.             add(factory.createHScrollbar(), control);
  484.  
  485.     }
  486.  
  487.     public void convertTabFolder(TabFolder control) {
  488.         if (control.getItemCount() == 0)
  489.             return;
  490.  
  491.         {
  492.             TabItem[] selection = control.getSelection();
  493.  
  494.             if (selection != null && selection.length > 0
  495.                     && selection[0].getControl() != null
  496.                     && selection[0].getControl().isVisible()) {
  497.                 Panel panel = factory.createPanel();
  498.                 add(panel, control, 0, computeTabsHeight());
  499.             }
  500.         }
  501.  
  502.         Tabs tabs = factory.createTabs();
  503.         StringBuilder content = new StringBuilder();
  504.         TabItem[] items = control.getItems();
  505.         for (int i = 0; i < items.length; i++) {
  506.             content.append(Action.removeMnemonics(items[i].getText()));
  507.             if (i < items.length - 1)
  508.                 content.append(',');
  509.         }
  510.         tabs.setText(content.toString());
  511.         tabs.setSelection(Integer.toString(control.getSelectionIndex()));
  512.         add(tabs, control);
  513.     }
  514.  
  515.     public void convertCTabFolder(CTabFolder control) {
  516.         if (control.getItemCount() == 0)
  517.             return;
  518.  
  519.         {
  520.             CTabItem selection = control.getSelection();
  521.  
  522.             if (selection != null && selection.getControl() != null
  523.                     && selection.getControl().isVisible()) {
  524.                 Panel panel = factory.createPanel();
  525.                 add(panel, control, 0, computeTabsHeight());
  526.             }
  527.         }
  528.  
  529.         Tabs tabs = factory.createTabs();
  530.         StringBuilder content = new StringBuilder();
  531.         CTabItem[] items = control.getItems();
  532.         for (int i = 0; i < items.length; i++) {
  533.             if (items[i].isShowing()) {
  534.                 if (content.length() > 0)
  535.                     content.append(',');
  536.                 content.append(Action.removeMnemonics(items[i].getText()));
  537.             }
  538.         }
  539.         tabs.setText(content.toString());
  540.         tabs.setSelection(Integer.toString(control.getSelectionIndex()));
  541.         add(tabs, control);
  542.     }
  543.  
  544.     public void convertToolBar(ToolBar control) {
  545.         ToolItem[] items = control.getItems();
  546.         for (int i = 0; i < items.length; i++) {
  547.             ToolItem item = items[i];
  548.             if ((item.getStyle() & SWT.SEPARATOR) != 0)
  549.                 if ((control.getStyle() & SWT.VERTICAL) != 0)
  550.                     add(factory.createHLine(), control, item.getBounds(), 0, 0);
  551.                 else
  552.                     add(factory.createVLine(), control, item.getBounds(), 0, 0);
  553.             else {
  554.                 {
  555.                     Icon icon = factory.createIcon();
  556.                     icon.setIcon(new IconDesc("image", IconSize.SMALL_LITERAL));
  557.                     add(icon, control, item.getBounds(), 6, 6);
  558.                 }
  559.  
  560.                 if ((item.getStyle() & SWT.DROP_DOWN) != 0) {
  561.                     Icon icon = factory.createIcon();
  562.                     icon.setIcon(new IconDesc("empty-arrow-down",
  563.                             IconSize.SMALL_LITERAL));
  564.                     add(icon, control, item.getBounds(), 6 + 16, 6);
  565.                 }
  566.             }
  567.         }
  568.     }
  569.  
  570.     public void convertCoolBar(CoolBar control) {
  571.         CoolItem[] items = control.getItems();
  572.         for (int i = 0; i < items.length; i++) {
  573.             CoolItem item = items[i];
  574.             if ((control.getStyle() & SWT.VERTICAL) != 0)
  575.                 add(factory.createHLine(), control, item.getBounds(), 0, 0);
  576.             else
  577.                 add(factory.createVLine(), control, item.getBounds(), 0, 0);
  578.         }
  579.     }
  580.  
  581.     public void convertSash(Sash control) {
  582.         if ((control.getStyle() & SWT.VERTICAL) != 0) {
  583.             add(factory.createVLine(), control);
  584.         } else {
  585.             add(factory.createHLine(), control);
  586.         }
  587.     }
  588.  
  589.     public void convertProgressBar(ProgressBar control) {
  590.         add(factory.createProgressBar(), control);
  591.     }
  592.  
  593.     public void convertComposite(Composite control) {
  594.         // if ((control.getStyle() & SWT.BORDER) != 0)
  595.         // add(factory.createPanel(), control);
  596.     }
  597.  
  598.     public void convertSpinner(Spinner control) {
  599.         com.wireframesketcher.model.Spinner spinner = factory.createSpinner();
  600.         spinner.setText(Integer.toString(control.getSelection()));
  601.         add(spinner, control);
  602.     }
  603.  
  604.     public void convertScale(Scale control) {
  605.         if ((control.getStyle() & SWT.VERTICAL) != 0)
  606.             add(factory.createVSlider(), control);
  607.         else
  608.             add(factory.createHSlider(), control);
  609.     }
  610.  
  611.     public void convertStyledText(StyledText control) {
  612.         if (isUsedAsLabel(control)) {
  613.             com.wireframesketcher.model.Text text = factory.createText();
  614.             text.setText(control.getText());
  615.             applyAlignment(text, control);
  616.             applyFont(text, control);
  617.             add(text, control);
  618.         } else {
  619.             TextArea textArea = factory.createTextArea();
  620.             textArea.setText(control.getText());
  621.             add(textArea, control);
  622.         }
  623.     }
  624.  
  625.     public void convertList(List control) {
  626.         com.wireframesketcher.model.List list = factory.createList();
  627.         String[] items = control.getItems();
  628.         StringBuilder content = new StringBuilder();
  629.         for (int i = 0; i < items.length; i++) {
  630.             if (content.length() > 0)
  631.                 content.append('\n');
  632.             content.append(items[i]);
  633.         }
  634.         list.setText(content.toString());
  635.  
  636.         if (control.getSelectionIndex() != -1)
  637.             list.setSelection(Integer.toString(control.getSelectionIndex()));
  638.  
  639.         add(list, control);
  640.     }
  641.  
  642.     public void convertDateTime(DateTime control) {
  643.         // XXX: Handle SWT.CALENDAR style
  644.         if ((control.getStyle() & SWT.CALENDAR) == 0) {
  645.             TextField textField = factory.createTextField();
  646.  
  647.             if (control.getChildren().length > 0
  648.                     && control.getChildren()[0] instanceof Text)
  649.                 textField.setText(((Text) control.getChildren()[0]).getText());
  650.  
  651.             add(textField, control);
  652.         }
  653.     }
  654.  
  655.     private int computeTabsHeight() {
  656.         return (int) (fontExtents.y * 1.8) - 1;
  657.     }
  658.  
  659.     private boolean isItemVisible(TableItem item) {
  660.         Point size = item.getParent().getSize();
  661.         Rectangle itemBounds = item.getBounds();
  662.         return itemBounds.y >= 0 && itemBounds.y < size.y
  663.                 || itemBounds.y + itemBounds.height > 5
  664.                 && itemBounds.y + itemBounds.height <= size.y;
  665.     }
  666.  
  667.     private boolean isItemVisible(TreeItem item) {
  668.         Point size = item.getParent().getSize();
  669.         Rectangle itemBounds = item.getBounds();
  670.         return itemBounds.y >= 0 && itemBounds.y < size.y
  671.                 || itemBounds.y + itemBounds.height > 5
  672.                 && itemBounds.y + itemBounds.height <= size.y;
  673.     }
  674.  
  675.     private static boolean isMultiLine(Control control) {
  676.         if ((control.getStyle() & SWT.H_SCROLL) != 0)
  677.             return true;
  678.         if ((control.getStyle() & SWT.V_SCROLL) != 0)
  679.             return true;
  680.         if ((control.getStyle() & SWT.MULTI) != 0)
  681.             return true;
  682.  
  683.         if (control instanceof Label || control instanceof CLabel) {
  684.             int fontHeight = getFontHeight(control);
  685.             return control.getSize().y >= 2 * fontHeight;
  686.         }
  687.  
  688.         return false;
  689.     }
  690.  
  691.     private static int getFontHeight(Control control) {
  692.         GC gc = new GC(control);
  693.         try {
  694.             return gc.getFontMetrics().getHeight();
  695.         } finally {
  696.             gc.dispose();
  697.         }
  698.     }
  699.  
  700.     private static void applyFont(FontSupport widget, Control control) {
  701.         int fontStyle = control.getFont().getFontData()[0].getStyle();
  702.         if ((fontStyle & SWT.BOLD) != 0)
  703.             widget.getFont().setBold(Boolean.TRUE);
  704.         if ((fontStyle & SWT.ITALIC) != 0)
  705.             widget.getFont().setItalic(Boolean.TRUE);
  706.     }
  707.  
  708.     private static void applyAlignment(TextAlignmentSupport widget,
  709.             Control control) {
  710.         if ((control.getStyle() & SWT.CENTER) != 0)
  711.             widget.setTextAlignment(TextAlignment.CENTER_LITERAL);
  712.         else if ((control.getStyle() & SWT.LEFT) != 0)
  713.             widget.setTextAlignment(TextAlignment.LEFT_LITERAL);
  714.         else if ((control.getStyle() & SWT.RIGHT) != 0)
  715.             widget.setTextAlignment(TextAlignment.RIGHT_LITERAL);
  716.     }
  717.  
  718.     private static boolean isUsedAsLabel(Control control) {
  719.         if ((control.getStyle() & SWT.BORDER) == 0
  720.                 && (control.getStyle() & SWT.READ_ONLY) != 0
  721.                 && control.getBackground().equals(
  722.                         control.getParent().getBackground()))
  723.             return true;
  724.         return false;
  725.     }
  726.  
  727.     private static String createPasswordText(String text) {
  728.         char[] chars = new char[text.length()];
  729.         Arrays.fill(chars, '*');
  730.         return new String(chars);
  731.     }
  732.  
  733.     private String removeLinkTags(String s) {
  734.         Pattern openTag = Pattern.compile("<a.*?>", Pattern.CASE_INSENSITIVE);
  735.         Pattern closeTag = Pattern.compile("</a>", Pattern.CASE_INSENSITIVE);
  736.         s = openTag.matcher(s).replaceAll("");
  737.         s = closeTag.matcher(s).replaceAll("");
  738.         return s;
  739.     }
  740.  
  741.     public Screen getScreen() {
  742.         return screen;
  743.     }
  744. }
  745.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement