fix sonarlint messages
This commit is contained in:
@ -771,8 +771,6 @@ public class WaveformView implements IWaveformView {
|
||||
if (currentTxSelection != null)
|
||||
currentTxSelection = null;
|
||||
selectionChanged = true;
|
||||
} else {
|
||||
System.err.println("Invalid selection");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -29,14 +29,17 @@ import org.eclipse.swt.graphics.Rectangle;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
|
||||
/**
|
||||
* Utility class for managing OS resources associated with SWT controls such as colors, fonts, images, etc.
|
||||
* Utility class for managing OS resources associated with SWT controls such as
|
||||
* colors, fonts, images, etc.
|
||||
* <p>
|
||||
* !!! IMPORTANT !!! Application code must explicitly invoke the <code>dispose()</code> method to release the
|
||||
* operating system resources managed by cached objects when those objects and OS resources are no longer
|
||||
* !!! IMPORTANT !!! Application code must explicitly invoke the
|
||||
* <code>dispose()</code> method to release the operating system resources
|
||||
* managed by cached objects when those objects and OS resources are no longer
|
||||
* needed (e.g. on application shutdown)
|
||||
* <p>
|
||||
* This class may be freely distributed as part of any application or plugin.
|
||||
* <p>
|
||||
*
|
||||
* @author scheglov_ke
|
||||
* @author Dan Rubel
|
||||
*/
|
||||
@ -46,57 +49,54 @@ public class SWTResourceManager {
|
||||
// Color
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
private static Map<RGB, Color> m_colorMap = new HashMap<RGB, Color>();
|
||||
private static Map<RGB, Color> colorMap = new HashMap<>();
|
||||
|
||||
private SWTResourceManager() {}
|
||||
|
||||
/**
|
||||
* Returns the system {@link Color} matching the specific ID.
|
||||
*
|
||||
* @param systemColorID
|
||||
* the ID value for the color
|
||||
* @param systemColorID the ID value for the color
|
||||
* @return the system {@link Color} matching the specific ID
|
||||
*/
|
||||
public static Color getColor(int systemColorID) {
|
||||
Display display = Display.getCurrent();
|
||||
return display.getSystemColor(systemColorID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Color} given its red, green and blue component values.
|
||||
*
|
||||
* @param r
|
||||
* the red component of the color
|
||||
* @param g
|
||||
* the green component of the color
|
||||
* @param b
|
||||
* the blue component of the color
|
||||
* @return the {@link Color} matching the given red, green and blue component values
|
||||
* @param r the red component of the color
|
||||
* @param g the green component of the color
|
||||
* @param b the blue component of the color
|
||||
* @return the {@link Color} matching the given red, green and blue component
|
||||
* values
|
||||
*/
|
||||
public static Color getColor(int r, int g, int b) {
|
||||
return getColor(new RGB(r, g, b));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Color} given its RGB value.
|
||||
*
|
||||
* @param rgb
|
||||
* the {@link RGB} value of the color
|
||||
* @param rgb the {@link RGB} value of the color
|
||||
* @return the {@link Color} matching the RGB value
|
||||
*/
|
||||
public static Color getColor(RGB rgb) {
|
||||
Color color = m_colorMap.get(rgb);
|
||||
if (color == null) {
|
||||
Display display = Display.getCurrent();
|
||||
color = new Color(display, rgb);
|
||||
m_colorMap.put(rgb, color);
|
||||
}
|
||||
return color;
|
||||
return colorMap.computeIfAbsent(rgb, k -> new Color(Display.getCurrent(), rgb));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose of all the cached {@link Color}'s.
|
||||
*/
|
||||
public static void disposeColors() {
|
||||
for (Color color : m_colorMap.values()) {
|
||||
for (Color color : colorMap.values()) {
|
||||
color.dispose();
|
||||
}
|
||||
m_colorMap.clear();
|
||||
colorMap.clear();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Image
|
||||
@ -105,12 +105,12 @@ public class SWTResourceManager {
|
||||
/**
|
||||
* Maps image paths to images.
|
||||
*/
|
||||
private static Map<String, Image> m_imageMap = new HashMap<String, Image>();
|
||||
private static Map<String, Image> imageMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Returns an {@link Image} encoded by the specified {@link InputStream}.
|
||||
*
|
||||
* @param stream
|
||||
* the {@link InputStream} encoding the image data
|
||||
* @param stream the {@link InputStream} encoding the image data
|
||||
* @return the {@link Image} encoded by the specified input stream
|
||||
*/
|
||||
protected static Image getImage(InputStream stream) throws IOException {
|
||||
@ -125,52 +125,55 @@ public class SWTResourceManager {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link Image} stored in the file at the specified path.
|
||||
*
|
||||
* @param path
|
||||
* the path to the image file
|
||||
* @param path the path to the image file
|
||||
* @return the {@link Image} stored in the file at the specified path
|
||||
*/
|
||||
public static Image getImage(String path) {
|
||||
Image image = m_imageMap.get(path);
|
||||
Image image = imageMap.get(path);
|
||||
if (image == null) {
|
||||
try {
|
||||
image = getImage(new FileInputStream(path));
|
||||
m_imageMap.put(path, image);
|
||||
imageMap.put(path, image);
|
||||
} catch (Exception e) {
|
||||
image = getMissingImage();
|
||||
m_imageMap.put(path, image);
|
||||
imageMap.put(path, image);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link Image} stored in the file at the specified path relative to the specified class.
|
||||
* Returns an {@link Image} stored in the file at the specified path relative to
|
||||
* the specified class.
|
||||
*
|
||||
* @param clazz
|
||||
* the {@link Class} relative to which to find the image
|
||||
* @param path
|
||||
* the path to the image file, if starts with <code>'/'</code>
|
||||
* @param clazz the {@link Class} relative to which to find the image
|
||||
* @param path the path to the image file, if starts with <code>'/'</code>
|
||||
* @return the {@link Image} stored in the file at the specified path
|
||||
*/
|
||||
public static Image getImage(Class<?> clazz, String path) {
|
||||
String key = clazz.getName() + '|' + path;
|
||||
Image image = m_imageMap.get(key);
|
||||
Image image = imageMap.get(key);
|
||||
if (image == null) {
|
||||
try {
|
||||
image = getImage(clazz.getResourceAsStream(path));
|
||||
m_imageMap.put(key, image);
|
||||
imageMap.put(key, image);
|
||||
} catch (Exception e) {
|
||||
image = getMissingImage();
|
||||
m_imageMap.put(key, image);
|
||||
imageMap.put(key, image);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
private static final int MISSING_IMAGE_SIZE = 10;
|
||||
|
||||
/**
|
||||
* @return the small {@link Image} that can be used as placeholder for missing image.
|
||||
* @return the small {@link Image} that can be used as placeholder for missing
|
||||
* image.
|
||||
*/
|
||||
private static Image getMissingImage() {
|
||||
Image image = new Image(Display.getCurrent(), MISSING_IMAGE_SIZE, MISSING_IMAGE_SIZE);
|
||||
@ -182,6 +185,7 @@ public class SWTResourceManager {
|
||||
//
|
||||
return image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Style constant for placing decorator image in top left corner of base image.
|
||||
*/
|
||||
@ -191,11 +195,13 @@ public class SWTResourceManager {
|
||||
*/
|
||||
public static final int TOP_RIGHT = 2;
|
||||
/**
|
||||
* Style constant for placing decorator image in bottom left corner of base image.
|
||||
* Style constant for placing decorator image in bottom left corner of base
|
||||
* image.
|
||||
*/
|
||||
public static final int BOTTOM_LEFT = 3;
|
||||
/**
|
||||
* Style constant for placing decorator image in bottom right corner of base image.
|
||||
* Style constant for placing decorator image in bottom right corner of base
|
||||
* image.
|
||||
*/
|
||||
public static final int BOTTOM_RIGHT = 4;
|
||||
/**
|
||||
@ -206,83 +212,77 @@ public class SWTResourceManager {
|
||||
* Maps images to decorated images.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<Image, Map<Image, Image>>[] m_decoratedImageMap = new Map[LAST_CORNER_KEY];
|
||||
private static Map<Image, Map<Image, Image>>[] decoratedImageMap = new Map[LAST_CORNER_KEY];
|
||||
|
||||
/**
|
||||
* Returns an {@link Image} composed of a base image decorated by another image.
|
||||
*
|
||||
* @param baseImage
|
||||
* the base {@link Image} that should be decorated
|
||||
* @param decorator
|
||||
* the {@link Image} to decorate the base image
|
||||
* @param baseImage the base {@link Image} that should be decorated
|
||||
* @param decorator the {@link Image} to decorate the base image
|
||||
* @return {@link Image} The resulting decorated image
|
||||
*/
|
||||
public static Image decorateImage(Image baseImage, Image decorator) {
|
||||
return decorateImage(baseImage, decorator, BOTTOM_RIGHT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an {@link Image} composed of a base image decorated by another image.
|
||||
*
|
||||
* @param baseImage
|
||||
* the base {@link Image} that should be decorated
|
||||
* @param decorator
|
||||
* the {@link Image} to decorate the base image
|
||||
* @param corner
|
||||
* the corner to place decorator image
|
||||
* @param baseImage the base {@link Image} that should be decorated
|
||||
* @param decorator the {@link Image} to decorate the base image
|
||||
* @param corner the corner to place decorator image
|
||||
* @return the resulting decorated {@link Image}
|
||||
*/
|
||||
public static Image decorateImage(final Image baseImage, final Image decorator, final int corner) {
|
||||
if (corner <= 0 || corner >= LAST_CORNER_KEY) {
|
||||
throw new IllegalArgumentException("Wrong decorate corner");
|
||||
}
|
||||
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[corner];
|
||||
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = decoratedImageMap[corner];
|
||||
if (cornerDecoratedImageMap == null) {
|
||||
cornerDecoratedImageMap = new HashMap<Image, Map<Image, Image>>();
|
||||
m_decoratedImageMap[corner] = cornerDecoratedImageMap;
|
||||
cornerDecoratedImageMap = new HashMap<>();
|
||||
decoratedImageMap[corner] = cornerDecoratedImageMap;
|
||||
}
|
||||
Map<Image, Image> decoratedMap = cornerDecoratedImageMap.get(baseImage);
|
||||
if (decoratedMap == null) {
|
||||
decoratedMap = new HashMap<Image, Image>();
|
||||
cornerDecoratedImageMap.put(baseImage, decoratedMap);
|
||||
}
|
||||
//
|
||||
Image result = decoratedMap.get(decorator);
|
||||
if (result == null) {
|
||||
Map<Image, Image> decoratedMap = cornerDecoratedImageMap.computeIfAbsent(baseImage,
|
||||
k -> new HashMap<Image, Image>());
|
||||
return decoratedMap.computeIfAbsent(decorator, k -> {
|
||||
Rectangle bib = baseImage.getBounds();
|
||||
Rectangle dib = decorator.getBounds();
|
||||
//
|
||||
result = new Image(Display.getCurrent(), bib.width, bib.height);
|
||||
//
|
||||
Image result = new Image(Display.getCurrent(), bib.width, bib.height);
|
||||
GC gc = new GC(result);
|
||||
gc.drawImage(baseImage, 0, 0);
|
||||
if (corner == TOP_LEFT) {
|
||||
switch (corner) {
|
||||
case TOP_LEFT:
|
||||
gc.drawImage(decorator, 0, 0);
|
||||
} else if (corner == TOP_RIGHT) {
|
||||
break;
|
||||
case TOP_RIGHT:
|
||||
gc.drawImage(decorator, bib.width - dib.width, 0);
|
||||
} else if (corner == BOTTOM_LEFT) {
|
||||
break;
|
||||
case BOTTOM_LEFT:
|
||||
gc.drawImage(decorator, 0, bib.height - dib.height);
|
||||
} else if (corner == BOTTOM_RIGHT) {
|
||||
break;
|
||||
case BOTTOM_RIGHT:
|
||||
gc.drawImage(decorator, bib.width - dib.width, bib.height - dib.height);
|
||||
break;
|
||||
default:
|
||||
// do nothing
|
||||
}
|
||||
gc.dispose();
|
||||
//
|
||||
decoratedMap.put(decorator, result);
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose all of the cached {@link Image}'s.
|
||||
*/
|
||||
public static void disposeImages() {
|
||||
// dispose loaded images
|
||||
{
|
||||
for (Image image : m_imageMap.values()) {
|
||||
image.dispose();
|
||||
}
|
||||
m_imageMap.clear();
|
||||
for (Image image : imageMap.values()) {
|
||||
image.dispose();
|
||||
}
|
||||
imageMap.clear();
|
||||
// dispose decorated images
|
||||
for (int i = 0; i < m_decoratedImageMap.length; i++) {
|
||||
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = m_decoratedImageMap[i];
|
||||
for (int i = 0; i < decoratedImageMap.length; i++) {
|
||||
Map<Image, Map<Image, Image>> cornerDecoratedImageMap = decoratedImageMap[i];
|
||||
if (cornerDecoratedImageMap != null) {
|
||||
for (Map<Image, Image> decoratedMap : cornerDecoratedImageMap.values()) {
|
||||
for (Image image : decoratedMap.values()) {
|
||||
@ -294,6 +294,7 @@ public class SWTResourceManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Font
|
||||
@ -302,45 +303,39 @@ public class SWTResourceManager {
|
||||
/**
|
||||
* Maps font names to fonts.
|
||||
*/
|
||||
private static Map<String, Font> m_fontMap = new HashMap<String, Font>();
|
||||
private static Map<String, Font> fontMap = new HashMap<>();
|
||||
/**
|
||||
* Maps fonts to their bold versions.
|
||||
*/
|
||||
private static Map<Font, Font> m_fontToBoldFontMap = new HashMap<Font, Font>();
|
||||
private static Map<Font, Font> fontToBoldFontMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Returns a {@link Font} based on its name, height and style.
|
||||
*
|
||||
* @param name
|
||||
* the name of the font
|
||||
* @param height
|
||||
* the height of the font
|
||||
* @param style
|
||||
* the style of the font
|
||||
* @param name the name of the font
|
||||
* @param height the height of the font
|
||||
* @param style the style of the font
|
||||
* @return {@link Font} The font matching the name, height and style
|
||||
*/
|
||||
public static Font getFont(String name, int height, int style) {
|
||||
return getFont(name, height, style, false, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Font} based on its name, height and style. Windows-specific strikeout and underline
|
||||
* flags are also supported.
|
||||
* Returns a {@link Font} based on its name, height and style. Windows-specific
|
||||
* strikeout and underline flags are also supported.
|
||||
*
|
||||
* @param name
|
||||
* the name of the font
|
||||
* @param size
|
||||
* the size of the font
|
||||
* @param style
|
||||
* the style of the font
|
||||
* @param strikeout
|
||||
* the strikeout flag (warning: Windows only)
|
||||
* @param underline
|
||||
* the underline flag (warning: Windows only)
|
||||
* @return {@link Font} The font matching the name, height, style, strikeout and underline
|
||||
* @param name the name of the font
|
||||
* @param size the size of the font
|
||||
* @param style the style of the font
|
||||
* @param strikeout the strikeout flag (warning: Windows only)
|
||||
* @param underline the underline flag (warning: Windows only)
|
||||
* @return {@link Font} The font matching the name, height, style, strikeout and
|
||||
* underline
|
||||
*/
|
||||
public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) {
|
||||
String fontName = name + '|' + size + '|' + style + '|' + strikeout + '|' + underline;
|
||||
Font font = m_fontMap.get(fontName);
|
||||
if (font == null) {
|
||||
return fontMap.computeIfAbsent(fontName, k -> {
|
||||
FontData fontData = new FontData(name, size, style);
|
||||
if (strikeout || underline) {
|
||||
try {
|
||||
@ -354,47 +349,45 @@ public class SWTResourceManager {
|
||||
logFontClass.getField("lfUnderline").set(logFont, Byte.valueOf((byte) 1)); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
System.err.println("Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
font = new Font(Display.getCurrent(), fontData);
|
||||
m_fontMap.put(fontName, font);
|
||||
}
|
||||
return font;
|
||||
return new Font(Display.getCurrent(), fontData);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a bold version of the given {@link Font}.
|
||||
*
|
||||
* @param baseFont
|
||||
* the {@link Font} for which a bold version is desired
|
||||
* @param baseFont the {@link Font} for which a bold version is desired
|
||||
* @return the bold version of the given {@link Font}
|
||||
*/
|
||||
public static Font getBoldFont(Font baseFont) {
|
||||
Font font = m_fontToBoldFontMap.get(baseFont);
|
||||
if (font == null) {
|
||||
FontData fontDatas[] = baseFont.getFontData();
|
||||
return fontToBoldFontMap.computeIfAbsent(baseFont, k -> {
|
||||
FontData[] fontDatas = baseFont.getFontData();
|
||||
FontData data = fontDatas[0];
|
||||
font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
|
||||
m_fontToBoldFontMap.put(baseFont, font);
|
||||
}
|
||||
return font;
|
||||
return new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose all of the cached {@link Font}'s.
|
||||
*/
|
||||
public static void disposeFonts() {
|
||||
// clear fonts
|
||||
for (Font font : m_fontMap.values()) {
|
||||
for (Font font : fontMap.values()) {
|
||||
font.dispose();
|
||||
}
|
||||
m_fontMap.clear();
|
||||
fontMap.clear();
|
||||
// clear bold fonts
|
||||
for (Font font : m_fontToBoldFontMap.values()) {
|
||||
for (Font font : fontToBoldFontMap.values()) {
|
||||
font.dispose();
|
||||
}
|
||||
m_fontToBoldFontMap.clear();
|
||||
fontToBoldFontMap.clear();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Cursor
|
||||
@ -403,40 +396,38 @@ public class SWTResourceManager {
|
||||
/**
|
||||
* Maps IDs to cursors.
|
||||
*/
|
||||
private static Map<Integer, Cursor> m_idToCursorMap = new HashMap<Integer, Cursor>();
|
||||
private static Map<Integer, Cursor> idToCursorMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Returns the system cursor matching the specific ID.
|
||||
*
|
||||
* @param id
|
||||
* int The ID value for the cursor
|
||||
* @param id int The ID value for the cursor
|
||||
* @return Cursor The system cursor matching the specific ID
|
||||
*/
|
||||
public static Cursor getCursor(int id) {
|
||||
Integer key = Integer.valueOf(id);
|
||||
Cursor cursor = m_idToCursorMap.get(key);
|
||||
if (cursor == null) {
|
||||
cursor = new Cursor(Display.getDefault(), id);
|
||||
m_idToCursorMap.put(key, cursor);
|
||||
}
|
||||
return cursor;
|
||||
return idToCursorMap.computeIfAbsent(key, k -> new Cursor(Display.getDefault(), id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose all of the cached cursors.
|
||||
*/
|
||||
public static void disposeCursors() {
|
||||
for (Cursor cursor : m_idToCursorMap.values()) {
|
||||
for (Cursor cursor : idToCursorMap.values()) {
|
||||
cursor.dispose();
|
||||
}
|
||||
m_idToCursorMap.clear();
|
||||
idToCursorMap.clear();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// General
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Dispose of cached objects and their underlying OS resources. This should only be called when the cached
|
||||
* objects are no longer needed (e.g. on application shutdown).
|
||||
* Dispose of cached objects and their underlying OS resources. This should only
|
||||
* be called when the cached objects are no longer needed (e.g. on application
|
||||
* shutdown).
|
||||
*/
|
||||
public static void dispose() {
|
||||
disposeColors();
|
||||
|
Reference in New Issue
Block a user