Пожалуйста, используйте вместо этого 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.Ява
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();
}
}