Title says it all. Let's say I have a right-click menu with "Strikethrough selected text" option. When I have selected some text in jtextpane, right-click --> "Strikethrough selected text" , and the selected text gets strikedthrough.
Any ideas?
Swing text components use Actions to provide the various formatting features of a text pane.
Following is the code for the UnderlineAction of the StyledEditorKit.
public static class UnderlineAction extends StyledTextAction {
/**
* Constructs a new UnderlineAction.
*/
public UnderlineAction() {
super("font-underline");
}
/**
* Toggles the Underline attribute.
*
* #param e the action event
*/
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setUnderline(sas, underline);
setCharacterAttributes(editor, sas, false);
}
}
}
So basically you will need to create your own "StrikeThroughAction" by replacing the "underline" StyleConstants methods to use the "strikethrough" StyleConstants methods.
Once you create a Action you can then use the Action by creating a JMenuItem or JButton with the Action. When the component is clicked the strike through attribute will then be added to the selected text.
in your right click action
objJTextPane.setContentType( "text/html" );
String[] args = objJTextPane.getText().split(objJTextPane.getSelectedText());
objJTextPane.setText("<strike>" + objJTextPane.getSelectedText() + "</strike>"+ args[1].toString());
apply your logic in splitting string.
Related
I'm looking for a way to open a split screen editor in an Eclipse RCP application programmatically.
From an open Editor I want to open another Editor. The purpose is to compare the content of Editor1 with the content of Editor2.
What I have is the following, but this creates a split screen Editor containing the content of Editor2 twice:
MPart editorPart = editor.getSite().getService(MPart.class);
if (editorPart == null) {
return;
}
editorPart.getTags().add(IPresentationEngine.SPLIT_HORIZONTAL);
I think best would be opening Editor2 left or below the current editor, so it has its own tab and close button.
The code below splits an editor by inserting one editor into another. This is what DnD for editor tabs does in Eclipse.
/**
* Inserts the editor into the container editor.
*
* #param ratio
* the ratio
* #param where
* where to insert ({#link EModelService#LEFT_OF},
* {#link EModelService#RIGHT_OF}, {#link EModelService#ABOVE} or
* {#link EModelService#BELOW})
* #param containerEditor
* the container editor
* #param editorToInsert
* the editor to insert
*/
public void insertEditor(float ratio, int where, MPart containerEditor, MPart editorToInsert) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
EModelService service = window.getService(EModelService.class);
MPartStack toInsert = getPartStack(editorToInsert);
MArea area = getArea(containerEditor);
MPartSashContainerElement relToElement = area.getChildren().get(0);
service.insert(toInsert, (MPartSashContainerElement) relToElement, where, ratio);
}
private MPartStack getPartStack(MPart childPart) {
MStackElement stackElement = childPart;
MPartStack newStack = BasicFactoryImpl.eINSTANCE.createPartStack();
newStack.getChildren().add(stackElement);
newStack.setSelectedElement(stackElement);
return newStack;
}
private MArea getArea(MPart containerPart) {
MUIElement targetParent = containerPart.getParent();
while (!(targetParent instanceof MArea))
targetParent = targetParent.getParent();
MArea area = (MArea) targetParent;
return area;
}
Examples of the use the insert method are below:
insertEditor(0.5f, EModelService.LEFT_OF, containerPart, childPart);
insertEditor(0.5f, EModelService.BELOW, containerPart, childPart);
In passing, code in class SplitDropAgent2 is responsible for the DnD capability of the editor tabs.
I'm having problems understanding how a TreeViewer's item is highlighted while a user is dragging an item.
Here's what happens: I start dragging the bottom item within the Treeviewer, and the items next to it highlight accordingly. The problem is, I can't get the highlighted item from the DragOver event. But if i drop the item from this position, the event in Drop method will have the "item" field holding the highlighted item. The tree's selection isn't changed when the highlight occurs
What i want to do: I want to change the image of the pointer according to the highlighted item. The problem is I don't know how to understand which one is highlighted. Another mistery to me is that in the Drop method the highlighted item will be the target of the drop (the secont Field from the top, in this case). I do not want to use SWT.FULL_SELECTION
Here's the image:
Source snippets (what i want is the functionality of DragOver in cases when I'm not directly hovering over an item)
final DropTarget valuesTarget = new DropTarget(tree, DND.DROP_MOVE);
valuesTarget.addDropListener(new DropTargetAdapter()
#Override
public void dragOver(DropTargetEvent event)
{
if (transfer.isSupportedType(event.currentDataType))
{
final DropTarget target = (DropTarget)event.widget;
final Tree tree = (Tree)target.getControl();
final Point relativeDropPoint = getRelativeDropPoint(event);
final TreeItem targetItem = tree.getItem(relativeDropPoint);
if (targetItem != null)
{
event.feedback =
DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;
if (event.item.getData() instanceof NotAcceptableClass)
{
event.detail = DND.DROP_NONE;
}
}
}
}
private Point getRelativeDropPoint(DropTargetEvent event)
{
final Tree tree = (Tree)((DropTarget)event.widget).getControl();
final Point tableLocation = tree.toDisplay(0, 0);
return new Point(event.x - tableLocation.x, event.y
- tableLocation.y);
}
Take the TreeItem directly from DropTargetEvent.item.
If you would be using JFace TreeViewer with associated content and label providers then you could use ViewerDropAdapter, which would take care of resolving the item.
I'm working on adding a preference page to my eclipse application (Juno). I would like create something similar to what you see on the following Eclipse preference page: Eclipse (Juno) > Window Menu > Preferences > Java > Compiler > Building. That preference page appears to be created using org.eclipse.swt.widgets.Tree, but I'm not sure. If that is the case, how did they create the TreeItems? Are they org.eclipse.swt.widgets.TreeItems? I need to add StringFieldEditors and IntegerFieldEditors, or some type of fields (TextArea??), with some labels in front of them, that I could validate later on. From what I understand, it's not possible to add a Composite to a TreeItem, so how should I go around this problem? Any suggestion is greatly appreciated. Thanks.
Need to add that, since I can't use the Eclipse internal packages, is there other way to implement what I described above using the public API?
Here is an idea, but this code places the TreeItems contents under the tree. Thoughts?
Composite comp = getFieldEditorParent();
Tree tree = new Tree(comp, SWT.NONE);
tree.setLayout(new FillLayout());
tree.setHeaderVisible(true);
TreeItem item1 = new TreeItem(tree, SWT.NONE);
item1.setText("Name1");
TreeItem item11 = new TreeItem(item1, SWT.NONE);
item11.setText("Name11");
StringFieldEditor s11 = new StringFieldEditor(
"name11",
"label11:",
comp);
item11.setData(s11);
TreeItem item12 = new TreeItem(item1, SWT.NONE);
item12.setText("Name12");
StringFieldEditor s12 = new StringFieldEditor(
"name12",
"label12:",
comp);
item12.setData(s12);
item1.setExpanded(true);
item11.setExpanded(true);
item12.setExpanded(true);
TreeItem item2 = new TreeItem(tree, SWT.NONE);
item2.setText("Name2");
If you are interested in the implementation of any UI element in Eclipse, then install the Eclipse SDK (via Help > Install New Software...) and use the plug-in spy. The spy tells you which class implements the UI element (in your case it's org.eclipse.jdt.internal.ui.preferences.JavaBuildPreferencePage in the org.eclipse.jdt.ui bundle). Since the SDK includes the source, you can jump right there from the spy's pop-up and look for yourself how it's done.
The problem was solved by using org.eclipse.ui.forms.widgets.ExpandableComposite.
See the example below. I hope this helps someone :).
protected final void createFieldEditors()
{
// Create the ScrolledComposite to scroll horizontally and vertically
fScrolledComposite = new ScrolledComposite(
getFieldEditorParent(),
SWT.H_SCROLL | SWT.V_SCROLL);
//Displays the scrollbars when the window gets smaller
fScrolledComposite.setAlwaysShowScrollBars(false);
// Sets the minimum size for the composite to work for scrolling
fScrolledComposite.setMinSize(fCompositeWidth, fCompositeHeight);
fScrolledComposite.setExpandHorizontal(true);
fScrolledComposite.setExpandVertical(true);
Composite composite = new Composite(fScrolledComposite, SWT.NONE);
composite.setLayout(new GridLayout());
fScrolledComposite.setContent(composite);
// Sets up the toolkit.
Display display = composite.getDisplay();
toolkit = new FormToolkit(display);
// Creates a form instance.
form = toolkit.createForm(composite);
form.getBody().setLayout(new GridLayout());
form.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
form.setText("Model: " + SignalGeneratorDevice.MODEL_ID);// Sets title of the Preference page
// Add the three main nodes to the preference page
addNode1();
}
/**
* Adds the first node to the preference page
*/
private void addNode1()
{
ExpandableComposite expandableComposite = createExpandableComposite(
"Signal Generator Device Host/Port:",
true);
Composite childComposite = createChildComposite(expandableComposite);
//Builds fields here (StringFieldEditor, IntegerFieldEditor, etc.)
..................
}
/**
* Creates an ExpandableComposite that will be added to the preference page
*
* #param label
* #param expanded
* #return
*/
private ExpandableComposite createExpandableComposite(
String label,
boolean expanded)
{
ExpandableComposite expandableComposite = null;
if (expanded) {
expandableComposite = toolkit.createExpandableComposite(
form.getBody(),
ExpandableComposite.TWISTIE | ExpandableComposite.CLIENT_INDENT
| ExpandableComposite.EXPANDED);
} else {
expandableComposite = toolkit
.createExpandableComposite(
form.getBody(),
ExpandableComposite.TWISTIE
| ExpandableComposite.CLIENT_INDENT);
}
expandableComposite.setText(label);
expandableComposite.setBackground(form.getBackground());
expandableComposite.addExpansionListener(new ExpansionAdapter() {
#Override
public void expansionStateChanged(
ExpansionEvent e)
{
form.pack();
}
});
GridData gd = new GridData();
expandableComposite.setLayoutData(gd);
return expandableComposite;
}
/**
* Creates a child composite for an ExpandableComposite
*
* #param expandableComposite
* #return
*/
private Composite createChildComposite(
ExpandableComposite expandableComposite)
{
Composite childComposite = new Composite(expandableComposite, SWT.None);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
//gd.horizontalAlignment = GridData.END;
childComposite.setLayoutData(gd);
expandableComposite.setClient(childComposite);
return childComposite;
}
As you can see from the images below the expand and collapse icons are gray, as is the row selection highlight. This causes you to not see the expand or collapse icon (Note: Not the folder icon) when the row is highlighted, I want to have a white expand or collapse icon for the row that is selected. How can that be done?
Something else that would also be cool is, to have the expand and collapse icons completely hidden until the JTree gains focus. like windows 7's tree.
Google says -according to this post: http://www.exampledepot.com/egs/javax.swing.tree/DefIcons.html - :
// Retrieve the three icons
Icon leafIcon = new ImageIcon("leaf.gif");
Icon openIcon = new ImageIcon("open.gif");
Icon closedIcon = new ImageIcon("closed.gif");
// Create tree
JTree tree = new JTree();
// Update only one tree instance
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
renderer.setLeafIcon(leafIcon);
renderer.setClosedIcon(closedIcon);
renderer.setOpenIcon(openIcon);
// Remove the icons
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);
// Change defaults so that all new tree components will have new icons
UIManager.put("Tree.leafIcon", leafIcon);
UIManager.put("Tree.openIcon", openIcon);
UIManager.put("Tree.closedIcon", closedIcon);
// Create tree with new icons
tree = new JTree();
// Update row height based on new icons;
Certainly, I'm not sure if you can modify only the color of the images on-the-go. But you can always create new icons, right?
You can try this. You should note however to get this to work, I had to override setUI on the tree to only allow my TreeUI.
private class IconTreeUI extends BasicTreeUI {
private Icon collapseIcon = null;
private Icon expandIcon = null;
#Override
public Icon getCollapsedIcon() {
if (collapseIcon == null) {
collapseIcon = new ImageIcon(yourCollapseImageHere);
}
return collapseIcon;
}
#Override
public Icon getExpandedIcon() {
if (expandIcon == null) {
expandIcon = new ImageIcon(yourExpandImageHere);
}
return expandIcon;
}}
Is it possible to align the icon for a tree menu item to the right instead of left?
Like this :
item1 >
item2 >
item3 >
Where the ">" is an image. I am using standard GWT. Tried with both CellTree and normal Tree.
/Andreas
Edit: Just realised this is a Google Web Toolkit question. I know nothing about Google Web Toolkit - but if they let you use normal CSS you can style any list in the way I describe below:
You can't do it with the standard list-style property, but you can definitely do it with background-image:
li {
list-style: none;
background-image: url('arrow.gif');
background-position: right center;
}
You can put any Widgets as treeItem into the Tree. So if you want to put an item inside the tree which has the icon left just create a horizonal panel, put your text and the icon inside and then put the horizontal panel into the tree... (I know this sounds a little bit complex but it is actually really simple and you can do alot of other cool stuf with it)
Tree t = new Tree();
HorizontalPanel hc = new HorizontalPanel();
hc.add(new Label("some text"));
hc.add(new Image("http://tueffel.net/images/icons/icon13.gif"));
TreeItem ti = new TreeItem(hc);
HorizontalPanel hc2 = new HorizontalPanel();
hc2.add(new Label("some text"));
hc2.add(new Image("http://tueffel.net/images/icons/icon13.gif"));
ti.addItem(new TreeItem(hc2));
t.addItem(ti);
RootPanel.get().add(t);
I have a CellTree with my own TreeModel which return a NodeInfo with custom renderer.
something like this:
viewModel = new MyTreeModel( dirs.getRootItems(), filter );
treeDrives = new CellTree( viewModel, null, treeResources );
public class MyTreeModel {
#Override
public <T> NodeInfo<?> getNodeInfo( final T value ) {
return new DefaultNodeInfo<Item>( new ListDataProvider<Item>( dirs ), new DirectoryCell(), selectionModel, null );
}
}
private final class DirectoryCell extends AbstractCell<Item> {
#Override
public void render( com.google.gwt.cell.client.Cell.Context context, Item value, SafeHtmlBuilder sb ) {
// my own html renderer ...
sb.appendHtmlConstant(value.toString);
sb.append"<img src=\"" + value.getImgUrl() + "\" //>");
}
new TreeItem(new HTML("Item Text <img src='path/to/image.png'>"));