/** * 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(); }
/** * Adds an image to this registry. This method fails if there is already an * image or descriptor for the given key. * <p> * Note that an image registry owns all of the image objects registered with * it, and automatically disposes of them when the SWT Display is disposed. * Because of this, clients must not register an image object that is * managed by another object. * </p> * * @param key * the key * @param image * the image, should not be <code>null</code> * @exception IllegalArgumentException * if the key already exists */ public void put(String key, Image image) { Entry entry = getEntry(key); if (entry == null) { entry = new Entry(); putEntry(key, entry); } if (entry.image != null || entry.descriptor != null) { throw new IllegalArgumentException( "ImageRegistry key already in use: " + key); //$NON-NLS-1$ } // Should be checking for a null image here. // Current behavior is that a null image won't be caught until dispose. // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130315 entry.image = image; entry.descriptor = new OriginalImageDescriptor(image, manager .getDevice()); try { manager.create(entry.descriptor); } catch (DeviceResourceException e) { } }
public Image getColumnImage(Object element, int index) { BindingElement be = (BindingElement) element; switch (index) { case COMMAND_NAME_COLUMN: final String commandId = be.getId(); final ImageDescriptor imageDescriptor = commandImageService.getImageDescriptor(commandId); if (imageDescriptor == null) { return null; } try { return localResourceManager.createImage(imageDescriptor); } catch (final DeviceResourceException e) { final String message = "Problem retrieving image for a command '" //$NON-NLS-1$ + commandId + '\''; final IStatus status = new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 0, message, e); WorkbenchPlugin.log(message, status); } return null; } return null; }
private Color getSpellingErrorColor(Composite composite) { AnnotationPreference pref = EditorsUI .getAnnotationPreferenceLookup().getAnnotationPreference( "org.eclipse.ui.workbench.texteditor.spelling"); // $NON-NLS-1$ String preferenceKey = pref.getColorPreferenceKey(); try { return fResources.createColor(PreferenceConverter.getColor(EditorsUI.getPreferenceStore(), preferenceKey)); } catch (DeviceResourceException e) { SVNUIPlugin.log(IStatus.ERROR, Policy.bind("internal"), e); //$NON-NLS-1$ return JFaceColors.getErrorText(composite.getDisplay()); } }
/** * Changes a control's font * @param control * @param style * @throws DeviceResourceException */ public static void changeFont(Control control, int style) throws DeviceResourceException { FontDescriptor boldDescriptor = FontDescriptor.createFrom(control.getFont()); final Font boldFont = boldDescriptor.createFont(control.getDisplay()); control.setFont(boldFont); control.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent arg0) { if (boldFont != null && !boldFont.isDisposed()) { boldFont.dispose(); } } }); }
/** * 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 Object createResource(Device device) throws DeviceResourceException { if (device == originalDisplay) { refCount++; return original; } return super.createResource(device); }
private Image findOrCreateImage(final ImageDescriptor imageDescriptor, final ResourceManager resourceManager) { if (imageDescriptor == null) { return null; } Image image = (Image) resourceManager.find(imageDescriptor); if (image == null) { try { image = resourceManager.createImage(imageDescriptor); } catch (final DeviceResourceException e) { // WorkbenchPlugin.log(e); } } return image; }
/** * Used in the script pyedit_list_bindings.py */ public Font getFont(FontData descriptor) throws DeviceResourceException { Font font = getResourceManager().createFont(FontDescriptor.createFrom(descriptor)); // Old implementation (for Eclipse 3.3) // Font font = (Font) SWTResourceUtil.getFontTable().get(descriptor); // if (font == null) { // font = new Font(Display.getCurrent(), descriptor); // SWTResourceUtil.getFontTable().put(descriptor, font); // } return font; }
private Color getColor(ResourceManager manager, RGB rgb) { try { return manager.createColor(rgb); } catch (DeviceResourceException e) { return manager.getDevice().getSystemColor(SWT.COLOR_BLACK); } }
/** * Creates a new instance * @param parent * @param controller * @param text * @param shortText */ public ComponentRiskMonitor(final Composite parent, final String text, final String shortText) { Display.getCurrent().getActiveShell(); // Images imageLow = Resources.getImage("bullet_green.png"); //$NON-NLS-1$ imageHigh = Resources.getImage("bullet_red.png"); //$NON-NLS-1$ // Layout GridLayout layout = SWTUtil.createGridLayout(1); layout.marginHeight = 0; layout.marginTop = 0; layout.marginBottom = 0; layout.verticalSpacing = 0; // Root this.root = new Composite(parent, SWT.NONE); this.root.setLayout(layout); this.root.setToolTipText(text); // Caption this.caption = new CLabel(root, SWT.CENTER); this.caption.setText(shortText); this.caption.setLayoutData(SWTUtil.createFillHorizontallyGridData()); this.caption.setToolTipText(text); this.caption.setImage(imageHigh); try { SWTUtil.changeFont(caption, SWT.BOLD); } catch (DeviceResourceException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Content Composite content = new Composite(root, SWT.NONE); content.setLayoutData(SWTUtil.createFillGridData()); content.setToolTipText(text); // Create meter Canvas canvas = new Canvas(content, SWT.DOUBLE_BUFFERED); canvas.setToolTipText(text); this.meter = new ComponentMeterFigure(); this.meter.setNeedleColor(XYGraphMediaFactory.getInstance().getColor(0, 0, 0)); this.meter.setValueLabelVisibility(true); this.meter.setRange(new Range(0, 100)); this.meter.setLoLevel(0); this.meter.setLoColor(XYGraphMediaFactory.getInstance().getColor(0, 150, 0)); this.meter.setLoloLevel(25); this.meter.setLoloColor(XYGraphMediaFactory.getInstance().getColor(255, 255, 0)); this.meter.setHiLevel(50); this.meter.setHiColor(XYGraphMediaFactory.getInstance().getColor(255, 200, 25)); this.meter.setHihiLevel(100); this.meter.setHihiColor(XYGraphMediaFactory.getInstance().getColor(255, 0, 0)); this.meter.setMajorTickMarkStepHint(50); LightweightSystem lws = new LightweightSystem(canvas); lws.setContents(this.meter); // Create label label = new CLabel(content, SWT.CENTER); label.setLayoutData(SWTUtil.createFillHorizontallyGridData()); label.setToolTipText(text); // Create responsive layout new ComponentResponsiveLayout(content, 100, 50, canvas, label); }
/** * Creates a new instance * @param parent * @param shortText */ public ComponentRiskThresholds(final Composite parent) { // Color profiles this.defaultColorProfile = KnobColorProfile.createDefaultSystemProfile(parent.getDisplay()); this.focusedColorProfile = KnobColorProfile.createFocusedBlueRedProfile(parent.getDisplay()); this.root = new Composite(parent, SWT.NONE); this.root.setLayout(SWTUtil.createGridLayout(1)); this.root.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent arg0) { if (defaultColorProfile != null && !defaultColorProfile.isDisposed()) { defaultColorProfile.dispose(); } if (focusedColorProfile != null && !focusedColorProfile.isDisposed()) { focusedColorProfile.dispose(); } } }); // Label Label label = new Label(root, SWT.CENTER); label.setText(CAPTION); GridData labeldata = SWTUtil.createFillHorizontallyGridData(); labeldata.horizontalAlignment = SWT.CENTER; label.setLayoutData(labeldata); try { SWTUtil.changeFont(label, SWT.BOLD); } catch (DeviceResourceException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Base Composite base = new Composite(root, SWT.NONE); base.setLayoutData(GridDataFactory.swtDefaults().grab(true, true).align(SWT.CENTER, SWT.CENTER).create()); // Layout GridLayout layout = SWTUtil.createGridLayout(6, true); layout.horizontalSpacing = 0; layout.verticalSpacing = 0; base.setLayout(layout); createSeparator(base, "Main", 2); createSeparator(base, "Derived", 4); createLabel(base, LABEL1); createLabel(base, LABEL2); createLabel(base, LABEL3); knob1 = createKnob(base); label1 = createLabel(base, knob1); knob2 = createKnob(base); label2 = createLabel(base, knob2); knob3 = createKnob(base); label3 = createLabel(base, knob3); }