use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class Resources method loadTheme.
Hashtable loadTheme(String id, boolean newerVersion) throws IOException {
Hashtable theme = new Hashtable();
theme.put("name", id);
// marks the theme as uninitialized so we can finish "wiring" cached resources
theme.put("uninitialized", Boolean.TRUE);
int size = input.readShort();
for (int iter = 0; iter < size; iter++) {
String key = input.readUTF();
if (key.startsWith("@")) {
theme.put(key, input.readUTF());
continue;
}
// if this is a simple numeric value
if (key.endsWith("Color")) {
theme.put(key, Integer.toHexString(input.readInt()));
continue;
}
if (key.endsWith("align") || key.endsWith("textDecoration")) {
theme.put(key, new Integer(input.readShort()));
continue;
}
// if this is a short numeric value for transparency
if (key.endsWith("ransparency")) {
theme.put(key, "" + (input.readByte() & 0xff));
continue;
}
if (key.endsWith("opacity")) {
theme.put(key, "" + (input.readInt() & 0xff));
continue;
}
// if this is a padding or margin then we will have the 4 values as bytes
if (key.endsWith("adding") || key.endsWith("argin")) {
if (minorVersion > 7) {
float p1 = input.readFloat();
float p2 = input.readFloat();
float p3 = input.readFloat();
float p4 = input.readFloat();
theme.put(key, "" + p1 + "," + p2 + "," + p3 + "," + p4);
} else {
int p1 = input.readByte() & 0xff;
int p2 = input.readByte() & 0xff;
int p3 = input.readByte() & 0xff;
int p4 = input.readByte() & 0xff;
theme.put(key, "" + p1 + "," + p2 + "," + p3 + "," + p4);
}
continue;
}
// padding and or margin type
if (key.endsWith("Unit")) {
byte p1 = input.readByte();
byte p2 = input.readByte();
byte p3 = input.readByte();
byte p4 = input.readByte();
theme.put(key, new byte[] { p1, p2, p3, p4 });
continue;
}
// border
if (key.endsWith("order")) {
int borderType = input.readShort() & 0xffff;
Object b = createBorder(input, borderType);
theme.put(key, b);
continue;
}
// if this is a font
if (key.endsWith("ont")) {
Font f;
// is this a new font?
if (input.readBoolean()) {
String fontId = input.readUTF();
f = (Font) resources.get(fontId);
// if the font is not yet loaded
if (f == null) {
theme.put(key, fontId);
continue;
}
} else {
f = Font.createSystemFont(input.readByte(), input.readByte(), input.readByte());
if (minorVersion > 4) {
boolean hasTTF = input.readBoolean();
if (hasTTF) {
String fileName = input.readUTF();
String fontName = input.readUTF();
int sizeSetting = input.readInt();
float fontSize = input.readFloat();
if (Font.isTrueTypeFileSupported()) {
f = createTrueTypeFont(f, fontName, fileName, fontSize, sizeSetting);
}
}
}
}
theme.put(key, f);
continue;
}
// the background property
if (key.endsWith("ackground")) {
int type = input.readByte() & 0xff;
int pos = key.indexOf('.');
if (pos > -1) {
key = key.substring(0, pos);
} else {
key = "";
}
theme.put(key + Style.BACKGROUND_TYPE, new Byte((byte) type));
switch(type) {
// Scaled Image
case 0xF1:
// Tiled Both Image
case MAGIC_INDEXED_IMAGE_LEGACY:
// the image name coupled with the type
theme.put(key + Style.BG_IMAGE, input.readUTF());
break;
// Aligned Image
case 0xf5:
// Tiled Vertically Image
case 0xF2:
// Tiled Horizontally Image
case 0xF3:
// the image name coupled with the type and with alignment information
String imageName = input.readUTF();
theme.put(key + Style.BG_IMAGE, imageName);
byte align = input.readByte();
theme.put(key + Style.BACKGROUND_ALIGNMENT, new Byte(align));
break;
// Horizontal Linear Gradient
case 0xF6:
// Vertical Linear Gradient
case 0xF7:
Float c = new Float(0.5f);
theme.put(key + Style.BACKGROUND_GRADIENT, new Object[] { new Integer(input.readInt()), new Integer(input.readInt()), c, c, new Float(1) });
break;
// Radial Gradient
case 0xF8:
int c1 = input.readInt();
int c2 = input.readInt();
float f1 = input.readFloat();
float f2 = input.readFloat();
float radialSize = 1;
if (minorVersion > 1) {
radialSize = input.readFloat();
}
theme.put(key + Style.BACKGROUND_GRADIENT, new Object[] { new Integer(c1), new Integer(c2), new Float(f1), new Float(f2), new Float(radialSize) });
break;
}
continue;
}
// if this is a background image bgImage
if (key.endsWith("derive")) {
theme.put(key, input.readUTF());
continue;
}
// if this is a background image bgImage
if (key.endsWith("bgImage")) {
String imageId = input.readUTF();
Image i = getImage(imageId);
// if the font is not yet loaded
if (i == null) {
theme.put(key, imageId);
continue;
}
theme.put(key, i);
continue;
}
if (key.endsWith("scaledImage")) {
if (input.readBoolean()) {
theme.put(key, "true");
} else {
theme.put(key, "false");
}
continue;
}
if (key.endsWith(Style.BACKGROUND_TYPE) || key.endsWith(Style.BACKGROUND_ALIGNMENT)) {
theme.put(key, new Byte(input.readByte()));
continue;
}
if (key.endsWith(Style.BACKGROUND_GRADIENT)) {
if (minorVersion < 2) {
theme.put(key, new Object[] { new Integer(input.readInt()), new Integer(input.readInt()), new Float(input.readFloat()), new Float(input.readFloat()) });
} else {
theme.put(key, new Object[] { new Integer(input.readInt()), new Integer(input.readInt()), new Float(input.readFloat()), new Float(input.readFloat()), new Float(input.readFloat()) });
}
continue;
}
// thow an exception no idea what this is
throw new IOException("Error while trying to read theme property: " + key);
}
return theme;
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class Resources method confirmBorder.
private Border confirmBorder(Hashtable h, String key) {
Object val = h.get(key);
if (val == null) {
return null;
}
if (!(val instanceof Border)) {
String[] value = (String[]) val;
if (value == null) {
throw new IllegalArgumentException("Couldn't find resource: " + key);
}
// the resource was not already loaded when we loaded the theme
// it must be loaded now so we can resolve the temporary name
Border imageBorder = createImageBorder(value);
return imageBorder;
}
return (Border) val;
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class Resources method getTheme.
/**
* Returns the theme resource from the file
*
* @param id name of the theme resource
* @return cached theme instance
*/
public Hashtable getTheme(String id) {
Hashtable h = (Hashtable) resources.get(id);
// theme can be null in valid use cases such as the resource editor
if (h != null && h.containsKey("uninitialized")) {
Enumeration e = h.keys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if (key.endsWith("font") || (key.endsWith("Image") && !key.endsWith("scaledImage"))) {
Object value = h.get(key);
if (value == null) {
throw new IllegalArgumentException("Couldn't find resource: " + key);
}
// it must be loaded now so we can resolve the temporary name
if (value instanceof String) {
Object o;
// we need to trigger multi image logic in the resource editor
if (key.endsWith("Image")) {
o = getImage((String) value);
} else {
o = resources.get(value);
}
if (o == null) {
throw new IllegalArgumentException("Theme entry for " + key + " could not be found: " + value);
}
h.put(key, o);
}
}
// been loaded yet when the border was created
if (key.endsWith("order")) {
Border b = confirmBorder(h, key);
h.put(key, b);
}
}
h.remove("uninitialized");
}
return h;
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class Table method paintGlass.
/**
* {@inheritDoc}
*/
protected void paintGlass(Graphics g) {
if ((drawBorder) && (innerBorder != INNER_BORDERS_NONE)) {
int xPos = getAbsoluteX();
int yPos = getAbsoluteY();
g.translate(xPos, yPos);
int rows = model.getRowCount();
int cols = model.getColumnCount();
if (includeHeader) {
rows++;
}
g.setColor(getStyle().getFgColor());
TableLayout t = (TableLayout) getLayout();
int actualWidth = Math.max(getWidth(), getScrollDimension().getWidth());
int actualHeight = Math.max(getHeight(), getScrollDimension().getHeight());
if (// inner borders cols/rows are supported only in collapsed mode
(collapseBorder) || (innerBorder != INNER_BORDERS_ALL) || (t.hasHorizontalSpanning()) || (t.hasVerticalSpanning())) {
// TODO - We currently don't support separate borders for tables with spanned cells
if ((innerBorder == INNER_BORDERS_ALL) || (innerBorder == INNER_BORDERS_ROWS)) {
if (t.hasVerticalSpanning()) {
// the components other than the ones that are at the last column.
for (int cellRow = 0; cellRow < rows - 1; cellRow++) {
for (int cellColumn = 0; cellColumn < cols; cellColumn++) {
// if this isn't the last row
if (cellRow + t.getCellVerticalSpan(cellRow, cellColumn) - 1 != rows - 1) {
// if this is a spanned through cell we don't want to draw a line here
if (t.isCellSpannedThroughHorizontally(cellRow, cellColumn)) {
continue;
}
int x = t.getColumnPosition(cellColumn);
int y = t.getRowPosition(cellRow);
int rowHeight = t.getRowPosition(cellRow + t.getCellVerticalSpan(cellRow, cellColumn)) - y;
int columnWidth;
if (cellColumn < getModel().getColumnCount() - 1) {
columnWidth = t.getColumnPosition(cellColumn + 1) - x;
} else {
columnWidth = getWidth() - y;
}
if ((innerBorder != INNER_BORDERS_ROWS) || (shouldDrawInnerBorderAfterRow(cellRow))) {
g.drawLine(x, y + rowHeight, x + columnWidth, y + rowHeight);
}
}
}
}
} else {
// this is much faster since we don't need to check spanning
for (int row = 1; row < rows; row++) {
int y = t.getRowPosition(row);
if ((innerBorder != INNER_BORDERS_ROWS) || (shouldDrawInnerBorderAfterRow(row - 1))) {
g.drawLine(0, y, actualWidth, y);
}
// g.drawLine(0+2, y+2, actualWidth-2, y+2);
}
}
}
if ((innerBorder == INNER_BORDERS_ALL) || (innerBorder == INNER_BORDERS_COLS)) {
if (t.hasHorizontalSpanning()) {
// the components other than the ones that are at the last column.
for (int cellRow = 0; cellRow < rows; cellRow++) {
for (int cellColumn = 0; cellColumn < cols - 1; cellColumn++) {
// if this isn't the last column
if (cellColumn + t.getCellHorizontalSpan(cellRow, cellColumn) - 1 != cols - 1) {
// if this is a spanned through cell we don't want to draw a line here
if (t.isCellSpannedThroughVertically(cellRow, cellColumn)) {
continue;
}
int x = t.getColumnPosition(cellColumn);
int y = t.getRowPosition(cellRow);
int rowHeight;
int columnWidth = t.getColumnPosition(cellColumn + t.getCellHorizontalSpan(cellRow, cellColumn)) - x;
if (cellRow < getModel().getRowCount() - 1) {
rowHeight = t.getRowPosition(cellRow + 1) - y;
} else {
rowHeight = getHeight() - y;
}
g.drawLine(x + columnWidth, y, x + columnWidth, y + rowHeight);
}
if (t.getCellHorizontalSpan(cellRow, cellColumn) > 1) {
cellColumn += t.getCellHorizontalSpan(cellRow, cellColumn) - 1;
}
}
}
} else {
for (int col = 1; col < cols; col++) {
int x = t.getColumnPosition(col);
g.drawLine(x, 0, x, actualHeight);
// g.drawLine(x+2, 0+2, x+2, actualHeight-2);
}
}
}
} else {
// if ((!t.hasHorizontalSpanning()) && (!t.hasVerticalSpanning())) {
for (int row = 0; row < rows; row++) {
int y = t.getRowPosition(row);
int h;
if (row + 1 < rows) {
h = t.getRowPosition(row + 1) - y;
} else {
h = getY() + actualHeight - y - 2;
}
for (int col = 0; col < cols; col++) {
int x = t.getColumnPosition(col);
int w;
if (col + 1 < cols) {
w = t.getColumnPosition(col + 1) - x;
} else {
w = getX() + actualWidth - x - 2;
}
Component comp = t.getComponentAt(row, col);
if ((comp.isVisible()) && ((drawEmptyCellsBorder) || ((comp.getWidth() - comp.getStyle().getPaddingRightNoRTL() - comp.getStyle().getPaddingLeftNoRTL() > 0) && (comp.getHeight() - comp.getStyle().getPaddingTop() - comp.getStyle().getPaddingBottom() > 0)))) {
int rightMargin = comp.getStyle().getMarginRightNoRTL();
int bottomMargin = comp.getStyle().getMarginBottom();
if (col == 0) {
// Since the first cell includes margins from both sides (left/right) so the next cell location is farther away - but we don't want to paint the border up to it
rightMargin *= 2;
}
if (row == 0) {
bottomMargin *= 2;
}
g.drawRect(x + comp.getStyle().getMarginLeftNoRTL(), y + comp.getStyle().getMarginTop(), w - 2 - rightMargin, h - 2 - bottomMargin);
}
}
}
}
g.translate(-xPos, -yPos);
}
}
use of com.codename1.ui.plaf.Border in project CodenameOne by codenameone.
the class Table method updateMargins.
private void updateMargins() {
TableLayout t = (TableLayout) getLayout();
int hSpace = horizontalBorderSpacing;
int vSpace = verticalBorderSpacing;
if (collapseBorder) {
// not relevant for collapse border
hSpace = 0;
vSpace = 0;
}
if ((!t.hasHorizontalSpanning()) && (!t.hasVerticalSpanning())) {
for (int row = 0; row < t.getRows(); row++) {
for (int col = 0; col < t.getColumns(); col++) {
Component cmp = null;
try {
cmp = t.getComponentAt(row, col);
} catch (Exception e) {
// parent of cmp can be null as well - TODO - check why
}
if (cmp != null) {
int leftMargin = (col == 0) ? hSpace : 0;
int topMargin = (row == 0) ? vSpace : 0;
cmp.getUnselectedStyle().setMargin(topMargin, vSpace, leftMargin, hSpace);
cmp.getSelectedStyle().setMargin(topMargin, vSpace, leftMargin, hSpace);
}
}
}
}
repaint();
}
Aggregations