gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / DefaultFilteredTreeController.java @ 2339
History | View | Annotate | Download (2.58 KB)
1 | 2111 | jjdelcerro | package org.gvsig.tools.swing.impl; |
---|---|---|---|
2 | |||
3 | import java.awt.event.ActionEvent; |
||
4 | import java.awt.event.KeyAdapter; |
||
5 | import java.awt.event.KeyEvent; |
||
6 | import javax.swing.JButton; |
||
7 | 2116 | omartinez | import javax.swing.JTree; |
8 | 2111 | jjdelcerro | import javax.swing.text.JTextComponent; |
9 | 2285 | jjdelcerro | import org.gvsig.tools.swing.api.FilteredModel.FilterFunction; |
10 | 2130 | jjdelcerro | import org.gvsig.tools.swing.api.TreeModelRecursiveWildcarMatchFilter; |
11 | 2116 | omartinez | import org.gvsig.tools.swing.api.FilteredTreeController; |
12 | import org.gvsig.tools.swing.api.FilteredTreeModel; |
||
13 | 2111 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
14 | import org.gvsig.tools.swing.api.ToolsSwingManager; |
||
15 | |||
16 | /**
|
||
17 | *
|
||
18 | * @author jjdelcerro
|
||
19 | */
|
||
20 | 2116 | omartinez | public class DefaultFilteredTreeController implements FilteredTreeController { |
21 | 2111 | jjdelcerro | |
22 | 2116 | omartinez | private final JTree tree; |
23 | private final JTextComponent text; |
||
24 | private final JButton button; |
||
25 | 2111 | jjdelcerro | |
26 | 2116 | omartinez | public DefaultFilteredTreeController(JTree tree, JTextComponent text, JButton button) { |
27 | this.tree = tree;
|
||
28 | this.text = text;
|
||
29 | this.button = button;
|
||
30 | this.initComponents();
|
||
31 | } |
||
32 | 2111 | jjdelcerro | |
33 | 2116 | omartinez | private void initComponents() { |
34 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
35 | FilteredTreeModel model = this.createModel(tree);
|
||
36 | this.tree.setModel(model);
|
||
37 | toolsSwingManager.addClearButton(this.text, (ActionEvent e) -> { |
||
38 | text.setText("");
|
||
39 | 2130 | jjdelcerro | clearFilter(); |
40 | 2116 | omartinez | }); |
41 | toolsSwingManager.setDefaultPopupMenu(this.text);
|
||
42 | this.text.addKeyListener(new KeyAdapter() { |
||
43 | @Override
|
||
44 | public void keyTyped(KeyEvent e) { |
||
45 | if (e.getKeyChar() == '\n') { |
||
46 | 2130 | jjdelcerro | setFilter(text.getText()); |
47 | 2116 | omartinez | } |
48 | } |
||
49 | }); |
||
50 | this.button.addActionListener((ActionEvent e) -> { |
||
51 | 2130 | jjdelcerro | setFilter(text.getText()); |
52 | 2116 | omartinez | }); |
53 | } |
||
54 | 2111 | jjdelcerro | |
55 | 2116 | omartinez | @Override
|
56 | public FilteredTreeModel createModel(JTree tree) { |
||
57 | ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager(); |
||
58 | FilteredTreeModel model = toolsSwingManager.createFilteredTreeModel(tree); |
||
59 | return model;
|
||
60 | } |
||
61 | 2111 | jjdelcerro | |
62 | 2130 | jjdelcerro | @Override
|
63 | 2116 | omartinez | public FilteredTreeModel getModel() {
|
64 | return (FilteredTreeModel) this.tree.getModel(); |
||
65 | } |
||
66 | 2111 | jjdelcerro | |
67 | 2130 | jjdelcerro | @Override
|
68 | public void clearFilter() { |
||
69 | this.getModel().setFilter(null); |
||
70 | } |
||
71 | |||
72 | @Override
|
||
73 | public void setFilter(String filter) { |
||
74 | 2285 | jjdelcerro | this.getModel().setFilter(filter);
|
75 | 2130 | jjdelcerro | } |
76 | |||
77 | @Override
|
||
78 | 2285 | jjdelcerro | public void setFilter(FilterFunction filter) { |
79 | if( filter == null ) { |
||
80 | 2310 | omartinez | filter = new TreeModelRecursiveWildcarMatchFilter(this.getModel()); |
81 | 2285 | jjdelcerro | } |
82 | this.getModel().setFilterFunction(filter);
|
||
83 | 2130 | jjdelcerro | } |
84 | 2111 | jjdelcerro | } |