Esempio n. 1
0
  /* (non-Javadoc)
   * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
   */
  @Override
  public ISecurity unmarshal(String v) throws Exception {
    if (v == null) {
      return null;
    }

    URI uri = new URI(v);
    if (repositoryService == null) {
      try {
        BundleContext context = CoreActivator.getDefault().getBundle().getBundleContext();
        ServiceReference serviceReference =
            context.getServiceReference(IRepositoryService.class.getName());
        repositoryService = (IRepositoryService) context.getService(serviceReference);
        context.ungetService(serviceReference);
      } catch (Exception e) {
        Status status =
            new Status(
                IStatus.ERROR, CoreActivator.PLUGIN_ID, 0, "Error reading repository service", e);
        CoreActivator.log(status);
      }
    }

    ISecurity security =
        repositoryService != null ? repositoryService.getSecurityFromURI(uri) : null;
    if (security == null) {
      Status status =
          new Status(
              IStatus.WARNING,
              CoreActivator.PLUGIN_ID,
              0,
              "Failed to load security " + uri.toString(),
              null);
      CoreActivator.log(status);
      return new FailsafeSecurity(uri);
    }

    return security;
  }
  /* (non-Javadoc)
   * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite, org.eclipse.ui.IMemento)
   */
  @Override
  public void init(IViewSite site, IMemento memento) throws PartInitException {
    super.init(site, memento);

    BundleContext bundleContext = UIActivator.getDefault().getBundle().getBundleContext();

    ServiceReference<ITradingSystemService> serviceReference =
        bundleContext.getServiceReference(ITradingSystemService.class);
    tradingSystemService = bundleContext.getService(serviceReference);

    activator = CoreActivator.getDefault();

    IDialogSettings rootDialogSettings = UIActivator.getDefault().getDialogSettings();
    dialogSettings = rootDialogSettings.getSection(VIEW_ID);
    if (dialogSettings == null) {
      dialogSettings = rootDialogSettings.addNewSection(VIEW_ID);
      dialogSettings.put(
          COLUMNS,
          new String[] {
            "org.eclipsetrader.ui.providers.LastTrade", //$NON-NLS-1$
            "org.eclipsetrader.ui.providers.BidPrice", //$NON-NLS-1$
            "org.eclipsetrader.ui.providers.AskPrice", //$NON-NLS-1$
            "org.eclipsetrader.ui.providers.Position", //$NON-NLS-1$
            "org.eclipsetrader.ui.providers.LastTradeDateTime", //$NON-NLS-1$
            "org.eclipsetrader.ui.providers.gain", //$NON-NLS-1$
          });
      IDialogSettings section = dialogSettings.addNewSection(COLUMN_NAMES);
      section.put("org.eclipsetrader.ui.providers.LastTrade", "Last"); // $NON-NLS-1$
      section.put("org.eclipsetrader.ui.providers.BidPrice", "Bid"); // $NON-NLS-1$
      section.put("org.eclipsetrader.ui.providers.AskPrice", "Ask"); // $NON-NLS-1$
      section.put("org.eclipsetrader.ui.providers.Position", "Position"); // $NON-NLS-1$
      section.put("org.eclipsetrader.ui.providers.LastTradeDateTime", "Date / Time"); // $NON-NLS-1$
      section.put("org.eclipsetrader.ui.providers.gain", "Gain"); // $NON-NLS-1$
      dialogSettings.addNewSection(COLUMN_WIDTHS);
    }

    settingsAction = new SettingsAction(site.getShell(), this);

    IActionBars actionBars = site.getActionBars();
    actionBars.setGlobalActionHandler(settingsAction.getId(), settingsAction);
    actionBars.updateActionBars();
  }
  /* (non-Javadoc)
   * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
   */
  @Override
  public void createPartControl(Composite parent) {
    viewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.MULTI);
    viewer.getTree().setHeaderVisible(true);
    viewer.getTree().setLinesVisible(false);

    createContextMenu();

    model = new TradingSystemsViewModel(tradingSystemService);

    String[] columns = dialogSettings.getArray(COLUMNS);
    IDialogSettings namesSection = dialogSettings.getSection(COLUMN_NAMES);

    List<ViewColumn> list = new ArrayList<ViewColumn>();
    for (int i = 0; i < columns.length; i++) {
      String name = namesSection.get(columns[i]);
      list.add(new ViewColumn(name, activator.getDataProviderFactory(columns[i])));
    }
    model.setDataProviders(list);

    for (final TradingSystemItem item : model.getList()) {
      PropertyChangeSupport changeSupport =
          (PropertyChangeSupport) item.getTradingSystem().getAdapter(PropertyChangeSupport.class);
      if (changeSupport != null) {
        changeSupport.addPropertyChangeListener(
            new PropertyChangeListener() {

              @Override
              public void propertyChange(PropertyChangeEvent evt) {
                Display.getDefault()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            if (viewer.getControl().isDisposed()) {
                              return;
                            }
                            viewer.refresh(item);
                          }
                        });
              }
            });
      }
    }

    final ObservableListTreeContentProvider contentProvider =
        new ObservableListTreeContentProvider(model, model);
    viewer.setContentProvider(contentProvider);

    IDialogSettings widthSettings = dialogSettings.getSection(COLUMN_WIDTHS);

    TreeViewerColumn viewerColumn = new TreeViewerColumn(viewer, SWT.NONE);

    int width = 300;
    if (widthSettings.get(LABEL_COLUMN) != null) {
      width = widthSettings.getInt(LABEL_COLUMN);
    }
    viewerColumn.getColumn().setWidth(width);

    viewerColumn.getColumn().addControlListener(columnControlListener);

    final List<IObservableMap> properties = new ArrayList<IObservableMap>();
    properties.add(new ViewerObservableMap(contentProvider.getKnownElements(), LABEL_COLUMN));
    for (ViewColumn column : model.getDataProviders()) {
      viewerColumn = new TreeViewerColumn(viewer, SWT.RIGHT);
      viewerColumn.getColumn().setText(column.getName());

      width = 100;
      if (widthSettings.get(column.getDataProviderFactory().getId()) != null) {
        width = widthSettings.getInt(column.getDataProviderFactory().getId());
      }
      viewerColumn.getColumn().setWidth(width);

      viewerColumn.getColumn().addControlListener(columnControlListener);
      column.addPropertyChangeListener(ViewColumn.PROP_NAME, columnPropertiesChangeListener);
      properties.add(
          new ViewerObservableMap(
              contentProvider.getKnownElements(), column.getDataProviderFactory().getId()));
    }
    createLabelProvider(properties.toArray(new IObservableMap[properties.size()]));

    model
        .getObservableDataProviders()
        .addListChangeListener(
            new IListChangeListener() {

              @Override
              public void handleListChange(ListChangeEvent event) {
                event.diff.accept(
                    new ListDiffVisitor() {

                      @Override
                      public void handleRemove(int index, Object element) {
                        TreeColumn treeColumn = viewer.getTree().getColumn(index + 1);
                        treeColumn.dispose();

                        ViewColumn column = (ViewColumn) element;
                        column.removePropertyChangeListener(
                            ViewColumn.PROP_NAME, columnPropertiesChangeListener);

                        IObservableMap observableMap = properties.remove(index + 1);
                        if (observableMap != null) {
                          observableMap.dispose();
                        }
                      }

                      @Override
                      public void handleAdd(int index, Object element) {
                        ViewColumn column = (ViewColumn) element;
                        TreeViewerColumn viewerColumn =
                            new TreeViewerColumn(viewer, SWT.RIGHT, index + 1);
                        viewerColumn.getColumn().setText(column.getName());

                        int width = 100;
                        IDialogSettings widthSettings = dialogSettings.getSection(COLUMN_WIDTHS);
                        if (widthSettings.get(column.getDataProviderFactory().getId()) != null) {
                          width = widthSettings.getInt(column.getDataProviderFactory().getId());
                        }
                        viewerColumn.getColumn().setWidth(width);

                        viewerColumn.getColumn().addControlListener(columnControlListener);
                        column.addPropertyChangeListener(
                            ViewColumn.PROP_NAME, columnPropertiesChangeListener);
                        properties.add(
                            index + 1,
                            new ViewerObservableMap(
                                contentProvider.getKnownElements(),
                                column.getDataProviderFactory().getId()));
                      }
                    });

                createLabelProvider(properties.toArray(new IObservableMap[properties.size()]));

                IDialogSettings namesSection = dialogSettings.getSection(COLUMN_NAMES);

                List<String> list = new ArrayList<String>();
                for (ViewColumn column : model.getDataProviders()) {
                  list.add(column.getDataProviderFactory().getId());
                  namesSection.put(column.getDataProviderFactory().getId(), column.getName());
                }
                dialogSettings.put(COLUMNS, list.toArray(new String[list.size()]));
              }
            });

    viewer.setComparator(new ViewerComparator());

    getSite().setSelectionProvider(viewer);

    viewer.setInput(model);
    viewer.expandAll();
  }