示例#1
0
  /**
   * Creates and returns the composite of the view.
   *
   * @return Composite
   */
  public Composite getComposite(Composite parent) {
    // composite is the composite we are building, it gets a formlayout
    Composite composite = new Composite(parent, SWT.NONE);
    FormLayout layout = new FormLayout();
    composite.setLayout(layout);
    layout.marginHeight = 5;
    layout.marginWidth = 5;
    FormData data;

    // creates the sash on the right of the tree
    final Sash vertSash = new Sash(composite, SWT.VERTICAL);
    data = new FormData();
    data.top = new FormAttachment(0, 0);
    data.bottom = new FormAttachment(100, 0);
    data.left = new FormAttachment(25, 0);
    vertSash.setLayoutData(data);
    vertSash.addSelectionListener(
        new SelectionAdapter() { // makes the sashes resizeable
          public void widgetSelected(SelectionEvent event) {
            ((FormData) vertSash.getLayoutData()).left = new FormAttachment(0, event.x);
            vertSash.getParent().layout();
          }
        });

    // construct tree in left side of composite
    Tree leftTree = new Tree(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    data = new FormData();
    data.bottom = new FormAttachment(100, 0);
    data.top = new FormAttachment(0, 0);
    data.right = new FormAttachment(vertSash, 0);
    data.left = new FormAttachment(0, 0);
    leftTree.setLayoutData(data);
    treeViewer = new TreeViewer(leftTree);
    treeViewer.setContentProvider(new MailBoxTreeContentProvider(control));
    treeViewer.setLabelProvider(new MailBoxTreeLabelProvider());
    treeViewer.setInput("root");

    // constructs the web browser
    Composite window = getFightPane(composite);
    data = new FormData();
    data.left = new FormAttachment(vertSash, 0);
    data.top = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.bottom = new FormAttachment(100, 0);
    window.setLayoutData(data);

    // references to the underlying tree object
    final Tree tree = treeViewer.getTree();
    // keylistener to handle deletions
    tree.addKeyListener(
        new KeyListener() {
          public void keyPressed(KeyEvent e) {
            if (e.character == 0x7f) {
              if (tree.getSelection().length == 1
                  && tree.getSelection()[0].getData() instanceof Folder) {
                // if there is one item selected and it is a folder, delete it
                deleteFolderAction.run();
              } else {
                // otherwise delete selected feeds
                unSubscribeAction.run();
              }
            }
          }

          public void keyReleased(KeyEvent e) {}
        });

    /// edits the right click menu to add appropriate actions
    final MenuManager treeMenuManager = new MenuManager();
    treeMenuManager.addMenuListener(
        new IMenuListener() {
          public void menuAboutToShow(IMenuManager arg0) {
            // first remove all actions, then add appropriate ones based on context
            treeMenuManager.removeAll();
            if (treeViewer.getSelection() instanceof IStructuredSelection) {
              IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
              if (selection.size() == 1) {
                Object o = selection.getFirstElement();
                // of the object is a folder add the following actions to the menu
                if (o instanceof Folder) {
                  Folder f = (Folder) o;
                  try {
                    treeMenuManager.add(subscribeAction);
                    treeMenuManager.add(new Separator());
                    treeMenuManager.add(newFolderAction);
                    // as long as its not the top folder we let the user delete it
                    if (f.getId() != control.getSubscribedFeeds().getId())
                      treeMenuManager.add(deleteFolderAction);
                  } catch (ControlException e) {
                    control.setStatus("Problem fetching top level folder");
                  }
                }
                // if its a feed we want different actions
                if (o instanceof Feed) {
                  Feed f = (Feed) o;
                  try {
                    // special actions for the trash and outbox
                    if (f.getId() == control.getTrash().getId()) {
                      treeMenuManager.add(emptyTrashAction);
                    } else if (f.getId() == control.getOutbox().getId()) {
                      treeMenuManager.add(newArticleAction);
                      treeMenuManager.add(exportOutboxAction);
                    } else {
                      // actions for normal feeds
                      treeMenuManager.add(updateFeedAction);
                      treeMenuManager.add(unSubscribeAction);
                      treeMenuManager.add(feedPropertiesAction);
                    }
                  } catch (ControlException e) {
                    control.setStatus("Problem fetching top level folder");
                  }
                }
              }
              // if there are multiple selections, do different things
              else if (selection.size() > 1) {
                boolean allFeeds = true;
                int trashId, outBoxId;
                // gets the trashid and boxid once so we don't need to query the database every
                // iteration
                // to check if the feed is trash or outbox
                try {
                  trashId = control.getTrash().getId();
                  outBoxId = control.getOutbox().getId();
                } catch (ControlException e) {
                  trashId = 0;
                  outBoxId = 0;
                }
                // gets iterator over selection
                Iterator iter = selection.iterator();
                // iterates through selection to check if the selection is only feeds
                while (iter.hasNext()) {
                  Object o = iter.next();
                  if (!(o instanceof Feed)) {
                    allFeeds = false;
                    break;
                  }
                  if (((Feed) o).getId() == trashId || ((Feed) o).getId() == outBoxId) {
                    allFeeds = false;
                    break;
                  }
                }
                if (allFeeds) {
                  // if the only selected items are feeds, then let them update all the selected
                  // feeds or unsubscribe
                  treeMenuManager.add(updateFeedAction);
                  treeMenuManager.add(unSubscribeAction);
                }
              }
            }
          }
        });
    // set the menu
    tree.setMenu(treeMenuManager.createContextMenu(tree));

    return composite;
  }
  @Override
  public void createPartControl(Composite parent) {

    this.setPartName("Ocaml Toplevel");

    composite = new Composite(parent, SWT.BORDER);
    composite.addControlListener(
        new ControlAdapter() {
          @Override
          public void controlResized(ControlEvent e) {
            resized();
          }
        });

    userText = new StyledText(this.composite, SWT.V_SCROLL);
    userText.setWordWrap(true);
    resultText = new StyledText(this.composite, SWT.V_SCROLL);
    resultText.setWordWrap(true);

    sash = new Sash(composite, SWT.HORIZONTAL | SWT.SMOOTH);

    sash.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            sash.setBounds(e.x, e.y, e.width, e.height);
            layout();
          }
        });

    Color c =
        Display.findDisplay(Thread.currentThread()).getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
    sash.setBackground(c);

    toplevel = new Toplevel(this, userText, resultText);

    IActionBars actionBars = this.getViewSite().getActionBars();
    IMenuManager dropDownMenu = actionBars.getMenuManager();
    IToolBarManager toolBarManager = actionBars.getToolBarManager();

    ImageDescriptor iconAdd = ImageRepository.getImageDescriptor(ImageRepository.ICON_ADD);
    Action actionNewToplevel =
        new Action("New toplevel", iconAdd) {
          @Override
          public void run() {
            try {
              IWorkbenchPage page =
                  PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
              OcamlToplevelView.this.secondaryId = "ocamltoplevelview" + new Random().nextInt();
              page.showView(ID, OcamlToplevelView.this.secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
            } catch (Exception e) {
              OcamlPlugin.logError("ocaml plugin error", e);
            }
          }
        };

    ImageDescriptor iconReset = ImageRepository.getImageDescriptor(ImageRepository.ICON_RESET);
    Action actionReset =
        new Action("Reset", iconReset) {
          @Override
          public void run() {
            toplevel.reset();
          }
        };

    ImageDescriptor iconInterrupt =
        ImageRepository.getImageDescriptor(ImageRepository.ICON_INTERRUPT);
    Action actionBreak =
        new Action("Interrupt", iconInterrupt) {
          @Override
          public void run() {
            toplevel.interrupt();
          }
        };

    ImageDescriptor iconClear = ImageRepository.getImageDescriptor(ImageRepository.ICON_CLEAR);
    Action actionClear =
        new Action("Clear", iconClear) {
          @Override
          public void run() {
            toplevel.clear();
          }
        };

    ImageDescriptor iconHelp = ImageRepository.getImageDescriptor(ImageRepository.ICON_HELP);
    Action actionHelp =
        new Action("Help", iconHelp) {
          @Override
          public void run() {
            toplevel.help();
          }
        };

    Action actionUseTopfind =
        new Action("Use Topfind") {
          @Override
          public void run() {
            toplevel.eval("#use \"topfind\";;");
          }
        };

    dropDownMenu.add(actionUseTopfind);

    toolBarManager.add(actionBreak);
    toolBarManager.add(actionClear);
    toolBarManager.add(actionReset);
    toolBarManager.add(actionNewToplevel);
    toolBarManager.add(actionHelp);

    if (bStartWhenCreated) toplevel.start();

    OcamlPlugin.setLastFocusedToplevelInstance(this);
  }
  public Composite createSashForm(final Composite composite) {
    if (!tv.isTabViewsEnabled()) {
      tableComposite = tv.createMainPanel(composite);
      return tableComposite;
    }

    ConfigurationManager configMan = ConfigurationManager.getInstance();

    int iNumViews = 0;

    UIFunctionsSWT uiFunctions = UIFunctionsManagerSWT.getUIFunctionsSWT();
    UISWTViewEventListenerWrapper[] pluginViews = null;
    if (uiFunctions != null) {
      UISWTInstance pluginUI = uiFunctions.getUISWTInstance();

      if (pluginUI != null) {
        pluginViews = pluginUI.getViewListeners(tv.getTableID());
        iNumViews += pluginViews.length;
      }
    }

    if (iNumViews == 0) {
      tableComposite = tv.createMainPanel(composite);
      return tableComposite;
    }

    FormData formData;

    final Composite form = new Composite(composite, SWT.NONE);
    FormLayout flayout = new FormLayout();
    flayout.marginHeight = 0;
    flayout.marginWidth = 0;
    form.setLayout(flayout);
    GridData gridData;
    gridData = new GridData(GridData.FILL_BOTH);
    form.setLayoutData(gridData);

    // Create them in reverse order, so we can have the table auto-grow, and
    // set the tabFolder's height manually

    final int TABHEIGHT = 20;
    tabFolder = new CTabFolder(form, SWT.TOP | SWT.BORDER);
    tabFolder.setMinimizeVisible(true);
    tabFolder.setTabHeight(TABHEIGHT);
    final int iFolderHeightAdj = tabFolder.computeSize(SWT.DEFAULT, 0).y;

    final Sash sash = new Sash(form, SWT.HORIZONTAL);

    tableComposite = tv.createMainPanel(form);
    Composite cFixLayout = tableComposite;
    while (cFixLayout != null && cFixLayout.getParent() != form) {
      cFixLayout = cFixLayout.getParent();
    }
    if (cFixLayout == null) {
      cFixLayout = tableComposite;
    }
    GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    cFixLayout.setLayout(layout);

    // FormData for Folder
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.bottom = new FormAttachment(100, 0);
    int iSplitAt = configMan.getIntParameter(tv.getPropertiesPrefix() + ".SplitAt", 3000);
    // Was stored at whole
    if (iSplitAt < 100) {
      iSplitAt *= 100;
    }

    double pct = iSplitAt / 10000.0;
    if (pct < 0.03) {
      pct = 0.03;
    } else if (pct > 0.97) {
      pct = 0.97;
    }

    // height will be set on first resize call
    sash.setData("PCT", new Double(pct));
    tabFolder.setLayoutData(formData);
    final FormData tabFolderData = formData;

    // FormData for Sash
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.bottom = new FormAttachment(tabFolder);
    formData.height = 5;
    sash.setLayoutData(formData);

    // FormData for table Composite
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.right = new FormAttachment(100, 0);
    formData.top = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment(sash);
    cFixLayout.setLayoutData(formData);

    // Listeners to size the folder
    sash.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            final boolean FASTDRAG = true;

            if (FASTDRAG && e.detail == SWT.DRAG) {
              return;
            }

            if (tabFolder.getMinimized()) {
              tabFolder.setMinimized(false);
              refreshSelectedSubView();
              ConfigurationManager configMan = ConfigurationManager.getInstance();
              configMan.setParameter(tv.getPropertiesPrefix() + ".subViews.minimized", false);
            }

            Rectangle area = form.getClientArea();
            tabFolderData.height = area.height - e.y - e.height - iFolderHeightAdj;
            form.layout();

            Double l = new Double((double) tabFolder.getBounds().height / form.getBounds().height);
            sash.setData("PCT", l);
            if (e.detail != SWT.DRAG) {
              ConfigurationManager configMan = ConfigurationManager.getInstance();
              configMan.setParameter(
                  tv.getPropertiesPrefix() + ".SplitAt", (int) (l.doubleValue() * 10000));
            }
          }
        });

    final CTabFolder2Adapter folderListener =
        new CTabFolder2Adapter() {
          public void minimize(CTabFolderEvent event) {
            tabFolder.setMinimized(true);
            tabFolderData.height = iFolderHeightAdj;
            CTabItem[] items = tabFolder.getItems();
            for (int i = 0; i < items.length; i++) {
              CTabItem tabItem = items[i];
              tabItem.getControl().setVisible(false);
            }
            form.layout();

            UISWTViewCore view = getActiveSubView();
            if (view != null) {
              view.triggerEvent(UISWTViewEvent.TYPE_FOCUSLOST, null);
            }

            ConfigurationManager configMan = ConfigurationManager.getInstance();
            configMan.setParameter(tv.getPropertiesPrefix() + ".subViews.minimized", true);
          }

          public void restore(CTabFolderEvent event) {
            tabFolder.setMinimized(false);
            CTabItem selection = tabFolder.getSelection();
            if (selection != null) {
              selection.getControl().setVisible(true);
            }
            form.notifyListeners(SWT.Resize, null);

            UISWTViewCore view = getActiveSubView();
            if (view != null) {
              view.triggerEvent(UISWTViewEvent.TYPE_FOCUSGAINED, null);
            }
            refreshSelectedSubView();

            ConfigurationManager configMan = ConfigurationManager.getInstance();
            configMan.setParameter(tv.getPropertiesPrefix() + ".subViews.minimized", false);
          }
        };
    tabFolder.addCTabFolder2Listener(folderListener);

    tabFolder.addSelectionListener(
        new SelectionListener() {
          public void widgetSelected(SelectionEvent e) {
            // make sure its above
            try {
              ((CTabItem) e.item).getControl().setVisible(true);
              ((CTabItem) e.item).getControl().moveAbove(null);

              // TODO: Need to viewDeactivated old one
              UISWTViewCore view = getActiveSubView();
              if (view != null) {
                view.triggerEvent(UISWTViewEvent.TYPE_FOCUSGAINED, null);
              }

            } catch (Exception t) {
            }
          }

          public void widgetDefaultSelected(SelectionEvent e) {}
        });

    tabFolder.addMouseListener(
        new MouseAdapter() {
          public void mouseDown(MouseEvent e) {
            if (tabFolder.getMinimized()) {
              folderListener.restore(null);
              // If the user clicked down on the restore button, and we restore
              // before the CTabFolder does, CTabFolder will minimize us again
              // There's no way that I know of to determine if the mouse is
              // on that button!

              // one of these will tell tabFolder to cancel
              e.button = 0;
              tabFolder.notifyListeners(SWT.MouseExit, null);
            }
          }
        });

    form.addListener(
        SWT.Resize,
        new Listener() {
          public void handleEvent(Event e) {
            if (tabFolder.getMinimized()) {
              return;
            }

            Double l = (Double) sash.getData("PCT");
            if (l != null) {
              tabFolderData.height =
                  (int) (form.getBounds().height * l.doubleValue()) - iFolderHeightAdj;
              form.layout();
            }
          }
        });

    // Call plugin listeners
    if (pluginViews != null) {
      for (UISWTViewEventListenerWrapper l : pluginViews) {
        if (l != null) {
          try {
            UISWTViewImpl view = new UISWTViewImpl(tv.getTableID(), l.getViewID(), l, null);
            addTabView(view);
          } catch (Exception e) {
            // skip, plugin probably specifically asked to not be added
          }
        }
      }
    }

    if (configMan.getBooleanParameter(tv.getPropertiesPrefix() + ".subViews.minimized", false)) {
      tabFolder.setMinimized(true);
      tabFolderData.height = iFolderHeightAdj;
    } else {
      tabFolder.setMinimized(false);
    }

    tabFolder.setSelection(0);

    return form;
  }