Ejemplo n.º 1
0
  private Section createFileSection(Composite container) {
    Section filesSection =
        toolkit.createSection(
            container, ExpandableComposite.TITLE_BAR | ExpandableComposite.CLIENT_INDENT);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(filesSection);
    Composite filesArea = toolkit.createComposite(filesSection);
    filesSection.setClient(filesArea);
    toolkit.paintBordersFor(filesArea);
    GridLayoutFactory.fillDefaults().extendedMargins(2, 2, 2, 2).applyTo(filesArea);

    ToolBar filesToolbar = new ToolBar(filesSection, SWT.FLAT);

    filesSection.setTextClient(filesToolbar);

    PatternFilter patternFilter =
        new PatternFilter() {
          @Override
          protected boolean isLeafMatch(Viewer viewer, Object element) {
            if (element instanceof CommitItem) {
              CommitItem commitItem = (CommitItem) element;
              return wordMatches(commitItem.path);
            }
            return super.isLeafMatch(viewer, element);
          }
        };
    patternFilter.setIncludeLeadingWildcard(true);
    FilteredCheckboxTree resourcesTreeComposite =
        new FilteredCheckboxTree(filesArea, toolkit, SWT.FULL_SELECTION, patternFilter) {
          @Override
          protected WorkbenchJob doCreateRefreshJob() {
            // workaround for file filter not having an explicit change
            // listener
            WorkbenchJob filterJob = super.doCreateRefreshJob();
            filterJob.addJobChangeListener(
                new JobChangeAdapter() {
                  public void done(IJobChangeEvent event) {
                    if (event.getResult().isOK()) {
                      getDisplay()
                          .asyncExec(
                              new Runnable() {
                                public void run() {
                                  updateFileSectionText();
                                }
                              });
                    }
                  }
                });
            return filterJob;
          }
        };
    Tree resourcesTree = resourcesTreeComposite.getViewer().getTree();
    resourcesTree.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TREE_BORDER);
    resourcesTreeComposite.setLayoutData(
        GridDataFactory.fillDefaults().hint(600, 200).grab(true, true).create());

    resourcesTree.addSelectionListener(new CommitItemSelectionListener());

    resourcesTree.setHeaderVisible(true);
    TreeColumn statCol = new TreeColumn(resourcesTree, SWT.LEFT);
    statCol.setText(UIText.CommitDialog_Status);
    statCol.setWidth(150);
    statCol.addSelectionListener(new HeaderSelectionListener(CommitItem.Order.ByStatus));

    TreeColumn resourceCol = new TreeColumn(resourcesTree, SWT.LEFT);
    resourceCol.setText(UIText.CommitDialog_Path);
    resourceCol.setWidth(415);
    resourceCol.addSelectionListener(new HeaderSelectionListener(CommitItem.Order.ByFile));

    filesViewer = resourcesTreeComposite.getCheckboxTreeViewer();
    new TreeViewerColumn(filesViewer, statCol).setLabelProvider(createStatusLabelProvider());
    new TreeViewerColumn(filesViewer, resourceCol).setLabelProvider(new CommitPathLabelProvider());
    ColumnViewerToolTipSupport.enableFor(filesViewer);
    filesViewer.setContentProvider(new CommitFileContentProvider());
    filesViewer.setUseHashlookup(true);
    IDialogSettings settings = org.eclipse.egit.ui.Activator.getDefault().getDialogSettings();
    if (settings.get(SHOW_UNTRACKED_PREF) != null) {
      // note, no matter how the dialog settings are, if
      // the preferences force us to include untracked files
      // we must show them
      showUntracked =
          Boolean.valueOf(settings.get(SHOW_UNTRACKED_PREF)).booleanValue()
              || getPreferenceStore().getBoolean(UIPreferences.COMMIT_DIALOG_INCLUDE_UNTRACKED);
    }

    filesViewer.addFilter(new CommitItemFilter());
    filesViewer.setInput(items.toArray());
    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    menuManager.addMenuListener(createContextMenuListener());
    filesViewer.getTree().setMenu(menuManager.createContextMenu(filesViewer.getTree()));
    filesViewer.addCheckStateListener(
        new ICheckStateListener() {

          public void checkStateChanged(CheckStateChangedEvent event) {
            updateFileSectionText();
          }
        });

    showUntrackedItem = new ToolItem(filesToolbar, SWT.CHECK);
    Image showUntrackedImage = UIIcons.UNTRACKED_FILE.createImage();
    UIUtils.hookDisposal(showUntrackedItem, showUntrackedImage);
    showUntrackedItem.setImage(showUntrackedImage);
    showUntrackedItem.setToolTipText(UIText.CommitDialog_ShowUntrackedFiles);
    showUntrackedItem.setSelection(showUntracked);
    showUntrackedItem.addSelectionListener(
        new SelectionAdapter() {

          public void widgetSelected(SelectionEvent e) {
            showUntracked = showUntrackedItem.getSelection();
            filesViewer.refresh(true);
            updateFileSectionText();
            updateMessage();
          }
        });

    ToolItem checkAllItem = new ToolItem(filesToolbar, SWT.PUSH);
    Image checkImage = UIIcons.CHECK_ALL.createImage();
    UIUtils.hookDisposal(checkAllItem, checkImage);
    checkAllItem.setImage(checkImage);
    checkAllItem.setToolTipText(UIText.CommitDialog_SelectAll);
    checkAllItem.addSelectionListener(
        new SelectionAdapter() {

          public void widgetSelected(SelectionEvent e) {
            filesViewer.setAllChecked(true);
            updateFileSectionText();
            updateMessage();
          }
        });

    ToolItem uncheckAllItem = new ToolItem(filesToolbar, SWT.PUSH);
    Image uncheckImage = UIIcons.UNCHECK_ALL.createImage();
    UIUtils.hookDisposal(uncheckAllItem, uncheckImage);
    uncheckAllItem.setImage(uncheckImage);
    uncheckAllItem.setToolTipText(UIText.CommitDialog_DeselectAll);
    uncheckAllItem.addSelectionListener(
        new SelectionAdapter() {

          public void widgetSelected(SelectionEvent e) {
            filesViewer.setAllChecked(false);
            updateFileSectionText();
            updateMessage();
          }
        });

    if (!allowToChangeSelection) {
      amendingItem.setSelection(false);
      amendingItem.setEnabled(false);
      showUntrackedItem.setSelection(false);
      showUntrackedItem.setEnabled(false);
      checkAllItem.setEnabled(false);
      uncheckAllItem.setEnabled(false);

      filesViewer.addCheckStateListener(
          new ICheckStateListener() {

            public void checkStateChanged(CheckStateChangedEvent event) {
              if (!event.getChecked()) filesViewer.setAllChecked(true);
              updateFileSectionText();
            }
          });
      filesViewer.setAllChecked(true);
    } else {
      final boolean includeUntracked =
          getPreferenceStore().getBoolean(UIPreferences.COMMIT_DIALOG_INCLUDE_UNTRACKED);
      for (CommitItem item : items) {
        if (!preselectAll && !preselectedFiles.contains(item.path)) continue;
        if (item.status == Status.ASSUME_UNCHANGED) continue;
        if (!includeUntracked && item.status == Status.UNTRACKED) continue;
        filesViewer.setChecked(item, true);
      }
    }
    statCol.pack();
    resourceCol.pack();
    return filesSection;
  }
  /** @see CustomReportDetailsPage#createControls(Composite) */
  @Override
  public Control createControls(Composite parent) {
    // filtered tree sets itself GridData, so set layout to gridlayout
    parent.setLayout(GridLayoutFactory.fillDefaults().create());

    // create pattern filter for FilteredTree
    PatternFilter filter = new TreePathPatternFilter();
    filter.setIncludeLeadingWildcard(true);

    // create FilteredTree
    FilteredTree filteredTree =
        new TreePathFilteredTree(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL, filter, true);

    treeViewer = filteredTree.getViewer();

    // set content provider
    contentProvider = new InstanceValidationReportDetailsContentProvider();
    treeViewer.setContentProvider(contentProvider);

    // set label provider
    treeViewer.setLabelProvider(new InstanceValidationReportDetailsLabelProvider(contentProvider));

    // set comparator
    treeViewer.setComparator(
        new ViewerComparator() {

          /** @see org.eclipse.jface.viewers.ViewerComparator#category(java.lang.Object) */
          @Override
          public int category(Object element) {
            if (element instanceof QName || element instanceof Definition<?>) return 0; // Path
            else if (element instanceof String) return 1; // Category
            else return 2; // InstanceValidationMessage
          }
        });

    // add menu on right-click
    MenuManager menuMgr = new MenuManager();
    Menu menu = menuMgr.createContextMenu(treeViewer.getTree());

    menuMgr.addMenuListener(
        new IMenuListener() {

          @Override
          public void menuAboutToShow(IMenuManager manager) {
            if (getValidSelection() == null) return;

            // selection is valid, offer link to transformed data view
            manager.add(new ShowExampleAction());
          }
        });

    // remove previous menus
    menuMgr.setRemoveAllWhenShown(true);

    // add menu to tree
    treeViewer.getTree().setMenu(menu);

    treeViewer.addDoubleClickListener(
        new IDoubleClickListener() {

          @Override
          public void doubleClick(DoubleClickEvent event) {
            showSelectionInDataView();
          }
        });

    Image noReportBaseImage =
        InstanceValidationUIPlugin.getDefault()
            .getImageRegistry()
            .get(InstanceValidationUIPlugin.IMG_INSTANCE_VALIDATION);
    reportImage =
        new DecorationOverlayIcon(
                noReportBaseImage,
                PlatformUI.getWorkbench()
                    .getSharedImages()
                    .getImageDescriptor(ISharedImages.IMG_DEC_FIELD_WARNING),
                IDecoration.BOTTOM_LEFT)
            .createImage();

    return filteredTree;
  }