Пример #1
0
 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setLayout(new FillLayout());
   final Table table = new Table(shell, SWT.BORDER);
   table.setHeaderVisible(true);
   final TableColumn column1 = new TableColumn(table, SWT.NONE);
   column1.setText("Column 1");
   final TableColumn column2 = new TableColumn(table, SWT.NONE);
   column2.setText("Column 2");
   TableItem item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"a", "3"});
   item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"b", "2"});
   item = new TableItem(table, SWT.NONE);
   item.setText(new String[] {"c", "1"});
   column1.setWidth(100);
   column2.setWidth(100);
   Listener sortListener =
       e -> {
         TableItem[] items = table.getItems();
         Collator collator = Collator.getInstance(Locale.getDefault());
         TableColumn column = (TableColumn) e.widget;
         int index = column == column1 ? 0 : 1;
         for (int i = 1; i < items.length; i++) {
           String value1 = items[i].getText(index);
           for (int j = 0; j < i; j++) {
             String value2 = items[j].getText(index);
             if (collator.compare(value1, value2) < 0) {
               String[] values = {items[i].getText(0), items[i].getText(1)};
               items[i].dispose();
               TableItem item1 = new TableItem(table, SWT.NONE, j);
               item1.setText(values);
               items = table.getItems();
               break;
             }
           }
         }
         table.setSortColumn(column);
       };
   column1.addListener(SWT.Selection, sortListener);
   column2.addListener(SWT.Selection, sortListener);
   table.setSortColumn(column1);
   table.setSortDirection(SWT.UP);
   shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   display.dispose();
 }
Пример #2
0
  /** Create internal GUI. */
  private void createComponents(Map<String, Object> currentValues) {
    /*
     * Sort alphabetically by label.
     */
    final Locale locale = Locale.getDefault();
    final Map<String, String> labels = Maps.newHashMap();
    for (Map.Entry<String, AttributeDescriptor> entry : attributeDescriptors.entrySet()) {
      labels.put(entry.getKey(), getLabel(entry.getValue()).toLowerCase(locale));
    }

    final Collator collator = Collator.getInstance(locale);
    final List<String> sortedKeys = Lists.newArrayList(labels.keySet());
    Collections.sort(
        sortedKeys,
        new Comparator<String>() {
          public int compare(String a, String b) {
            return collator.compare(labels.get(a), labels.get(b));
          }
        });

    /*
     * Create editors and inquire about their layout needs.
     */
    editors = Maps.newHashMap();
    final Map<String, AttributeEditorInfo> editorInfos = Maps.newHashMap();

    int maxColumns = 1;
    for (String key : sortedKeys) {
      final AttributeDescriptor descriptor = attributeDescriptors.get(key);

      IAttributeEditor editor = null;
      try {
        editor = EditorFactory.getEditorFor(this.componentClazz, descriptor);
        final AttributeEditorInfo info =
            editor.init(bindable, descriptor, globalEventsProvider, currentValues);

        editorInfos.put(key, info);
        maxColumns = Math.max(maxColumns, info.columns);
      } catch (EditorNotFoundException ex) {
        Utils.logError(
            "No editor for attribute: " + descriptor.key + " (class: " + descriptor.type + ")",
            false);

        /*
         * Skip editor.
         */
        editor = null;
      }

      editors.put(key, editor);
    }

    /*
     * Prepare the layout for this editor.
     */
    final GridLayout layout = GUIFactory.zeroMarginGridLayout();
    layout.makeColumnsEqualWidth = false;

    layout.numColumns = maxColumns;
    this.setLayout(layout);

    /*
     * Create visual components for editors.
     */
    final GridDataFactory labelFactory = GridDataFactory.fillDefaults().span(maxColumns, 1);

    boolean firstEditor = true;
    for (String key : sortedKeys) {
      final AttributeDescriptor descriptor = attributeDescriptors.get(key);
      final IAttributeEditor editor = editors.get(key);
      final AttributeEditorInfo editorInfo = editorInfos.get(key);

      if (editor == null) {
        // Skip attributes without the editor.
        continue;
      }

      final Object defaultValue;
      if (currentValues != null && currentValues.get(key) != null) {
        defaultValue = currentValues.get(key);
      } else {
        defaultValue = attributeDescriptors.get(key).defaultValue;
      }

      // Add label to editors that do not have it.
      if (!editorInfo.displaysOwnLabel) {
        final Label label = new Label(this, SWT.LEAD);
        final GridData gd = labelFactory.create();
        if (!firstEditor) {
          gd.verticalIndent = SPACE_BEFORE_LABEL;
        }
        label.setLayoutData(gd);

        label.setText(getLabel(descriptor) + (descriptor.requiredAttribute ? " (required)" : ""));

        /*
         * Add validation overlay.
         */
        addValidationOverlay(descriptor, editor, defaultValue, label);

        AttributeInfoTooltip.attach(label, descriptor);
      }

      // Add the editor, if available.
      editor.createEditor(this, maxColumns);

      // Set the default value for the editor.
      editor.setValue(defaultValue);
      editors.put(editor.getAttributeKey(), editor);

      /*
       * Forward events from this editor to all our listeners.
       */
      editor.addAttributeListener(forwardListener);

      firstEditor = false;
    }
  }