Ok. I am attempting to make a simple little text editor for java practice. I have a TextArea in JavaFX 8 that I type text into. I would like to be able to create and fill a Text object and then send that text object to the printer. So far, I have failed miserably. The printer just spits out a blank sheet of paper. It's acting as if there is no content to be printed.
I found an example where the text object is wrapped in java's TextFlow like this..
TextFlow printArea = new TextFlow(new Text(textDocument.getText()));
that at least prints SOMETHING, but it's only the first line of text entered.
Here is my print code:
static void printOperation(TextArea textDocument) {
Text extractedText = new Text(textDocument.getText());
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPageSetupDialog(textDocument.getScene().getWindow())
&& printerJob.showPrintDialog(textDocument.getScene().getWindow())) {
if (printerJob.printPage(extractedText)) {
printerJob.endJob();
} else {
System.out.println("Failed to print");
}
} else {
System.out.println("Canceled");
}
}
//There is a print menu option that calls the print method
print.setOnAction((ActionEvent e) -> {
printOperation(textDocument);
});
Text by seems to have it's origin at the bottom left corner of the node not at the top left one. The text simply is not inside the printed area.
The second attempt probably fails to achieve the desired output, since you do not set any limit for the width.
You could ensure the node is on screen by using a StackPane as parent of the text. Furthermore I recommend setting the wrappingWidth property:
static void printOperation(TextArea textDocument) {
Text extractedText = new Text(textDocument.getText());
extractedText.setWrappingWidth(450);
// use pane to place the text
StackPane container = new StackPane(extractedText);
container.setAlignment(Pos.TOP_LEFT);
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPageSetupDialog(textDocument.getScene().getWindow())
&& printerJob.showPrintDialog(textDocument.getScene().getWindow())) {
if (printerJob.printPage(container)) {
printerJob.endJob();
} else {
System.out.println("Failed to print");
}
} else {
System.out.println("Canceled");
}
}
You should probably set the wrappingWidth according to the page size/margin chosen by the user instead of hardcoding the value though...
I've started using PDFClown some weeks ago. My purpose is multi-word highlighting, mainly on newspapers. Starting from the org.pdfclown.samples.cli.TextHighlightSample example, I succeeded in extracting multi-word positions and highlighting them. I even solved some problems due to text ordering and matching in most cases.
Unfortunately my framework includes FPDI and it does not consider PDFAnnotations. So, all the content outside of a page content stream, like text annotations and other so called markup annotations, get lost.
So any suggestion on creating "Text Highlighting" with PdfClown and without using PDF annotations?
To not have the highlight in an annotation but instead in the actual page content stream, one has to put the graphic commandos into the page content stream which in case of the org.pdfclown.samples.cli.TextHighlightSample example are implicitly put into the normal annotation appearance stream.
This can be implemented like this:
org.pdfclown.files.File file = new org.pdfclown.files.File(resource);
Pattern pattern = Pattern.compile("S", Pattern.CASE_INSENSITIVE);
TextExtractor textExtractor = new TextExtractor(true, true);
for (final Page page : file.getDocument().getPages())
{
final List<Quad> highlightQuads = new ArrayList<Quad>();
Map<Rectangle2D, List<ITextString>> textStrings = textExtractor.extract(page);
final Matcher matcher = pattern.matcher(TextExtractor.toString(textStrings));
textExtractor.filter(textStrings, new TextExtractor.IIntervalFilter()
{
#Override
public boolean hasNext()
{
return matcher.find();
}
#Override
public Interval<Integer> next()
{
return new Interval<Integer>(matcher.start(), matcher.end());
}
#Override
public void process(Interval<Integer> interval, ITextString match)
{
{
Rectangle2D textBox = null;
for (TextChar textChar : match.getTextChars())
{
Rectangle2D textCharBox = textChar.getBox();
if (textBox == null)
{
textBox = (Rectangle2D) textCharBox.clone();
}
else
{
if (textCharBox.getY() > textBox.getMaxY())
{
highlightQuads.add(Quad.get(textBox));
textBox = (Rectangle2D) textCharBox.clone();
}
else
{
textBox.add(textCharBox);
}
}
}
highlightQuads.add(Quad.get(textBox));
}
}
#Override
public void remove()
{
throw new UnsupportedOperationException();
}
});
// Highlight the text pattern match!
ExtGState defaultExtGState = new ExtGState(file.getDocument());
defaultExtGState.setAlphaShape(false);
defaultExtGState.setBlendMode(Arrays.asList(BlendModeEnum.Multiply));
PrimitiveComposer composer = new PrimitiveComposer(page);
composer.getScanner().moveEnd();
// TODO: reset graphics state here.
composer.applyState(defaultExtGState);
composer.setFillColor(new DeviceRGBColor(1, 1, 0));
{
for (Quad markupBox : highlightQuads)
{
Point2D[] points = markupBox.getPoints();
double markupBoxHeight = points[3].getY() - points[0].getY();
double markupBoxMargin = markupBoxHeight * .25;
composer.drawCurve(new Point2D.Double(points[3].getX(), points[3].getY()),
new Point2D.Double(points[0].getX(), points[0].getY()),
new Point2D.Double(points[3].getX() - markupBoxMargin, points[3].getY() - markupBoxMargin),
new Point2D.Double(points[0].getX() - markupBoxMargin, points[0].getY() + markupBoxMargin));
composer.drawLine(new Point2D.Double(points[1].getX(), points[1].getY()));
composer.drawCurve(new Point2D.Double(points[2].getX(), points[2].getY()),
new Point2D.Double(points[1].getX() + markupBoxMargin, points[1].getY() + markupBoxMargin),
new Point2D.Double(points[2].getX() + markupBoxMargin, points[2].getY() - markupBoxMargin));
composer.fill();
}
}
composer.flush();
}
file.save(new File(RESULT_FOLDER, "multiPage-highlight-content.pdf"), SerializationModeEnum.Incremental);
(HighlightInContent.java method testHighlightInContent)
You will recognize the text extraction frame from the original example. Merely now the quads from a whole page are collected before they are processed, and the processing code (which mostly has been borrowed from TextMarkup.refreshAppearance()) draws forms representing the quads into the page content.
Beware, to make this work generically, the graphics state has to be reset before inserting the new instructions (the position is marked with a TODO comment). This can be done either by applying save/restore state or by actually counteracting unwanted changed state entries. Unfortunately I did not see how to do the former in PDF Clown and have not yet had the time to do the latter.
I have a text in the Link SWT widget created as follow:
Link message = new Link(parent, SWT.WRAP);
message.setText(myMessage);
I want the text (in myMessage variable) be selectable, to grant users to copy it.
How can I do this?
I have used Link widget because I need hyperlinks in the text to be clickable.
The SWT Link widget is not selectable. To work around this I can think of either
provide a context menu for the Link with a Copy menu item that copies the text to the clipboard
place a Copy (tool) button next to the Link that copies the text to the clipboard
use a Browser widget which is selectable but harder to layout and requires extra work to trigger the functinality when the link is selected
if you don't mind the extra dependency to org.eclipse.ui.forms, use the FormText. The FormText can show hyperlinks and allows to select and copy text
Why not using a StyledText to allow text selection ?
String string = "This is sample text with a link and some other link here.";
final StyledText styledText = new StyledText (shell, SWT.MULTI | SWT.BORDER);
styledText.setText(string);
String link1 = "link";
String link2 = "here";
StyleRange style = new StyleRange();
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
int[] ranges = {string.indexOf(link1), link1.length(), string.indexOf(link2), link2.length()};
StyleRange[] styles = {style, style};
styledText.setStyleRanges(ranges, styles);
styledText.addListener(SWT.MouseDown, new Listener() {
#Override
public void handleEvent(Event event) {
// It is up to the application to determine when and how a link should be activated.
// In this snippet links are activated on mouse down when the control key is held down
if ((event.stateMask & SWT.MOD1) != 0) {
try {
int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
StyleRange style = styledText.getStyleRangeAtOffset(offset);
if (style != null && style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) {
System.out.println("Click on a Link");
}
} catch (IllegalArgumentException e) {
// no character under event.x, event.y
}
}
}
});
Full example here
First question on Stack Overflow, so bear with me! I am new to d3.js, but have been consistently amazed by what others are able to accomplish with it... and almost as amazed by how little headway I've been able to make with it myself! Clearly I'm not grokking something, so I hope that the kind souls here can show me the light.
My intention is to make a connected graph based on the CSV data. My CSV data has different information some of them need to be represented as a nodes, some of them need to be used as an information for ltool-tip. I can read the data from CSV by using this snippet of code:
d3.csv("data/project.csv", function(links) {
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
link.source = nodeByName(link.Project);
link.target = nodeByName(link.Problem);
});
Which will connect the node P1, to three nodes Problems (URLs).
The problem comes after, I could not see the label on the selected nodes, How could I use the extract information in the CSV files such as "Date, Summary, URL". Is there any way to use mouse click, when I click the node I need its information appears on SVG board to be used for further analysis, such as showing the URL, short text of the summary, and when the user click on the board it should show the the complete information.
Here is the code that I used from this link
var width = 800,
height = 600;
var svg = d3.select("#visualization")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("svg:g");
var force = d3.layout.force()
.gravity(0.2)
.distance(200)
.charge(-1500)
.size([width, height]);
var container = svg.append("g")
.attr("id", "container");
d3.csv("data/project.csv", function(links) {
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
link.source = nodeByName(link.Project);
link.target = nodeByName(link.Problem);
});
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodesByName);
//define a scale for color mapping
var colormapping = d3.scale.ordinal()
.domain([0,nodes.length])
.range(['#A700E6','#D95B96','#F4DA88','#22C1BE','#F24957','#DBEF91','#CF8EE8','#FF9B58','#B8FFC4','#91AEFF','#E873D3','#CCB298']);
//create label node tooltip
var labeltooltip = d3.select("body").append("div")
.attr("class", "labeltooltip")
.style("opacity", 1e-6);
var zoom = d3.behavior.zoom()
.scaleExtent([1, 10])
.on("zoom", function() {
container.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
});
svg.call(zoom);
//links
var link = container.selectAll(".line")
.data(links)
.enter()
.append("line")
.attr("stroke-width",4)
// .attr("stroke-width",function (d) { return linethickness(d.value); })
.style("stroke", "gray");
// Create the node circles.
var node = container.selectAll(".node")
.data(nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("r", 20)
.attr("fill", function (d,i) {return d3.rgb(colormapping(i)); })
.call(force.drag);
// Start the force layout.
force
.nodes(nodes)
.links(links)
.on("tick", tick)
.start();
node.on("mousemove", function(d) {
labeltooltip.selectAll("p").remove();
labeltooltip.style("left", (d3.event.pageX+15) + "px").style("top", (d3.event.pageY-10) + "px");
labeltooltip.append("p").attr("class", "tooltiptext").html("<span>Id: </span>" + d.Score );
labeltooltip.append("p").attr("class", "tooltiptext").html("<span>Score: </span>" + d.Score);
});
node.on("mouseover", function(d) {
labeltooltip.transition()
.duration(500)
.style("opacity", 1);
link.style('stroke', function(l) {
if (d === l.source || d === l.target)
return d3.rgb('#C20606');
else
return 'gray';
});
link.style('opacity', function(o) {
return o.source === d || o.target === d ? 1 : 0;
});
node.style("opacity", function(o) {
if (o.id != d.id)
return neighboring(d.id, o.id) ? 1 : 0;
});
});
node.on("mouseout", function(d) {
labeltooltip.transition()
.duration(500)
.style("opacity", 1e-6);
link.style('stroke', 'gray');
link.style('opacity', 1);
node.style("opacity", 1);
});
var circletext = node.append("svg:text")
.text(function(d) {return d.name;})
.attr("class","labelText");
function tick() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
circletext.attr("x", function(d) { return d.x-25; });
circletext.attr("y", function(d) { return d.y-25;});
}
function nodeByName(name) {
return nodesByName[name] || (nodesByName[name] = {name: name});
}
addZoomMoveIcon("#labelgraph");
});
UPDATE
My CSV data
Project,Problem, Score,Data,Summary,id
p1,Problem1,10,2014-09-04T13:55:05.623-04:00, text text text text, 1
p1,Problem2,5,2014-09-04T13:55:05.623-04:00, text text text text,2
p1,Problem3,11,2014-09-04T13:55:05.623-04:00, text text text text,3
The final results that I am aiming for would be like the following figure:
Where the yellow box should appear on the SVG when the "problem" node clicked.
The first issue you need to address is where to correctly add the node text. Right now you are nesting the text inside the circles, but the text labels need to be siblings of the circles (they do not even need to live right next to each circle). You can replace this:
var circletext = node.append("svg:text")
.text(function(d) {return d.name;})
.attr("class","labelText");
With:
var circletext = container.selectAll("g")
.data(nodes)
.enter()
.append("text").text(function(d) {return d.name})
.attr("class", "labelText");
(Reference: https://www.dashingd3js.com/svg-text-element)
Next though, if you want to display the Score/Data/Summary note that these are attached to the links, not the nodes (there are only three of them, not four). However, at the point of node creation, you can pass this info from the link to the node. Modify the nodeByName function to add the new attributes for the nodes:
function nodeByName(name,score,data,summary) {
return nodesByName[name] || (nodesByName[name] = {name: name, score: score, data: data, summary: summary});
}
Then modify the call to this function when the links are being created:
links.forEach(function(link) {
link.source = nodeByName(link.Project);
link.target = nodeByName(link.Problem,link.Score,link.Data,link.Summary);
});
To get the labeltooltip to display in a stationary spot, and not next to the nodes, you'll need to remove the bits on the node mouseover and mouseout events that move the div around, and probably just add the div to the html instead of dynamically adding it.
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);
}