Example #1
0
  private void updateLabels() {
    Object value = source.getPropertyValue(descriptor.getId());
    if (nameLayout != null && !nameLayout.isDisposed()) {
      nameLayout.setText(descriptor.getDisplayName());
      nameLayout.setStyle(
          new TextStyle(canvas.getFont(), canvas.getForeground(), null),
          0,
          nameLayout.getText().length());
    }
    ILabelDescriptor labelDescriptor = descriptor.getLabelDescriptor();
    ImageDescriptor image = labelDescriptor == null ? null : labelDescriptor.getImage(value);
    if (valueImage != null) {
      valueImage.dispose();
    }
    valueImage = image == null ? null : image.createImage(false, Display.getCurrent());

    ColorDescriptor color = labelDescriptor == null ? null : labelDescriptor.getForeground(value);
    if (valueColor != null) {
      valueColor.dispose();
    }
    valueColor = color == null ? null : color.createColor(Display.getCurrent());

    FontDescriptor font = labelDescriptor == null ? null : labelDescriptor.getFont(value);
    if (valueFont != null) {
      valueFont.dispose();
    }
    valueFont = font == null ? null : font.createFont(Display.getCurrent());

    if (valueLayout != null && !valueLayout.isDisposed()) {
      String valueText =
          labelDescriptor == null
              ? (value == null
                  ? "" //$NON-NLS-1$
                  : value.toString())
              : labelDescriptor.getText(value);
      if (valueText == null) valueText = ""; // $NON-NLS-1$
      valueLayout.setText(valueText);
      valueLayout.setStyle(
          new TextStyle(
              valueFont == null ? canvas.getFont() : valueFont,
              valueColor == null ? canvas.getForeground() : valueColor,
              null),
          0,
          valueText.length());
    }
    canvas.layout(true);
    canvas.redraw();
  }
  /**
   * Given a label figure object, this will calculate the correct Font needed to display into screen
   * coordinates, taking into account the current mapmode. This will typically be used by direct
   * edit cell editors that need to display independent of the zoom or any coordinate mapping that
   * is taking place on the drawing surface.
   *
   * @param label the label to use for the font calculation
   * @return the <code>Font</code> that is scaled to the screen coordinates. Note: the returned
   *     <code>Font</code> should not be disposed since it is cached by a common resource manager.
   */
  protected Font getScaledFont(IFigure label) {
    Font scaledFont = label.getFont();
    FontData data = scaledFont.getFontData()[0];
    Dimension fontSize = new Dimension(0, MapModeUtil.getMapMode(label).DPtoLP(data.getHeight()));
    label.translateToAbsolute(fontSize);

    if (Math.abs(data.getHeight() - fontSize.height) < 2) fontSize.height = data.getHeight();

    try {
      FontDescriptor fontDescriptor = FontDescriptor.createFrom(data);
      cachedFontDescriptors.add(fontDescriptor);
      return getResourceManager().createFont(fontDescriptor);
    } catch (DeviceResourceException e) {
      Trace.catching(
          DiagramUIPlugin.getInstance(),
          DiagramUIDebugOptions.EXCEPTIONS_CATCHING,
          getClass(),
          "getScaledFont",
          e); //$NON-NLS-1$
      Log.error(
          DiagramUIPlugin.getInstance(),
          DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING,
          "getScaledFont",
          e); //$NON-NLS-1$
    }
    return JFaceResources.getDefaultFont();
  }
  /* (non-Javadoc)
   * @see org.eclipse.jface.viewers.IFontProvider#getFont(java.lang.Object)
   */
  public Font getFont(Object element) {
    IWorkbenchAdapter2 adapter = getAdapter2(element);
    if (adapter == null) {
      return null;
    }

    FontData descriptor = adapter.getFont(element);
    if (descriptor == null) {
      return null;
    }

    try {
      return resourceManager.createFont(FontDescriptor.createFrom(descriptor));
    } catch (Exception e) {
      Log.log(e);
      return null;
    }
  }
  /**
   * This method obtains the fonts that are being used by the figure at its zoom level.
   *
   * @param gep the associated <code>GraphicalEditPart</code> of the figure
   * @param actualFont font being used by the figure
   * @param display
   * @return <code>actualFont</code> if zoom level is 1.0 (or when there's an error), new Font
   *     otherwise.
   */
  private Font getZoomLevelFont(Font actualFont, Display display) {
    Object zoom = getEditPart().getViewer().getProperty(ZoomManager.class.toString());

    if (zoom != null) {
      double zoomLevel = ((ZoomManager) zoom).getZoom();

      if (zoomLevel == 1.0f) return actualFont;

      FontData[] fd = new FontData[actualFont.getFontData().length];
      FontData tempFD = null;

      for (int i = 0; i < fd.length; i++) {
        tempFD = actualFont.getFontData()[i];

        fd[i] =
            new FontData(
                tempFD.getName(), (int) (zoomLevel * tempFD.getHeight()), tempFD.getStyle());
      }

      try {
        FontDescriptor fontDescriptor = FontDescriptor.createFrom(fd);
        cachedFontDescriptors.add(fontDescriptor);
        return getResourceManager().createFont(fontDescriptor);
      } catch (DeviceResourceException e) {
        Trace.catching(
            DiagramUIPlugin.getInstance(),
            DiagramUIDebugOptions.EXCEPTIONS_CATCHING,
            getClass(),
            "getZoomLevelFonts",
            e); //$NON-NLS-1$
        Log.error(
            DiagramUIPlugin.getInstance(),
            DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING,
            "getZoomLevelFonts",
            e); //$NON-NLS-1$

        return actualFont;
      }
    } else return actualFont;
  }
  @Override
  public void update(ViewerCell cell) {
    super.update(cell);

    SourceFilesViewer.TranslationUnitInfo tuInfo = null;
    Object element = cell.getElement();
    if (element instanceof ITranslationUnit) {
      tuInfo = SourceFilesViewer.fetchTranslationUnitInfo((Executable) viewer.getInput(), element);
    }

    int orgColumnIndex = cell.getColumnIndex();

    if (orgColumnIndex == 0) {
      if (element instanceof String) {
        cell.setText((String) element);
        Font italicFont =
            resourceManager.createFont(
                FontDescriptor.createFrom(viewer.getTree().getFont()).setStyle(SWT.ITALIC));
        cell.setFont(italicFont);
      } else {
        cell.setFont(viewer.getTree().getFont());
      }
    } else if (orgColumnIndex == 1) {
      cell.setText(null);
      if (tuInfo != null) {
        if (tuInfo.location != null) {
          cell.setText(tuInfo.location.toOSString());
          if (tuInfo.exists)
            cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
          else cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
        }
      }
      cell.setImage(null);
    } else if (orgColumnIndex == 2) {
      cell.setText(null);
      if (tuInfo != null && tuInfo.originalLocation != null) {
        cell.setText(tuInfo.originalLocation.toOSString());
        if (tuInfo.originalExists)
          cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
        else cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
      }
      cell.setImage(null);
    } else if (orgColumnIndex == 3) {
      cell.setText(null);
      if (tuInfo != null) {
        if (tuInfo.exists) {
          cell.setText(Long.toString(tuInfo.fileLength));
        }
      }
      cell.setImage(null);
    } else if (orgColumnIndex == 4) {
      cell.setText(null);
      if (tuInfo != null) {
        if (tuInfo.exists) {
          String dateTimeString =
              DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
                  .format(new Date(tuInfo.lastModified));
          cell.setText(dateTimeString);
        }
      }
      cell.setImage(null);
    } else if (orgColumnIndex == 5) {
      cell.setText(null);
      if (tuInfo != null) {
        if (tuInfo.location != null) {
          String fileExtension = tuInfo.location.getFileExtension();
          if (fileExtension != null) cell.setText(fileExtension.toLowerCase());
        }
      }
      cell.setImage(null);
    }
  }