Revision 11440
trunk/libraries/libUIComponent/src-test/org/gvsig/gui/beans/treelist/TestTreeList.java | ||
---|---|---|
1 | 1 |
package org.gvsig.gui.beans.treelist; |
2 | 2 |
|
3 |
import java.awt.event.ComponentEvent; |
|
4 |
import java.awt.event.ComponentListener; |
|
5 |
|
|
6 | 3 |
import javax.swing.JFrame; |
7 | 4 |
import javax.swing.UIManager; |
8 | 5 |
|
9 | 6 |
import org.gvsig.gui.beans.table.exceptions.NotInitializeException; |
10 | 7 |
|
11 |
public class TestTreeList implements ComponentListener{
|
|
8 |
public class TestTreeList { |
|
12 | 9 |
private int w = 200, h = 380; |
13 | 10 |
private JFrame frame = new JFrame(); |
14 | 11 |
private TreeListContainer tlist; |
... | ... | |
36 | 33 |
|
37 | 34 |
frame.getContentPane().add(tlist); |
38 | 35 |
frame.setSize(w, h); |
39 |
frame.addComponentListener(this); |
|
40 | 36 |
frame.setVisible(true); |
41 | 37 |
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); |
42 | 38 |
} |
... | ... | |
53 | 49 |
System.out.println("Tabla no inicializado"); |
54 | 50 |
} |
55 | 51 |
} |
56 |
|
|
57 |
public void componentHidden(ComponentEvent e) { |
|
58 |
// TODO Auto-generated method stub |
|
59 |
} |
|
60 |
|
|
61 |
public void componentMoved(ComponentEvent e) { |
|
62 |
// TODO Auto-generated method stub |
|
63 |
} |
|
64 |
|
|
65 |
public void componentResized(ComponentEvent e) { |
|
66 |
tlist.setComponentSize(frame.getWidth() - 10, frame.getHeight() - 20); |
|
67 |
} |
|
68 |
|
|
69 |
public void componentShown(ComponentEvent e) { |
|
70 |
// TODO Auto-generated method stub |
|
71 |
} |
|
72 | 52 |
} |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/treelist/TreeListContainer.java | ||
---|---|---|
18 | 18 |
*/ |
19 | 19 |
package org.gvsig.gui.beans.treelist; |
20 | 20 |
|
21 |
import java.awt.FlowLayout; |
|
22 |
import java.awt.GridBagConstraints; |
|
23 |
import java.awt.GridBagLayout; |
|
21 |
import java.awt.BorderLayout; |
|
22 |
import java.awt.Dimension; |
|
23 |
import java.awt.event.ActionEvent; |
|
24 |
import java.awt.event.ActionListener; |
|
24 | 25 |
import java.util.ArrayList; |
25 | 26 |
import java.util.Hashtable; |
26 | 27 |
|
27 | 28 |
import javax.swing.DefaultListModel; |
29 |
import javax.swing.JButton; |
|
28 | 30 |
import javax.swing.JList; |
29 | 31 |
import javax.swing.JPanel; |
30 | 32 |
import javax.swing.JScrollPane; |
33 |
import javax.swing.JSplitPane; |
|
31 | 34 |
import javax.swing.JTree; |
32 | 35 |
import javax.swing.ListSelectionModel; |
33 | 36 |
import javax.swing.tree.DefaultMutableTreeNode; |
34 | 37 |
import javax.swing.tree.DefaultTreeModel; |
35 | 38 |
|
36 |
import org.gvsig.gui.beans.BaseComponent; |
|
37 | 39 |
import org.gvsig.gui.beans.treelist.listeners.TreeListComponentListener; |
38 | 40 |
import org.gvsig.gui.beans.treelist.listeners.TreeListListener; |
39 | 41 |
import org.gvsig.i18n.Messages; |
... | ... | |
47 | 49 |
* Nacho Brodin (brodin_ign@gva.es) |
48 | 50 |
*/ |
49 | 51 |
|
50 |
public class TreeListContainer extends BaseComponent {
|
|
52 |
public class TreeListContainer extends JPanel implements ActionListener {
|
|
51 | 53 |
private static final long serialVersionUID = 6665259638830401366L; |
52 |
private int wComp = 190; |
|
53 |
private int hComp = 360; |
|
54 |
private int hTree = (int)Math.floor(hComp * 0.68); |
|
55 |
private int hList = hComp - hTree; |
|
56 | 54 |
private Hashtable map; |
57 | 55 |
|
58 |
private JPanel pMain = null; |
|
59 | 56 |
private JScrollPane pTree = null; |
60 | 57 |
private JScrollPane pList = null; |
61 | 58 |
private JTree tree = null; |
62 | 59 |
private JList list = null; |
60 |
private JButton bAdd = null; |
|
61 |
private JButton bDel = null; |
|
63 | 62 |
private DefaultMutableTreeNode raiz = null; |
64 | 63 |
private TreeListListener listener = null; |
65 | 64 |
private ArrayList listListeners = new ArrayList(); |
65 |
private JSplitPane jSplitPane1 = null; |
|
66 |
private JPanel jPanelButtons = null; |
|
66 | 67 |
|
67 | 68 |
/** |
68 | 69 |
* This method initializes |
69 |
* |
|
70 | 70 |
*/ |
71 | 71 |
public TreeListContainer() { |
72 | 72 |
super(); |
... | ... | |
74 | 74 |
initialize(); |
75 | 75 |
listener.setList(getList()); |
76 | 76 |
listener.setTree(getTree()); |
77 |
this.setPreferredSize(new Dimension(160, 0)); |
|
77 | 78 |
} |
78 | 79 |
|
79 | 80 |
/** |
80 | 81 |
* This method initializes this |
81 |
* |
|
82 | 82 |
*/ |
83 | 83 |
private void initialize() { |
84 | 84 |
map = new Hashtable(); |
85 | 85 |
raiz = new DefaultMutableTreeNode(Messages.getText("filtros")); |
86 |
this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
|
87 |
FlowLayout flowLayout = new FlowLayout(); |
|
88 |
flowLayout.setHgap(0); |
|
89 |
flowLayout.setVgap(0); |
|
90 |
this.setLayout(flowLayout); |
|
91 |
this.add(getPMain(), null); |
|
92 |
|
|
86 |
setLayout(new BorderLayout()); |
|
87 |
|
|
88 |
getPList().setMinimumSize(new Dimension(0, 60)); |
|
89 |
jSplitPane1 = new JSplitPane(); |
|
90 |
jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); |
|
91 |
jSplitPane1.setTopComponent(getPTree()); |
|
92 |
jSplitPane1.setRightComponent(getPList()); |
|
93 |
jSplitPane1.setResizeWeight(1.0); |
|
94 |
jSplitPane1.setContinuousLayout(true); |
|
95 |
// jSplitPane1.setOneTouchExpandable(true); |
|
96 |
this.add(jSplitPane1, BorderLayout.CENTER); |
|
97 |
this.add(getJPanelButtons(), BorderLayout.SOUTH); |
|
93 | 98 |
} |
94 | 99 |
|
95 |
/** |
|
96 |
* This method initializes jPanel |
|
97 |
* |
|
98 |
* @return javax.swing.JPanel |
|
99 |
*/ |
|
100 |
private JPanel getPMain() { |
|
101 |
if (pMain == null) { |
|
102 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
|
103 |
gridBagConstraints1.gridx = 0; |
|
104 |
gridBagConstraints1.gridy = 1; |
|
105 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
|
106 |
gridBagConstraints.gridx = 0; |
|
107 |
gridBagConstraints.gridy = 0; |
|
108 |
pMain = new JPanel(); |
|
109 |
pMain.setLayout(new GridBagLayout()); |
|
110 |
pMain.setPreferredSize(new java.awt.Dimension(wComp,hComp)); |
|
111 |
pMain.add(getPTree(), gridBagConstraints); |
|
112 |
pMain.add(getPList(), gridBagConstraints1); |
|
100 |
private JPanel getJPanelButtons() { |
|
101 |
if (jPanelButtons == null) { |
|
102 |
jPanelButtons = new JPanel(); |
|
103 |
jPanelButtons.setPreferredSize(new Dimension(0, 30)); |
|
104 |
bAdd = new JButton("+"); |
|
105 |
bDel = new JButton("-"); |
|
106 |
jPanelButtons.add(bAdd); |
|
107 |
jPanelButtons.add(bDel); |
|
108 |
bAdd.addActionListener(this); |
|
109 |
bDel.addActionListener(this); |
|
113 | 110 |
} |
114 |
return pMain;
|
|
111 |
return jPanelButtons;
|
|
115 | 112 |
} |
116 |
|
|
113 |
|
|
117 | 114 |
/** |
118 | 115 |
* This method initializes jPanel |
119 | 116 |
* |
... | ... | |
122 | 119 |
private JScrollPane getPTree() { |
123 | 120 |
if (pTree == null) { |
124 | 121 |
pTree = new JScrollPane(); |
125 |
pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree)); |
|
126 | 122 |
pTree.setViewportBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
127 | 123 |
pTree.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
128 | 124 |
pTree.setViewportView(getTree()); |
... | ... | |
138 | 134 |
private JScrollPane getPList() { |
139 | 135 |
if (pList == null) { |
140 | 136 |
pList = new JScrollPane(); |
141 |
pList.setPreferredSize(new java.awt.Dimension(wComp, hList)); |
|
142 | 137 |
pList.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED)); |
143 | 138 |
pList.setBackground(java.awt.Color.white); |
144 | 139 |
pList.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
... | ... | |
176 | 171 |
} |
177 | 172 |
return list; |
178 | 173 |
} |
179 |
|
|
180 | 174 |
|
181 |
public void setComponentSize(int w, int h){ |
|
182 |
wComp = w; |
|
183 |
hComp = h; |
|
184 |
hTree = (int)Math.floor(hComp * 0.68); |
|
185 |
hList = hComp - hTree; |
|
186 |
this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
|
187 |
pMain.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
|
188 |
pTree.setPreferredSize(new java.awt.Dimension(wComp, hTree)); |
|
189 |
pList.setPreferredSize(new java.awt.Dimension(wComp, hList)); |
|
190 |
} |
|
191 |
|
|
192 | 175 |
//------------------------------------------- |
193 | 176 |
//M?TODOS DE ARBOL |
194 | 177 |
|
... | ... | |
277 | 260 |
public void setMap(Hashtable map) { |
278 | 261 |
this.map = map; |
279 | 262 |
} |
280 |
} |
|
263 |
|
|
264 |
public void actionPerformed(ActionEvent e) { |
|
265 |
if (e.getSource() == bAdd) { |
|
266 |
listener.insertElement(); |
|
267 |
} |
|
268 |
if (e.getSource() == bDel) { |
|
269 |
listener.deleteElement(); |
|
270 |
} |
|
271 |
} |
|
272 |
} |
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/treelist/listeners/TreeListListener.java | ||
---|---|---|
38 | 38 |
* Nacho Brodin (brodin_ign@gva.es) |
39 | 39 |
*/ |
40 | 40 |
|
41 |
public class TreeListListener implements MouseListener, MouseMotionListener, TreeListComponentListener{ |
|
42 |
|
|
43 |
private JTree tree = null; |
|
41 |
public class TreeListListener implements MouseListener, MouseMotionListener, TreeListComponentListener { |
|
42 |
private JTree tree = null; |
|
44 | 43 |
private JList list = null; |
45 | 44 |
private ArrayList listListeners = null; |
46 | 45 |
private TreeListEvent ev = new TreeListEvent(); |
... | ... | |
108 | 107 |
*/ |
109 | 108 |
private boolean draggElement = false; |
110 | 109 |
|
111 |
public void mouseClicked(MouseEvent e) { |
|
112 |
|
|
113 |
if(e.getSource() == list){ |
|
114 |
//Si es doble click y hay alg?n elemento seleccionado en la lista lo eliminamos |
|
115 |
if(e.getClickCount() == 2 && list.getSelectedIndex() != -1){ |
|
116 |
DefaultListModel model = (DefaultListModel)list.getModel(); |
|
117 |
|
|
118 |
//Lanzamos el evento de eliminar un elemento |
|
119 |
ev.resetValues(); |
|
120 |
ev.setElementRemoved((String)list.getSelectedValue()); |
|
121 |
elementRemoved(ev); |
|
122 |
|
|
123 |
model.remove(list.getSelectedIndex()); |
|
124 |
} |
|
110 |
public void deleteElement() { |
|
111 |
if (list.getSelectedIndex() != -1) { |
|
112 |
DefaultListModel model = (DefaultListModel) list.getModel(); |
|
113 |
|
|
114 |
// Lanzamos el evento de eliminar un elemento |
|
115 |
ev.resetValues(); |
|
116 |
ev.setElementRemoved((String) list.getSelectedValue()); |
|
117 |
elementRemoved(ev); |
|
118 |
|
|
119 |
model.remove(list.getSelectedIndex()); |
|
125 | 120 |
} |
126 |
|
|
127 |
if(e.getSource() == tree){ |
|
128 |
//Si es doble click y hay alg?n elemento seleccionado en el arbol lo a?adimos |
|
129 |
if(e.getClickCount() == 2 && draggLabel != null){ |
|
130 |
DefaultListModel model = (DefaultListModel)list.getModel(); |
|
131 |
|
|
132 |
//Lanzamos el evento de a?adir un elemento |
|
133 |
ev.resetValues(); |
|
134 |
ev.setElementAdded(draggLabel); |
|
135 |
elementAdded(ev); |
|
136 |
|
|
137 |
model.addElement(draggLabel); |
|
138 |
draggLabel = null; |
|
139 |
} |
|
121 |
} |
|
122 |
|
|
123 |
public void insertElement() { |
|
124 |
if (draggLabel != null) { |
|
125 |
DefaultListModel model = (DefaultListModel) list.getModel(); |
|
126 |
|
|
127 |
// Lanzamos el evento de a?adir un elemento |
|
128 |
ev.resetValues(); |
|
129 |
ev.setElementAdded(draggLabel); |
|
130 |
elementAdded(ev); |
|
131 |
|
|
132 |
model.addElement(draggLabel); |
|
133 |
// draggLabel = null; |
|
140 | 134 |
} |
141 | 135 |
} |
142 | 136 |
|
137 |
public void mouseClicked(MouseEvent e) { |
|
138 |
if (e.getSource() == list) |
|
139 |
// Si es doble click y hay alg?n elemento seleccionado en la lista lo eliminamos |
|
140 |
if (e.getClickCount() == 2) |
|
141 |
deleteElement(); |
|
142 |
|
|
143 |
if (e.getSource() == tree) |
|
144 |
// Si es doble click y hay alg?n elemento seleccionado en el arbol lo a?adimos |
|
145 |
if (e.getClickCount() == 2) |
|
146 |
insertElement(); |
|
147 |
} |
|
148 |
|
|
143 | 149 |
public void mouseEntered(MouseEvent e) { |
144 | 150 |
if(e.getSource() == list) |
145 | 151 |
enterInList = true; |
Also available in: Unified diff