/**
   * Construct the window.
   *
   * @param display
   */
  public ExceptionWindow(Display display, Exception ex) {
    this.display = display;
    this.ex = ex;
    showDetails = false;
    detailsSwitched = false;
    shell = new Shell(display, SWT.MAX | SWT.CLOSE | SWT.RESIZE | SWT.APPLICATION_MODAL);

    try {
      shell.setText(Resources.getStr(this, "title"));
    } catch (ResourceException rex) {
      shell.setText("Medley error");
    }
    // on close remember last height of stack trace window
    shell.addShellListener(
        new ShellAdapter() {
          public void shellClosed(ShellEvent e) {
            detailsHeight = stackTrace.getSize().y;
          }
        });

    FormLayout layout = new FormLayout();
    shell.setLayout(layout);
    initWidgets();
  }
  /** Initialize the widgets in the window. */
  protected void initWidgets() {
    shell.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // error icon
    final Image imgErr = new Image(display, "img/error.gif");
    errorIcon = new Canvas(shell, SWT.NO_REDRAW_RESIZE);
    errorIcon.addPaintListener(
        new PaintListener() {
          public void paintControl(PaintEvent e) {
            e.gc.drawImage(imgErr, 0, 0);
          }
        });

    // create the label containing error message
    errorMessage = new Label(shell, SWT.WRAP);
    try {
      errorMessage.setText(
          (ex.getMessage() == null) ? Resources.getStr(this, "unknownError") : ex.getMessage());
    } catch (ResourceException ex) {
      errorMessage.setText(
          (ex.getMessage() == null) ? "Unknown error (see details)." : ex.getMessage());
    }
    errorMessage.pack();

    // the OK button
    okButton = new Button(shell, SWT.PUSH);
    try {
      okButton.setText(Resources.getStr(this, "close"));
    } catch (ResourceException ex) {
      okButton.setText("&Close");
    }
    okButton.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event e) {
            shell.close();
          }
        });

    // the Details button
    detailsButton = new Button(shell, SWT.PUSH);
    try {
      detailsButton.setText(Resources.getStr(this, showDetails ? "detailsOpen" : "detailsClosed"));
    } catch (ResourceException ex) {
      detailsButton.setText(showDetails ? "&Details <<" : "&Details >>");
    }
    // final boolean s = showDetails;
    final Button fbtn = detailsButton;
    detailsButton.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event e) {
            showDetails = !showDetails;
            detailsSwitched = true;
            setLayout();
            detailsSwitched = false;
            try {
              fbtn.setText(Resources.getStr(this, showDetails ? "detailsOpen" : "detailsClosed"));
            } catch (ResourceException ex) {
              fbtn.setText(showDetails ? "&Details <<" : "&Details >>");
            }
          }
        });

    // the text area with stack trace
    stackTrace =
        new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI | SWT.HORIZONTAL | SWT.VERTICAL);
    StringWriter buf = new StringWriter();
    ex.printStackTrace(new PrintWriter(buf));
    stackTrace.setText(buf.toString());

    errorMessage.pack();
    setLayout();
  }
Exemple #3
0
  /**
   * Initialise widgets in pane.
   *
   * @throws MedleyException
   */
  protected void initWidgets() throws MedleyException {
    table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
    table.setLinesVisible(false);
    table.setHeaderVisible(true);
    // add columns
    TableColumn column = new TableColumn(table, SWT.NULL);
    column.setText(Resources.getStr(this, "value"));
    column.setWidth(120);
    column = new TableColumn(table, SWT.NULL);
    column.setText(Resources.getStr(this, "actions"));
    column.setWidth(Settings.ACTION_COLUMN_WIDTH);
    column.setResizable(false);

    // fill in table contents and refresh cache
    refresh();

    final TableEditor valueEditor = new TableEditor(table);
    valueEditor.grabHorizontal = true;
    final TableEditor deleteEditor = new TableEditor(table);
    deleteEditor.grabHorizontal = true;

    table.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
            // Clean up any previous editor control
            Control oldEditor = valueEditor.getEditor();
            if (oldEditor != null) oldEditor.dispose();
            oldEditor = deleteEditor.getEditor();
            if (oldEditor != null) oldEditor.dispose();

            // Identify the selected row
            final TableItem titem = (TableItem) e.item;
            final int tagValueId = ((Integer) titem.getData(ID)).intValue();
            if (titem == null) return;

            // the editor for group name
            Text name = new Text(table, SWT.FLAT);
            name.setText(titem.getText(COL_VALUE));
            name.addFocusListener(
                new FocusAdapter() {
                  public void focusLost(FocusEvent ev) {
                    Text txt = (Text) valueEditor.getEditor();
                    valueEditor.getItem().setText(COL_VALUE, txt.getText());
                    // save the change to the model
                    try {
                      // update the tag group name
                      TagValue tv = new TagValue(tagValueId);
                      tv.setValue(txt.getText());
                      tv.dispose();
                    } catch (MedleyException ex) {
                      (new ExceptionWindow(getDisplay(), ex)).show();
                    }
                  }
                });
            valueEditor.setEditor(name, titem, COL_VALUE);

            // the delete button
            Button button = new Button(table, SWT.FLAT);
            try {
              button.setText(Resources.getStr(this, "delete"));
            } catch (ResourceException ex) {
              (new ExceptionWindow(getDisplay(), ex)).show();
            }
            button.addListener(
                SWT.Selection,
                new Listener() {
                  public void handleEvent(Event e) {
                    try {
                      TagValue.delete(tagValueId);
                    } catch (MedleyException ex) {
                      (new ExceptionWindow(getDisplay(), ex)).show();
                    }
                    table.remove(table.indexOf(titem));
                    table.deselectAll();
                    valueEditor.getEditor().dispose();
                    deleteEditor.getEditor().dispose();
                  }
                });
            deleteEditor.setEditor(button, titem, COL_DELETE);
          }
        });

    if (table.getItemCount() > 0) {
      TableColumn[] cols = table.getColumns();
      for (int i = 0; i < cols.length; i++) {
        if (cols[i].getResizable()) {
          cols[i].pack();
        }
      }
    }

    table.setSize(table.computeSize(SWT.DEFAULT, 200));
    FormData data = new FormData();
    data.top = new FormAttachment(0, Settings.MARGIN_TOP);
    data.bottom =
        new FormAttachment(
            100, -Settings.MARGIN_BOTTOM - Settings.BUTTON_HEIGHT - Settings.ITEM_SPACING_V);
    data.left = new FormAttachment(0, Settings.MARGIN_LEFT);
    data.right = new FormAttachment(100, -Settings.MARGIN_RIGHT);
    table.setLayoutData(data);

    // add value button
    addValueBtn = new Button(this, SWT.NONE);
    addValueBtn.setText(Resources.getStr(this, "addTagValue"));
    addValueBtn.addListener(
        SWT.Selection,
        new Listener() {
          public void handleEvent(Event e) {
            try {
              TagValue tv = TagValue.create(tag.getId());
              TableItem item = new TableItem(table, SWT.NULL);
              item.setText(COL_VALUE, tv.getValue());
              item.setData(ID, new Integer(tv.getId()));
              tv.dispose();
              table.setSelection(new TableItem[] {item});
            } catch (MedleyException ex) {
              (new ExceptionWindow(getDisplay(), ex)).show();
            }
          }
        });
    data = new FormData();
    data.bottom = new FormAttachment(100, -Settings.MARGIN_BOTTOM);
    data.left = new FormAttachment(0, Settings.MARGIN_LEFT);
    data.width = Settings.BUTTON_WIDTH;
    data.height = Settings.BUTTON_HEIGHT;
    addValueBtn.setLayoutData(data);
  }