コード例 #1
0
ファイル: SwtFocusHandler.java プロジェクト: appnativa/rare
  SwtFocusHandler(Composite composite) {
    assert composite != null;
    assert Display.getCurrent() != null; // On SWT event thread

    this.composite = composite;
    display = composite.getDisplay();
    composite.addFocusListener(this);
    composite.addKeyListener(this);
  }
コード例 #2
0
ファイル: ChartsComposite.java プロジェクト: c0debrain/pm
  private void initGUI() {
    try {

      this.setOrientation(SWT.VERTICAL);

      this.setBackground(MainGui.cHART_DARKER);
      Color innerBgColor = MainGui.cHART_LIGHT;

      this.addDisposeListener(
          new DisposeListener() {

            public void widgetDisposed(DisposeEvent evt) {
              rootShellClosed(evt);
            }
          });

      this.addListener(
          SWT.Hide,
          new Listener() {

            @Override
            public void handleEvent(Event event) {
              ChartsComposite.this.shutDownDisplay();
            }
          });

      {
        try {
          // Set cross-platform Java L&F (also called "Metal")
          UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException e) {
          LOGGER.error(e);
        } catch (ClassNotFoundException e) {
          LOGGER.error(e);
        } catch (InstantiationException e) {
          LOGGER.error(e);
        } catch (IllegalAccessException e) {
          LOGGER.error(e);
        }

        mainChartComposite = new Composite(this, SWT.EMBEDDED | SWT.NO_BACKGROUND);
        final Frame chartFrame = SWT_AWT.new_Frame(mainChartComposite);
        final Panel rootHeavyPanel = new Panel();
        rootHeavyPanel.setLayout(new BorderLayout());
        chartFrame.add(rootHeavyPanel);

        mainChartWraper =
            new ChartMain(ChartsComposite.DEFAULT_START_DATE, JFreeChartTimePeriod.DAY);
        mainChartPanel =
            new ChartPanel(
                mainChartWraper.initChart(stripedCloseFunction), true, true, true, false, true) {
              private static final long serialVersionUID = 1L;

              @Override
              public void restoreAutoBounds() {
                // Do nothing (disable mouse left drag zoom)
              }
            };
        mainChartPanel.setMouseZoomable(false, false);
        mainChartPanel.setMinimumDrawWidth(0);
        mainChartPanel.setMinimumDrawHeight(0);
        mainChartPanel.setMaximumDrawWidth(Display.getCurrent().getClientArea().width);
        mainChartPanel.setMaximumDrawHeight(Display.getCurrent().getClientArea().height);

        mainChartPanel.getPopupMenu().addSeparator();
        JMenuItem deleteVLines = new JMenuItem("Remove vertical lines");
        deleteVLines.addActionListener(
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                chartDisplayStrategy.removeVLines();
              }
            });
        mainChartPanel.getPopupMenu().add(deleteVLines);
        JMenuItem deleteHLines = new JMenuItem("Remove horizontal lines");
        deleteHLines.addActionListener(
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                chartDisplayStrategy.removeHLines();
              }
            });
        mainChartPanel.getPopupMenu().add(deleteHLines);

        mainChartPanel.getPopupMenu().addSeparator();
        JMenuItem lookAndFeelInfoItem = new JMenuItem("Look and feel Info");
        lookAndFeelInfoItem.addActionListener(
            new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                LOGGER.info(UIManager.getLookAndFeel());
              }
            });
        mainChartPanel.getPopupMenu().add(lookAndFeelInfoItem);

        mainChartPanel.addMouseListener(
            new ClickListener() {

              @Override
              public void singleClick(final MouseEvent event) {

                Display.getDefault()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            Boolean isSlidingArea =
                                getMainChartWraper()
                                    .isSlidingArea(
                                        mainChartComposite.getSize().y, event.getPoint().y);
                            if (isSlidingArea) return;

                            int button = event.getButton();
                            if (button == MouseEvent.BUTTON1) {
                              Point2D clickPoint =
                                  mainChartPanel.translateScreenToJava2D(event.getPoint());
                              Rectangle2D plotArea = mainChartPanel.getScreenDataArea();

                              if (plotArea.getMaxY() > clickPoint.getY()
                                  && clickPoint.getY() > plotArea.getY()
                                  && plotArea.getMaxX() > clickPoint.getX()
                                  && clickPoint.getX() > plotArea.getX()) {
                                chartDisplayStrategy.addVLineAt(clickPoint, plotArea);
                              }
                            }
                          }
                        });
              }

              @Override
              public void doubleClick(final MouseEvent event) {

                Display.getDefault()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            Boolean isSlidingArea =
                                getMainChartWraper()
                                    .isSlidingArea(
                                        mainChartComposite.getSize().y, event.getPoint().y);
                            if (isSlidingArea) return;

                            int button = event.getButton();
                            if (button == MouseEvent.BUTTON1) {
                              Point2D clickPoint = getPointCoordinates(event.getPoint());
                              Rectangle2D plotArea = mainChartPanel.getScreenDataArea();

                              if (plotArea.getMaxY() > clickPoint.getY()
                                  && clickPoint.getY() > plotArea.getY()
                                  && plotArea.getMaxX() > clickPoint.getX()
                                  && clickPoint.getX() > plotArea.getX()) {
                                chartDisplayStrategy.removeVLineAt(clickPoint, plotArea);
                                chartDisplayStrategy.removeHLineAt(clickPoint, plotArea);
                              }
                            }
                          }
                        });
              }
            });

        // Slider
        chartPanelFocusGain = false;
        mainChartPanel.addMouseListener(
            new MouseAdapter() {

              @Override
              public void mouseEntered(MouseEvent e) {
                chartPanelFocusGain = true;
              }

              @Override
              public void mouseExited(MouseEvent e) {
                chartPanelFocusGain = false;
              }

              @Override
              public void mousePressed(final MouseEvent e) {
                panelClickPosition = e.getPoint();
                Display.getDefault()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            Boolean isSlidingArea =
                                getMainChartWraper()
                                    .isSlidingArea(mainChartComposite.getSize().y, e.getPoint().y);
                            if (isSlidingArea) {
                              isInPanelChartSlider = true;
                            } else {
                              isInPanelChartSlider = false;
                            }
                          }
                        });
              }

              @Override
              public void mouseReleased(final MouseEvent e) {
                Display.getDefault()
                    .asyncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            if (isInPanelChartSlider != null
                                && isInPanelChartSlider
                                && panelClickPosition != null) {
                              int indicChartSlideIncrement =
                                  (int)
                                      ((((double) panelClickPosition.y - e.getPoint().y)
                                              / (double) mainChartComposite.getSize().y)
                                          * 100);
                              chartDisplayStrategy.slideChart(indicChartSlideIncrement);
                            }
                            isInPanelChartSlider = false;
                          }
                        });
              }
            });

        // focus bizz (useful?)
        mainChartPanel.addMouseMotionListener(
            new MouseMotionListener() {

              @Override
              public void mouseMoved(final MouseEvent e) {

                // Sliding
                if (!closeRequested) {
                  Display.getDefault()
                      .asyncExec(
                          new Runnable() {

                            @Override
                            public void run() {

                              if (!closeRequested) {
                                try {
                                  Cursor cursor = ChartsComposite.this.getCursor();
                                  if (cursor == null
                                      || (!cursor.equals(CursorFactory.getCursor(SWT.CURSOR_WAIT))
                                          && !cursor.equals(
                                              CursorFactory.getCursor(SWT.CURSOR_APPSTARTING)))) {
                                    Boolean isSlidingArea =
                                        getMainChartWraper()
                                            .isSlidingArea(
                                                mainChartComposite.getSize().y, e.getPoint().y);
                                    if (isSlidingArea) {
                                      ChartsComposite.this.setCursor(
                                          CursorFactory.getCursor(SWT.CURSOR_SIZENS));
                                    } else {
                                      ChartsComposite.this.setCursor(
                                          CursorFactory.getCursor(SWT.CURSOR_ARROW));
                                    }
                                  }

                                } catch (Throwable e) {
                                  try {
                                    ChartsComposite.this.setCursor(
                                        CursorFactory.getCursor(SWT.CURSOR_ARROW));
                                  } catch (Throwable e1) {
                                    LOGGER.warn(e1, e1);
                                  }
                                  LOGGER.warn(e, e);
                                }
                              }
                            }
                          });
                }

                // Gain focus mgt
                if (!closeRequested) {
                  Display.getDefault()
                      .asyncExec(
                          new Runnable() {
                            public void run() {
                              try {
                                if (!closeRequested) {
                                  if (!mainChartComposite.isDisposed()
                                      && !mainChartComposite.isFocusControl()) {
                                    int cpt = 0;
                                    while (chartPanelFocusGain && cpt < 200) {
                                      Thread.sleep(10);
                                      cpt++;
                                    }
                                    if (chartPanelFocusGain && !mainChartComposite.isDisposed()) {
                                      mainChartComposite.forceFocus();
                                    }
                                  }
                                }
                              } catch (Throwable e) {
                                LOGGER.warn(e, e);
                              }
                            }
                          });
                }
              }

              @Override
              public void mouseDragged(MouseEvent e) {
                // Nothing
              }
            });

        mainChartComposite.addKeyListener(
            new org.eclipse.swt.events.KeyListener() {

              @Override
              public void keyReleased(org.eclipse.swt.events.KeyEvent e) {
                if (((e.stateMask & SWT.CTRL) == SWT.CTRL)
                    && ((e.stateMask & SWT.ALT) == SWT.ALT)
                    && (e.keyCode == 'p')) {
                  try {
                    chartDisplayStrategy.exportBarChartPng();
                  } catch (InvalidParameterException exception) {
                    chartDisplayStrategy.showPopupDialog(exception.getMessage(), "Ok", null, null);
                  }
                }
                if (e.keyCode == SWT.ARROW_UP) {
                  chartDisplayStrategy.slideChart(+1);
                }
                if (e.keyCode == SWT.ARROW_DOWN) {
                  chartDisplayStrategy.slideChart(-1);
                }
              }

              @Override
              public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
                //
              }
            });

        mainChartComposite.addMouseTrackListener(
            new MouseTrackListener() {

              @Override
              public void mouseHover(org.eclipse.swt.events.MouseEvent e) {}

              @Override
              public void mouseExit(org.eclipse.swt.events.MouseEvent e) {}

              @Override
              public void mouseEnter(org.eclipse.swt.events.MouseEvent e) {
                mainChartPanel.requestFocusInWindow();
              }
            });

        rootHeavyPanel.add(mainChartPanel);
        chartFrame.pack();
        chartFrame.setVisible(true);
      }
      {
        chartBoutonsGroup = new Group(this, SWT.NONE);
        chartBoutonsGroup.setBackground(innerBgColor);
        GridLayout portfolioBoutonsGroupLayout = new GridLayout();
        portfolioBoutonsGroupLayout.numColumns = 1;
        portfolioBoutonsGroupLayout.verticalSpacing = 0;
        portfolioBoutonsGroupLayout.marginHeight = 0;
        chartBoutonsGroup.setLayout(portfolioBoutonsGroupLayout);
        GridData portfolioInfosGroupData = new GridData(GridData.FILL_HORIZONTAL);
        chartBoutonsGroup.setLayoutData(portfolioInfosGroupData);

        chartBoutonsGroup.setText("Portfolios charting : ");
        chartBoutonsGroup.setFont(MainGui.DEFAULTFONT);
        chartBoutonsGroup.setBackground(innerBgColor);

        {
          popusGroup = new Group(chartBoutonsGroup, SWT.NONE);
          GridData popusGroupData = new GridData(SWT.FILL, SWT.FILL, true, false);
          popusGroup.setLayoutData(popusGroupData);
          popusGroup.setBackground(innerBgColor);
          RowLayout popusGroupL = new RowLayout(SWT.HORIZONTAL);
          popusGroupL.justify = true;
          popusGroupL.fill = true;
          popusGroupL.wrap = false;
          popusGroupL.marginHeight = 0;
          popusGroup.setLayout(popusGroupL);
        }

        // Sliding
        {
          slidingGroup = new Group(chartBoutonsGroup, SWT.NONE);
          GridData slidingGroupData = new GridData(SWT.FILL, SWT.FILL, true, false);
          slidingGroup.setLayoutData(slidingGroupData);
          slidingGroup.setBackground(innerBgColor);
          GridLayout slidingGroupL = new GridLayout();
          slidingGroupL.numColumns = 5;
          slidingGroupL.marginHeight = 0;
          slidingGroupL.verticalSpacing = 0;
          slidingGroup.setLayout(slidingGroupL);
          /// start
          {
            startDateLabel = new Label(slidingGroup, SWT.NONE);
            GridData startOneYearBackData = new GridData(SWT.BEGINNING, SWT.FILL, false, false);
            startOneYearBackData.horizontalSpan = 2;
            startDateLabel.setLayoutData(startOneYearBackData);
            startDateLabel.setText(
                DateFormat.getDateInstance(DateFormat.MEDIUM).format(slidingStartDate) + "");
            startDateLabel.setBackground(innerBgColor);
            startDateLabel.setFont(MainGui.DEFAULTFONT);
          }
          /// sliding sliders
          Composite slidingSliderGroup = new Composite(slidingGroup, SWT.NONE);
          slidingSliderGroup.setSize(1000, SWT.DEFAULT);
          GridData slidersGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
          slidersGridData.verticalSpan = 2;
          slidingSliderGroup.setLayoutData(slidersGridData);
          slidingSliderGroup.setBackground(innerBgColor);
          slidingSliderGroup.setForeground(innerBgColor);
          FillLayout slidingSliderGroupL = new FillLayout(SWT.VERTICAL);
          slidingSliderGroup.setLayout(slidingSliderGroupL);
          {
            sliderStartDate = new Slider(slidingSliderGroup, SWT.HORIZONTAL);
            sliderStartDate.setThumb(1);
            sliderStartDate.setMaximum(100);
            sliderStartDate.addListener(
                SWT.MouseExit,
                new Listener() {

                  public void handleEvent(Event arg0) {
                    sliderChangesApply();
                  }
                });
          }
          {
            sliderEndDate = new Slider(slidingSliderGroup, SWT.HORIZONTAL);
            sliderEndDate.setThumb(1);
            sliderEndDate.setMinimum(0);
            sliderEndDate.setSelection(100);
            sliderEndDate.addListener(
                SWT.MouseExit,
                new Listener() {

                  public void handleEvent(Event arg0) {
                    sliderChangesApply();
                  }
                });
          }
          // end
          {
            endDateLabel = new Label(slidingGroup, SWT.NONE);
            GridData endOneYearBackData = new GridData(SWT.END, SWT.FILL, false, false);
            endOneYearBackData.horizontalSpan = 2;
            endDateLabel.setLayoutData(endOneYearBackData);
            endDateLabel.setText(
                DateFormat.getDateInstance(DateFormat.MEDIUM).format(slidingEndDate));
            endDateLabel.setBackground(innerBgColor);
            endDateLabel.setFont(MainGui.DEFAULTFONT);
          }

          // but start
          {
            Button startOneYearBack = new Button(slidingGroup, SWT.ARROW | SWT.LEFT);
            GridData startOneYearBackData = new GridData(SWT.END, SWT.FILL, false, true);
            startOneYearBack.setLayoutData(startOneYearBackData);
            startOneYearBack.setToolTipText("Move start date one year backward.");
            startOneYearBack.addSelectionListener(
                new SelectionListener() {

                  @Override
                  public void widgetSelected(SelectionEvent e) {
                    handle();
                  }

                  private void handle() {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(slidingStartDate);
                    calendar.add(Calendar.YEAR, -1);
                    firstStartDate = calendar.getTime();
                    sliderStartDate.setSelection(0);
                    startSliderUpdateConditional(
                        sliderStartDate, startDateLabel, sliderEndDate, endDateLabel);
                  }

                  @Override
                  public void widgetDefaultSelected(SelectionEvent e) {
                    handle();
                  }
                });
            startOneYearBack.addListener(
                SWT.MouseExit,
                new Listener() {

                  @Override
                  public void handleEvent(Event event) {
                    sliderChangesApply();
                  }
                });
          }

          {
            Button startOneYearForward = new Button(slidingGroup, SWT.ARROW | SWT.RIGHT);
            GridData startOneYearBackData = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
            startOneYearForward.setLayoutData(startOneYearBackData);
            startOneYearForward.setToolTipText("Move start date one year forward.");
            startOneYearForward.addSelectionListener(
                new SelectionListener() {

                  @Override
                  public void widgetSelected(SelectionEvent e) {
                    handle();
                  }

                  private void handle() {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(slidingStartDate);
                    calendar.add(Calendar.YEAR, +1);
                    if (calendar.getTime().before(slidingEndDate)) {
                      firstStartDate = calendar.getTime();
                      sliderStartDate.setSelection(0);
                      startSliderUpdateConditional(
                          sliderStartDate, startDateLabel, sliderEndDate, endDateLabel);
                    } else {
                      chartDisplayStrategy.showPopupDialog(
                          "To move the start date further forward, you will need to move the end date first.",
                          "Ok",
                          null,
                          null);
                    }
                  }

                  @Override
                  public void widgetDefaultSelected(SelectionEvent e) {
                    handle();
                  }
                });
            startOneYearForward.addListener(
                SWT.MouseExit,
                new Listener() {

                  @Override
                  public void handleEvent(Event event) {
                    sliderChangesApply();
                  }
                });
          }
          /// but end

          {
            Button endOneYearBack = new Button(slidingGroup, SWT.ARROW | SWT.LEFT);
            GridData endOneYearBackData = new GridData(SWT.END, SWT.FILL, false, true);
            endOneYearBack.setLayoutData(endOneYearBackData);
            endOneYearBack.setToolTipText("Move end date one year backward.");
            endOneYearBack.addSelectionListener(
                new SelectionListener() {

                  @Override
                  public void widgetSelected(SelectionEvent e) {
                    handle();
                  }

                  private void handle() {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(slidingEndDate);
                    calendar.add(Calendar.YEAR, -1);
                    if (calendar.getTime().after(slidingStartDate)) {
                      lastEndDate = calendar.getTime();
                      sliderEndDate.setSelection(100);
                      endSliderUpdateConditional(
                          sliderEndDate, endDateLabel, sliderStartDate, startDateLabel);
                    } else {
                      chartDisplayStrategy.showPopupDialog(
                          "To move the end date further backward, you will need to move the start date first.",
                          "Ok",
                          null,
                          null);
                    }
                  }

                  @Override
                  public void widgetDefaultSelected(SelectionEvent e) {
                    handle();
                  }
                });
            endOneYearBack.addListener(
                SWT.MouseExit,
                new Listener() {

                  @Override
                  public void handleEvent(Event event) {
                    sliderChangesApply();
                  }
                });
          }
          {
            Button endOneYearForward = new Button(slidingGroup, SWT.ARROW | SWT.RIGHT);
            GridData endOneYearBackData = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
            endOneYearForward.setLayoutData(endOneYearBackData);
            endOneYearForward.setToolTipText("Move end date one year forward.");
            endOneYearForward.addSelectionListener(
                new SelectionListener() {

                  @Override
                  public void widgetSelected(SelectionEvent e) {
                    handle();
                  }

                  private void handle() {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(slidingEndDate);
                    calendar.add(Calendar.YEAR, +1);
                    Date newDate = EventSignalConfig.getNewDate();
                    if (calendar.getTime().after(newDate)) {
                      calendar.setTime(newDate);
                    }
                    lastEndDate = calendar.getTime();
                    sliderEndDate.setSelection(100);
                    endSliderUpdateConditional(
                        sliderEndDate, endDateLabel, sliderStartDate, startDateLabel);
                  }

                  @Override
                  public void widgetDefaultSelected(SelectionEvent e) {
                    handle();
                  }
                });
            endOneYearForward.addListener(
                SWT.MouseExit,
                new Listener() {

                  @Override
                  public void handleEvent(Event event) {
                    sliderChangesApply();
                  }
                });
          }

          sliderStartDate.addListener(
              SWT.Selection,
              new Listener() {
                public void handleEvent(Event event) {
                  startSliderUpdateConditional(
                      sliderStartDate, startDateLabel, sliderEndDate, endDateLabel);
                }
              });

          sliderEndDate.addListener(
              SWT.Selection,
              new Listener() {

                public void handleEvent(Event event) {
                  endSliderUpdateConditional(
                      sliderEndDate, endDateLabel, sliderStartDate, startDateLabel);
                }
              });

          slidingGroup.layout();
        }
      }

      this.layout();
      this.pack();

    } catch (Exception e) {
      LOGGER.error("", e);
    }
  }