无法创建具有多页面编辑器的插件?

发布于 2024-11-28 00:24:42 字数 308 浏览 0 评论 0原文

我想使用多页编辑器(eclipse RCP)。我想遵循这个 教程 但当我创建新项目时,我无法获得“带有多页面编辑器的插件”。我只有: 你好 有风景 有介绍 邮件模板

有谁知道如何在创建新的 RCP 项目时使用多页面编辑器获取选项插件吗? 谢谢

PS:我使用 Galileo 3.5.2

I would like to use multiple-page editor (eclipse RCP). I want to follow this tutorial
but I cannot get "plug-in with multiple page editor" when I create a new project. I have only :
Hello
with a view
with an introduction
mail template

Does anyone have an idea about how to get the option plug-in with multiple page editor when creating a new RCP project?
Thnx

PS: I use Galileo 3.5.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-12-05 00:24:42

请改用 Eclipse Indigo。

否则,您可以从空插件项目创建。

这是我的多页面编辑器的示例。 PropertyFileEditor 是多页编辑器。希望这会对您有所帮助。

FileDocumentProvider.java

package com.bosch.training.eclipseplugin.editors;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.bosch.training.eclipseplugin.LinkedProperties;

public class PropertyFileEditor extends MultiPageEditorPart {

    public static String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.PropertyFileEditor";

    private Text m_keyText;
    private Text m_valueText;
    private TableViewer m_tableViewer;
    private IPath m_filePath;

    private Properties m_properties;

    private FileEditor m_firstPage;

    public PropertyFileEditor() {
    }

    @Override
    protected void createPages() {
        try {
            m_filePath = ((FileEditorInput) getEditorInput()).getFilePath();
            m_firstPage = new FileEditor();
            addPage(m_firstPage, (FileEditorInput) getEditorInput());
            addPage(createDesignPage());
            setPagesText();
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }

    private void setPagesText() {
        setPageText(0, "Plain Text");
        setPageText(1, "Properties");
    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        m_firstPage.doSave(monitor);
    }

    @Override
    public void doSaveAs() {

    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

    private Control createDesignPage() {
        Composite parent = new Composite(getContainer(), SWT.NONE);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(1, false));

        // First row
        Composite composite1 = new Composite(parent, SWT.NONE);
        composite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        composite1.setLayout(new GridLayout(3, false));

        m_keyText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_keyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_keyText.setText("");

        m_valueText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_valueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_valueText.setText("");

        Button applyButton = new Button(composite1, SWT.PUSH);
        applyButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
        applyButton.setText("Apply");
        applyButton.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                m_properties.put(m_keyText.getText(), m_valueText.getText());
                // Update table
                TableItem tableItem= new TableItem(m_tableViewer.getTable(), SWT.NONE);
                tableItem.setText(new String[] { m_keyText.getText(), m_valueText.getText() });
                // Update editor
                IDocument doc = m_firstPage.getDocumentProvider().getDocument(getEditorInput());
                int offset;
                try {
                    offset = doc.getLineOffset(doc.getNumberOfLines() - 1);
                    doc.replace(offset, 0, m_keyText.getText() + "=" + m_valueText.getText() + "\n");
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                // set text = ""
                m_keyText.setText("");
                m_valueText.setText("");
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        // Second row
        Composite composite2 = new Composite(parent, SWT.NONE);
        composite2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite2.setLayout(new GridLayout(1, false));

        m_tableViewer = new TableViewer(composite2, SWT.FILL);
        Table table = m_tableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        TableColumn columnKey = new TableColumn(table, SWT.LEAD);
        columnKey.setText("Key");
        columnKey.setWidth(300);
        TableColumn columnValue = new TableColumn(table, SWT.FILL);
        columnValue.setText("Value");
        columnValue.setWidth(300);

        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        m_tableViewer.setContentProvider(new IStructuredContentProvider() {
            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

            @Override
            public void dispose() {
            }

            @Override
            public Object[] getElements(Object inputElement) {
                return (Object[]) inputElement;
            }
        });
        m_tableViewer.setLabelProvider(new ITableLabelProvider() {
            @Override
            public void removeListener(ILabelProviderListener listener) {
            }

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            @Override
            public void dispose() {
            }

            @Override
            public void addListener(ILabelProviderListener listener) {
            }

            @Override
            public String getColumnText(Object element, int columnIndex) {
                if (element instanceof Entry) {
                    switch (columnIndex) {
                    case 0:
                        return String.valueOf(((Entry) element).getKey());
                    case 1:
                        return String.valueOf(((Entry) element).getValue());
                    }
                }
                return "";
            }

            @Override
            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }
        });

        m_tableViewer.setInput(loadProperties());

        m_firstPage.getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(new IDocumentListener() {

            @Override
            public void documentChanged(DocumentEvent event) {
                m_tableViewer.setInput(loadProperties());
            }

            @Override
            public void documentAboutToBeChanged(DocumentEvent event) {
            }
        });

        return parent;
    }

    private Object[] loadProperties() {
        IDocument document = m_firstPage.getFileDocumentProvider().getDocument(getEditorInput());
        m_properties = new LinkedProperties();
        ByteArrayInputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(document.get().getBytes());
            m_properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return m_properties.entrySet().toArray();
    }

}

FileEditor.java

package com.bosch.training.eclipseplugin.editors;

import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension3;

public class FileEditor extends AbstractTextEditor {

    public static final String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.FileEditor";

    private FileDocumentProvider m_fileDocumentProvider;

    public FileEditor() {
        setKeyBindingScopes(new String[] { "org.eclipse.ui.textEditorScope" });
        internal_init();
    }

    protected void internal_init() {
        configureInsertMode(ITextEditorExtension3.SMART_INSERT, false);
        m_fileDocumentProvider = new FileDocumentProvider();
        setDocumentProvider(m_fileDocumentProvider);
    }

    public FileDocumentProvider getFileDocumentProvider() {
        return m_fileDocumentProvider;
    }

}

FileEditorInput.java

package com.bosch.training.eclipseplugin.editors;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PlatformUI;

public class FileEditorInput implements IPathEditorInput {

    private IPath m_filePath;

    public FileEditorInput(IPath path) {
        if (path == null) {
            throw new IllegalArgumentException();
        }
        this.m_filePath = path;
    }

    @Override
    public int hashCode() {
        return m_filePath.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof FileEditorInput)) {
            return false;
        }
        FileEditorInput other = (FileEditorInput) obj;
        return m_filePath.equals(other.m_filePath);
    }

    @Override
    public boolean exists() {
        return m_filePath.toFile().exists();
    }

    @Override
    public ImageDescriptor getImageDescriptor() {
        return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(m_filePath.toString());
    }

    @Override
    public String getName() {
        return m_filePath.toString();
    }

    @Override
    public String getToolTipText() {
        return m_filePath.makeRelative().toOSString();
    }

    @Override
    public IPath getPath() {
        return m_filePath;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Object getAdapter(Class adapter) {
        return null;
    }

    @Override
    public IPersistableElement getPersistable() {
        // no persistence
        return null;
    }

    public IPath getFilePath() {
        return m_filePath;
    }

    public void setFilePath(IPath filePath) {
        m_filePath = filePath;
    }

}

PropertyFileEditor.java

package com.bosch.training.eclipseplugin.editors;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.bosch.training.eclipseplugin.LinkedProperties;

public class PropertyFileEditor extends MultiPageEditorPart {

    public static String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.PropertyFileEditor";

    private Text m_keyText;
    private Text m_valueText;
    private TableViewer m_tableViewer;
    private IPath m_filePath;

    private Properties m_properties;

    private FileEditor m_firstPage;

    public PropertyFileEditor() {
    }

    @Override
    protected void createPages() {
        try {
            m_filePath = ((FileEditorInput) getEditorInput()).getFilePath();
            m_firstPage = new FileEditor();
            addPage(m_firstPage, (FileEditorInput) getEditorInput());
            addPage(createDesignPage());
            setPagesText();
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }

    private void setPagesText() {
        setPageText(0, "Plain Text");
        setPageText(1, "Properties");
    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        m_firstPage.doSave(monitor);
    }

    @Override
    public void doSaveAs() {

    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

    private Control createDesignPage() {
        Composite parent = new Composite(getContainer(), SWT.NONE);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(1, false));

        // First row
        Composite composite1 = new Composite(parent, SWT.NONE);
        composite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        composite1.setLayout(new GridLayout(3, false));

        m_keyText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_keyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_keyText.setText("");

        m_valueText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_valueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_valueText.setText("");

        Button applyButton = new Button(composite1, SWT.PUSH);
        applyButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
        applyButton.setText("Apply");
        applyButton.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                m_properties.put(m_keyText.getText(), m_valueText.getText());
                // Update table
                TableItem tableItem= new TableItem(m_tableViewer.getTable(), SWT.NONE);
                tableItem.setText(new String[] { m_keyText.getText(), m_valueText.getText() });
                // Update editor
                IDocument doc = m_firstPage.getDocumentProvider().getDocument(getEditorInput());
                int offset;
                try {
                    offset = doc.getLineOffset(doc.getNumberOfLines() - 1);
                    doc.replace(offset, 0, m_keyText.getText() + "=" + m_valueText.getText() + "\n");
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                // set text = ""
                m_keyText.setText("");
                m_valueText.setText("");
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        // Second row
        Composite composite2 = new Composite(parent, SWT.NONE);
        composite2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite2.setLayout(new GridLayout(1, false));

        m_tableViewer = new TableViewer(composite2, SWT.FILL);
        Table table = m_tableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        TableColumn columnKey = new TableColumn(table, SWT.LEAD);
        columnKey.setText("Key");
        columnKey.setWidth(300);
        TableColumn columnValue = new TableColumn(table, SWT.FILL);
        columnValue.setText("Value");
        columnValue.setWidth(300);

        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        m_tableViewer.setContentProvider(new IStructuredContentProvider() {
            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

            @Override
            public void dispose() {
            }

            @Override
            public Object[] getElements(Object inputElement) {
                return (Object[]) inputElement;
            }
        });
        m_tableViewer.setLabelProvider(new ITableLabelProvider() {
            @Override
            public void removeListener(ILabelProviderListener listener) {
            }

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            @Override
            public void dispose() {
            }

            @Override
            public void addListener(ILabelProviderListener listener) {
            }

            @Override
            public String getColumnText(Object element, int columnIndex) {
                if (element instanceof Entry) {
                    switch (columnIndex) {
                    case 0:
                        return String.valueOf(((Entry) element).getKey());
                    case 1:
                        return String.valueOf(((Entry) element).getValue());
                    }
                }
                return "";
            }

            @Override
            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }
        });

        m_tableViewer.setInput(loadProperties());

        m_firstPage.getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(new IDocumentListener() {

            @Override
            public void documentChanged(DocumentEvent event) {
                m_tableViewer.setInput(loadProperties());
            }

            @Override
            public void documentAboutToBeChanged(DocumentEvent event) {
            }
        });

        return parent;
    }

    private Object[] loadProperties() {
        IDocument document = m_firstPage.getFileDocumentProvider().getDocument(getEditorInput());
        m_properties = new LinkedProperties();
        ByteArrayInputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(document.get().getBytes());
            m_properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return m_properties.entrySet().toArray();
    }

}

Please use the Eclipse Indigo instead.

Otherwise, you can create from the empty plugin project.

Here is my example of multiple pages editor. PropertyFileEditor is multiple pages editor. Hope this will help you.

FileDocumentProvider.java

package com.bosch.training.eclipseplugin.editors;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.bosch.training.eclipseplugin.LinkedProperties;

public class PropertyFileEditor extends MultiPageEditorPart {

    public static String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.PropertyFileEditor";

    private Text m_keyText;
    private Text m_valueText;
    private TableViewer m_tableViewer;
    private IPath m_filePath;

    private Properties m_properties;

    private FileEditor m_firstPage;

    public PropertyFileEditor() {
    }

    @Override
    protected void createPages() {
        try {
            m_filePath = ((FileEditorInput) getEditorInput()).getFilePath();
            m_firstPage = new FileEditor();
            addPage(m_firstPage, (FileEditorInput) getEditorInput());
            addPage(createDesignPage());
            setPagesText();
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }

    private void setPagesText() {
        setPageText(0, "Plain Text");
        setPageText(1, "Properties");
    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        m_firstPage.doSave(monitor);
    }

    @Override
    public void doSaveAs() {

    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

    private Control createDesignPage() {
        Composite parent = new Composite(getContainer(), SWT.NONE);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(1, false));

        // First row
        Composite composite1 = new Composite(parent, SWT.NONE);
        composite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        composite1.setLayout(new GridLayout(3, false));

        m_keyText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_keyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_keyText.setText("");

        m_valueText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_valueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_valueText.setText("");

        Button applyButton = new Button(composite1, SWT.PUSH);
        applyButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
        applyButton.setText("Apply");
        applyButton.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                m_properties.put(m_keyText.getText(), m_valueText.getText());
                // Update table
                TableItem tableItem= new TableItem(m_tableViewer.getTable(), SWT.NONE);
                tableItem.setText(new String[] { m_keyText.getText(), m_valueText.getText() });
                // Update editor
                IDocument doc = m_firstPage.getDocumentProvider().getDocument(getEditorInput());
                int offset;
                try {
                    offset = doc.getLineOffset(doc.getNumberOfLines() - 1);
                    doc.replace(offset, 0, m_keyText.getText() + "=" + m_valueText.getText() + "\n");
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                // set text = ""
                m_keyText.setText("");
                m_valueText.setText("");
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        // Second row
        Composite composite2 = new Composite(parent, SWT.NONE);
        composite2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite2.setLayout(new GridLayout(1, false));

        m_tableViewer = new TableViewer(composite2, SWT.FILL);
        Table table = m_tableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        TableColumn columnKey = new TableColumn(table, SWT.LEAD);
        columnKey.setText("Key");
        columnKey.setWidth(300);
        TableColumn columnValue = new TableColumn(table, SWT.FILL);
        columnValue.setText("Value");
        columnValue.setWidth(300);

        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        m_tableViewer.setContentProvider(new IStructuredContentProvider() {
            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

            @Override
            public void dispose() {
            }

            @Override
            public Object[] getElements(Object inputElement) {
                return (Object[]) inputElement;
            }
        });
        m_tableViewer.setLabelProvider(new ITableLabelProvider() {
            @Override
            public void removeListener(ILabelProviderListener listener) {
            }

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            @Override
            public void dispose() {
            }

            @Override
            public void addListener(ILabelProviderListener listener) {
            }

            @Override
            public String getColumnText(Object element, int columnIndex) {
                if (element instanceof Entry) {
                    switch (columnIndex) {
                    case 0:
                        return String.valueOf(((Entry) element).getKey());
                    case 1:
                        return String.valueOf(((Entry) element).getValue());
                    }
                }
                return "";
            }

            @Override
            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }
        });

        m_tableViewer.setInput(loadProperties());

        m_firstPage.getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(new IDocumentListener() {

            @Override
            public void documentChanged(DocumentEvent event) {
                m_tableViewer.setInput(loadProperties());
            }

            @Override
            public void documentAboutToBeChanged(DocumentEvent event) {
            }
        });

        return parent;
    }

    private Object[] loadProperties() {
        IDocument document = m_firstPage.getFileDocumentProvider().getDocument(getEditorInput());
        m_properties = new LinkedProperties();
        ByteArrayInputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(document.get().getBytes());
            m_properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return m_properties.entrySet().toArray();
    }

}

FileEditor.java

package com.bosch.training.eclipseplugin.editors;

import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension3;

public class FileEditor extends AbstractTextEditor {

    public static final String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.FileEditor";

    private FileDocumentProvider m_fileDocumentProvider;

    public FileEditor() {
        setKeyBindingScopes(new String[] { "org.eclipse.ui.textEditorScope" });
        internal_init();
    }

    protected void internal_init() {
        configureInsertMode(ITextEditorExtension3.SMART_INSERT, false);
        m_fileDocumentProvider = new FileDocumentProvider();
        setDocumentProvider(m_fileDocumentProvider);
    }

    public FileDocumentProvider getFileDocumentProvider() {
        return m_fileDocumentProvider;
    }

}

FileEditorInput.java

package com.bosch.training.eclipseplugin.editors;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.PlatformUI;

public class FileEditorInput implements IPathEditorInput {

    private IPath m_filePath;

    public FileEditorInput(IPath path) {
        if (path == null) {
            throw new IllegalArgumentException();
        }
        this.m_filePath = path;
    }

    @Override
    public int hashCode() {
        return m_filePath.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof FileEditorInput)) {
            return false;
        }
        FileEditorInput other = (FileEditorInput) obj;
        return m_filePath.equals(other.m_filePath);
    }

    @Override
    public boolean exists() {
        return m_filePath.toFile().exists();
    }

    @Override
    public ImageDescriptor getImageDescriptor() {
        return PlatformUI.getWorkbench().getEditorRegistry().getImageDescriptor(m_filePath.toString());
    }

    @Override
    public String getName() {
        return m_filePath.toString();
    }

    @Override
    public String getToolTipText() {
        return m_filePath.makeRelative().toOSString();
    }

    @Override
    public IPath getPath() {
        return m_filePath;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Object getAdapter(Class adapter) {
        return null;
    }

    @Override
    public IPersistableElement getPersistable() {
        // no persistence
        return null;
    }

    public IPath getFilePath() {
        return m_filePath;
    }

    public void setFilePath(IPath filePath) {
        m_filePath = filePath;
    }

}

PropertyFileEditor.java

package com.bosch.training.eclipseplugin.editors;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.bosch.training.eclipseplugin.LinkedProperties;

public class PropertyFileEditor extends MultiPageEditorPart {

    public static String EDITOR_ID = "com.bosch.training.eclipseplugin.editors.PropertyFileEditor";

    private Text m_keyText;
    private Text m_valueText;
    private TableViewer m_tableViewer;
    private IPath m_filePath;

    private Properties m_properties;

    private FileEditor m_firstPage;

    public PropertyFileEditor() {
    }

    @Override
    protected void createPages() {
        try {
            m_filePath = ((FileEditorInput) getEditorInput()).getFilePath();
            m_firstPage = new FileEditor();
            addPage(m_firstPage, (FileEditorInput) getEditorInput());
            addPage(createDesignPage());
            setPagesText();
        } catch (PartInitException e) {
            e.printStackTrace();
        }
    }

    private void setPagesText() {
        setPageText(0, "Plain Text");
        setPageText(1, "Properties");
    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        m_firstPage.doSave(monitor);
    }

    @Override
    public void doSaveAs() {

    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

    private Control createDesignPage() {
        Composite parent = new Composite(getContainer(), SWT.NONE);
        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        parent.setLayout(new GridLayout(1, false));

        // First row
        Composite composite1 = new Composite(parent, SWT.NONE);
        composite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        composite1.setLayout(new GridLayout(3, false));

        m_keyText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_keyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_keyText.setText("");

        m_valueText = new Text(composite1, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
        m_valueText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        m_valueText.setText("");

        Button applyButton = new Button(composite1, SWT.PUSH);
        applyButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
        applyButton.setText("Apply");
        applyButton.addSelectionListener(new SelectionListener() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                m_properties.put(m_keyText.getText(), m_valueText.getText());
                // Update table
                TableItem tableItem= new TableItem(m_tableViewer.getTable(), SWT.NONE);
                tableItem.setText(new String[] { m_keyText.getText(), m_valueText.getText() });
                // Update editor
                IDocument doc = m_firstPage.getDocumentProvider().getDocument(getEditorInput());
                int offset;
                try {
                    offset = doc.getLineOffset(doc.getNumberOfLines() - 1);
                    doc.replace(offset, 0, m_keyText.getText() + "=" + m_valueText.getText() + "\n");
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                // set text = ""
                m_keyText.setText("");
                m_valueText.setText("");
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
            }
        });

        // Second row
        Composite composite2 = new Composite(parent, SWT.NONE);
        composite2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        composite2.setLayout(new GridLayout(1, false));

        m_tableViewer = new TableViewer(composite2, SWT.FILL);
        Table table = m_tableViewer.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        TableColumn columnKey = new TableColumn(table, SWT.LEAD);
        columnKey.setText("Key");
        columnKey.setWidth(300);
        TableColumn columnValue = new TableColumn(table, SWT.FILL);
        columnValue.setText("Value");
        columnValue.setWidth(300);

        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        m_tableViewer.setContentProvider(new IStructuredContentProvider() {
            @Override
            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            }

            @Override
            public void dispose() {
            }

            @Override
            public Object[] getElements(Object inputElement) {
                return (Object[]) inputElement;
            }
        });
        m_tableViewer.setLabelProvider(new ITableLabelProvider() {
            @Override
            public void removeListener(ILabelProviderListener listener) {
            }

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            @Override
            public void dispose() {
            }

            @Override
            public void addListener(ILabelProviderListener listener) {
            }

            @Override
            public String getColumnText(Object element, int columnIndex) {
                if (element instanceof Entry) {
                    switch (columnIndex) {
                    case 0:
                        return String.valueOf(((Entry) element).getKey());
                    case 1:
                        return String.valueOf(((Entry) element).getValue());
                    }
                }
                return "";
            }

            @Override
            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }
        });

        m_tableViewer.setInput(loadProperties());

        m_firstPage.getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(new IDocumentListener() {

            @Override
            public void documentChanged(DocumentEvent event) {
                m_tableViewer.setInput(loadProperties());
            }

            @Override
            public void documentAboutToBeChanged(DocumentEvent event) {
            }
        });

        return parent;
    }

    private Object[] loadProperties() {
        IDocument document = m_firstPage.getFileDocumentProvider().getDocument(getEditorInput());
        m_properties = new LinkedProperties();
        ByteArrayInputStream inputStream = null;
        try {
            inputStream = new ByteArrayInputStream(document.get().getBytes());
            m_properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return m_properties.entrySet().toArray();
    }

}
人生戏 2024-12-05 00:24:42

我无法告诉您这个模板在伽利略中是否可用。它位于靛蓝。

但此模板仅结合了您可以自己完成的两个步骤。创建一个空插件项目,打开 MANIFEST.MF 以打开插件编辑器并选择扩展选项卡。

在那里您可以添加编辑器和向导的扩展。

对于多页编辑器,单击“添加”,然后选择 org.eclipse.ui.editors。在对话框底部的模板区域中,选择多页编辑器。然后输入教程中所示的属性。

通过选择 org.eclipse.ui.newWizard 和新建文件向导模板,重复创建新向导。

I cannot tell you, if this template is available in Galileo. It is in Indigo.

But this template just combines two steps you can do on your own. Create an empty plugin project, open the MANIFEST.MF to open the plugin editor and select the extension tabs.

There you can add extension for editors and for wizards.

For a multipage editor click on Add, then select org.eclipse.ui.editors. In the template area at the bottom of the dialog select Multi-Page editor. Then enter the properties as show in your tutorial.

Repeat for the new wizard by selectin org.eclipse.ui.newWizard and the New File Wizard template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文