BlackBerry: setting column width and row height for GridFieldManager - blackberry

I'm having trouble wrapping my head around this problem, because it actually works when the GridFieldManager is instantiated in the constructor of the MainScreen. If I load it after my json callback, or choose another month to load the gridfield again, I get the columns and rows being larger that I had set it.
theCalendar = new GridFieldManager(rows,7,GridFieldManager.FIXED_SIZE);
int column_width = Display.getWidth() / 7;
for(int y=0;y<7;y++){
theCalendar.setColumnProperty(y, GridFieldManager.FIXED_SIZE, column_width);
System.out.println("column width:"+theCalendar.getColumnWidth(y));
}
for(int o=0;o<rows;o++){
theCalendar.setRowProperty(o, GridFieldManager.FIXED_SIZE, 40);
}
Like I said, if I fire this code in something other than the constructor, the rows and columns are larger than what I had set it as. I even checked getColumnWidth() and it comes up as 68, which is right ( 480 / 7 ). Is there something wrong with my labelfields that I'm adding to it? I feel like it is a bug.

I had a similar problem with the width of columns. I could solve it using GridFieldManager.PREFERRED_SIZE_WITH_MAXIMUM instead of GridFieldManager.FIXED_SIZE.

Related

Is there a way to have google sheets read imperial height as a number?

I'm making a sheet with details about a bunch of fictional characters, and one column I want to have is their height. I would also really like to use Conditional Formatting with a Color Scale to color-code the tallest and shortest characters, and everything in between.
Unfortunately, I live in the US, and am used to height expressed in feet and inches (e.g. 5'10''), which Google Sheets of course does not recognize as a number. Is there any way to remedy this, besides writing everything in terms of just inches (e.g. 60), such that I could apply conditional formatting directly to the column?
I've tried different formats (e.g. 5'10), and I considered having a hidden column with just the inch value and have conditional formatting work off of that row (doesn't work with Color Scale as far as I can tell, since you can't input a custom formula). One thought I had is somehow formatting things as an improper fraction with a denominator of 12, but hiding the denominator? But I have no idea how that would work. I've Googled as best I can, but I haven't found anything (everything's just about changing row height, which makes sense in hindsight).
I understand that you have two goals in mind. First of all, you should decide which unit length to use for managing heights. I have chosen inches, but you could work with feet if you need. This will simplify the scenario and will allow you to work easily with the data, but you could always create a function that translates inches to the foot/inches combo in order to show the data to a third party. This is the example table that I will use:
And this is my code, I will explain it at the bottom:
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
data = sortTable(data);
sheet.getDataRange().setValues(data);
for (var i = 1; i < data.length; i++) {
data[i][2] = gradient(data.length, i);
}
for (var i = 1; i < data.length; i++) {
sheet.getRange(i, 2).setBackground("#" + data[i][2][0] + data[i][2][1] +
data[i][2][2]);
}
}
function sortTable(data) {
data.sort(function(a, b) {
return b[1] - a[1];
})
return data;
}
function gradient(arraySize, position) {
var relativePosition = position / arraySize;
var topColor = [parseInt("00", 16), parseInt("7A", 16), parseInt("33",
16)]; // Green
var bottomColor = [parseInt("FF", 16), parseInt("FF", 16), parseInt("FF",
16)]; // White
var positionColor = [0, 0, 0];
for (var i = 0; i < 3; i++) {
positionColor[i] = Math.floor(topColor[i] * (1 - relativePosition) +
bottomColor[i] * relativePosition).toString(16);
}
return positionColor;
}
First of all you have to read the data with a combination of getValues()/setValues(), and once you do that you can sort the table based on height so you can create the gradient later. Please notice how I separated the sorting function for better clarity.
After that you need the gradient color for setBackground(). To do so I developed a simple linear gradient function that calculates the RGB code from the top to the bottom. In my example the gradient fades from green to white, but you can change it. I also separated the gradient script into its own function. At this point you already have the sorted table and its gradient colors, so you only have to use setValues() and you are done. Feel free to leave any comment if you have doubts about this approach. This would be the final result:
UPDATE
Based in your comments I get that you need an imperial height format. For that case, you could use =INT(B2)&"' "&TRIM(TEXT(ROUND(MOD(B2,1)*12*16,0)/16,"# ??/??")&"""") (assuming that B2 contains the height). This approach will use Sheets Formulas to calculate the remainder part of the height, and its expression as an irreducible fraction. This is the final result:

iOS: Tapku Calendar: Need to select multiple dates

I've been checking the Tapku Calendar code for a bit and searched and read all the relevant questions and responses here however none seem to really offer the correct solution to the problem: How to select multiple dates, either programmatically or by tapping. Just a simple blue tile over two adjacent dates would make me happy :-) The post below seems to have a similar question however the answer does not work. The place in the code is not hit unless the month changes - not exactly what I am looking for. What would be great is a higher-level implementation of selectDate: that would select multiple dates. But just the right place to tweak in the library would be a great place to start is anyone is more familiar with the code. Much appreciated.
iOS: Tapku calendar library - allow selecting multiple dates for current month
So after a bit of stepping through code, I have this rudimentary method using a hammer. I adopted most of the code from TKCalendarMonthView.m->selectDay:day method. The method I created basically creates a new TKCalendarMonthTiles object and fills in the details and then adds subviews onto the main TKCalendarMonthTiles object (self). I tag the subviews so I can first get rid of them if they exist at the beginning of the method as I only want to select one additional day (you could leave the subviews attached if you want them to remain in the UI). I don't track the dates or store them or anything however this meets my needs.
The idea is to simply create a view with the correct tile image you want to use and one that contains the text label of the actual "date" like "14" then add those views as subviews to self. The borrowed code does all the calculations for "where" that date tile resides in the grid, so the view is drawn at the correct location. Code:
- (void)markDay:(int)day {
// First, remove any old subviews
[[self viewWithTag:42] removeFromSuperview];
[[self viewWithTag:43] removeFromSuperview];
int pre = firstOfPrev < 0 ? 0 : lastOfPrev - firstOfPrev + 1;
int tot = day + pre;
int row = tot / 7;
int column = (tot % 7)-1;
TKCalendarMonthTiles *deliveryTile = [[TKCalendarMonthTiles alloc] init];
deliveryTile.selectedImageView.image = [UIImage imageWithContentsOfFile:TKBUNDLE(#"TapkuLibrary.bundle/Images/calendar/MyDateTile.png")];
deliveryTile.currentDay.text = [NSString stringWithFormat:#"%d",day];
if(column < 0){
column = 6;
row--;
}
CGRect r = deliveryTile.selectedImageView.frame;
r.origin.x = (column*46);
r.origin.y = (row*44)-1;
deliveryTile.selectedImageView.frame = r;
deliveryTile.currentDay.frame = r;
[[deliveryTile selectedImageView] setTag:42];
[[deliveryTile currentDay] setTag:43];
[self addSubview:deliveryTile.selectedImageView];
[self addSubview:deliveryTile.currentDay];
} // markDay:
I call this method at the end of TKCalendarMonthView.m->selectDay:day as well as at the end of TKCalendarMonthView.m->-reactToTouch:down. Limited testing so far so good. Off to figure out why the timezone setting keeps thinking its tomorrow (I am in Pacific time zone).
Cheers, Michael

Putting several images next to each other in a pdfcell with itextsharp

I'm using itextsharp with a aspmvc project to create PDF versions of some of my pages. I have a very basic parser which takes simple html (plus some style info supplied seperately) and creates a pdf. When my parser encounts a table it loops over rows then cells, creating a PdfPCell for each cell. It then loops over child elements of the cell adding them to the PdfPCell. It's pretty basic but it's worked for me so far.
The problem is that I now have a table one column of which contains a number of icons, indicating different status for the row. When these images get added then end up one above the other in the pdf, instead of next to each other.
I create the image with
Dim I As iTextSharp.text.Image = Image.GetInstance(HttpContext.Current.Server.MapPath(El.Attributes("src").InnerText))
I have tried
I.Alignment = Image.TEXTWRAP Or Image.ALIGN_LEFT Or Image.ALIGN_MIDDLE
and adding a text chunk afterwards containing a space but it doesn't help. The only suggestion I have seen is to use I.SetAbsolutePosition(). I'd rather avoid absolute position but am prepared to try it - except I can't work out how to find the current X position to use?
Any help much appreciated.
Adam
To get the proper side-by-side flow, wrap the images/text in a Paragraph object, adding them one by one using Chunk and Phrase objects. Something (sorry, I don't do VB) like this:
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
p.Add(new Phrase("Test "));
p.Add(new Chunk(image, 0, 0));
p.Add(new Phrase(" more text "));
p.Add(new Chunk(image, 0, 0));
p.Add(new Chunk(image, 0, 0));
p.Add(new Phrase(" end."));
cell.AddElement(p);
table.AddCell(cell);
table.AddCell(new PdfPCell(new Phrase("test 2")));
document.Add(table);
EDIT: One way to add whitespace between the images. Will only work with images; if you try this with mixed text/image(s), they will overlap:
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
float offset = 20;
for (int i = 0; i < 4; ++i) {
p.Add(new Chunk(image, offset * i, 0));
}
cell.AddElement(p);
table.AddCell(cell);
table.AddCell(new PdfPCell(new Phrase("cell 2")));
document.Add(table);
See the Chunk documentation.

how to calculate Labelfield and HorizontalFieldmanager height in blackberry

I have HorizontalFieldManager,VerticalFieldManager and LabelField. LabelField placed in HorizontalFieldManager and multiple HorizontalFieldManager are placed in VerticalFieldManager.
When i try to get LabelField height using labelfield.getHeight(); it returns 0 . if there are multiple line in Labelfield, it also give me height returns 0. same issue i m facing for HorizontalFieldManager .
After getting there height i want to calculate VerticalFieldManager height and set height dynamically for the screen.
How can i calculate the height of Label or Horizontalfieldmanager?
Use labelField.getPreferredHeight() and manager.getPreferredHeight() not labelField.getHeight()
This should work for you
The method Farid suggests it's a bit difficult to use, because you will need labelWidth.
For multiline label this labelWidth may not be the same as parent managers available width, or some exact width you had set, because each line can have different width depending on if words did fit maxWidth or didn't.
NOTE: as Nate pointed out in comments, it's a good idea to also add advance for spaces.
I modified the code to include that.
Here is the method I use to overcome these problems:
public int getPreferredHeight() {
String text = getText();
int maxWidth = getManager().getPreferredWidth();
int spaceAdvance = getFont().getAdvance(' ');
String[] words = StringUtilities.stringToWords(text);
int lastWordAdvance = 0;
int lines = 1;
for (int i = 0; i < words.length; i++) {
int wordAdvance = getFont().getAdvance(words[i]) + spaceAdvance;
if (lastWordAdvance + wordAdvance < maxWidth ) {
lastWordAdvance += wordAdvance;
} else {
lines++;
lastWordAdvance = wordAdvance;
}
}
return (int)lines * getFont().getHeight();
}
If you can leave the decision about the size of the LabelField until after it has been laid out, then you will get it accurately from getHeight(). This means you can actually get the correct result, including factoring in any margin or padding for the LabelField, that I think
int maxWidth = getManager().getPreferredWidth();
will miss.
Initially this seems quite difficult, because typically it is good to know the height when you add the Field to the screen. But the general principle is that you should do this in the Manager's sublayout, so you are just moving the code that is dependent on the height, a bit later in the process. And this has the benefit that the layout is dynamic, so if the LabelField's text is changed, then layout will be invoked and your code that is dependent on the height gets re-invoked too.
It is also possible to use logic like this in sublayout():
super.sublayout(...);
if (myField.getHeight() < 100 ) {
myField.setMargin((100 - myField.getHeight())/2, 0, (100 - myField.getHeight())/2, 0);
super.sublayout(...);
}
This is not a production suitable example, hard coding a pixel height is not recommended. It is just an easy example to understand....

Large SWT Image is not printed properly

Hei, SWT Gurus, I have a pretty weird situation. The thing is, that I am trying to print gantt chart in my Eclipse RCP application. Gantt chart is quite long and sometimes high as well. Its dimensions are following: height=3008px (2 vertical pages), width > 20000px. My print area can fit something like 1400x2000 px. First of all, I am creating image out with my chart (image is OK, I can save it separately and see, that everything is there). However, in order to print it on the paper, I am printing it piece by piece (moving source X and Y positions respectively). This algorithm was working fine for some time, but now something strange happened:
When chart is not high enough and can fit on 1 vertical page, then it is printed normally, but when it is 2 vertical pages, then only second vertical page is printed and first one is left out. There are no errors, nor anything, that could help me. I thought, may be there is not enough heap memory, so I allocated -Xmx1014m space to my application, but it didn't helped. So, I am really lost, and can not find any solution or even an explanation to this problem. I was trying to simply print image by gc.drwImage(image, x, y), but is also printed me only the second half of it. I am also printing some text after every try of printing an image, and it is printed
The code, that is responsible for printing an image is following:
for (int verticalPageNumber = 0; verticalPageNumber <= pageCount.vGanttChartPagesCount; verticalPageNumber++) {
// horizontal position needs to be reset to 0 before printing next bunch of horizontal pages
int imgPieceSrcX = 0;
for (int horizontalPageNumber = 0; horizontalPageNumber <= pageCount.hGanttChartPagesCount; horizontalPageNumber++) {
// Calculate bounds for the next page
final Rectangle printBounds = PrintingUtils.calculatePrintBounds(printerClientArea, scaleFactor, verticalPageNumber, horizontalPageNumber);
if (shouldPrint(printer.getPrinterData(), currentPageNr)
&& nextPageHasSomethingToPrint(imgPieceSrcX, imgPieceSrcY, totalGanttChartArea.width, totalGanttChartArea.height)) {
printer.startPage();
final Transform printerTransform = PrintingUtils.setUpTransform(printer, printerClientArea, scaleFactor, gc, printBounds);
printHeader(gc, currentPageNr, printBounds);
imgPieceSrcY = printBounds.y;
final int imgPieceSrcHeight =
imgPieceSrcY + printBounds.height < ganttChartImage.getBounds().height ? printBounds.height : ganttChartImage.getBounds().height
- imgPieceSrcY;
final int imgPieceSrcWidth =
imgPieceSrcX + printBounds.width < ganttChartImage.getBounds().width ? printBounds.width : ganttChartImage.getBounds().width
- imgPieceSrcX;
if (imgPieceSrcHeight > 0 && imgPieceSrcWidth > 0) {
// gantt chart is printed as image, piece by piece
gc.drawImage(ganttChartImage,
imgPieceSrcX, imgPieceSrcY,
imgPieceSrcWidth, imgPieceSrcHeight,
printBounds.x, printBounds.y,
imgPieceSrcWidth, imgPieceSrcHeight); // destination width and height equal to source width and height to prevent
// stretching/shrinking of image
// move x and y to print next image piece
gc.drawText("Text " + currentPageNr, imgPieceSrcX, imgPieceSrcY);
imgPieceSrcX += printBounds.width;
}
currentPageNr++;
printer.endPage();
printerTransform.dispose();
}
}
Thanks in advance.

Resources