Eclipse RCP: Open Split Screen Editor - java

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.

Related

Open JFrame on same screen(monitor) as JavaFX' 8 dialog

I am working on application that has a usual login window (JavaFX' 8 dialog), and then on accept it opens new window (JFrame). If it runs in multi-monitor environment, it should work the way, that JFrame would be opened on the same monitor where JavaFX' 8 dialog was closed.
Right now I could open JFrame on monitor that I can choose manually using
GraphicsEnvironment, but at this point I can't figure out on which monitor JavaFX' 8 dialog was closed. Any ideas? Thank you.
Maybe you could save your Position when you close your JavaFX dialog:
private void saveWindowPosition() {
Preferences userPrefs = Preferences.userNodeForPackage(getClass());
userPrefs.putDouble("stage.x", primaryStage.getX());
userPrefs.putDouble("stage.y", primaryStage.getY());
}
and read it out like this:
private void restoreWindowPosition() {
Preferences userPrefs = Preferences.userNodeForPackage(getClass());
// get window location from user preferences: use x=100, y=100 as
// default
double x = userPrefs.getDouble("stage.x", 100);
double y = userPrefs.getDouble("stage.y", 100);
primaryStage.setX(x);
primaryStage.setY(y);
}
Well, here is what worked for me:
/**
* As an argument you pass a Scene(of Java FX 8 Dialog in my example),
* by closing it you want JFrame to open on same monitor.
**/
public static int getActiveStageLocation(Scene scene){
List interScreens = Screen.getScreensForRectangle(scene.getWindow().getX(),
scene.getWindow().getY(),
scene.getWindow().getWidth(),
scene.getWindow().getHeight());
Screen activeScreen = (Screen) interScreens.get(0);
Rectangle2D r = activeScreen.getBounds();
double position = r.getMinX();
return (int) position;
}
Then you simple set location of your JFrame:
/**
* Some code for instantiating frame and so on....
* Parameter scene you could get for example from your FXML
**/
myFrame.setLocation(getActiveStageLocation(scene), myFrame.getY());
myFrame.setVisible(true);

How to strikethrough selected text in JTextPane? (java)

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.

How can i change the information icon in progressbardialog?

I want a dialog box where i can show the progressbar like eclipse progressbar.But i donot want the information icon.In that place I want to place a widget .So i am extending progressbardialogclass. But My query is how can i remove the icon.
package viewerdemo;
public class Imagesequencerdialog extends ProgressMonitorDialog {
/**
* This is a label for the warning image
*/
private Label label;
/**
* This a reference of link class to provide the link to go to hex file
*/
private Link link;
/**
* This is the warning message which will be displayed in dialog
*/
private String warning_msg;
/**This is the path to the hex files
*
*/
private String linkpath;
/**
* This is a constructor which initializes warning message of the dialog and path of the hexfile
* #param shell
* #param warning_msg
* #param linkpath
*/
protected Imagesequencerdialog(Shell shell) {
super(shell);
// TODO Auto-generated constructor stub
}
/**
* This will create the dialog for the warning message
*
* #param parent
* #return Control
*/
protected org.eclipse.swt.widgets.Control createDialogArea(org.eclipse.swt.widgets.Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
GridLayout gl_container = new GridLayout(2, false);
gl_container.verticalSpacing = 0;
gl_container.marginRight = 5;
gl_container.marginLeft = 10;
container.setLayout(gl_container);
ImageSequencer imageSequencer=new ImageSequencer(container, SWT.NONE, new Image[]{
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_1),
SampleToolBoxImageRegistry.getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_2),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_3),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_4),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_5),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_6),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_7),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_8),
SampleToolBoxImageRegistry.getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_9),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_10),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_11),
SampleToolBoxImageRegistry. getImage(SampleToolBoxImageRegistry.IMG_INDICATOR_D_12),
},
150,true);
imageSequencer.setLayoutData(new GridData(SWT.BEGINNING,SWT.BEGINNING,false,false));
Label label=new Label(container, SWT.NONE);
label.setText("Scanning..");
return parent;
}
/**
* This method creates the ok button in the dialog
* #param parent
*/
/**
* This method sets the title of the dialog
* #param shell
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setText("Initialization Status"); //$NON-NLS-1$
}
/** * Return the initial size of the dialog.
*#return Point :size of the dialog
*
*
*/
#Override
protected Point getInitialSize() {
return new Point(450, 140);
}
}
You need to override configureShell method. Then you can remove the image.

Eclipse preference page

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;
}

Moving focus from JTextArea using tab key

As stated, I want to change the default TAB behaviour within a JTextArea (so that it acts like a JTextField or similar component)
Here's the event action
private void diagInputKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.KEY_PRESSED == java.awt.event.KeyEvent.VK_TAB) {
actionInput.transferFocus();
}
}
And here's the listener
diagInput.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
diagInputKeyPressed(evt);
}
});
I've tried evt.KEY_TYPED as well with no joy.
Any ideas?
quick edit: I've also tried requestFocus() in place of transferFocus()
According to this class:
/**
* Some components treat tabulator (TAB key) in their own way.
* Sometimes the tabulator is supposed to simply transfer the focus
* to the next focusable component.
* <br/>
* Here s how to use this class to override the "component's default"
* behavior:
* <pre>
* JTextArea area = new JTextArea(..);
* <b>TransferFocus.patch( area );</b>
* </pre>
* This should do the trick.
* This time the KeyStrokes are used.
* More elegant solution than TabTransfersFocus().
*
* #author kaimu
* #since 2006-05-14
* #version 1.0
*/
public class TransferFocus {
/**
* Patch the behaviour of a component.
* TAB transfers focus to the next focusable component,
* SHIFT+TAB transfers focus to the previous focusable component.
*
* #param c The component to be patched.
*/
public static void patch(Component c) {
Set<KeyStroke>
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);
}
}
Note that patch() can be even shorter, according to Joshua Goldberg in the comments, since the goal is to get back default behaviors overridden by JTextArea:
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERS‌​AL_KEYS, null);
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERS‌​AL_KEYS, null);
This is used in question "How can I modify the behavior of the tab key in a JTextArea?"
The previous implementation involved indeed a Listener, and the a transferFocus():
/**
* Override the behaviour so that TAB key transfers the focus
* to the next focusable component.
*/
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_TAB) {
System.out.println(e.getModifiers());
if(e.getModifiers() > 0) a.transferFocusBackward();
else a.transferFocus();
e.consume();
}
}
e.consume(); might have been what you missed to make it work in your case.

Categories