📄 thinlet.java
字号:
}
}
/**
* Gets the preferred size of the root component
*
* @return a dimension object indicating the root component's preferred size
*/
public Dimension getPreferredSize() {
return getPreferredSize(content);
}
/**
*
* @throws java.lang.IllegalArgumentException
*/
private Dimension getPreferredSize(Object component) {
int width = getInteger(component, "width", 0);
int height = getInteger(component, "height", 0);
if ((width > 0) && (height > 0)) {
return new Dimension(width, height);
}
String classname = getClass(component);
if ("label" == classname) {
return getSize(component, 0, 0);
}
if (("button" == classname) || ("togglebutton" == classname)) {
boolean link = ("button" == classname) && (get(component, "type") == "link");
return getSize(component, link ? 0 : 12, link ? 0 : 6);
}
if ("checkbox" == classname) {
Dimension d = getSize(component, 0, 0);
d.width = d.width + block + 3;
d.height = Math.max(block, d.height);
return d;
}
if ("combobox" == classname) {
if (getBoolean(component, "editable", true)) {
Dimension size = getFieldSize(component);
Image icon = getIcon(component, "icon", null);
if (icon != null) {
size.width += icon.getWidth(this);
size.height = Math.max(size.height, icon.getHeight(this) + 2);
}
size.width += block;
return size;
} else {
// maximum size of current values and choices including 2-2-2-2 insets
Dimension size = getSize(component, 4 , 4);
for (Object item = get(component, ":comp"); item != null; item = get(item, ":next")) {
Dimension d = getSize(item, 4 , 4);
size.width = Math.max(d.width, size.width); size.height = Math.max(d.height, size.height);
}
size.width += block;
if (size.height == 4) { // no content nor items, set text height
Font customfont = (Font) get(component, "font");
FontMetrics fm = getFontMetrics((customfont != null) ? customfont : font);
size.height = fm.getAscent() + fm.getDescent() + 4;
}
return size;
}
}
if (("textfield" == classname) || ("passwordfield" == classname)) {
return getFieldSize(component);
}
if ("textarea" == classname) {
int columns = getInteger(component, "columns", 0);
int rows = getInteger(component, "rows", 0); // 'e' -> 'm' ?
Font currentfont = (Font) get(component, "font");
FontMetrics fm = getFontMetrics((currentfont != null) ? currentfont : font);
return new Dimension(
((columns > 0) ? (columns * fm.charWidth('e') + 2) : 76) + 2 + block,
((rows > 0) ? (rows * fm.getHeight() - fm.getLeading() + 2) : 76) + 2 + block);
}
if ("tabbedpane" == classname) {
String placement = getString(component, "placement", "top");
boolean horizontal = ((placement != "left") && (placement != "right"));
int tabsize = 0; // max tab height (for horizontal),
// max tabwidth (for vertical), or sum of tab heights for stacked
int contentwidth = 0; int contentheight = 0; // max content size
for (Object tab = get(component, ":comp");
tab != null; tab = get(tab, ":next")) {
Dimension d = getSize(tab, 0, 0);
if (placement == "stacked") { tabsize += d.height + 3; }
else { tabsize = Math.max(tabsize, horizontal ? d.height + 5 : d.width + 9); }
Object comp = get(tab, ":comp");
if ((comp != null) && getBoolean(comp, "visible", true)) {
Dimension dc = getPreferredSize(comp);
contentwidth = Math.max(contentwidth, dc.width);
contentheight = Math.max(contentheight, dc.height);
}
}
return new Dimension(contentwidth + (horizontal ? 4 : (tabsize + 3)),
contentheight + (horizontal ? (tabsize + 3) : 4));
}
if (("panel" == classname) || (classname == "dialog")) {
// title text and icon height
Dimension size = getSize(component, 0, 0);
// add border size
if (classname == "dialog") {
size.width = 8; size.height += 8; // title width neglected
}
else if (getBoolean(component, "border", false)) { // bordered panel
size.width = 2; size.height += (size.height > 0) ? 1 : 2; // title includes line
}
else { size.width = 0; } // title width is clipped
// add paddings
size.width += getInteger(component, "left", 0) + getInteger(component, "right", 0);
size.height += getInteger(component, "top", 0) + getInteger(component, "bottom", 0);
// add content preferred size
int gap = getInteger(component, "gap", 0);
int[][] grid = getGrid(component, gap);
if (grid != null) { // has components
size.width += getSum(grid[0], 0, grid[0].length, gap, false);
size.height += getSum(grid[1], 0, grid[1].length, gap, false);
}
return size;
}
else if ("desktop" == classname) {
Dimension size = new Dimension();
for (Object comp = get(component, ":comp");
comp != null; comp = get(comp, ":next")) {
String iclass = getClass(comp);
if ((iclass != "dialog") && (iclass != ":popup") &&
(iclass != ":combolist")) {
Dimension d = getPreferredSize(comp);
size.width = Math.max(d.width, size.width);
size.height = Math.max(d.height, size.height);
}
}
return size;
}
if ("spinbox" == classname) {
Dimension size = getFieldSize(component);
size.width += block;
return size;
}
if ("progressbar" == classname) {
boolean horizontal = ("vertical" != get(component, "orientation"));
return new Dimension(horizontal ? 76 : 6, horizontal ? 6 : 76);
}
if ("slider" == classname) {
boolean horizontal = ("vertical" != get(component, "orientation"));
return new Dimension(horizontal ? 76 : 10, horizontal ? 10 : 76);
}
if ("splitpane" == classname) {
boolean horizontal = ("vertical" != get(component, "orientation"));
Object comp1 = get(component, ":comp");
Dimension size = ((comp1 == null) || !getBoolean(comp1, "visible", true)) ?
new Dimension() : getPreferredSize(comp1);
Object comp2 = get(comp1, ":next");
if ((comp2 != null) && getBoolean(comp2, "visible", true)) {
Dimension d = getPreferredSize(comp2);
size.width = horizontal ? (size.width + d.width) :
Math.max(size.width, d.width);
size.height = horizontal ? Math.max(size.height, d.height) :
(size.height + d.height);
}
if (horizontal) { size.width += 5; } else { size.height += 5; }
return size;
}
if (("list" == classname) ||
("table" == classname) || ("tree" == classname)) {
return new Dimension(76 + 2 + block, 76 + 2 + block);
}
if ("separator" == classname) {
return new Dimension(1, 1);
}
if ("menubar" == classname) {
Dimension size = new Dimension(0, 0);
for (Object menu = get(component, ":comp");
menu != null; menu = get(menu, ":next")) {
Dimension d = getSize(menu, 8, 4);
size.width += d.width;
size.height = Math.max(size.height, d.height);
}
return size;
}
if ("bean" == classname) {
return ((Component) get(component, "bean")).getPreferredSize();
}
throw new IllegalArgumentException((String) classname);
}
/**
* @param component a container
* @param gap space between components
* @return null for zero visible subcomponent, otherwise an array contains the following lists:
* <ul><li>columnwidths, preferred width of grid columns</li>
* <li>rowheights, preferred heights of grid rows</li>
* <li>columnweights, grid column-width weights</li>
* <li>rowweights, grid row-height weights</li>
* <li>gridx, horizontal location of the subcomponents</li>
* <li>gridy, vertical locations</li>
* <li>gridwidth, column spans</li>
* <li>gridheight, row spans</li></ul>
*/
private int[][] getGrid(Object component, int gap) {
int count = 0; // count of the visible subcomponents
for (Object comp = get(component, ":comp"); comp != null;
comp = get(comp, ":next")) {
if (getBoolean(comp, "visible", true)) { count++; }
}
if (count == 0) { return null; } // zero subcomponent
int columns = getInteger(component, "columns", 0);
int icols = (columns != 0) ? columns : count;
int irows = (columns != 0) ? ((count + columns - 1) / columns) : 1;
int[][] grid = {
new int[icols], new int[irows], // columnwidths, rowheights
new int[icols], new int[irows], // columnweights, rowweights
new int[count], new int[count], // gridx, gridy
new int[count], new int[count] }; // gridwidth, gridheight
int[] columnheight = new int[icols];
int[][] cache = null; // preferredwidth, height, columnweight, rowweight
int i = 0; int x = 0; int y = 0;
int nextsize = 0;
for (Object comp = get(component, ":comp");
comp != null; comp = get(comp, ":next")) {
if (!getBoolean(comp, "visible", true)) { continue; }
int colspan = ((columns != 0) && (columns < count)) ?
Math.min(getInteger(comp, "colspan", 1), columns) : 1;
int rowspan = (columns != 1) ? getInteger(comp, "rowspan", 1) : 1;
for (int j = 0; j < colspan; j++) {
if ((columns != 0) && (x + colspan > columns)) {
x = 0; y++; j = -1;
}
else if (columnheight[x + j] > y) {
x += (j + 1); j = -1;
}
}
if (y + rowspan > grid[1].length) {
int[] rowheights = new int[y + rowspan];
System.arraycopy(grid[1], 0, rowheights, 0, grid[1].length);
grid[1] = rowheights;
int[] rowweights = new int[y + rowspan];
System.arraycopy(grid[3], 0, rowweights, 0, grid[3].length);
grid[3] = rowweights;
}
for (int j = 0; j < colspan; j++) {
columnheight[x + j] = y + rowspan;
}
int weightx = getInteger(comp, "weightx", 0);
int weighty = getInteger(comp, "weighty", 0);
Dimension d = getPreferredSize(comp);
if (colspan == 1) {
grid[0][x] = Math.max(grid[0][x], d.width); // columnwidths
grid[2][x] = Math.max(grid[2][x], weightx); // columnweights
}
else {
if (cache == null) { cache = new int[4][count]; }
cache[0][i] = d.width;
cache[2][i] = weightx;
if ((nextsize == 0) || (colspan < nextsize)) { nextsize = colspan; }
}
if (rowspan == 1) {
grid[1][y] = Math.max(grid[1][y], d.height); // rowheights
grid[3][y] = Math.max(grid[3][y], weighty); // rowweights
}
else {
if (cache == null) { cache = new int[4][count]; }
cache[1][i] = d.height;
cache[3][i] = weighty;
if ((nextsize == 0) || (rowspan < nextsize)) { nextsize = rowspan; }
}
grid[4][i] = x; //gridx
grid[5][i] = y; //gridy
grid[6][i] = colspan; //gridwidth
grid[7][i] = rowspan; //gridheight
x += colspan;
i++;
}
while (nextsize != 0) {
int size = nextsize; nextsize = 0;
for (int j = 0; j < 2; j++) { // horizontal, vertical
for (int k = 0; k < count; k++) {
if (grid[6 + j][k] == size) { // gridwidth, gridheight
int gridpoint = grid[4 + j][k]; // gridx, gridy
int weightdiff = cache[2 + j][k];
for (int m = 0; (weightdiff > 0) && (m < size); m++) {
weightdiff -= grid[2 + j][gridpoint + m];
}
if (weightdiff > 0) {
int weightsum = cache[2 + j][k] - weightdiff;
for (int m = 0; (weightsum > 0) && (m < size); m++) {
int weight = grid[2 + j][gridpoint + m];
if (weight > 0) {
int weightinc = weight * weightdiff / weightsum;
grid[2 + j][gridpoint + m] += weightinc;
weightdiff -= weightinc;
weightsum -= weightinc;
}
}
grid[2 + j][gridpoint + size - 1] += weightdiff;
}
int sizediff = cache[j][k];
int weightsum = 0;
for (int m = 0; (sizediff > 0) && (m < size); m++) {
sizediff -= grid[j][gridpoint + m];
weightsum += grid[2 + j][gridpoint + m];
}
if (sizediff > 0) {
for (int m = 0; (weightsum > 0) && (m < size); m++) {
int weight = grid[2 + j][gridpoint + m];
if (weight > 0) {
int sizeinc = weight * sizediff / weightsum;
grid[j][gridpoint + m] += sizeinc;
sizediff -= sizeinc;
weightsum -= weight;
}
}
grid[j][gridpoint + size - 1] += sizediff;
}
}
else if ((grid[6 + j][k] > size) &&
((nextsize == 0) || (grid[6 + j][k] < nextsize))) {
nextsize = grid[6 + j][k];
}
}
}
}
return grid;
}
/**
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -