📄 treeviewpart.java
字号:
package demo.pluginA.treeview.internal;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JFileChooser;
import java.io.File;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Button;
import demo.pluginA.treeview.TreeviewPlugin;
import demo.pluginA.treeview.data.ITreeEntry;
import demo.pluginA.treeview.xmlutils.XMLWrite;
import javax.swing.JFrame;
public class TreeViewPart extends ViewPart implements PropertyChangeListener {
private TreeViewer tv;
private String filepath;
public TreeViewPart() {
filepath = null;
}
public void createPartControl(Composite parent) {
Composite topComp = new Composite(parent, SWT.None);
GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
topComp.setLayout(gridLayout);
tv = new TreeViewer(topComp, SWT.BORDER);
// tell the workspace that show the property
getSite().setSelectionProvider(tv);
// set the layout of the tree
GridData gridTree = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridTree.verticalIndent = 5;
final Tree tree = tv.getTree();
tree.setHeaderVisible(true);
tree.setLayoutData(gridTree);
ColumnViewerToolTipSupport.enableFor(tv);
TreeColumn column = new TreeColumn(tree, SWT.LEFT);
column.setText("");
column.setWidth(250);
column = new TreeColumn(tree, SWT.LEFT);
column.setText("Option");
column.setWidth(50);
column = new TreeColumn(tree, SWT.LEFT);
column.setText("Description");
column.setWidth(150);
column = new TreeColumn(tree, SWT.LEFT);
column.setText("ID");
column.setWidth(50);
// set the layout of the button
Composite buttonComp = new Composite(topComp, SWT.None);
Button newButton = new Button(buttonComp, SWT.None);
Button loadButton = new Button(buttonComp, SWT.None);
Button saveButton = new Button(buttonComp, SWT.None);
Button saveasButton = new Button(buttonComp, SWT.None);
GridLayout buttonLayout = new GridLayout(4, true);
buttonLayout.marginHeight = 0;
buttonLayout.marginWidth = 0;
// buttonLayout
buttonLayout.horizontalSpacing = 60;
buttonComp.setLayout(buttonLayout);
GridData newG = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
newG.horizontalSpan = 1;
GridData saveG = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
saveG.horizontalSpan = 1;
GridData loadG = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
loadG.horizontalSpan = 1;
GridData saveasG = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
saveasG.horizontalSpan = 1;
newButton.setLayoutData(newG);
saveButton.setLayoutData(saveG);
loadButton.setLayoutData(loadG);
saveasButton.setLayoutData(saveasG);
newButton.setText("New");
loadButton.setText("Load");
saveButton.setText("Save");
saveasButton.setText("Save as");
// Add actionListener for each button
newButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
filepath=null;
Object inputObj = TreeviewPlugin.loadDataFactory(true,
"");// C:\\testWrite.xml
tv.setInput(inputObj);
}
});
saveButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
if (filepath == null) {
/*InputDialog dialog = new InputDialog(
null,
"Save",
"Path of the xml file you want to save: (for example: C:\\testWrite.xml)",
"", null);
if (dialog.open() == InputDialog.OK) {
filepath = dialog.getValue();
}*/
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("c:\\temp"));
fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
fileChooser.setSelectedFile(new File("demo.xml"));
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".xml")
|| f.isDirectory();
}
public String getDescription() {
return "XML Files(*.XML|*.xml)";
}
});
int status = fileChooser.showSaveDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
filepath = fileChooser.getSelectedFile().getParent()
+ "\\" + fileChooser.getSelectedFile().getName();
}
}
List list = (List) tv.getInput();
XMLWrite xw = new XMLWrite(filepath, list);
try {
xw.ListToDoc();
} catch (Exception error) {
// TODO Auto-generated catch block
error.printStackTrace();
}
if (xw.isValid() == false) {
MessageDialog.openInformation(null, "Message",
"Save error");
filepath=null;
} else
MessageDialog.openInformation(null, "Message",
"Saving is finished");
}
});
loadButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
/*InputDialog dialog = new InputDialog(
null,
"Load",
"Input the path of the xml file: (for example: C:\\testWrite.xml)",
"", null);
if (dialog.open() == InputDialog.OK) {// 如果单击OK按钮
Object inputObj = TreeviewPlugin.loadDataFactory(
false, dialog.getValue());// C:\\testWrite.xml
if(inputObj==null)
filepath=null;
tv.setInput(inputObj);
} // 得到Dialog输入值*/
JFileChooser fileChooser = new JFileChooser("c:\\temp");
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".xml")
|| f.isDirectory();
}
public String getDescription() {
return "XML Files(*.XML|*.xml)";
}
});
int status = fileChooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
Object inputObj = TreeviewPlugin.loadDataFactory(
false,
fileChooser.getSelectedFile().getParent() + "\\" +
fileChooser.getSelectedFile().getName());// C:\\testWrite.xml
if(inputObj==null)
filepath=null;
tv.setInput(inputObj);
}
}
});
saveasButton
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
/*InputDialog dialog = new InputDialog(
null,
"Save as",
"Path of the xml file you want to save: (for example: C:\\testWrite.xml)",
"", null);
if (dialog.open() == InputDialog.OK) {
filepath = dialog.getValue();
}*/
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File("c:\\temp"));
fileChooser.setFileSelectionMode(fileChooser.FILES_AND_DIRECTORIES);
fileChooser.setSelectedFile(new File("demo.xml"));
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".xml")
|| f.isDirectory();
}
public String getDescription() {
return "XML Files(*.XML|*.xml)";
}
});
int status = fileChooser.showSaveDialog(new JFrame());
if (status == JFileChooser.APPROVE_OPTION) {
filepath = fileChooser.getSelectedFile().getParent()
+ "\\" + fileChooser.getSelectedFile().getName();
}
List list = (List) tv.getInput();
XMLWrite xw = new XMLWrite(filepath, list);
try {
xw.ListToDoc();
} catch (Exception error) {
// TODO Auto-generated catch block
error.printStackTrace();
}
if (xw.isValid() == false) {
MessageDialog.openInformation(null, "Message",
"Save error");
filepath=null;
} else
MessageDialog.openInformation(null, "Message",
"Saving is finished");
}
});
//Set contentprovider and laberprovider
tv.setContentProvider(new TreeViewerContentProvider());
tv.setLabelProvider(new TreeViewerLabelProvider());
// -------------begin to assign the actions
TreeActionGroup actionGroup = new TreeActionGroup(tv);
actionGroup.fillContextMenu(new MenuManager());
// -------------finish assigning
tv.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
if (tv.getSelection() instanceof StructuredSelection) {
Object obj = ((StructuredSelection) tv.getSelection())
.getFirstElement();
if (obj instanceof ITreeEntry)
((ITreeEntry) obj)
.addPropertyChangeListener(TreeViewPart.this);
}
}
});
}
public void setFocus() {
}
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("get property change:::" + evt.getPropertyName());
if (evt.getPropertyName().equals(ITreeEntry.PROP_NAME)) {
Object obj = evt.getNewValue();
this.refreshObject(obj);
}
}
public void refreshObject(Object oo) {
System.out.println("refresh object:" + oo.toString());
tv.refresh(oo, true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -