I am using Geotools to display a road map of Bangalore - java

I want to know how to highlight any specific road. For example i want to color a road yellow that has id=1. I am using Java to display the map.

I have found how to highlight the road which has ID=1. Steps that I followed are:
//create a filter object
Filter filter;
//create a datastore object from .shp file
FileDataStore store= FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource=store.getFeatureSource();
//I am using CQL query to select the road that is ID=1
filter=CQL.toFilter("ID=1");
//create a SimpleFeatureCollection object for the filtered features
SimpleFeatureCollection fc=featureSource.getFeatures(filter);
//create a feature iterator to traverse through the selected features
SimpleFeatureIterator iter=fc.features();
//create a Set object to store the featureIdentifiers.
Set<FeatureId> IDs=new HashSet<FeatureId>();
//add the selected features to IDs
try{
while(iter.hasNext()){
SimpleFeature f=iter.next();
IDs.add(f.getIdentifier());
System.out.println(" "+f.getIdentifier());
}
}
finally{
iter.close();
}
//create style object to store style of selected features
Style style=createSelectedStyle(IDs);
MapContext map=new DefaultMapContext();
//show the map
map.addLayer(featureSource,style);
JMapFrame.showMap(map);
//defining the createSelectedStyle method
private Style createSelectedStyle(Set<FeatureId> IDs) {
Rule selectedRule = createRule(SELECTED_COLOUR, SELECTED_COLOUR);
selectedRule.setFilter(ff.id(IDs));
Rule otherRule = createRule(LINE_COLOUR, FILL_COLOUR);
otherRule.setElseFilter(true);
FeatureTypeStyle fts = sf.createFeatureTypeStyle();
fts.rules().add(selectedRule);
fts.rules().add(otherRule);
Style style2 = sf.createStyle();
style2.featureTypeStyles().add(fts);
return style2;
}
//defining the createRule method
private Rule createRule(Color outlineColor, Color fillColor) {
Symbolizer symbolizer = null;
Fill fill = null;//not required if working with line
Stroke stroke = sf.createStroke(ff.literal(outlineColor), ff.literal(LINE_WIDTH));
symbolizer = sf.createLineSymbolizer(stroke, "the_geom");
Rule rule = sf.createRule();
rule.symbolizers().add(symbolizer);
return rule;
}

Related

method select for MultiSelectComboBox not working

I have a MultiSelectComboBox but when trying to select some items through its select() method nothing happens.
My code looks as follows:
Binder<Technology> binder = new BeanValidationBinder<>(Technology.class);
MultiSelectComboBox<TechnologyLabel> multiComboBox = new MultiSelectComboBox<>("Labels");
binder
.forField(this.multiComboBox)
.bind(Technology::getLabels, Technology::setLabels);
List<TechnologyLabel> labelList = technologyLayout.technologyLabelService.getTechnologyLabels(technology);
List<Label> containedLabels = new ArrayList<>();
for (var tl : labelList) {
containedLabels.add(tl.getLabel());
}
List<Label> labels = labelService.findAllLabels();
for (var label : labels) {
if (!containedLabels.contains(label)) {
labelList.add(new TechnologyLabel(technology, label));
}
}
multiComboBox.setItems(labelList);
multiComboBox.setItemLabelGenerator(TechnologyLabel::getLabelName);
note.setMaxHeight("10em");
multiComboBox.select(labelList.get(0);
What I do above is querying my ManyToMany relation to find all labels given the technology. Then I find all the labels and create a list from that. Lastly, I try to select first item of the list, but it does not show a tick next to it.

JavaFX: TableView: get selected row cell values

I'm working on a project where the TableView looks like this:
The datas are inside are a MySQL database, and I use a modell and an observableArrayList to get them.
I want to make an initialize method which read the whole selected Line and save it into the TextFields (like Name-Name, Address - Address etc). I tried with this, and now I can Select the Whole Line data, but can't split it into Strings to set the TextField texts:
ListViewerModell details = (ListViewerModell) table.getSelectionModel().getSelectedItem();
//Checking if any row selected
if (details != null) {
//??? - split
editName.setText("*Name*");
editAddress.setText("*Address*");
}
Tried with the modell getName(), getAdress() methods but I got NullPointerException and TablePos methods.
// create table
TableView<Person> table = new TableView<Person>();
// create columns
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Person, Address> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(new PropertyValueFactory("address"));
// add columns
table.getColumns().setAll(nameCol, addressCol);
// get data from db, return object List<Person> from DB
ObservableList<Person> persons = getPersonsFromDB();
table.setItems(persons);
tableView.setOnMouseClicked((MouseEvent event) -> {
if (event.getClickCount() > 1) {
onEdit();
}
});
public void onEdit() {
// check the table's selected item and get selected item
if (table.getSelectionModel().getSelectedItem() != null) {
Person selectedPerson = table.getSelectionModel().getSelectedItem();
nameTextField.setText(selectedPerson.getName());
addressTextField.setText(selectedPerson.getAddress());
}
}
One way could be extracting the value of the columns of the selected row, like so :
Object selectedItems = table.getSelectionModel().getSelectedItems().get(0);
String first_Column = selectedItems.toString().split(",")[0].substring(1);
System.out.println(first_Column);

Wicket: Palette set default selected

I'm trying to implement a palette. I try to set a default selected list but it's empty.
myLists:
// here I get a Set of the categorys which are already in that group
Set<Category> selectedCategorysSet = new HashSet<Category>();
selectedCategorysSet = group.getCategorys();
// here I get all categorys exists
List<Category> listCategory = new ArrayList<Category>();
listCategory = catDao.getAll(Category.class);
List<Category> selectedCats = new ArrayList<Category>();
List<Category> tmpList = new ArrayList<Category>();
// the palette doesnt accept an Set so I added the set to a List
selectedCats.addAll(selectedCategorysSet);
// here I delete every Category from the whole List which is already selected (stored in a temporary list)
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);
/*
two multiple select boxes which switches items between each other
*/
IChoiceRenderer<Category> renderer = new ChoiceRenderer<Category>("title","categoryId");
final Palette<Category> palette = new Palette<Category>("palette",
new ListModel<Category>(selectedCats),
new CollectionModel<Category>(listCategory),
renderer, 10, false);
I already debugged that code, it works but my selected values are empty.
Here is a picture of my debugged variables:
but the selected field is still empty!
what am I doing wrong?
You should not delete every Category from the whole List which is already selected.
Palette component must store whole list of values in it's choicesModel which is listCategory in your code.
So, just remove following code from your implementation:
for(Category catList:listCategory){
for(Category cat:selectedCategorysSet){
if(cat.getCategoryId() == catList.getCategoryId()){
tmpList.add(catList);
}
}
}
listCategory.removeAll(tmpList);

how to Create table in word doc using docx4j in specific bookmark without overwritting the word doc

I need to create a table at the location of particular bookmark. ie i need to find the bookmark and insert the table . how can i do this using docx4j
Thanks in Advance
Sorry Jason, I am new to Stackoverflow so i couldnt write my problem clearly, here is my situation and problem.
I made changes in that code as you suggested and to my needs, and the code is here
//loop through the bookmarks
for (CTBookmark bm : rt.getStarts()) {
// do we have data for this one?
String bmname =bm.getName();
// find the right bookmark (in this case i have only one bookmark so check if it is not null)
if (bmname!=null) {
String value = "some text for testing run";
//if (value==null) continue;
List<Object> theList = null;
//create bm list
theList = ((ContentAccessor)(bm.getParent())).getContent();
// I set the range as 1 (I assume this start range is to say where the start the table creating)
int rangeStart = 1;
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
// create the table
Tbl table = factory.createTbl();
//add boards to the table
addBorders(table);
for(int rows = 0; rows<1;rows++)
{// create a row
Tr row = factory.createTr();
for(int colm = 0; colm<1;colm++)
{
// create a cell
Tc cell = factory.createTc();
// add the content to cell
cell.getContent().add(wordPackage.getMainDocumentPart()
.createParagraphOfText("cell"+colm));
// add the cell to row
row.getContent().add(cell);
}
// add the row to table
table.getContent().add(row);
// now add a run (to test whether run is working or not)
org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(t);
t.setValue(value);
//add table to list
theList.add(rangeStart, table);
//add run to list
//theList.add(rangeStart, run);
}
I dont need to delete text in bookmark so i removed it.
I dont know whats the problem, program is compiling but I cannot open the word doc , it says "unknown error". I test to write some string "value" it writes perfectly in that bookmark and document is opening but not in the case of table. Please help me
Thanks in advance
You can adapt the sample code BookmarksReplaceWithText.java
In your case:
line 89: the parent won't be p, it'll be body or tc. You could remove the test.
line 128: instead of adding a run, you want to insert a table
You can use TblFactory to create your table, or the docx4j webapp to generate code from a sample docx.
For some reason bookmark replacement with table didn't workout for me, so I relied on text replacement with table. I created my tables from HTML using XHTML importer for my use case
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String xhtml= <your table HTML>;
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
int ct = 0;
List<Integer> tableIndexes = new ArrayList<>();
List<Object> documentContents = documentPart.getContent();
for (Object o: documentContents) {
if (o.toString().contains("PlaceholderForTable1")) {
tableIndexes.add(ct);
}
ct++;
}
for (Integer i: tableIndexes) {
documentPart.getContent().remove(i.intValue());
documentPart.getContent().addAll(i.intValue(), XHTMLImporter.convert( xhtml, null));
}
In my input word doc, I defined text 'PlaceholderForTable1' where I want to insert my table.

How to Update an Existing Record in a Dataset

I can't seem to update an existing record in my table using a strongly-typed dataset. I can add a new record, but if I make changes to an existing record it doesn't work.
Here is my code:
private void AddEmplMaster()
{
dsEmplMast dsEmpMst = new dsEmplMast();
SqlConnection cn = new SqlConnection();
cn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["cn.ConnectionString"];
SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM UPR00100", cn);
SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
da1.Fill(dsEmpMst.UPR00100);
DataTable dtMst = UpdateEmpMst(dsEmpMst);
da1.Update(dsEmpMst.UPR00100);
}
This procedure is called from above to assign the changed fields to a record:
private DataTable UpdateEmpMst(dsEmplMast dsEmpMst)
{
DataTable dtMst = new DataTable();
try
{
dsEmplMast.UPR00100Row empRow = dsEmpMst.UPR00100.NewUPR00100Row();
empRow.EMPLOYID = txtEmplId.Text.Trim();
empRow.LASTNAME = txtLastName.Text.Trim();
empRow.FRSTNAME = txtFirstName.Text.Trim();
empRow.MIDLNAME = txtMidName.Text.Trim();
empRow.ADRSCODE = "PRIMARY";
empRow.SOCSCNUM = txtSSN.Text.Trim();
empRow.DEPRTMNT = ddlDept.SelectedValue.Trim();
empRow.JOBTITLE = txtJobTitle.Text.Trim();
empRow.STRTDATE = DateTime.Today;
empRow.EMPLOYMENTTYPE = "1";
dsEmpMst.UPR00100.Rows.Add(empRow);
}
catch { }
return dtMst;
}
Thank you
UPDATE:
Ok I figured it out. In my UpdateEmpMst() procedure I had to check if the record exists then to retrieve it first. If not then create a new record to add. Here is what I added:
try
{
dsEmplMast.UPR00100Row empRow;
empRow = dsEmpMst.UPR00100.FindByEMPLOYID(txtEmplId.Text.Trim());
if (empRow == null)
{
empRow = dsEmpMst.UPR00100.NewUPR00100Row();
dsEmpMst.UPR00100.Rows.Add(empRow);
}
then I assign my data to the new empRow I created and updates fine.
In order to edit an existing record in a dataset, you need to access a particular column of data in a particular row. The data in both typed and untyped datasets can be accessed via the following:
With the indices of the tables, rows, and columns collections.
By passing the table and column names as strings to their respective collections.
Although typed datasets can use the same syntax as untyped datasets, there are additional advantages to using typed datasets. For more information, see the "To update existing records using typed datasets" section below.
To update existing records in either typed or untyped datasets
Assign a value to a specific column within a DataRow object.
The table and column names of untyped datasets are not available at design time and must be accessed through their respective indices.

Categories

Resources