KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > persistent > file > FatFileViewer


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.persistent.file;
47
48 import java.awt.BorderLayout JavaDoc;
49 import java.awt.GridLayout JavaDoc;
50 import java.awt.event.ActionEvent JavaDoc;
51 import java.awt.event.ActionListener JavaDoc;
52 import java.awt.event.MouseAdapter JavaDoc;
53 import java.awt.event.MouseEvent JavaDoc;
54 import java.io.File JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.nio.ByteBuffer JavaDoc;
57
58 import javax.swing.DefaultListModel JavaDoc;
59 import javax.swing.JButton JavaDoc;
60 import javax.swing.JFileChooser JavaDoc;
61 import javax.swing.JFrame JavaDoc;
62 import javax.swing.JLabel JavaDoc;
63 import javax.swing.JList JavaDoc;
64 import javax.swing.JPanel JavaDoc;
65 import javax.swing.JScrollPane JavaDoc;
66 import javax.swing.JTextArea JavaDoc;
67
68 import org.mr.MantaAgent;
69 import org.mr.core.util.byteable.Byteable;
70 import org.mr.core.util.byteable.ByteableInputStream;
71
72
73
74 /**
75  * @author Amir Shevat
76  *
77  */

78 public class FatFileViewer {
79         
80     private static JLabel JavaDoc fatName;
81     private static JLabel JavaDoc entryCount;
82     private static DefaultListModel JavaDoc EntriesListModel = new DefaultListModel JavaDoc() ;
83     private static JTextArea JavaDoc asBuffer;
84     private final static JFileChooser JavaDoc fc = new JFileChooser JavaDoc("./");
85     private static JFrame JavaDoc frame;
86     private static JLabel JavaDoc fileSizeLabel;
87     private static JLabel JavaDoc assignedClusterLabel;
88     private static MantaFatFile mff;
89     private static JTextArea JavaDoc entryToString;
90     
91     
92     public static void main(String JavaDoc[] args) {
93         frame = new JFrame JavaDoc("FAT File Utility Comprehension King - Fat FUCK");
94         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
95         
96         MantaAgent.getInstance().init();
97         
98         BorderLayout JavaDoc outter = new BorderLayout JavaDoc();
99         outter.setHgap(2);
100         outter.setVgap(2);
101         JPanel JavaDoc panel = new JPanel JavaDoc(outter);
102         frame.setContentPane(panel);
103         
104         GridLayout JavaDoc topLayout = new GridLayout JavaDoc(7,1);
105         topLayout.setHgap(2);
106         topLayout.setVgap(2);
107         JPanel JavaDoc top = new JPanel JavaDoc(topLayout);
108         
109         JLabel JavaDoc loadCap = new JLabel JavaDoc("Load new file:");
110             
111         top.add(loadCap);
112         JButton JavaDoc load = new JButton JavaDoc("Load me");
113         top.add(load);
114         load.addActionListener(new ActionListener JavaDoc() {
115               public void actionPerformed(ActionEvent JavaDoc e) {
116                 doLoad(e);
117               }
118         });
119         
120         JLabel JavaDoc detailsCap = new JLabel JavaDoc("FAT file details:");
121         top.add(detailsCap);
122         
123         fatName = new JLabel JavaDoc("File name: N/A");
124         top.add(fatName);
125         
126         
127         entryCount = new JLabel JavaDoc("Entry Count: N/A");
128         top.add(entryCount);
129         
130         fileSizeLabel = new JLabel JavaDoc("File size: N/A");
131         top.add(fileSizeLabel);
132         
133         assignedClusterLabel = new JLabel JavaDoc("Assigned Cluster: N/A");
134         top.add(assignedClusterLabel);
135         
136         
137         /**
138          * bottom
139          *
140          */

141         BorderLayout JavaDoc botLayout = new BorderLayout JavaDoc();
142         botLayout.setHgap(5);
143         botLayout.setVgap(5);
144         JPanel JavaDoc bot = new JPanel JavaDoc(botLayout);
145         
146         
147         JPanel JavaDoc leftBot = new JPanel JavaDoc(new BorderLayout JavaDoc());
148         
149         JLabel JavaDoc filesCap = new JLabel JavaDoc("Fat entries:");
150         leftBot.add(filesCap,BorderLayout.NORTH);
151         final JList JavaDoc fileList = new JList JavaDoc();
152         
153          fileList.addMouseListener(new MouseAdapter JavaDoc() {
154              public void mouseClicked(MouseEvent JavaDoc e) {
155                  int index = fileList.locationToIndex(e.getPoint());
156                  loadEntry((String JavaDoc)EntriesListModel.get(index));
157                  
158              }
159          });
160
161         fileList.setModel(EntriesListModel) ;
162         EntriesListModel.addElement("N/A");
163         
164         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(fileList);
165         
166         leftBot.add(scrollPane,BorderLayout.CENTER);
167         
168         bot.add(leftBot,BorderLayout.WEST);
169         
170         
171         JPanel JavaDoc rightBot = new JPanel JavaDoc(new BorderLayout JavaDoc());
172         // as buffer panel adds to right bottem
173
JPanel JavaDoc asBufferPan = new JPanel JavaDoc(new BorderLayout JavaDoc());
174         
175         JLabel JavaDoc bufferCap = new JLabel JavaDoc("Entry as buffer:");
176         asBufferPan.add(bufferCap, BorderLayout.NORTH);
177         asBuffer = new JTextArea JavaDoc(2,30);
178         asBuffer.setWrapStyleWord(true);
179         
180         scrollPane = new JScrollPane JavaDoc(asBuffer);
181         asBufferPan.add(scrollPane, BorderLayout.CENTER);
182         
183         rightBot.add(asBufferPan,BorderLayout.NORTH);
184         
185 // as string panel adds to right bottem
186
JPanel JavaDoc asStringPan = new JPanel JavaDoc(new BorderLayout JavaDoc());
187         
188         JLabel JavaDoc asStringCap = new JLabel JavaDoc("Entry as String:");
189         asStringPan.add(asStringCap, BorderLayout.NORTH);
190         entryToString = new JTextArea JavaDoc(2,30);
191         entryToString.setWrapStyleWord(true);
192         
193         scrollPane = new JScrollPane JavaDoc(entryToString);
194         asStringPan.add(scrollPane, BorderLayout.CENTER);
195         
196         rightBot.add(asStringPan,BorderLayout.CENTER);
197         
198         
199         bot.add(rightBot,BorderLayout.CENTER);
200         
201         panel.add(top , BorderLayout.NORTH );
202         panel.add(bot, BorderLayout.CENTER);
203         
204         
205         frame.pack();
206         frame.setVisible(true);
207     }
208
209     protected static void loadEntry(String JavaDoc entryName) {
210         if(mff==null)return;
211         asBuffer.setText("");
212         ByteBuffer JavaDoc buf = mff.load(Integer.parseInt(entryName));
213         String JavaDoc bufferToString = " ";
214         byte[] under = buf.array();
215         for (int i = 0; i < under.length; i++) {
216             byte underByte = under[i];
217             bufferToString +=underByte+" ";
218         }
219         asBuffer.setText(bufferToString);
220         try {
221             ByteableInputStream in = new ByteableInputStream();
222             in.setUnderLine(buf);
223             Byteable result = in.readByteable();
224             entryToString.setText(result.toString());
225         } catch (IOException JavaDoc e) {
226             entryToString.setText("error creating object from buffer"+e.getMessage());
227             e.printStackTrace();
228         }
229         asBuffer.setCaretPosition(0);
230         entryToString.setCaretPosition(0);
231     }
232
233     protected static final void doLoad(ActionEvent JavaDoc e) {
234         int returnVal = fc.showOpenDialog(frame);
235
236         if (returnVal == JFileChooser.APPROVE_OPTION) {
237             File JavaDoc file = fc.getSelectedFile();
238             //this is where the application would open the file.
239
System.out.println("Opening: " + file.getAbsolutePath());
240             String JavaDoc fileName = file.getName();
241             fatName.setText("File name: "+fileName);
242              
243             mff = new MantaFatFile(file.getAbsolutePath());
244             entryCount.setText("Entry Count: "+mff.getFileInFatCount());
245               
246             int assignedClusterCount = mff.getAssignedClusterCount();
247             assignedClusterLabel.setText("Assigned Cluster: "+assignedClusterCount);
248               
249             int fileSize = mff.getCurrentFileSize();
250             fileSizeLabel.setText("File size: "+fileSize+" Bytes");
251             
252             EntriesListModel.clear();
253             // fill the entries list
254
int[] entries= mff.getFileList();
255             for (int i = 0; i < entries.length; i++) {
256                 EntriesListModel.addElement(String.valueOf(entries[i]));
257             }
258           
259           
260         }
261     }
262         
263 }
264
Popular Tags