categoryselectfield.java
来自「Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI」· Java 代码 · 共 156 行
JAVA
156 行
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.gui.internal.edit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import org.rapla.entities.Category;
import org.rapla.framework.RaplaContext;
import org.rapla.framework.RaplaException;
import org.rapla.gui.TreeFactory;
import org.rapla.gui.toolkit.DialogUI;
import org.rapla.gui.toolkit.RaplaButton;
import org.rapla.gui.toolkit.RecursiveNode;
public class CategorySelectField extends AbstractEditField
{
public boolean changeButtonText = true;
RaplaButton selectButton = new RaplaButton(RaplaButton.SMALL);
Category selectedCategory;
Category rootCategory;
public RaplaButton getButton() {
return selectButton;
}
public CategorySelectField(RaplaContext sm,String fieldName,Category rootCategory) throws RaplaException {
super( sm);
setFieldName( fieldName );
this.rootCategory = rootCategory;
selectButton.setAction(new SelectionAction());
selectButton.setHorizontalAlignment(RaplaButton.LEFT);
selectButton.setText(getString("select"));
selectButton.setIcon(getIcon("icon.tree"));
}
public class SelectionAction extends AbstractAction {
private static final long serialVersionUID = 1L;
DialogUI dialog;
JTree tree;
public void actionPerformed(ActionEvent evt) {
try {
tree = new JTree();
tree.setCellRenderer(getTreeFactory().createRenderer());
//tree.setVisibleRowCount(15);
tree.setRootVisible( false );
tree.setShowsRootHandles(true);
tree.setModel(getTreeFactory().createModel(rootCategory));
tree.addMouseListener(new MouseAdapter() {
// End dialog when a leaf is double clicked
public void mousePressed(MouseEvent e) {
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if (selPath != null && e.getClickCount() == 2) {
RecursiveNode node = (RecursiveNode)selPath.getLastPathComponent();
if (node.isLeaf()) {
dialog.getButton(0).doClick();
}
}
}
});
selectCategory(selectedCategory);
JScrollPane scrollPane = new JScrollPane(tree);
scrollPane.setMinimumSize(new Dimension(300, 200));
scrollPane.setPreferredSize(new Dimension(400, 260));
dialog = DialogUI.create(getContext()
,getComponent()
,true
,scrollPane
,new String[] { getString("select"),getString("cancel")});
dialog.setTitle(getName());
dialog.start();
if (dialog.getSelectedIndex() == 0) {
Object newValue = null;
TreePath path = tree.getSelectionPath();
if (path != null) {
newValue = ((RecursiveNode)path.getLastPathComponent()).getUserObject();
}
if (( newValue == null && selectedCategory != null)
|| ( newValue !=null && !newValue.equals(selectedCategory))
) {
setValue( newValue );
fireContentChanged();
}
}
} catch (RaplaException ex) {
showException(ex,selectButton);
}
}
private void selectCategory(Category category) {
ArrayList path = new ArrayList();
while (true) {
if (category == null)
return;
if (category.equals(rootCategory))
break;
path.add(category);
category = category.getParent();
}
RecursiveNode.selectUserObjects(tree,path.toArray());
}
}
public Object getValue() {
return selectedCategory;
}
final private TreeFactory getTreeFactory() {
return (TreeFactory) getService(TreeFactory.ROLE);
}
public void setValue(Object object) {
String text;
selectedCategory = (Category) object;
if ( changeButtonText ) {
if (selectedCategory != null) {
text = selectedCategory.getPath(rootCategory,getI18n().getLocale());
} else {
text = getString("nothing_selected");
}
selectButton.setText(text);
}
}
public JComponent getComponent() {
return selectButton;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?