Syntax Coloring for a custom list of words in Eclipse editor - eclipse

We are using a custom editor for a specific kind of files. Now the problem here is, i am new to this eclipse related stuff. I have to highlight list of words with a specific colour. I checked online found some rules but i dont have basic idea how to use them. It would be great if someone help me how to add a specific colour for given list of words in custom editor.
Thanks in advance.

You can use this code put it in your reconciler class : `
private final TextAttribute wordAttribute = new TextAttribute(new Color(Display.getCurrent(), new RGB(0, 0, 255));
public static final String[] keywords = { "define your keywords here" };
....................................................................
RuleBasedScanner scanner = new RuleBasedScanner();
IRule[] rules = new IRule[5]; //set as many rules as you want just change the number in the bracket
....................................................................
WordRule rule = new WordRule(new IWordDetector() {//the IWordDetector class is an anonymous class so you'll have to define and override this two functions below as you want
#Override
public boolean isWordStart(char c) {
// TODO Auto-generated method stub
return Character.isJavaIdentifierStart(c);
}
#Override
public boolean isWordPart(char c) {
// TODO Auto-generated method stub
return Character.isJavaIdentifierStart(c);
}
});
for (int i = 0; i < keywords.length; i++) {
rule.addWord(keywords[i], new Token(wordAttribute));
}
rules[0] = rule;
scanner.setRules(rules);
}`
I think it should color your words as it does to me.

Related

Proper way to implement custom Css attribute with Itext and html2Pdf

I'm using Itext 7 and their html2Pdf lib.
Is there a way to implement for example cmyk colors.
.wootWorkingCMYK-color{
color: cmyk( 1 , 0.69 , 0.08 , 0.54);
}
I know the itext core part pretty good, looking for away to use the html2Pdf side. I'm aware of the CssApplierFactory but this seems to be to far up the chain.
Well, of course there is a way of processing custom CSS properties like cmyk colors, but unfortunately the code would be quite bulky and you will need to write quite some code for different cases. I will show how to apply custom color for font, but e.g. for backgrounds, borders or other cases you will need to write separate code in a similar way. Reason behind it is that iText layout structure, although designed with HTML/CSS in mind, is not 100% similar and has some differences you have to code around.
Having that said, if you can fork, build and use your custom version from sources, this is the way I would advice to go. Although it has drawbacks like having to rebase to get updates, the solution would be simpler and more generic. To do that, search for usages of CssUtils.parseRgbaColor in pdfHTML module, and you will find that it is used in BackgroundApplierUtil, BorderStyleApplierUtil, FontStyleApplierUtil, OutlineApplierUtil. There you will find code like
if (!CssConstants.TRANSPARENT.equals(cssColorPropValue)) {
float[] rgbaColor = CssUtils.parseRgbaColor(cssColorPropValue);
Color color = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
float opacity = rgbaColor[3];
transparentColor = new TransparentColor(color, opacity);
} else {
transparentColor = new TransparentColor(ColorConstants.BLACK, 0f);
}
Which I belive you can tweak to process cmyk as well, knowing that you know core part pretty well.
Now, the solution without custom pdfHTML version is to indeed start with implementing ICssApplierFactory, or subclassing default implementation DefaultCssApplierFactory. We are mostly interested in customizing implementation of SpanTagCssApplier and BlockCssApplier, but you can consult with DefaultTagCssApplierMapping to get the full list of appliers and cases they are used in, so that you can decide which of them you want to process in your code.
I will show you how to add support for custom color space for font color in the two main applier classes I mentioned and you can work from there.
private static class CustomCssApplierFactory implements ICssApplierFactory {
private static final ICssApplierFactory DEFAULT_FACTORY = new DefaultCssApplierFactory();
#Override
public ICssApplier getCssApplier(IElementNode tag) {
ICssApplier defaultApplier = DEFAULT_FACTORY.getCssApplier(tag);
if (defaultApplier instanceof SpanTagCssApplier) {
return new CustomSpanTagCssApplier();
} else if (defaultApplier instanceof BlockCssApplier) {
return new CustomBlockCssApplier();
} else {
return defaultApplier;
}
}
}
private static class CustomSpanTagCssApplier extends SpanTagCssApplier {
#Override
protected void applyChildElementStyles(IPropertyContainer element, Map<String, String> css, ProcessorContext context, IStylesContainer stylesContainer) {
super.applyChildElementStyles(element, css, context, stylesContainer);
String color = css.get("color2");
if (color != null) {
color = color.trim();
if (color.startsWith("cmyk")) {
element.setProperty(Property.FONT_COLOR, new TransparentColor(parseCmykColor(color)));
}
}
}
}
private static class CustomBlockCssApplier extends BlockCssApplier {
#Override
public void apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker) {
super.apply(context, stylesContainer, tagWorker);
IPropertyContainer container = tagWorker.getElementResult();
if (container != null) {
String color = stylesContainer.getStyles().get("color2");
if (color != null) {
color = color.trim();
if (color.startsWith("cmyk")) {
container.setProperty(Property.FONT_COLOR, new TransparentColor(parseCmykColor(color)));
}
}
}
}
}
// You might want a safer implementation with better handling of corner cases
private static DeviceCmyk parseCmykColor(String color) {
final String delim = "cmyk(), \t\r\n\f";
StringTokenizer tok = new StringTokenizer(color, delim);
float[] res = new float[]{0, 0, 0, 0};
for (int k = 0; k < 3; ++k) {
if (tok.hasMoreTokens()) {
res[k] = Float.parseFloat(tok.nextToken());
}
}
return new DeviceCmyk(res[0], res[1], res[2], res[3]);
}
Having that custom code, you should configure the ConverterProperties accordingly and pass it to HtmlConverter:
ConverterProperties properties = new ConverterProperties();
properties.setCssApplierFactory(new CustomCssApplierFactory());
HtmlConverter.convertToPdf(..., properties);
You might have noticed that I used color2 instead of color, and this is for a reason. pdfHTML has a mechanism of CSS property validation (as browsers do as well), to discard invalid CSS properties when calculating effective properties for an element. Unfortunately, there is no mechanism of customizing this validation logic currently and of course it treats cmyk colors as invalid declarations at the moment. Thus, if you really want to have custom color property, you will have to preprocess your HTML and replace declarations like color: cmyk... to color2: cmyk.. or whatever the property name you might want to use.
As I mentioned at the start of the answer, my recommendation is to build your own custom version :)

Dynamically add widgets in a cell to represent "tags" in Datagrid

In a GWT web app, I am using a DataGrid to manage elements from a database. I represent a list of elements as rows, the columns being editable fields of their characteristics (id, name, description). I am mostly using the EditTextCell class.
I now want to create a custom cell, for a column that has to represent a list of "tags" that can be attached to every element. From this cell, tags could be added, using a + button (that makes a drop-down menu appear or something), and deleted. Each tag should be a kind of button, or interactive widget (I later want to display pop-up with info, trigger actions, etc).
Actually, it would not be so different from the "tags" bar on the Stack Overflow website...
So I have been looking for a solution:
I thought this would be easy to do. I imagined just putting a FlowPanel in the cell, adding/removing Buttons/Widgets dynamically. But it turns out that in GWT Widgets and Cells and very different objects apparently..
I read making use of the AbstractCell class to create a custom cell allows to do anything, but its working is very low level and obscure to me.
I saw that CompositeCell allows to combine various cell widgets into one cell, but I have not found if it is possible to do it dynamically, or if the widgets are going to be the same for all lines throughout a column. I mostly saw examples about, for instance, how to put two Buttons in every cell of a single column.
What is the easiest way to implement what I need?
EDIT:
So, after some tests, I am going for Andrei's suggestion and going "low-level", creating a custom cell extending AbstractCell<>. I could create an appropriate "render" function, that generates a list of html "button", and also attaches Javascript calls to my Java functions when triggering a Javascript event (onclick, onmouseover, onmouseout...).
It is working pretty well. For instance, by clicking the "+" button at the end a tag list, it calls a MenuBar widget that presents the list of tags that can be added.
But I am struggling to find a way to update the underlying data when adding a tag.
To sum up:
I have a CustomData class that represents the data I want to display in each line of the table. It also contains the list of tags as a Set.
ModelTable (extends DataGrid) is my table.
CustomCell (extends AbstractCell) can renders the list of tags as several buttons on a line.
A click on a "+" button in a cell makes a AddTagMenu popup drop down, from which I can click on the tag to add.
How do I update the content of the cell?
I tried playing around with onBrowserEvent, onEnterKeyDown, bus events... with no success. At best I can indeed add a tag element to the underlying object, but the table is not updated.
It's not possible to meet your requirements without going really "low-level", as you call it.
It's relatively easy to create a cell that would render tags exactly as you want them. Plus icon is also easy, if this is the only action on the cell. However, it is very difficult to make every tag within a cell an interactive widget, because the DataGrid will not let you attach handlers to HTML rendered within a cell. You will need to supply your own IDs to these widgets, and then attach handlers to them in your code. The problem, however, is that when the DataGrid refreshes/re-renders, your handlers will most likely be lost. So you will have to attach them again to every tag in every cell on every change in the DataGrid.
A much simpler approach is to create a composite widget that represents a "row", and then add these "rows" to a FlowPanel. You can easily make it look like a table with CSS, and supply your own widget that looks like a table header. You will need to recreate some of the functionality of the DataGrid, e.g. sorting when clicked on "column" header - if you need this functionality, of course.
As you have already noted, using CompositeCell could be a way to get what you want.
The idea is to create a cell for every tag and then (during rendering) decide which one should be shown (rendered). Finally combine all those cells into one by creating a CompositeCell.
The main disadvantage of this solution is that you need to know all possible tags before you create a DataGrid.
So, if you have a fixed list of possible tags or can get a list of all existing tags and this list is reasonably small, here is a solution.
First, we need to know which tag is represented by a column so I extended a Column class to keep information about a tag. Please, note that TagColumn uses ButtonCell and also handles update when the button is clicked:
public class TagColumn extends Column<DataType, String> {
private TagEnum tag;
public TagColumn(TagEnum tag) {
super(new ButtonCell());
this.tag = tag;
setFieldUpdater(new FieldUpdater<DataType, String>() {
#Override
public void update(int index, DataType object, String value) {
Window.alert("Tag " + getTag().getName() + " clicked");
}
});
}
public TagEnum getTag() {
return tag;
}
#Override
public String getValue(DataType object) {
return tag.getName();
}
}
Then create a cell for each tag (I have hard-coded all tags in a TagEnum):
List<HasCell<DataType, ?>> tagColumns = new ArrayList<HasCell<DataType, ?>>();
for(TagEnum tag : TagEnum.values())
tagColumns.add(new TagColumn(tag));
Now, the most important part: decide either to show the tag or not - overwrite render method of the CompositeCell:
CompositeCell<DataType> tagsCell = new CompositeCell<DataType>(tagColumns) {
#Override
protected <X> void render(Context context, DataType value, SafeHtmlBuilder sb, HasCell<DataType, X> hasCell) {
if(value.getTagList().contains(((TagColumn) hasCell).getTag()))
super.render(context, value, sb, hasCell);
else
sb.appendHtmlConstant("<span></span>");
}
};
This is important to always render any element (for example empty span when the tag should not be shown). Otherwise the CompositeCell's implemantation will get confused when accessing sibling elements.
Finally, full, working example code:
private DataGrid<DataType> getGrid() {
DataGrid<DataType> grid = new DataGrid<DataType>();
List<HasCell<DataType, ?>> tagColumns = new ArrayList<HasCell<DataType, ?>>();
for(TagEnum tag : TagEnum.values())
tagColumns.add(new TagColumn(tag));
CompositeCell<DataType> tagsCell = new CompositeCell<DataType>(tagColumns) {
#Override
protected <X> void render(Context context, DataType value, SafeHtmlBuilder sb, HasCell<DataType, X> hasCell) {
if(value.getTagList().contains(((TagColumn) hasCell).getTag()))
super.render(context, value, sb, hasCell);
else
sb.appendHtmlConstant("<span></span>");
}
};
Column<DataType, DataType> tagsColumn = new Column<DataType, DataType>(tagsCell) {
#Override
public DataType getValue(DataType object) {
return object;
}
};
grid.addColumn(tagsColumn, "Tags");
grid.setRowData(Arrays.asList(
new DataType(Arrays.asList(TagEnum.gwt)),
new DataType(Arrays.asList(TagEnum.table, TagEnum.datagrid)),
new DataType(Arrays.asList(TagEnum.datagrid, TagEnum.widget, TagEnum.customCell)),
new DataType(Arrays.asList(TagEnum.gwt, TagEnum.table, TagEnum.widget, TagEnum.customCell)),
new DataType(Arrays.asList(TagEnum.gwt, TagEnum.customCell)),
new DataType(Arrays.asList(TagEnum.gwt, TagEnum.table, TagEnum.datagrid, TagEnum.widget, TagEnum.customCell))
)
);
return grid;
}
public class TagColumn extends Column<DataType, String> {
private TagEnum tag;
public TagColumn(TagEnum tag) {
super(new ButtonCell());
this.tag = tag;
setFieldUpdater(new FieldUpdater<DataType, String>() {
#Override
public void update(int index, DataType object, String value) {
Window.alert("Tag " + getTag().getName() + " clicked");
}
});
}
public TagEnum getTag() {
return tag;
}
#Override
public String getValue(DataType object) {
return tag.getName();
}
}
public class DataType {
List<TagEnum> tagList;
public DataType(List<TagEnum> tagList) {
this.tagList = tagList;
}
public List<TagEnum> getTagList() {
return tagList;
}
}
public enum TagEnum {
gwt ("gwt"),
table ("table"),
datagrid ("datagrid"),
widget ("widget"),
customCell ("custom-cell");
private String name;
private TagEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

Layout update GWT

I use RPC calls to connect to mySql and bring text data from there.
My page is defined as split Layout.
my problem is that I don't know how to update the main layout with different text.
if i use the clear() method it will remove all the layout !
"p" is the splitLayout.
RPC:
rpcService.getChapterTxt(selectedBook,bookChapters[selectedBook],
new AsyncCallback<List<BibleTxt>>(){
public void onFailure(Throwable caught)
{
Window.alert("Failed getting Chapter");
}
public void onSuccess(List<BibleTxt> result)
{
int i = 0 ;
String verseText ="";
//Label verseLabel = new Label();
PPanel chapterPar = new PPanel();
HTML page= new HTML(verseText);
for(i=0;i<result.size();i++)
{
verseText = result.get(i).getVerseText();
//verseLabel.setText(verseText);
page.setText(page.getText() + verseText);
}
chapterPar.add(page);
//p.clear();
p.add(chapterPar); // adds the main layout
}
});
Why you don't reuse the text component changing its content text instead of continuously detaching/attaching elements to the widget hierarchy. That way should perform better and cause less problems.

Remove JavaFX 2 LineChart Legend Items

I have a line chart with many series. These series are grouped into one or more super-series. Each super-series may have many "breaks" in the line in order to accurately depict when a monitor process is not actively collecting data. Each data break is actually starting a new series.
I have been able to successfully overcome several technical issues with this such as the chart assigning a new color to each new series, chart line symbol color not matching the series color, etc. All is working quite well right now, except that each time I add a new series to the chart, it adds an item to the legend.
Is there a way to remove items from the legend, or do I have to hide the default legend and add my own custom legend pane?
Don't show the legend:
chart.setLegendVisible(false);
You can then create your own custom pane to make your own legend and render it how you wish.
After several failed attempts at implementing various suggestions, I found that the best way to allow a user to show/hide a data series in a JavaFx Chart (or sub-classes thereof) is to extend the chart class you want to use and override its updateLegend() method.
It's quite simple actually. Here's an example using a basic HBox as the legend containing check boxes as the legend items. In this example I have decided to make my LineChart with fixed axes types (CategoryAxis and NumberAxis). You might choose to leave your sub-class with generics for axes.
public class AtopLineChart<X, Y> extends LineChart<String, Number>
{
/**
* #param xAxis
* #param yAxis
*/
public AtopLineChart(final CategoryAxis xAxis, final NumberAxis yAxis)
{
super(xAxis, yAxis);
}
/* (non-Javadoc)
* #see javafx.scene.chart.LineChart#updateLegend()
*/
#Override
protected void updateLegend()
{
final HBox legend = new HBox();
legend.setVisible(true);
for (final javafx.scene.chart.XYChart.Series<String, Number> series : getData())
{
final CheckBox cb = new CheckBox(series.getName());
cb.setUserData(series);
cb.setSelected(true);
cb.addEventHandler(ActionEvent.ACTION, e ->
{
final CheckBox box = (CheckBox) e.getSource();
#SuppressWarnings("unchecked")
final Series<String, Number> s = (Series<String, Number>) box.getUserData();
s.getNode().setVisible(box.isSelected());
});
legend.getChildren().add(cb);
}
setLegend(legend);
}
}
I'll leave it as an exercise for the reader to make the legend more readable, for example, borders around each checkbox and binding the color of the series to the something showing that color in the checkbox for the series.
One other thing, you might want to check the getLegendSide() method to decide which kind of layout container to use for the legend, i.e. HBox for TOP and BOTTOM but VBOX for LEFT and RIGHT. Your choice.
You can find a node based on it's type (and optionally style name) using this method:
private static Node findNode(final Parent aParent, final String aClassname, final String aStyleName) {
if (null != aParent) {
final ObservableList<Node> children = aParent.getChildrenUnmodifiable();
if (null != children) {
for (final Node child : children) {
String className = child.getClass().getName();
if (className.contains("$")) {
className = className.substring(0, className.indexOf("$"));
}
if (0 == aClassname.compareToIgnoreCase(className)) {
if ((null == aStyleName) || (0 == aStyleName.length())) {
// No aStyleName, just return this:
return child;
}
else {
final String styleName = child.getStyleClass().toString();
if (0 == aStyleName.compareToIgnoreCase(styleName)) {
return child;
}
}
}
if (child instanceof Parent) {
final Node node = findNode((Parent) child, aClassname, aStyleName);
if (null != node) {
return node;
}
}
}
}
}
return null;
}
Calling it with the chart in question to retrieve the Legend node:
Legend legend = (Legend) findNode(chart, Legend.class.getName(), "chart-legend");
Which you can then iterate through the children and remove the ones you don't want to be displayed:
for (final Node legendItem : legend.getChildren()) {
final Label legendLabel = (Label) legendItem;
if (0 == legendLabel.getText().compareToIgnoreCase("the name of the legend I want hidden (or replaced with some other test)")) {
legend.getChildren().remove(legendItem);
break;
}
}
JavaFX also has a lookup function which "Finds this Node, or the first sub-node, based on the given CSS selector." and acts similar to the findNode function from this answer.
From a similar case, https://stackoverflow.com/a/27819227/2341336
This solution takes advantage of streams in Java and directly modifies the Legend object.
However, this is deprecated so it is not recommended.
Since you are already dealing with Legend, you can work with its
items, removing those you don't need, so the legend shows only two
items.
Using streams, you can mark the first two items as "Valid"/"Invalid"
and the rest as "Remove", for instance, and finally you just remove
these last items.
private void updateStyleSheet() {
Legend legend = (Legend)lineChart.lookup(".chart-legend");
AtomicInteger count = new AtomicInteger();
legend.getItems().forEach(item->{
if(count.get()==0){
item.setText("Valid");
} else if(count.get()==1){
item.setText("Invalid");
} else {
item.setText("Remove");
}
count.getAndIncrement();
});
legend.getItems().removeIf(item->item.getText().equals("Remove"));
...
}

Wicket palette with dropdownchoice

I created a palette which contains values, and i created a DropDownChoice ddcdomaines with an AjaxFormComponentUpdatingBehavior to update choice list, but i dont know how to do it, can you help me?
Here my code:
List<PartnerDomainDTO> domaines = partenaireHelper.allDomains();
ChoiceRenderer choiceRenderer = new ChoiceRenderer("label", "sid");
final Palette palette =
new Palette("partenaires", new PropertyModel(offre, "partenaires"), new Model(
(Serializable) partenairesPossibles), renderer, 10, false) {
private static final long serialVersionUID = 1178320215146881229L;
boolean first = true;
#Override
public Iterator getSelectedChoices() {
if (first) {
first = false;
return partenairesExistants.iterator();
}
return super.getSelectedChoices();
}
};
palette.setOutputMarkupId(true);
palette.setOutputMarkupPlaceholderTag(true);
DropDownChoice ddcdomaines = new DropDownChoice("domaines", new Model(domaines.get(0)), domaines, choiceRenderer);
ddcdomaines.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
remove(palette);
Palette palette1 =
new Palette(
"partenaires",
new PropertyModel(offre, "partenaires"),
new Model(new ArrayList<Partenaire>()),
renderer,
10,
false);
palette1.setOutputMarkupId(true);
palette1.setOutputMarkupPlaceholderTag(true);
add(palette1);
target.addComponent(palette);
target.addComponent(palette1);
}
});
add(palette);
add(ddcdomaines);
}
Here is an excellent example of DropDownChoice with AjaxFormComponentUpdatingBehavior:
http://wicketstuff.org/wicket/ajax/choice
Click on the Source Code link to see the source. If you need more information, than you need to provide more detail in your question.
Update: According to the JavaDoc, there is a specific way to update Palettes using Ajax:
http://wicket.apache.org/apidocs/1.4/org/apache/wicket/extensions/markup/html/form/palette/Palette.html
Make sure you follow those instructions before doing anything else.
In your code, you are attempting to remove one Palette and put in another Palette. While this might work in the long run, it is not very Wicket-y. Sadly, Palette does not have a public method to change the choices. However, if you keep a reference to your choicesModel, then you can modify that list and the Palette should see the change on re-render. Something like this:
final Model<List<String>> choicesModel = new Model<List<String>>(partenairesPossibles)
final Palette palette = new Palette("partenaires", new PropertyModel(offre, "partenaires"), choicesModel, renderer, 10, false) {...};
And then in your Ajax onUpdate
choicesModel.setObject(/* Insert your other list */);
target.addComponent(palette);
Be careful with keeping track of the selected choices and the possible choices. I'm not sure what will happen if these don't match up. Hope that helps!