Fitting Vaadin grid into the screen - vaadin-grid

I am displaying books information in the Vaadin grid. It is displayed almost as I wanted it, but still having problem that I don't know how to solve. Therefore I would appreciate advice(s). Here is how I am creating this grid:
private void createBooksTable() {
this.booksTable = new Grid<>();
this.booksTable.setWidth("100%");
this.yearRenderer = new ActionYearRenderer();
this.booksTable.addColumn(this.yearRenderer).setWidth("8%");
this.booksTable.addColumn(new NewTagRenderer()).setWidth("3%");
this.booksTable.addColumn(new AuthorListRenderer()).setWidth("18%");
this.booksTable.addColumn(new TitleRenderer()).setWidth("50%");
this.booksTable.addColumn(new RatingRenderer()).setWidth("4%");
this.booksTable.addColumn(new ReaderRatingRenderer()).setWidth("3%");
this.booksTable.addColumn(new StatusRenderer()).setWidth("5%");
this.booksTable.setItemDetailsRenderer(new BookDetailsRenderer());
}
As you can see, the sum of all width is less than 100%, but last column is going outside of the screen and being cut. Maybe it happening because of some default column padding, as I see quite a gap between columns. I didn't find a way to fit all columns into the screen. If you know the way to do it, please tell me.

I would recommend set the widest column to grow the remaining space instead of setting definitive percent width.
this.booksTable.addColumn(new TitleRenderer()).setFlexGrow(1);

Related

Label Offset function in R Mapview do not alter the position of the text

I have been trying to adjust the position of labels in Mapview. However, the functions that I have seen discussed won't work for me. The one that comes closest so far is addStaticLabels.
library(mapview)
mapview(breweries) %>% addStaticLabels(label = breweries$zipcode,
direction = 'left',
offset = c(12,-15),
textsize = "10px")
No matter what offset I give it doesn't change where the text is. This makes it really hard to see the text and the point.

iText: Different table rows with different cell/column widths

How to create with Java a table with three rows as follows:
First row has one cell 100% entire table width
Second row has first from the left cell width 50mm, and second 20mm and third 30mm which in total is 100% of table width
Third row has first from the left cell width 30mm, and second 50mm and third 10mm which in total is 90% of table width
How would the code from the iText look like?
So you want to use iText to create a table that looks like this:
The PDF in the screen shot was created using the TableMeasurements example. The resulting PDF can also be downloaded for inspection: table_measurements.pdf
The first thing that jumps to the eye when looking at this screen shot is the fact that the table doesn't look "complete". This means that we will have to complete the table the way I already explained on SO yesterday (and many times before that): Why the 2nd row of table won't be written? (which was actually a duplicate of How to generate pdf if our column less than the declared table column and ItextSharp, number of Cells not dividable by the length of the row and Odd Numbered Cell Not Added To Pdf and PdfTable: last cell is not visible and ...)
In the comment section, I was asked:
How can I complete row with cells with no border?
I answered:
Use table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
Note that PdfPCell.NO_BORDER also works as PdfPCell extends the Rectangle class.
In your case, we'd have something like this:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(10);
table.setTotalWidth(Utilities.millimetersToPoints(100));
table.setLockedWidth(true);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
table.addCell(getCell(10));
table.addCell(getCell(5));
table.addCell(getCell(3));
table.addCell(getCell(2));
table.addCell(getCell(3));
table.addCell(getCell(5));
table.addCell(getCell(1));
table.completeRow();
document.add(table);
document.close();
}
To make the example more realistic, I created a table with an exact width of 100 mm. For the width to be acknowledged, I lock the width. As already explained, I make sure that default cells have no border. After adding all the cells with the different widths (10 cm, 5 cm, 3 cm, 2 cm, 3 cm, 5 cm, 1 cm), I complete the row.
What does the getCell() method look like, you might wonder. This was already answered by Amedee in the comments (which for some reason you ignored):
private PdfPCell getCell(int cm) {
PdfPCell cell = new PdfPCell();
cell.setColspan(cm);
cell.setUseAscender(true);
cell.setUseDescender(true);
Paragraph p = new Paragraph(
String.format("%smm", 10 * cm),
new Font(Font.FontFamily.HELVETICA, 8));
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
return cell;
}
We create a PdfPCell and we set the colspan to reflect the width in cm. I added some more fancy stuff. I didn't use any functionality in this example that isn't explained on the official web site or on StackOverflow.
For more rowspan and colspan examples, please take a look at the Colspan and rowspan section in the official documentation.

Change height of figures in GEF programmatically

I have a case where I have to add figures in another figure, i.e., nested figures.
My figure which will contain other figure extends org.eclipse.draw2d.Figure.
I want to change the height of this figure when it contains more than 4 figures, so that all the nested figures are visible at once.
So, in the paintFigure(Graphics graphics) method, I do this:
Rectangle r = getBounds();
if( getChildren().size() > 4 ) {
setBounds(new Rectangle(r.x, r.y, r.width, r.height + getChildren().size()-4)*10));
}
But this doesn't change the height. Well it does change it, for the time being, but when it is called again the next time, it again has the previous height, not the updated one.
Hence, visually the figure height doesn't change at all.
Any ideas on what am I doing wrong?
Or to do this in a different and/or better way?
I was updating the height in the paintFigure(), it was changing to previous height because it was taking values from the model. I changed the height in the model, and it worked fine.

Slickgrid: Final column autosize to use all remaining space

I'm using SlickGrid and struggling to find an elegant solution to the following:
All columns must have a specific initial width when first rendered but be resizable afterwards
The final column should auto fill the remaining column space when the window is resized
I've seen:
Make one column fill remaining space in SlickGrid without messing up explicit width columns
resizing of grid when resizing browser window
How do I autosize the column in SlickGrid?
But these don't seem to quite do what I need.
If I use the forceFitColumns option, then all columns will autosize (unless I put a maxsize on them).
Using resizeCanvas on window.resize works well - but it still only works if forceFitColumns is true.
If I set minWidth=maxWidth - then I can't resize the column.
Any suggestions?
I'm not sure it would correct all your problem but in my case I do use the forceFitColumns and then depending how I want my column to react in size I will use a combination of minWidth and width, and in some cases the ones that will never exceed a certain width, I would then use a maxWidth as well. Now the problem you have is when setting the minWidth to be the same with as maxWidth this of course will make it unresizable, well think about it you set a minimum and a maximum, SlickGrid is respecting it by now being able to size it afterwards. I also have my grid which takes 95% width of my screen so I have a little padding on the side and with it I use a auto-resize using jQuery.
Here is my code:
// HTML Grid Container
<div id="myGridContainer" style="width:95%;">
<div class="grid-header" style="width:100%">
<label>ECO: Log / Slickgrid</label>
<span style="float:right" class="ui-icon ui-icon-search" title="Toggle search panel" onclick="toggleFilterRow1()"></span>
</div>
<div id="myGrid" style="width:100%;height:600px;"></div>
<div id="myPager"></div>
</div>
// My SlickGrid Options
var options = {
enableCellNavigation: true,
forceFitColumns: true
};
// The browser auto-resize
$(window).resize(function () {
$("#myGrid").width($("myGridContainer").width());
$(".slick-viewport").width($("#myGrid").width());
grid.resizeCanvas();
});
EDIT
I also was annoyed by the fact that using all of these together is blocking you from resizing the width of the column. I came up with a different solution, much later after, which makes the fields to expand (take available width) and does not block you afterwards on resizing the width. So this new solution I believe is giving you exactly what you are looking for... First of all remove the maxWidth property and only use minWidth and width, actually you could probably use only the width if you wanted. Now I had to unfortunately, modify 1 of the core file slick.grid.js with the following code:
//-- slick.grid.js --//
// on line 69 insert this code
autoExpandColumns: false,
// on line 1614 PREVIOUS CODE
if (options.forceFitColumns) {
autosizeColumns();
}
// on line 1614 change to this NEW CODE
if (options.forceFitColumns || options.autoExpandColumns) {
autosizeColumns();
}
then going back to my grid definition, I replace my previous options with this:
// My NEW SlickGrid Options
var options = {
enableCellNavigation: true,
forceFitColumns: false, // make sure the force fit is false
autoExpandColumns: true // <-- our new property is now usable
};
with this new change it has some functionality of the force fit (expanding) but does not restrict you on resizing your columns width afterwards like the force fit does. I also tested it with the columnPicker, if you hide a column it's resizing the others accordingly. I also modified the file slick.columnpicker.js to include a checkbox for that property but that is totally optional...I can add the code for that too if any of you want it as well. Voila!!! :)
EDIT #2
I realized much later that there's no need to modify the core file, we can simply call grid.autosizeColumns() after the grid creation. Like this
var options = { forceFitColumns: false };
grid = new Slick.Grid("#myGrid", data, columns, options);
// will take available space only on first load
grid.autosizeColumns();
This will automatically resize the columns to fit the screen on first load but will not give you the restriction of the forceFitcolumns flag.
I know it's kind late for this reply.
But i've managed to do that without having to change things at slick.grid.js or set min/maxWidth at columns array.
Instead what i did was to iterate through the columns array adding the values of "width" field of each column and then i've did a simple math count to set the last column width as innerWidth - totalColumsWidth + lastColumnWidth.
Code:
function lastColumnWidth(columns)
{
var widthSum = 0;
angular.forEach(columns, function(col) {
if(col.width) { widthSum = col.width + widthSum; }
});
if(window.innerWidth > widthSum) {
columns[columns.length-1].width = columns[columns.length-1].width + (window.innerWidth - widthSum);
}
return columns;
}

Evenly distribute items on the screen

I am trying to solve this little puzzle (the algorithm): I have N image icons and I want to distribute them evenly on users screen. Say, I put them in a table. If there is one image, there will be one cell in a table. If two - one row with two columns, if three - one row and three columns, if four - two rows, two columns... and so on until row space is gone and since then the table should only grow in columns without adding extra rows.
I'm trying to figure an algorithm for this and perhaps this is something that has a solution already somewhere?
My attempt is so far something like this:
obtain_max_rows()
obtain_visible_columns()
if (number_of_pictures > max_rows*max_columns)
{
columns = roundup(number_of_pictures/max_rows)
for(max_rows){generate row;for columns{generate column}}
}
else
{
**here comes to trouble...**
}
This logic is bit silly though - it somehow needs to think cases where there are 12 pictures on first screen and 2 on the other trying to balance it say 8/6 or somehow like that.
I think I found your answer:
typedef struct{int width, height;} rectangle;
...
rectangle findOptimalDivision(int numberOfCuts){
int x = numberOfCuts, y = (int)sqrt(numberOfCuts);
while (x%y) { // when y = 1 it will always exit out (anything can be divided by 1)
y--;
}
return (rectangle){y,numberOfCuts/y};
}
This should give you the height and width of the table (or rectangle) in terms of cells.
I tested this with 1 - 100 as the parameter and it seems to give the proper outputs.
Message me if you have any problems.

Resources