GWT openlayers, DrawFeature line style - java

I am using gwt openlayers to draw some linestrings on the map. I would like to change the draw feature line appearance. I noticed that PathHandler class has setStyle method, but setting style using this method does not change the line appearance.
private DrawFeature createDrawFeature() {
DrawFeatureOptions options = new DrawFeatureOptions();
options.onFeatureAdded(getStyle());
PathHandler handler = new PathHandler();
handler.setStyle(style);
return new DrawFeature(layer, handler, options );
}
private Style getStyle() {
Style style = new Style();
style.setStrokeColor("#ffffff");
style.setStrokeWidth(2.0);
return style;
}
I was trying to set different style options but there was no effect.
Does anyone know how to change appearance of DrawFeature line?

The handler that does the drawing (Point, Path, or Polygon) is in charge if the style of your sketches (features before they are completed).
So to style the sketches you do :
//Create a style. We want a blue dashed line.
final Style drawStyle = new Style(); //create a Style to use
drawStyle.setFillColor("white");
drawStyle.setGraphicName("x");
drawStyle.setPointRadius(4);
drawStyle.setStrokeWidth(3);
drawStyle.setStrokeColor("#66FFFF");
drawStyle.setStrokeDashstyle("dash");
//create a StyleMap using the Style
StyleMap drawStyleMap = new StyleMap(drawStyle);
//Create PathHanlderOptions using this StyleMap
PathHandlerOptions phOpt = new PathHandlerOptions();
phOpt.setStyleMap(drawStyleMap);
//Create DrawFeatureOptions and set the PathHandlerOptions (that have the StyleMap, that have the Style we wish)
DrawFeatureOptions drawFeatureOptions = new DrawFeatureOptions();
drawFeatureOptions.setHandlerOptions(phOpt);
PathHandler pathHanlder = new PathHandler();
// Create the DrawFeature control to draw on the map, and pass the DrawFeatureOptions to control the style of the sketch
final DrawFeature drawLine = new DrawFeature(vectorLayer, pathHanlder, drawFeatureOptions);
map.addControl(drawLine);
drawLine.activate();
I also added an example to she showcase : http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=DrawFeature%20style%20example

Related

Text not getting center aligned horizontally in itext? [duplicate]

I have a C# application that generates a PDF invoice. In this invoice is a table of items and prices. This is generated using a PdfPTable and PdfPCells.
I want to be able to right-align the price column but I cannot seem to be able to - the text always comes out left-aligned in the cell.
Here is my code for creating the table:
PdfPTable table = new PdfPTable(2);
table.TotalWidth = invoice.PageSize.Width;
float[] widths = { invoice.PageSize.Width - 70f, 70f };
table.SetWidths(widths);
table.AddCell(new Phrase("Item Name", tableHeadFont));
table.AddCell(new Phrase("Price", tableHeadFont));
SqlCommand cmdItems = new SqlCommand("SELECT...", con);
using (SqlDataReader rdrItems = cmdItems.ExecuteReader())
{
while (rdrItems.Read())
{
table.AddCell(new Phrase(rdrItems["itemName"].ToString(), tableFont));
double price = Convert.ToDouble(rdrItems["price"]);
PdfPCell pcell = new PdfPCell();
pcell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
pcell.AddElement(new Phrase(price.ToString("0.00"), tableFont));
table.AddCell(pcell);
}
}
Can anyone help?
I'm the original developer of iText, and the problem you're experiencing is explained in my book.
You're mixing text mode and composite mode.
In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.
How to solve your problem?
Either work in text mode by adding your Phrase to the cell in a different way.
Or work in composite mode and use a Paragraph for which you define the alignment.
The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
// cell.VerticalAlignment = PdfCell.ALIGN_TOP;
//cell.VerticalAlignment = align;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
Here is my derivation of user2660112's answer - one method to return a cell for insertion into a bordered and background-colored table, and a similar, but borderless/colorless variety:
private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BackgroundColor = color;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
private static PdfPCell GetCellForBorderlessTable(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
cell.BorderWidth = PdfPCell.NO_BORDER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
return cell;
}
These can then be called like so:
Font timesRoman9Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLACK);
Font timesRoman9BoldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 9, BaseColor.BLACK);
Phrase phrasesec1Heading = new Phrase("Duckbills Unlimited", timesRoman9BoldFont);
PdfPCell cellSec1Heading = GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, BaseColor.YELLOW);
tblHeadings.AddCell(cellSec1Heading);
Phrase phrasePoisonToe = new Phrase("Poison Toe Toxicity Level (Metric Richter Scale, adjusted for follicle hue)", timesRoman9Font);
PdfPCell cellPoisonToe = GetCellForBorderlessTable(phrasePoisonToe, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellPoisonToe);
I ended up here searching for java Right aligning text in PdfPCell. So no offense if you are using java please use given snippet to achieve right alignment.
private PdfPCell getParagraphWithRightAlignCell(Paragraph paragraph) {
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorderColor( BaseColor.WHITE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
return cell;
}
In getParagraphWithRightAlignCell pass paragraph
Thanks
Perhaps its because you are mixing the different ways to add the cells? Have you tried explicitly creating a cell object, massaging it however you want then adding it for every cell?
Another thing you could try is setting the vertical alignment as well as the horizontal.
cell.HorizontalAlignment = Element.ALIGN_RIGHT;

Set cell background color and text size using Google Java API

I want to set Spreadsheet cell background color and text size. I use this Java code to set the text into the cell but I can't find a solution how to set the style.
CellData setUserEnteredValue = new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("cell text"));
Is there any solution?
I had to go through allot of useless answers, but this worked for me. Here you go:
requests.add(new Request()
.setRepeatCell(new RepeatCellRequest()
.setCell(new CellData()
.setUserEnteredValue( new ExtendedValue().setStringValue("cell text"))
.setUserEnteredFormat(new CellFormat()
.setBackgroundColor(new Color()
.setRed(Float.valueOf("1"))
.setGreen(Float.valueOf("0"))
.setBlue(Float.valueOf("0"))
)
.setTextFormat(new TextFormat()
.setFontSize(15)
.setBold(Boolean.TRUE)
)
)
)
.setRange(new GridRange()
.setSheetId(sheetID)
.setStartRowIndex(1)
.setEndRowIndex(0)
.setStartColumnIndex(0)
.setEndColumnIndex(1)
)
.setFields("*")
)
);
AFAIK this is not possible in the Java Spreadsheet API, you instead have to use the Apps Script;
https://developers.google.com/apps-script/reference/spreadsheet/
From their documentation, how to set the background;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D5");
range.setBackground("red");
and how to set the font size;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("B2");
cell.setFontSize(20);
Update
See flo5783's answer below, v4 now offers the ability to do this.
Looks like there is a class designed exactly for this:
CellFormat
In particular these following methods:
public CellFormat setBackgroundColor(ColorbackgroundColor)
and
public CellFormat setTextFormat(TextFormattextFormat)
I haven't coded in Java in ages so I won't try to give you a working code example, but I think you'll be able to figure it out easily from this.
EDIT: Here's a basic example starting from your code:
CellData setUserEnteredValue = new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("cell text"));
CellFormat myFormat = new CellFormat();
myFormat.setBackgroundColor(new Color().setRed(1)); // red background
myFormat.setTextFormat(new TextFormat().setFontSize(16)); // 16pt font
setUserEnteredValue.setUserEnteredFormat(myFormat);
// For handling single cells this is how you do it, where j and k are row and columns in g sheet .
CellData setUserEnteredValue =new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue(maxs));
List<CellData> values1.add(setUserEnteredValue);
requests.add(new Request()
.setUpdateCells(new UpdateCellsRequest()
.setStart(new GridCoordinate()
.setRowIndex(j)
.setColumnIndex(k))
.setRows(Arrays.asList(
new RowData().setValues(values1)))
.setFields("userEnteredValue,userEnteredFormat.backgroundColor")));
CellFormat myFormat = new CellFormat();
myFormat.setBackgroundColor(new Color().setRed(Float.valueOf("1"))); // red background
setUserEnteredValue.setUserEnteredFormat(myFormat);
You can't change the background color or the font size on a CellData object. You instead need to iterate over some cell ranges. From there, you can set the background color based on the cell value. You can then make your code dependable, like a 2-step process.
From another answer on SO:
//Sets the row color depending on the value in the "Status" column.
function setRowColors() {
var range = SpreadsheetApp.getActiveSheet().getDataRange();
var statusColumnOffset = getStatusColumnOffset();
for (var i = range.getRow(); i < range.getLastRow(); i++) {
rowRange = range.offset(i, 0, 1); //Play with this range to get your desired columns.
status = rowRange.offset(0, statusColumnOffset).getValue(); //The text value we need to evaluate.
if (status == 'Completed') {
rowRange.setBackgroundColor("#99CC99");
} else if (status == 'In Progress') {
rowRange.setBackgroundColor("#FFDD88");
} else if (status == 'Not Started') {
rowRange.setBackgroundColor("#CC6666");
}
}
}

JTextPane - HTMLDocument: when adding/removing a new style, other attributes also changes

I have a JTextPane (or JEditorPane) in which I want to add some buttons to format text (as shown in the picture).
When I change the selected text to Bold (making a new Style), the font family (and others attributes) also changes. Why? I want to set (or remove) the bold attribute in the selected text and other stays unchanged, as they were.
This is what I'm trying:
private void setBold(boolean flag){
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
int start = editorPane.getSelectionStart();
int end = editorPane.getSelectedText().length();
StyleContext ss = doc.getStyleSheet();
//check if BoldStyle exists and then add / remove it
Style style = ss.getStyle("BoldStyle");
if(style == null){
style = ss.addStyle("BoldStyle", null);
style.addAttribute(StyleConstants.Bold, true);
} else {
style.addAttribute(StyleConstants.Bold, false);
ss.removeStyle("BoldStyle");
}
doc.setCharacterAttributes(start, end, style, true);
}
But as I explained above, other attributes also change:
Any help will be appreciated. Thanks in advance!
http://oi40.tinypic.com/riuec9.jpg
What you are trying to do can be accomplished with one of the following two lines of code:
new StyledEditorKit.BoldAction().actionPerformed(null);
or
editorPane.getActionMap().get("font-bold").actionPerformed(null);
... where editorPane is an instance of JEditorPane of course.
Both will seamlessly take care of any attributes already defined and supports text selection.
Regarding your code, it does not work with previously styled text because you are overwriting the corresponding attributes with nothing. I mean, you never gather the values for the attributes already set for the current selected text using, say, the getAttributes() method. So, you are effectively resetting them to whatever default the global stylesheet specifies.
The good news is you don't need to worry about all this if you use one of the snippets above. Hope that helps.
I made some minor modifications to your code and it worked here:
private void setBold(boolean flag){
HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
int start = editorPane.getSelectionStart();
int end = editorPane.getSelectionEnd();
if (start == end) {
return;
}
if (start > end) {
int life = start;
start = end;
end = life;
}
StyleContext ss = doc.getStyleSheet();
//check if BoldStyle exists and then add / remove it
Style style = ss.getStyle(editorPane.getSelectedText());
if(style == null){
style = ss.addStyle(editorPane.getSelectedText(), null);
style.addAttribute(StyleConstants.Bold, true);
} else {
style.addAttribute(StyleConstants.Bold, false);
ss.removeStyle(editorPane.getSelectedText());
}
doc.setCharacterAttributes(start, end - start, style, true);
}

add GWT-Highchart to Tab in TabSet

This is my code :
HighChart chart = new HighChart(title, PIE, data);
VLayout vlayout = new VLayout(title);
vlayout.setHeight100();
vlayout.setWidth100();
vlayout.addMember(chart);
Tab tab = new Tab();
tab.setTitle(title);
tab.setPane(vlayout);
tab.setCanClose(true);
tabset.addTab(tab);
The HighChart class contain the showcase example code.
The result is an empty tab, any solutions?
I solved the problem, the ​​RootLayoutPanel who did not accept the chart display, a simple replacement by a layout.draw(); did the trick

Changing the background color of a paragraph in JTextPane (Java Swing)

Is it possible to change the background color of a paragraph in Java Swing? I tried to set it using the setParagraphAttributes method (code below) but doesn't seem to work.
StyledDocument doc = textPanel.getStyledDocument();
Style style = textPanel.addStyle("Hightlight background", null);
StyleConstants.setBackground(style, Color.red);
Style logicalStyle = textPanel.getLogicalStyle();
doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle("Hightlight background"), true);
textPanel.setLogicalStyle(logicalStyle);
UPDATE:
I just found out about a class called Highlighter.I dont think you should be using the setbackground style. Use the DefaultHighlighter class instead.
Highlighter h = textPanel.getHighlighter();
h.addHighlight(1, 10, new DefaultHighlighter.DefaultHighlightPainter(
Color.red));
The first two parameters of the addHighlight method are nothing but the starting index and ending index of the text you want to highlight. You can call this method multiple timesto highlight discontinuous lines of text.
OLD ANSWER:
I have no idea why the setParagraphAttributes method doesnt seem to work. But doing this seems to work.
doc.insertString(0, "Hello World", textPanel.getStyle("Hightlight background"));
Maybe you can work a hack around this for now...
I use:
SimpleAttributeSet background = new SimpleAttributeSet();
StyleConstants.setBackground(background, Color.RED);
Then you can change existing attributes using:
doc.setParagraphAttributes(0, doc.getLength(), background, false);
Or add attributes with text:
doc.insertString(doc.getLength(), "\nEnd of text", background );
Easy way to change the background color of selected text or paragraph.
//choose color from JColorchooser
Color color = colorChooser.getColor();
//starting position of selected Text
int start = textPane.getSelectedStart();
// end position of the selected Text
int end = textPane.getSelectionEnd();
// style document of text pane where we change the background of the text
StyledDocument style = textPane.getStyledDocument();
// this old attribute set of selected Text;
AttributeSet oldSet = style.getCharacterElement(end-1).getAttributes();
// style context for creating new attribute set.
StyleContext sc = StyleContext.getDefaultStyleContext();
// new attribute set with new background color
AttributeSet s = sc.addAttribute(oldSet, StyleConstants.Background, color);
// set the Attribute set in the selected text
style.setCharacterAttributes(start, end- start, s, true);

Categories