Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jreversepro/gui/JClassEditPanel.java


1   /*
2    * @(#)JClassEditPanel.java
3    *
4    * JReversePro - Java Decompiler / Disassembler.
5    * Copyright (C) 2000 Karthik Kumar.
6    * EMail: akkumar@users.sourceforge.net
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it , under the terms of the GNU General Public License as published
10   * by the Free Software Foundation; either version 2 of the License,
11   * or (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   * See the GNU General Public License for more details.
17   * You should have received a copy of the GNU General Public License
18   * along with this program.If not, write to
19   *  The Free Software Foundation, Inc.,
20   *  59 Temple Place - Suite 330,
21   *  Boston, MA 02111-1307, USA.
22   */
23  package jreversepro.gui;
24  
25  import javax.swing.JPanel;
26  import javax.swing.JFrame;
27  import javax.swing.JScrollPane;
28  import javax.swing.JOptionPane;
29  import javax.swing.JSplitPane;
30  import javax.swing.JTree;
31  import javax.swing.SwingUtilities;
32  
33  import java.awt.Font;
34  import java.awt.Dimension;
35  import java.awt.GridLayout;
36  
37  import javax.swing.tree.DefaultMutableTreeNode;
38  
39  import java.io.FileInputStream;
40  import java.io.FileOutputStream;
41  import java.io.PrintStream;
42  import java.io.File;
43  
44  import java.util.List;
45  
46  /**
47   *   JClassEditPanel - is the Main Panel that appears in the
48   * application main Frame.
49   * @author Karthik Kumar
50   * @version 1.3
51   **/
52  public class JClassEditPanel extends JPanel {
53  
54      /**
55       * This is the name of the Root element appearing
56       * at the top of the hierarchy.
57       **/
58      static final String TREE_ROOT = "Java_Lang_Object";
59  
60      /**
61       * Default font of the font GUI components, including
62       * the editor and the tree.
63       **/
64      public static final String DEFAULT_FONT = "SansSerif";
65  
66      /**
67       * GUI Component containing the source code.
68       **/
69      private JJavaDocumentEditor mTxtJava;
70  
71      /**
72       * Tree component containing the fields and the methods.
73       **/
74      private JTree   mTreeFieldMethod;
75  
76      /**
77       * Root Node of the tree.
78       **/
79      private DefaultMutableTreeNode mRoot;
80  
81      /**
82       * Font of the GUI components.
83       **/
84      private Font mAppFont;
85  
86      /**
87       * Constructor.
88       **/
89      public JClassEditPanel() {
90          mTxtJava = new JJavaDocumentEditor();
91          mAppFont = new Font(DEFAULT_FONT , Font.PLAIN , 12 );
92  
93          // initTree
94          mRoot  = new DefaultMutableTreeNode(TREE_ROOT);
95          mTreeFieldMethod = new JTree(mRoot);
96  
97          JScrollPane ScrDocument = new JScrollPane( mTxtJava );
98          JScrollPane ScrTree = new JScrollPane( mTreeFieldMethod );
99  
100         ScrDocument.setPreferredSize(new Dimension(500, 200));
101         ScrTree.setPreferredSize(new Dimension(500, 200));
102 
103         setSize(500 , 200 );
104 
105         //arrange Components
106         JSplitPane mSplitter = new JSplitPane(
107                         JSplitPane.HORIZONTAL_SPLIT ,
108                         false ,
109                         ScrTree ,
110                         ScrDocument);
111         mSplitter.setDividerLocation( 0.5 );
112         setLayout( new GridLayout(1,1) );
113         add( mSplitter);
114     }
115 
116     /**
117      * Sets the Font of the GUI components.
118      * @param aFont New Font to be set.
119      **/
120     public void setEditorFont( Font aFont ) {
121         mAppFont = aFont;
122         mTxtJava.setFont(aFont);
123         mTreeFieldMethod.setFont(aFont);
124     }
125 
126     /**
127      * Get the Font for the GUI components.
128      * @return Returns the current font of the GUI components.
129      **/
130     public final Font getEditorFont() {
131         return mAppFont;
132     }
133 
134     /**
135      * Writes the code present in aCode to the Editor.
136      * @param aCode Decompiled Java source code to be
137      * written onto the editor.
138      **/
139     public void writeCode( String aCode ) {
140         mTxtJava.setText(aCode);
141     }
142 
143     /**
144      * Writes the contents of the editor onto the File marked by
145      * aOutputFile.
146      * @param aOutputFile OutputFile onto which the code is to be writen.
147      * @param aParent Parent Frame of this component.
148      * @return true, if file contents written.
149      *          false, otherwise.
150      **/
151     public boolean writeToFile(JFrame aParent , File aOutputFile ) {
152         try {
153             FileInputStream FlTemp = new FileInputStream(aOutputFile);
154             //File Exists
155             if ( !confirmOverwrite(aParent ,aOutputFile) ) {
156                 return false;
157             }
158             FlTemp.close();
159         }
160         catch( Exception _ex ) {
161         }
162         try {
163             FileOutputStream out = new FileOutputStream(aOutputFile);
164             PrintStream PrOut = new PrintStream( out );
165             PrOut.print( mTxtJava.getText() );
166             PrOut.close();
167             out.close();
168             PrOut = null;
169             out = null;
170             String Msg = "Contents written onto " +
171                     aOutputFile.toString() +  " successfully";
172             JOptionPane.showMessageDialog(
173                 aParent ,
174                 Msg ,
175                 "File Saved" ,
176                 JOptionPane.INFORMATION_MESSAGE);
177             return true;
178         }
179         catch( Exception _ex ) {
180             System.err.println( _ex );
181             return false;
182         }
183     }
184 
185 
186     /**
187      * Creates the tree model of the GUI version.
188      * @param aParent  Parent Frame
189      * @param aFileName Name of the File.
190      * @param aChildren Children nodes of the tree root.
191      * @param aMaxIndex Maximum Children of the tree root.
192      **/
193     public void createModel( JFrame aParent ,String aFileName ,
194                         List aChildren) {
195         mRoot.removeAllChildren();
196         DefaultMutableTreeNode ClassName =
197                         new DefaultMutableTreeNode(aFileName);
198 
199         for ( int  i = 0 ; i < aChildren.size() ; i++ ) {
200             ClassName.add(new DefaultMutableTreeNode(aChildren.get(i) ) );
201         }
202         mRoot.add(ClassName);
203         SwingUtilities.updateComponentTreeUI(aParent);
204         mTreeFieldMethod.expandRow(1);
205     }
206 
207     /**
208      * @param aParent Parent Frame
209      * @param aOutputFile File  which already exists.
210      * @return true, if user prompts to overwrite file.
211      *          false, otherwise.
212      **/
213     private boolean confirmOverwrite( JFrame aParent , File aOutputFile ) {
214         int a = JOptionPane.showConfirmDialog( aParent ,
215                         "OverWrite File " + aOutputFile.toString() ,
216                         "Confirm Overwrite" ,
217                         JOptionPane.YES_NO_CANCEL_OPTION ,
218                         JOptionPane.WARNING_MESSAGE );
219         return ( a == JOptionPane.YES_OPTION);
220     }
221 }