I am working on an eclipse Plugin, and I would like to use an Editor, set some listeners on the current page(good terminology?), and remove these listeners when the user switches on another page (basically, the user is editing several files, as you could do with the default JAVA editor).
For the moment I have written a class extending StructuredTextEditor. The behavior of the plugin was the one expected, but when I try to work on several files, many problems occur. The main problem, according to me, is that I am not able to get notified when the user opens another page.
I read (and tested) a few things about MultiPageEditor, but it seems like it doesn't integrate an XML editor as default editor. How should I proceed in order to get a MultiPageEditor, with XML syntax coloring, and get notified when the user changes the current page to adjust my listeners ?
Thanks for reading.
the code is not perfect but at least you will have an example of a MultiPageEditor integrating an XMLEditor: https://github.com/fusesource/fuseide/blob/8.0.0.Beta2/editor/plugins/org.fusesource.ide.camel.editor/src/org/fusesource/ide/camel/editor/CamelEditor.java
The idea is to call addPage(new StructuredTextEditor()) inside createPages() method.
regards,
In your editor you can listen to selection changes in the editor text using:
getSelectionProvider().addSelectionChangedListener(listener);
where listener implements ISelectionChangedListener.
This applies to any editor derived from AbstractTextEditor (which includes StructuredTextEditor.
You need to do this fairly late in the editor creation. In the createPartControl method works:
#Override
public void createPartControl(final Composite parent)
{
super.createPartControl(parent);
getSelectionProvider().addSelectionChangedListener(listener);
}
Related
I am working on an eclipse-plugin which makes use of a custom XMLMultiPageEditor by extending XMLMultiPageEditorPart and XMLTableTreeView by extending XMLTableTreeViewer the second one as well implements a IResourceChangeListener.
I provide this plugin in eclipse-mars and eclipse-neon, however the wired behavior only shows up in eclipse-neon.
The editor works fine so far, the only problem is, that the tree-view is completely blank when a related xml-document is opened with it:
I can even modify the document in the source-view and save it .. the tree-view stays blank.
The only events which bring the tree-view to life are:
re-size the editor-window with the mouse
open some other document and than switch back to the tab of the open xml-doc.
After that, the tree-view works like it should. If I now change the xml in the source-view, the tree-view is updated immediately.
Any ideas how I could trigger a refresh of the tree-view manually ?
Is this maybe a bug in eclipse-neon ?
Ok, the problem seems to be, that the x/y size of the TreeViewer in eclipse-neon is initialized with (0,0). Here a hack to fix that:
...
public class MyXMLTableTreeViewer extends XMLTableTreeViewer implements IResourceChangeListener{
....
public MyXMLTableTreeViewer(Composite parent, IEditorPart parentEditor)
{
super(parent);
....
Point size = getControl().getSize();
size.x = 1000;
getControl().setSize(size);
}
It seems to be sufficient to only set x to some value ... 10 already gives an image, but the "node" section than looks a bit pressed, so I picket 1000.
Even if it is just internal API, it looks like a bug for me. So if you are further interested, take a look to the eclipse bugreport.
Both XMLMultiPageEditorPart and XMLTableTreeViewer are in internal packages. This means that they are not part of the Eclipse APIs and can be changed by the Eclipse developers without warning (see Eclipse API Rules of Engagement).
It may well be the case that something was changed between Eclipse Mars and Neon. You cannot rely on internal classes working the same between releases. It is not an Eclipse bug because you are not using official APIs.
I'd prefer it from Property editor -> Customize Code.
I need to select a folder in one instance and a file in another.
The function will be: On pressing jButton1, jFileChooser1 opens and on confirmation of selection, returns the folder/file path selected to a String strFolderPath/strFilePath.
Thanks in advance for any help.
"I'd prefer it from Property editor -> Customize Code."
No you don't. That's editing the auo-generated code. Yo don't want/need that. The auto-generated code is just for initializing the component and laying them out. It's not meant to be altered (unless you really know what you're doing)
What you want is to add a listener to the button, and write your code for choosing a file in the listener callback. In Netbeans editor you can simply:
Right click on the button from the design view and select Events->Action->actionPerformed
If you go to the source code view, you will see something like
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
Inside that method is where out write your code for the file chooser. If you need help with that, I suuggest you take a look at How to use File Choosers. A very simple code example can also be found at the JFileChooser api javadoc
As an aside, these are pretty basic Swing use cases. I would strongly urge you to put down the GUI editor and learn to hand code first. It will make using the editor as tool much easier once you understand the code behind it. If you do want to follow this advice, keep handy the Swing tutorials and slowly go through it at your own pace. There's a lot to take in, but no on can become a ninja overnight, you need to be a grasshopper first :-)
I have an existing project for which i have decided to create a GUI for in Netbeans. The problem I am encountering is the fact that every component that i drag-and-drop is private in the source and is unmodifiable. Must i create getters for everything?
I mainly just need this problem resolved for appending to the TextArea.
Thank you in advance
If you want to change that globally, go to the options dialog, then select miscellaneous, and pick the gui builder tab. You can configure the default modifier there.
By right clicking on a component in the Inspector panel, you can influence the generated code, even though it is in an editor-fold and not directly editable. For example, right click on a JList and edit the Properties > model to add text entries; right click on Code > Post Creation Code to add a code snippet affecting the selection model:
itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
Examine the code in the editor-fold to see the generated changes.
See also Introduction to GUI Building.
You should see some tags in the code, something to do with begin and end of variable area. Usually there are 2 different sets of tags, any code between those tags will regenerate when you modify the gui with the form builder.
You can write your own code outside of those tags and it should remain even after you make changes. Getters and setters are a good idea if you need to update your object from another class. I've done that before with some text areas where I had a utility class update the text in it.
First of all I'm using netbeans as my IDE and I don't know if this is causing it. When I run my program (even if I have build it and run the .jar) I think it selects the tab that was previously selected (before quiting). So if for example I close the app with the third tab selected, it starts up with that selected again. Is there a known solution for this? The selectedIndex property on the jTabbedPane is set to 0. Shouldn't this property be the default onLoad value?
Thx in advance, Jimmy
PS. BTW for some reason it didn't submit my question in Opera (?)
tabbedPaneName.setSelectedIndex(0);
just put that line in the place where the tabbed pane would be loaded
if a button actuion will load the tabbed pane then put the line there
but change tabbedPaneName to YOUR tabbed pane name.
Same problem here with Netbeans 6.8 and JTabbedPane. Neither setSelectedIndex() nor setSelectedComponent() makes a difference. The getSelectedIndex() returns the value previously set, but the pane is not selected correctly.
The reason for this is that the SingleFrameApplication saves it's state and restores the saved state on the next restart. This is done in the code generated by the GUI builder.
You could see that startup() and configureWindow() methods of the SingleFrameApplication are overridden.
Workarounds:
You could override the shutdown() method as well, then modifications to the configuration will not be saved. Note that the original will still be restored, so ensure that the required configuration is saved.
Modifying the startup() method also helps:
MyView myView = new MyView(this);
myView.getFrame().setVisible(true);
myView.getFrame().pack();
The only way it can be set to an index other than zero is if the Java code contains:
tabbedPane.setSelectedIndex(...);
So search the source code for that line and fix it.
Besides using JTabbedPane.setSelectedIndex(), it's also possible to select a tab by calling JTabbedPane.setSelectedComponent(). Have you searched the code for setSelectedComponent() as well?
I had the same problem and found an easy workaround.
In netbean's GUI-builder I set my tabbedpane to not enabled. Later in my program I checked if it was not enabled and in that case called MyTabbedPane.setEnabled(true); and MyTabbedPane.setSelectedIndex(0);
Same problem. Had to go back to NetBeans 7.0.1 to update a JSR 296 application and Java 7 runs it differently than previous versions did so the last tab created was always the one that had focus. Couldn't get anything to change that in the constructor, but finally found just wrapping the same call (setSelectedIndex()) in a call to invokeLater() solves it.
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
tabMain.setSelectedIndex(0);
}
}
);
I am currently working on a project in which I have a Java GUI class and another class which contains its relevant methods.
I want a text area in the GUI to be updated with the content of a string in the other class whenever it changes. What is the easiest way to watch for these changes?
Cheers!
You're looking for data binding. Java unfortunately has no own support for that, but there are several libraries to choose from, like for example JGoodies data binding.
If you want to roll your own, there's the ubiquitous observer pattern which you doubtless already know from Swing :). Just add listener support to the class holding the strings and add a listener to it that updates the text area, when an event comes.
Make the "other class" a proper bean that supports PropertyChangeListeners. Then create a PropertyChangeLister which acts on changes in the "other class" and which updates the textarea.
Something like this:
otherClass.addPropertyChangeListener("propertyname", new PropertyChangeListener() {
void propertyChange(PropertyChangeEvent evt) {
textarea.setText(evt.getNewValue());
}
}
See
https://docs.oracle.com/javase/1.5.0/docs/api/java/beans/PropertyChangeListener.html
https://docs.oracle.com/javase/1.5.0/docs/api/java/beans/PropertyChangeSupport.html
http://java.sun.com/docs/books/tutorial/javabeans/properties/bound.html
Have a look at BeansBinding
It does almost exactly what you need. Only thing is that your otherClass must support Java Beans listeners, as described by #ordnungswidrig