use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawComboBox.
/**
* {@inheritDoc}
*/
public void drawComboBox(Graphics g, List cb) {
int border = 2;
Style style = cb.getStyle();
int leftPadding = style.getPaddingLeft(cb.isRTL());
int rightPadding = style.getPaddingRight(cb.isRTL());
setFG(g, cb);
ListModel model = cb.getModel();
ListCellRenderer renderer = cb.getRenderer();
Object value = model.getItemAt(model.getSelectedIndex());
int comboImageWidth;
if (comboImage != null) {
comboImageWidth = comboImage.getWidth();
} else {
comboImageWidth = style.getFont().getHeight();
}
int cellX = cb.getX() + style.getPaddingTop();
if (cb.isRTL()) {
cellX += comboImageWidth;
}
if (model.getSize() > 0) {
Component cmp = renderer.getListCellRendererComponent(cb, value, model.getSelectedIndex(), cb.hasFocus());
cmp.setX(cellX);
cmp.setY(cb.getY() + style.getPaddingTop());
cmp.setWidth(cb.getWidth() - comboImageWidth - rightPadding - leftPadding);
cmp.setHeight(cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom());
cmp.paint(g);
}
g.setColor(style.getBgColor());
int y = cb.getY();
int height = cb.getHeight();
int width = comboImageWidth + border;
int x = cb.getX();
if (cb.isRTL()) {
x += leftPadding;
} else {
x += cb.getWidth() - comboImageWidth - rightPadding;
}
if (comboImage != null) {
g.drawImage(comboImage, x, y + height / 2 - comboImage.getHeight() / 2);
} else {
int color = g.getColor();
// brighten or darken the color slightly
int destColor = findDestColor(color);
g.fillLinearGradient(g.getColor(), destColor, x, y, width, height, false);
g.setColor(color);
g.drawRect(x, y, width, height - 1);
width--;
height--;
// g.drawRect(x, y, width, height);
g.translate(x + 1, y + 1);
g.setColor(0x111111);
int x1 = scaleCoordinate(2.5652081f, 16, width);
int y1 = scaleCoordinate(4.4753664f, 16, height);
int x2 = scaleCoordinate(8.2872691f, 16, width);
int y2 = scaleCoordinate(10f, 16, height);
int x3 = scaleCoordinate(13.516078f, 16, width);
int y3 = y1;
g.fillTriangle(x1, y1, x2, y2, x3, y3);
g.translate(-1, -1);
g.setColor(style.getFgColor());
g.fillTriangle(x1, y1, x2, y2, x3, y3);
// g.setColor(style.getFgColor());
// g.fillTriangle(x1 + 2, y1 + 2, x2, y2 - 2, x3 - 2, y3 + 2);
g.translate(-x, -y);
}
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class RoundBorder method stroke.
/**
* Sets the stroke of the circle/rectangle
* @param stroke the thickness of the stroke object
* @param mm set to true to indicate the value is in millimeters, false indicates pixels
* @return border instance so these calls can be chained
*/
public RoundBorder stroke(float stroke, boolean mm) {
strokeThickness = stroke;
if (strokeThickness == 0) {
this.stroke = null;
return this;
}
strokeMM = mm;
if (mm) {
stroke = Display.getInstance().convertToPixels(stroke);
}
return stroke(new Stroke(stroke, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1));
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class UIManager method parseStyle.
/**
* Creates a style by providing style strings in a specific format. This method allows for the use of inline styles
* to override the styles in {@link com.codename1.ui.Component}
* @param theme Theme used to retrieve images referenced in the style strings.
* @param id The style ID (UIID) to use to cache the style inside the theme.
* @param prefix Prefix to use for styles. Corresponds to the {@literal prefix} argument in {@link #getComponentStyleImpl(java.lang.String, boolean, java.lang.String)
* @param baseStyle The style class from which this new style should derive.
* @param selected True if this is for a selected style.
* @param styleString Array of style strings to be parsed. The format is {@literal key1:value1; key2:value2; etc...}. While this looks similar to CSS, it is important to note that it is NOT
* CSS. The keys and values correspond to the properties of {@link com.codename1.ui.plaf.Style} and their associated values.
* @return A style object representing the styles that were provided in the styleString.
*
* <h3>Example Usage</h3>
*
* {@code
* Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
* "fgColor:ff0000; font:18mm; border: 1px solid ff0000; bgType:none; padding: 3mm; margin: 1mm");
*
* // Create a 9-piece image border on the fly:
* Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
* "border:splicedImage /notes.png 0.3 0.4 0.3 0.4");
* // This splices the image found at /notes.png into 9 pieces. Splice insets are specified by the 4 floating point values
* // at the end of the border directive: [top] [right] [bottom] [left].
* }
*/
Style parseStyle(Resources theme, String id, String prefix, String baseStyle, boolean selected, String... styleString) {
String cacheKey = selected ? id + ".sel" : id + "." + prefix;
String originalId = id;
if (id == null || id.length() == 0) {
// if no id return the default style
id = "";
} else {
id = id + ".";
}
if (Arrays.toString(styleString).equals(parseCache().get(cacheKey)) && ((selected && selectedStyles.containsKey(id)) || (!selected && this.styles.containsKey(id)))) {
return getComponentStyleImpl(originalId, selected, prefix);
}
parseCache().put(cacheKey, Arrays.toString(styleString));
Style base = baseStyle != null ? getComponentStyleImpl(baseStyle, selected, prefix) : null;
Map<String, String> styles = new HashMap<String, String>();
for (String str : styleString) {
StyleParser.parseString(styles, str);
}
StyleInfo styleInfo = new StyleInfo(styles);
if (prefix != null && prefix.length() > 0) {
id += prefix;
}
if (themeProps == null) {
resetThemeProps(null);
}
if (baseStyle != null) {
themeProps.put(id + "derive", baseStyle);
} else {
themeProps.remove(id + "derive");
}
String val = null;
Integer bgColor = styleInfo.getBgColor();
if (bgColor != null) {
themeProps.put(id + Style.BG_COLOR, Integer.toHexString(bgColor));
} else {
themeProps.remove(id + Style.BG_COLOR);
}
Integer fgColor = styleInfo.getFgColor();
if (fgColor != null) {
themeProps.put(id + Style.FG_COLOR, Integer.toHexString(fgColor));
} else {
themeProps.remove(id + Style.FG_COLOR);
}
BorderInfo border = styleInfo.getBorder();
if (border != null) {
themeProps.put(id + Style.BORDER, border.createBorder(theme));
} else {
themeProps.remove(id + Style.BORDER);
}
Integer bgType = styleInfo.getBgType();
if (bgType != null) {
themeProps.put(id + Style.BACKGROUND_TYPE, bgType.byteValue());
} else {
themeProps.remove(id + Style.BACKGROUND_TYPE);
}
ImageInfo bgImage = styleInfo.getBgImage();
if (bgImage != null) {
themeProps.put(id + Style.BG_IMAGE, bgImage.getImage(theme));
} else {
themeProps.remove(id + Style.BG_IMAGE);
}
MarginInfo margin = styleInfo.getMargin();
if (margin != null) {
float[] marginArr = margin.createMargin(base);
themeProps.put(id + Style.MARGIN, marginArr[Component.TOP] + "," + marginArr[Component.BOTTOM] + "," + marginArr[Component.LEFT] + "," + marginArr[Component.RIGHT]);
byte[] unitArr = margin.createMarginUnit(base);
themeProps.put(id + Style.MARGIN_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
} else {
themeProps.remove(id + Style.MARGIN);
themeProps.remove(id + Style.MARGIN_UNIT);
}
PaddingInfo padding = styleInfo.getPadding();
if (padding != null) {
float[] paddingArr = padding.createPadding(base);
themeProps.put(id + Style.PADDING, paddingArr[Component.TOP] + "," + paddingArr[Component.BOTTOM] + "," + paddingArr[Component.LEFT] + "," + paddingArr[Component.RIGHT]);
byte[] unitArr = padding.createPaddingUnit(base);
themeProps.put(id + Style.PADDING_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
} else {
themeProps.remove(id + Style.PADDING);
themeProps.remove(id + Style.PADDING_UNIT);
}
Integer transparency = styleInfo.getTransparency();
if (transparency != null) {
themeProps.put(id + Style.TRANSPARENCY, String.valueOf(transparency.intValue()));
} else {
themeProps.remove(id + Style.TRANSPARENCY);
}
Integer opacity = styleInfo.getOpacity();
if (opacity != null) {
themeProps.put(id + Style.OPACITY, String.valueOf(opacity.intValue()));
} else {
themeProps.remove(id + Style.OPACITY);
}
Integer alignment = styleInfo.getAlignment();
if (alignment != null) {
themeProps.put(id + Style.ALIGNMENT, alignment);
} else {
themeProps.remove(id + Style.ALIGNMENT);
}
Integer textDecoration = styleInfo.getTextDecoration();
if (textDecoration != null) {
themeProps.put(id + Style.TEXT_DECORATION, textDecoration);
} else {
themeProps.remove(id + Style.TEXT_DECORATION);
}
FontInfo font = styleInfo.getFont();
if (font != null) {
themeProps.put(id + Style.FONT, font.createFont(base));
} else {
themeProps.remove(id + Style.FONT);
}
if (selected)
selectedStyles.remove(id);
else
this.styles.remove(id);
return getComponentStyleImpl(originalId, selected, prefix);
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class EditableResources method saveTheme.
private void saveTheme(DataOutputStream output, Hashtable theme, boolean newVersion) throws IOException {
theme.remove("name");
output.writeShort(theme.size());
for (Object currentKey : theme.keySet()) {
String key = (String) currentKey;
output.writeUTF(key);
if (key.startsWith("@")) {
if (key.endsWith("Image")) {
output.writeUTF(findId(theme.get(key), true));
} else {
output.writeUTF((String) theme.get(key));
}
continue;
}
// if this is a simple numeric value
if (key.endsWith("Color")) {
output.writeInt(Integer.decode("0x" + theme.get(key)));
continue;
}
if (key.endsWith("align") || key.endsWith("textDecoration")) {
output.writeShort(((Number) theme.get(key)).shortValue());
continue;
}
// if this is a short numeric value
if (key.endsWith("transparency")) {
output.writeByte(Integer.parseInt((String) theme.get(key)));
continue;
}
if (key.endsWith("opacity")) {
output.writeInt(Integer.parseInt((String) theme.get(key)));
continue;
}
// if this is a padding or margin then we will have the 4 values as bytes
if (key.endsWith("padding") || key.endsWith("margin")) {
String[] arr = ((String) theme.get(key)).split(",");
output.writeFloat(Float.parseFloat(arr[0]));
output.writeFloat(Float.parseFloat(arr[1]));
output.writeFloat(Float.parseFloat(arr[2]));
output.writeFloat(Float.parseFloat(arr[3]));
continue;
}
// padding and or margin type
if (key.endsWith("Unit")) {
for (byte b : (byte[]) theme.get(key)) {
output.writeByte(b);
}
continue;
}
if (key.endsWith("border")) {
Border border = (Border) theme.get(key);
writeBorder(output, border, newVersion);
continue;
}
// if this is a font
if (key.endsWith("font")) {
com.codename1.ui.Font f = (com.codename1.ui.Font) theme.get(key);
// is this a new font?
boolean newFont = f instanceof EditorFont;
output.writeBoolean(newFont);
if (newFont) {
String fontId = findId(f);
output.writeUTF(fontId);
} else {
output.writeByte(f.getFace());
output.writeByte(f.getStyle());
output.writeByte(f.getSize());
if (f instanceof EditorTTFFont && (((EditorTTFFont) f).getFontFile() != null || ((EditorTTFFont) f).getNativeFontName() != null)) {
output.writeBoolean(true);
EditorTTFFont ed = (EditorTTFFont) f;
if (ed.getNativeFontName() != null) {
output.writeUTF(ed.getNativeFontName());
output.writeUTF(ed.getNativeFontName());
} else {
output.writeUTF(ed.getFontFile().getName());
output.writeUTF(((java.awt.Font) ed.getNativeFont()).getPSName());
}
output.writeInt(ed.getSizeSetting());
output.writeFloat(ed.getActualSize());
} else {
output.writeBoolean(false);
}
}
continue;
}
// if this is a background image
if (key.endsWith("bgImage")) {
String imageId = findId(theme.get(key), true);
if (imageId == null) {
imageId = "";
}
output.writeUTF(imageId);
continue;
}
if (key.endsWith("scaledImage")) {
output.writeBoolean(theme.get(key).equals("true"));
continue;
}
if (key.endsWith("derive")) {
output.writeUTF((String) theme.get(key));
continue;
}
// if this is a background gradient
if (key.endsWith("bgGradient")) {
Object[] gradient = (Object[]) theme.get(key);
output.writeInt(((Integer) gradient[0]).intValue());
output.writeInt(((Integer) gradient[1]).intValue());
output.writeFloat(((Float) gradient[2]).floatValue());
output.writeFloat(((Float) gradient[3]).floatValue());
output.writeFloat(((Float) gradient[4]).floatValue());
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
output.writeByte(((Number) theme.get(key)).intValue());
continue;
}
// thow an exception no idea what this is
throw new IOException("Error while trying to read theme property: " + key);
}
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class ImageRGBEditor method findImageUseImpl.
private static void findImageUseImpl(com.codename1.ui.Image resourceValue, Vector users, EditableResources res, JLabel borderPreview) {
for (String themeName : res.getThemeResourceNames()) {
Hashtable theme = res.getTheme(themeName);
for (Object key : theme.keySet()) {
Object value = theme.get(key);
if (value instanceof com.codename1.ui.Image) {
if (value.equals(resourceValue)) {
addToUsers((String) key, users);
}
}
if (value instanceof Border) {
Border b = (Border) value;
// BORDER_TYPE_IMAGE
if (Accessor.getType(b) == Accessor.TYPE_IMAGE || Accessor.getType(b) == Accessor.TYPE_IMAGE_HORIZONTAL || Accessor.getType(b) == Accessor.TYPE_IMAGE_VERTICAL) {
com.codename1.ui.Image[] images = Accessor.getImages(b);
for (int i = 0; i < images.length; i++) {
if (images[i] == resourceValue) {
addToUsers((String) key, users);
if (borderPreview != null && borderPreview.getIcon() == null) {
int borderWidth = Math.max(100, b.getMinimumWidth());
int borderHeight = Math.max(100, b.getMinimumHeight());
com.codename1.ui.Image img = com.codename1.ui.Image.createImage(borderWidth, borderHeight);
com.codename1.ui.Label l = new com.codename1.ui.Label("Preview");
l.getStyle().setBorder(b);
l.setSize(new com.codename1.ui.geom.Dimension(borderWidth, borderHeight));
l.paintComponent(img.getGraphics());
CodenameOneImageIcon icon = new CodenameOneImageIcon(img, borderWidth, borderHeight);
borderPreview.setIcon(icon);
}
}
}
}
}
}
}
// check if a timeline is making use of said image and replace it
for (String image : res.getImageResourceNames()) {
com.codename1.ui.Image current = res.getImage(image);
if (current instanceof com.codename1.ui.animations.Timeline) {
com.codename1.ui.animations.Timeline time = (com.codename1.ui.animations.Timeline) current;
for (int iter = 0; iter < time.getAnimationCount(); iter++) {
com.codename1.ui.animations.AnimationObject o = time.getAnimation(iter);
if (AnimationAccessor.getImage(o) == resourceValue) {
addToUsers(image, users);
}
}
}
}
// check if a UI resource is making use of the image
UIBuilderOverride builder = new UIBuilderOverride();
for (String uiResource : res.getUIResourceNames()) {
com.codename1.ui.Container c = builder.createContainer(res, uiResource);
if (ResourceEditorView.findImageInContainer(c, resourceValue)) {
addToUsers(uiResource, users);
}
}
}
Aggregations