KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > misc > TextTreePanel


1 package org.enhydra.jawe.misc;
2
3 import org.enhydra.jawe.xml.panels.*;
4
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.beans.*;
8 import java.net.URL JavaDoc;
9 import java.util.*;
10
11 import javax.swing.*;
12 import javax.swing.undo.*;
13 import javax.swing.event.*;
14 import javax.swing.tree.*;
15
16 import org.enhydra.jawe.*;
17 import org.enhydra.jawe.xml.*;
18 import org.enhydra.jawe.xml.elements.*;
19
20 /**
21  * Class represents left side of TextView
22  * @author Zoran Milakovic zoran@prozone.co.yu
23  * @version 1.0
24  */

25 public class TextTreePanel
26     extends XMLPanel {
27
28    private TreeModel treeModel;
29    private final JTree jtree;
30    private TextPreview textPreview;
31
32    public TextTreePanel( org.enhydra.jawe.xml.elements.Package myOwner,
33                                  TextPreview txPreview, String JavaDoc title ) {
34       super( myOwner, 2, title, XMLPanel.BOX_LAYOUT, true, true );
35
36       this.textPreview = txPreview;
37       treeModel = new TextTreeModel(myOwner);
38
39       jtree = new JTree( treeModel ) {
40          public boolean isCellEditable( int row, int col ) {
41             return false;
42          }
43       };
44
45       // setting some tree properties
46
jtree.getSelectionModel().setSelectionMode
47           ( TreeSelectionModel.SINGLE_TREE_SELECTION );
48       jtree.setRootVisible( true );
49       DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
50       renderer.setBackground( Utils.getColor( JaWEConfig.getInstance().
51                                               getBackgroundColor() ) );
52       renderer.setBackgroundNonSelectionColor( Utils.getColor( JaWEConfig.
53           getInstance().getBackgroundColor() ) );
54       jtree.setCellRenderer( renderer );
55       jtree.setBackground( Utils.getColor( JaWEConfig.getInstance().
56                                            getBackgroundColor() ) );
57
58       jtree.addTreeSelectionListener( new TreeSelectionListener() {
59          public void valueChanged( TreeSelectionEvent e ) {
60             DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
61                 jtree.getLastSelectedPathComponent();
62             if ( node == null )
63                return;
64
65             org.enhydra.jawe.xml.XMLElement element =
66                 ( org.enhydra.jawe.xml.XMLElement )
67                 node.getUserObject();
68             textPreview.display( element );
69          }
70       } );
71
72       /** MouseListener for jtree */
73       MouseListener mouseListener = new MouseAdapter() {
74          public void mouseClicked( MouseEvent me ) {
75             if ( SwingUtilities.isRightMouseButton( me ) ) {
76                int x = me.getX();
77                int y = me.getY();
78 //Create the popup menu.
79
JPopupMenu popup = new JPopupMenu();
80 //properties item
81
JMenuItem menuItem1 = BarFactory.createMenuItem(
82                    Utils.getUnqualifiedClassName( org.enhydra.jawe.actions.EditProperties.class )
83                    );
84                menuItem1.addActionListener(
85                    new ShowPropertyDialogActionListener( jtree, x, y, textPreview ) );
86 //expand all item
87
JMenuItem menuItem2 = BarFactory.createMenuItem("ExpandAll");
88                menuItem2.addActionListener(
89                    new ExpandAllActionListener( jtree, x, y ) );
90 //collapse all item
91
JMenuItem menuItem3 = BarFactory.createMenuItem("CollapseAll");
92                menuItem3.addActionListener(
93                    new CollapseAllActionListener( jtree, x, y ) );
94                popup.add( menuItem1 );
95                popup.add( menuItem2 );
96                popup.add( menuItem3 );
97                popup.show( jtree, me.getX(), me.getY() );
98             }
99          }
100       };
101
102       jtree.addMouseListener( mouseListener );
103
104       jtree.addTreeExpansionListener( new JaWETreeExpansionListener( this.textPreview ));
105
106       JScrollPane allItemsPane = new JScrollPane();
107       allItemsPane.setViewportView( jtree );
108       allItemsPane.setBackground( Color.lightGray );
109       allItemsPane.setPreferredSize( new Dimension( 300, 300 ) );
110       add( allItemsPane );
111    }
112
113    public TreeModel getTreeModel() {
114       return treeModel;
115    }
116
117    public JTree getTree() {
118       return jtree;
119    }
120
121    /**
122     * Class that implements listener used for propertie dialog
123     */

124    class ShowPropertyDialogActionListener
125        implements ActionListener {
126
127       JTree jtree;
128       int x;
129       int y;
130       TextPreview textPreview;
131
132       public ShowPropertyDialogActionListener( JTree jtree, int x, int y, TextPreview textPreview ) {
133          this.jtree = jtree;
134          this.x = x;
135          this.y = y;
136          this.textPreview = textPreview;
137       }
138
139       public void actionPerformed( ActionEvent e ) {
140          showPropertyDialog( jtree, x, y );
141          this.textPreview.refreshView();
142       }
143
144       private void showPropertyDialog( JTree jtree, int x, int y ) {
145       TreePath treePath = jtree.getPathForLocation( x, y );
146       Object JavaDoc[] pathArray = treePath.getPath();
147       DefaultMutableTreeNode selectedNode = ( DefaultMutableTreeNode )
148           pathArray[pathArray.length - 1];
149       XMLElementDialog d = new XMLElementDialog(
150           ( JFrame ) JaWE.getInstance().getMainFrame(),
151           ResourceManager.getLanguageDependentString(
152           "DialogProperties" ) + " - " +
153           ( ( XMLElement ) selectedNode.getUserObject() ).toName()
154           );
155       d.editXMLElement( ( ( XMLElement ) selectedNode.getUserObject() ).
156                         getPanel(), true, false );
157    }
158
159    }
160
161    /**
162     * Class that implements listener used for expand all
163     */

164    class ExpandAllActionListener
165        implements ActionListener {
166       JTree jtree;
167       int x;
168       int y;
169       public ExpandAllActionListener( JTree jtree, int x, int y ) {
170          this.jtree = jtree;
171          this.x = x;
172          this.y = y;
173       }
174
175       public void actionPerformed( ActionEvent e ) {
176          XMLUtil.expandAll(this.jtree, jtree.getPathForLocation(x,y), true);
177       }
178    }
179
180    /**
181     * Class that implements listener used for collapse all
182     */

183    class CollapseAllActionListener
184        implements ActionListener {
185       JTree jtree;
186       int x;
187       int y;
188       public CollapseAllActionListener( JTree jtree, int x, int y ) {
189          this.jtree = jtree;
190          this.x = x;
191          this.y = y;
192       }
193
194       public void actionPerformed( ActionEvent e ) {
195          XMLUtil.expandAll(this.jtree, jtree.getPathForLocation(x,y), false);
196       }
197    }
198
199    /**
200     * Class that implements listener used for expand and collapse
201     */

202    class JaWETreeExpansionListener
203        implements TreeExpansionListener {
204
205       TextPreview textPreview;
206
207       public JaWETreeExpansionListener(TextPreview textPreview) {
208          this.textPreview = textPreview;
209       }
210
211       public void treeExpanded( TreeExpansionEvent e ) {
212          TreePath expandedPath = e.getPath();
213          ((XMLElement)((DefaultMutableTreeNode)expandedPath.
214                        getLastPathComponent()).getUserObject()).setCollapsed(false);
215 // display();
216
}
217
218       public void treeCollapsed( TreeExpansionEvent e ) {
219          TreePath collapsedPath = e.getPath();
220          ((XMLElement)((DefaultMutableTreeNode)collapsedPath.
221                        getLastPathComponent()).getUserObject()).setCollapsed(true);
222 // display();
223
}
224
225 // private void display() {
226
// DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
227
// jtree.getLastSelectedPathComponent();
228
// if ( node != null ) {
229
// org.enhydra.jawe.xml.XMLElement element =
230
// ( org.enhydra.jawe.xml.XMLElement )
231
// node.getUserObject();
232
// textPreview.display( element );
233
// }
234
// }
235

236    }
237
238
239 }
240
Popular Tags