How to show standard editor statusbar items for MultiPageEditor - eclipse

When I open a file in a TextEditor the status bar shows information like positions, whether the file is writable or not ...
Now I have created a MultiPageEditor that contains a class derived from org.eclipse.ui.editors.text.TextEditor. If I edit a file with this editor the status bar remains empty.
Is there an easy way make the standard status bar items visible if the TextEditor is embedded in a MultiPageEditor?
Im using Eclipse Kepler.

In your MultiPageEditorActionBarContributor class you need to do:
private StatusLineContributionItem _showLine;
...
_showLine = new StatusLineContributionItem(ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION, true, 14);
#Override
public void setActivePage(IEditorPart part)
{
if (part instanceof ITextEditorExtension)
{
ITextEditorExtension extension = (ITextEditorExtension)part;
extension.setStatusField(_showLine, ITextEditorActionConstants.STATUS_CATEGORY_INPUT_POSITION);
}
}
#Override
public void contributeToStatusLine(IStatusLineManager statusLineManager)
{
statusLineManager.add(_showLine);
}

Related

Eclipse: How to update View when file open/switch to new file

I am trying to update the view content when I switch or open a new file in the Eclipse project explorer. I tried using "IResourceChangeListener" but it is not triggered (only on changed on the file itself). I also looked looked at https://www.wideskills.com/eclipse-plugin-tutorial (specific on the view part), and Eclipse forums, without success. I thought it would be easy thing... Any Ideas ? small code example will be great !
After hours of searching (and based on #greg-449 comment) I was able to generate a small example that get notified when a new file is opened and is currently visible in the editor:
in the derived ViewPart class:
#Override
public void createPartControl(Composite parent) {
.
.
.
workbench.getPartService().addPartListenr(new IPartListener2() {
public void partOpened(IWorkbenchPartReference partRef) {
String part = partRef.toString();
showMessage("partOpened is " + part);
}
public void partVisible(IWorkbenchPartReference partRef) {
String part = partRef.toString();
showMessage("partVisible is " + part);
}
});
}

Create new NetBeans "save as" module

My goal is simple - save the current HTML file in the NetBeans editor with one additional line at the top and bottom of the file, and with the extension of ".h".
This is my first attempt at a NetBeans module, but following some tutorials and research, I got as far as adding an entry to the popup menu when you right-click on an HTML file in the editor. It currently just shows a "Hello World" message:
The code to do that is here:
package ksmiller99.savehtmlasarduinoresource;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
#ActionID(
category = "Edit",
id = "ksmiller99.savehtmlasarduinoresource.SaveHtmlAsArduinoResource"
)
#ActionRegistration(
displayName = "#CTL_SaveHtmlAsArduinoResource"
)
#ActionReference(path = "Editors/text/html/Popup")
#Messages("CTL_SaveHtmlAsArduinoResource=Save as Arduino Resource")
public final class SaveHtmlAsArduinoResource implements ActionListener {
#Override
public void actionPerformed(ActionEvent ev) {
//todo add a line to top and bottom of current file and save with .h extension
JOptionPane.showMessageDialog(null, "Hello Save As World");
}
}
How can I access the contents of the current editor? Would a different approach make more sense?
I'm using NetBeans 12.0, JDK 13, Windows 10.
Use the New Action wizard to create the source code for a Conditionally Enabled action, enabled when User Selects One Node.
In the 2nd wizard panel select File Type Context Menu and choose text/html as content type. If you want your action to appear only in the context menu you can disable Global Menu Item.
You should end up with code like this:
#ActionID(
category = "File",
id = "org.test.TestHtmlAction"
)
#ActionRegistration(
displayName = "#CTL_TestHtmlAction"
)
#ActionReference(path = "Loaders/text/html/Actions", position = 0)
#Messages("CTL_TestHtmlAction=TestHtmlAction")
public final class TestHtmlAction implements ActionListener
{
private final DataObject context;
private static final Logger LOGGER = Logger.getLogger(TestHtmlAction.class.getName());
public TestHtmlAction(DataObject context)
{
this.context = context;
}
#Override
public void actionPerformed(ActionEvent ev)
{
FileObject file = context.getPrimaryFile();
LOGGER.info("context=" + context.getName() + " file.getPath()=" + file.getPath());
}
}
The wizard creates a context aware action, which is enabled only when user selects a single HTML file node. The DataObject parameter gives you the context of the selected node, so you can retrieve the file path etc.

E4: drag an object from a TableViewer to Windows Explorer (or OS specific file system)

In my Eclipse RCP application I display some business data in a TableViewer.
I want the user to be able to drag a row from the table viewer and drop it on the windows desktop/explorer. Windows should then create a file with the data from the selected row that I could provide in the dragSetData(..) method of the DragSourceAdapter class.
How to implement this? It seems that using FileTransfer as the dragSourceSupport on the table viewer is the way to go as it trigger a call to the dragSetData() method. But what object should I create and assign to "event.data" in this method?
A working example would be appreciated.
I've implemented the reverse without problem, i.e. drag a file from windows explorer onto the TableViewer and add a row in the table. There are plenty on sample for this on the net but can't find a sample of the opposite, drag from eclipse to the OS
[edit + new requirement]
So I understand that I have to create a temporary file somewhere and set the name of that temp file in event.data in dragSetData()
Q: is there a simpler way to do that, eg set somewhere (iun data) the content of the file directly without the temp file?
There is another requirement. When the drop operation is about to occur, I want to show a popup to the user that will have to choose what "business data" from the "row" he wants to export and the name of the file that will be created. I tried the following (only asking for the filename for now) but it does not work as expected as the popup shows up as soon as the cursor reach the first pixel outside my app. I would like to show the popup just "before" the drop operation occurs.
Q: is there a way to have this popup show just before the drop operation occurs, ie when the user "release" the mouse button?
#Override
public void dragSetData(final DragSourceEvent event){
if (FileTransfer.getInstance().isSupportedType(event.dataType)) {
// Will be a more complex dialog with multiple fields..
InputDialog inputDialog = new InputDialog(shell, "Please enter a file name", "File Name:", "", null);
if (inputDialog.open() != Window.OK) {
event.doit = false;
return;
}
event.data = new String[] { inputDialog.getValue() };
}
}
The event.data for FileTransfer is an array of file path strings.
You DragSourceAdapter class might look something like:
public class MyDragSourceAdapter extends DragSourceAdapter
{
private final StructuredViewer viewer;
public MyDragSourceAdapter(final StructuredViewer viewer)
{
super();
this.viewer = viewer;
}
#Override
public void dragStart(final DragSourceEvent event)
{
IStructuredSelection selection = viewer.getStructuredSelection();
if (selection == null)
return;
// TODO check if the selection contains any files
// TODO set event.doit = false if not
}
#Override
public void dragSetData(final DragSourceEvent event)
{
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
IStructuredSelection selection = viewer.getStructuredSelection();
List<String> files = new ArrayList<>(selection.size());
// TODO add files in the selection to 'files'
event.data = files.toArray(new String [files.size()]);
}
}
and you install it on your viewer with:
MyDragSourceAdapter adapter = new MyDragSourceAdapter(viewer);
viewer.addDragSupport(DND.DROP_COPY, new Transfer [] {FileTransfer.getInstance()}, adapter);

how to use text editors in Eclipse Mars programmatically, using the compatiblity layer?

I am using the 3.x-4.x compatibility layer and I'm attempting to open and use editors. Our existing code looks like this in the class that implements IPartListener2:
public void partOpened(IWorkbenchPartReference partRef) {
if (partRef instanceof IEditorReference) {
//force editor area visible
partRef.getPage().setEditorAreaVisible(true);
// if the editors are currently minimimized and we try to maximize them, then we'll cause a bug.
if (partRef.getPage().getPartState(partRef) == IWorkbenchPage.STATE_MINIMIZED) {
return; // so exit here
}
//check preferences
final boolean maximized = EnterpriseManager.isMaximized();
if (maximized) {
partRef.getPage().setPartState(partRef, 1);
}
}
}
The problem is now whenever an editor is opened in Mars, it doesn't open maximized as a tab on the page layout as before, but in a much smaller window size.
Also, selecting the Restore Icon on an open editor's rule bar causes erratic positioning of the editor with one section of the editor in one part of the layout and other sections displaying in other areas of the page layout.
How am I to implement correct, predictable usage of editors with the compatibility layer?
Just guessing but it may be that Eclipse 4 does not like the setEditorAreaActive and setPartState methods being called while partOpened is being processed. You could try using Display.asyncExec to delay calling these until after the partOpened code has completed.
Something like:
public void partOpened(final IWorkbenchPartReference partRef) {
Display.getDefault().asyncExec(new Runnable()
{
#Override
public void run()
{
if (partRef instanceof IEditorReference) {
//force editor area visible
partRef.getPage().setEditorAreaVisible(true);
// if the editors are currently minimimized and we try to maximize them, then we'll cause a bug.
if (partRef.getPage().getPartState(partRef) == IWorkbenchPage.STATE_MINIMIZED) {
return; // so exit here
}
//check preferences
final boolean maximized = EnterpriseManager.isMaximized();
if (maximized) {
partRef.getPage().setPartState(partRef, IWorkbenchPage.STATE_MAXIMIZED);
}
}
}
});
}

Handling drag and drop of files inside Eclipse Package Explorer

I'm trying to create an Eclipse plugin to support a proprietary project file format. My goal is to be able to drag and drop a file in the Project Explorer (any type of file) onto a file of the type I support, and have the name of the file being dragged appended to the end of the proprietary file.
Right now, I have a custom editor that can parse out some data from an existing file in a manageable way. This means that I have an editor associated with the file type, such that my special icon shows up next to it. I don't know if that's relevant.
I'm attempting to use the extension point "org.eclipse.ui.dropActions" but I'm not sure how to register my DropActionDelegate (implements org.eclipse.ui.part.IDropActionDelegate) such that it will be called when a file is dropped onto one of my type within the Project Explorer.
Anybody have any ideas? Am I even on the right track with the DropActionDelegate?
You are on the right track implementing an IDropActionDelegate:
class DropActionDelegate implements IDropActionDelegate {
#Override
public boolean run(Object source, Object target) {
String transferredData (String) target; // whatever type is needed
return true; // if drop successful
}
}
The purpose of the extension point org.eclipse.ui.dropActions is to provide drop behaviour to views which you don't have defined yourself (like the Project Explorer).
You register the drop action extension like this:
<extension point="org.eclipse.ui.dropActions">
<action
id="my_drop_action"
class="com.xyz.DropActionDelegate">
</action>
</extension>
Don't forget to attach an adequate listener to your editor in your plugin code:
class DragListener implements DragSourceListener {
#Override
public void dragStart(DragSourceEvent event) {
}
#Override
public void dragSetData(DragSourceEvent event) {
PluginTransferData p;
p = new PluginTransferData(
"my_drop_action", // must be id of registered drop action
"some_data" // may be of arbitrary type
);
event.data = p;
}
#Override
public void dragFinished(DragSourceEvent event) {
}
}