001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.internal.edit;
014:
015: import java.awt.Dimension;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.MouseAdapter;
018: import java.awt.event.MouseEvent;
019: import java.util.ArrayList;
020:
021: import javax.swing.AbstractAction;
022: import javax.swing.JComponent;
023: import javax.swing.JScrollPane;
024: import javax.swing.JTree;
025: import javax.swing.tree.TreePath;
026:
027: import org.rapla.entities.Category;
028: import org.rapla.framework.RaplaContext;
029: import org.rapla.framework.RaplaException;
030: import org.rapla.gui.TreeFactory;
031: import org.rapla.gui.toolkit.DialogUI;
032: import org.rapla.gui.toolkit.RaplaButton;
033: import org.rapla.gui.toolkit.RecursiveNode;
034:
035: public class CategorySelectField extends AbstractEditField {
036: public boolean changeButtonText = true;
037:
038: RaplaButton selectButton = new RaplaButton(RaplaButton.SMALL);
039: Category selectedCategory;
040: Category rootCategory;
041:
042: public RaplaButton getButton() {
043: return selectButton;
044: }
045:
046: public CategorySelectField(RaplaContext sm, String fieldName,
047: Category rootCategory) throws RaplaException {
048: super (sm);
049: setFieldName(fieldName);
050: this .rootCategory = rootCategory;
051: selectButton.setAction(new SelectionAction());
052: selectButton.setHorizontalAlignment(RaplaButton.LEFT);
053: selectButton.setText(getString("select"));
054: selectButton.setIcon(getIcon("icon.tree"));
055: }
056:
057: public class SelectionAction extends AbstractAction {
058: private static final long serialVersionUID = 1L;
059:
060: DialogUI dialog;
061: JTree tree;
062:
063: public void actionPerformed(ActionEvent evt) {
064: try {
065: tree = new JTree();
066: tree.setCellRenderer(getTreeFactory().createRenderer());
067: //tree.setVisibleRowCount(15);
068: tree.setRootVisible(false);
069: tree.setShowsRootHandles(true);
070: tree.setModel(getTreeFactory()
071: .createModel(rootCategory));
072: tree.addMouseListener(new MouseAdapter() {
073: // End dialog when a leaf is double clicked
074: public void mousePressed(MouseEvent e) {
075: TreePath selPath = tree.getPathForLocation(e
076: .getX(), e.getY());
077: if (selPath != null && e.getClickCount() == 2) {
078: RecursiveNode node = (RecursiveNode) selPath
079: .getLastPathComponent();
080: if (node.isLeaf()) {
081: dialog.getButton(0).doClick();
082: }
083: }
084: }
085: });
086:
087: selectCategory(selectedCategory);
088: JScrollPane scrollPane = new JScrollPane(tree);
089: scrollPane.setMinimumSize(new Dimension(300, 200));
090: scrollPane.setPreferredSize(new Dimension(400, 260));
091: dialog = DialogUI.create(getContext(), getComponent(),
092: true, scrollPane, new String[] {
093: getString("select"),
094: getString("cancel") });
095: dialog.setTitle(getName());
096: dialog.start();
097: if (dialog.getSelectedIndex() == 0) {
098: Object newValue = null;
099: TreePath path = tree.getSelectionPath();
100: if (path != null) {
101: newValue = ((RecursiveNode) path
102: .getLastPathComponent())
103: .getUserObject();
104: }
105: if ((newValue == null && selectedCategory != null)
106: || (newValue != null && !newValue
107: .equals(selectedCategory))) {
108: setValue(newValue);
109: fireContentChanged();
110: }
111: }
112: } catch (RaplaException ex) {
113: showException(ex, selectButton);
114: }
115: }
116:
117: private void selectCategory(Category category) {
118: ArrayList path = new ArrayList();
119: while (true) {
120: if (category == null)
121: return;
122: if (category.equals(rootCategory))
123: break;
124: path.add(category);
125: category = category.getParent();
126: }
127: RecursiveNode.selectUserObjects(tree, path.toArray());
128: }
129: }
130:
131: public Object getValue() {
132: return selectedCategory;
133: }
134:
135: final private TreeFactory getTreeFactory() {
136: return (TreeFactory) getService(TreeFactory.ROLE);
137: }
138:
139: public void setValue(Object object) {
140: String text;
141: selectedCategory = (Category) object;
142: if (changeButtonText) {
143: if (selectedCategory != null) {
144: text = selectedCategory.getPath(rootCategory, getI18n()
145: .getLocale());
146: } else {
147: text = getString("nothing_selected");
148: }
149: selectButton.setText(text);
150: }
151: }
152:
153: public JComponent getComponent() {
154: return selectButton;
155: }
156:
157: }
|