001: package net.sf.jdec.ui.util;
002:
003: import net.sf.jdec.ui.main.UILauncher;
004: import net.sf.jdec.util.Util;
005:
006: import java.awt.*;
007: import java.awt.event.*;
008: import java.io.File;
009: import java.io.IOException;
010: import java.util.Arrays;
011: import java.util.StringTokenizer;
012: import java.util.ArrayList;
013: import java.util.Enumeration;
014: import java.util.zip.ZipEntry;
015: import java.util.jar.JarFile;
016:
017: import javax.swing.*;
018: import javax.swing.border.TitledBorder;
019:
020: public class JarPreviewFrame extends JFrame implements ActionListener {
021:
022: private JPanel panel1 = null;
023: private JPanel resultpanel = null;
024: private JPanel jarName = new JPanel();
025: private JButton preview = null;
026: private JButton srchclz = null;
027: private JButton openJar = null;
028: private String filePath = null;
029: private Object pkgInfo[] = null;
030: private Object general[] = null;
031: private JTextField cpath = null;
032: private JLabel pkgname = new JLabel();
033: private JButton tip = new JButton("Search Tip");
034: {
035: tip.addActionListener(L.getL());
036:
037: }
038: private static JarPreviewFrame parent = null;
039:
040: public static void main(String[] args) {
041: new JarPreviewFrame();
042: }
043:
044: public void actionPerformed(ActionEvent e) {
045:
046: if (e.getSource() == openJar) {
047:
048: JFileChooser filechooser = new JFileChooser();
049: javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter() {
050: public boolean accept(File f) {
051: return f.getAbsolutePath().endsWith(".jar");
052: }
053:
054: public String getDescription() {
055: return "*.jar";
056: }
057: };
058:
059: // filechooser.setFileFilter(filter);
060: filechooser.setVisible(true);
061:
062: int returnVal = filechooser.showOpenDialog(this );
063: if (returnVal == JFileChooser.APPROVE_OPTION) {
064: filePath = filechooser.getSelectedFile()
065: .getAbsolutePath();
066: jarName.removeAll();
067: JLabel fp = new JLabel(filePath);
068: fp.setForeground(Color.BLUE);
069:
070: jarName.add(fp);
071: jarName.revalidate();
072: }
073:
074: }
075:
076: else if (e.getSource() == preview) {
077:
078: try {
079: if (filePath != null) {
080: boolean valid = isJarValid(filePath);
081: if (valid) {
082: pkgInfo = PreviewJar.getPackages(filePath);
083: general = PreviewJar.getJarDetails(filePath);
084: showResult();
085: }
086: }
087: } catch (IOException e1) {
088:
089: JOptionPane.showMessageDialog(null, e1.getMessage());
090: }
091:
092: }
093:
094: }
095:
096: public JarPreviewFrame() {
097: super ("Jdec Preview Jar");
098: createComponents();
099: parent = this ;
100: setVisible(true);
101:
102: }
103:
104: private void createComponents() {
105:
106: setSize(530, 520);
107: // setResizable(false);
108: panel1 = new JPanel();
109: openJar = new JButton("Open Jar");
110: panel1.add(openJar, BorderLayout.WEST);
111: preview = new JButton("Preview Jar");
112: panel1.add(preview, BorderLayout.EAST);
113: tip.setEnabled(false);
114: //panel1.add(tip);
115:
116: getContentPane().add(panel1, BorderLayout.NORTH);
117: JSeparator sep = new JSeparator();
118: getContentPane().add(sep);
119: resultpanel = new JPanel();
120: getContentPane().add(resultpanel, BorderLayout.CENTER);
121: getContentPane().add(jarName, BorderLayout.SOUTH);
122: openJar.addActionListener(this );
123: preview.addActionListener(this );
124:
125: }
126:
127: private boolean isJarValid(String filePath) throws IOException {
128: boolean ok = true;
129: File f = new File(filePath);
130: if (!f.exists())
131: throw new IOException("File Does Not Exist");
132: if (f.getAbsolutePath().endsWith(".jar") == false) {
133: throw new IOException("File is not a jar");
134: }
135: return ok;
136:
137: }
138:
139: private void showResult() {
140: try {
141:
142: if (pkgInfo == null)
143: throw new Exception(
144: "Package info could not be generated");
145: pkgInfo = Util.genericRemoveDuplicates(
146: Arrays.asList(pkgInfo)).toArray();
147: final JComboBox combo = new JComboBox();
148: for (int i = 0; i < pkgInfo.length; i++) {
149: Object o = pkgInfo[i];
150: combo.addItem(o.toString());
151: }
152:
153: JLabel lbl = new JLabel("AllPackages");
154: resultpanel.removeAll();
155: resultpanel.revalidate();
156: resultpanel.repaint();
157: //resultpanel.setLayout(new BoxLayout(resultpanel,BoxLayout.Y_AXIS));
158: JPanel header = new JPanel();
159: header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
160: header.add(lbl);
161: header.add(combo);
162: JButton filteradd = new JButton("Add Pkg To Filter");
163: header.add(filteradd);
164: filteradd
165: .setToolTipText("The selected Package in the combo box will be transferred to Filter Screen");
166: filteradd.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: ExtraOptions ff = UILauncher.getUIutil()
169: .getFilterframe();
170: JPanel inclpanel = ff.getInclPanel();
171: JComboBox incllist = ff.getInclList();
172: incllist.removeItem("All");
173: incllist.removeItem(combo.getSelectedItem()
174: .toString());
175: incllist.addItem(combo.getSelectedItem().toString()
176: .replaceAll("/", "."));
177: ArrayList list = ff.getIncludedListAsArrayList();
178: list.remove("All");
179: list.add(combo.getSelectedItem().toString()
180: .replaceAll("/", "."));
181: inclpanel.revalidate();
182:
183: }
184: });
185: JPanel genPanel = new JPanel();
186: genPanel
187: .setLayout(new BoxLayout(genPanel, BoxLayout.Y_AXIS));
188:
189: for (int z = 0; z < general.length; z++) {
190: final java.lang.String val = general[z].toString();
191: JLabel jl = new JLabel(val);
192:
193: final int k = val.indexOf(":");
194: java.lang.String val1 = "";
195: if (k != -1)
196: val1 = val.substring(0, k);
197: if (general[z].toString().length() > 75) {
198: JButton v = new JButton("Show Value");
199: v.addActionListener(new ActionListener() {
200:
201: public void actionPerformed(ActionEvent ae) {
202:
203: JOptionPane.showMessageDialog(
204: JarPreviewFrame.this , val
205: .substring(k));
206: }
207: });
208: JPanel temp = new JPanel();
209: temp.setLayout(new FlowLayout());
210: temp.add(new JLabel(val1));
211: temp.add(v);
212: genPanel.add(temp);
213:
214: } else
215: genPanel.add(jl);
216: }
217: resultpanel.add(header, BorderLayout.NORTH);
218: genPanel.setBorder(new TitledBorder(""));
219: resultpanel.add(genPanel, BorderLayout.EAST);
220: JPanel search = new JPanel();
221: search.removeAll();
222:
223: search.setBorder(new TitledBorder(
224: "Find Class in a Jar In path"));
225:
226: search.setLayout(new BoxLayout(search, BoxLayout.Y_AXIS));
227:
228: JPanel path = new JPanel();
229: path.setLayout(new BoxLayout(path, BoxLayout.X_AXIS));
230: path.add(new JLabel("Enter search path"));
231: cpath = new JTextField(20);
232: path.add(cpath);
233:
234: JPanel CLASS = new JPanel();
235: CLASS.setLayout(new BoxLayout(CLASS, BoxLayout.X_AXIS));
236: CLASS
237: .add(new JLabel(
238: "Enter ClassName(Qualified) To search"));
239: final JTextField clname = new JTextField(20);
240: srchclz = new JButton("Search");
241: CLASS.add(clname);
242: CLASS.add(srchclz);
243:
244: JPanel pkg = new JPanel();
245: pkg.setLayout(new BoxLayout(pkg, BoxLayout.X_AXIS));
246: search.add(path);
247: search.add(CLASS);
248: search.add(pkg);
249: search.revalidate();
250: resultpanel.add(new JSeparator());
251: // resultpanel.remove(search);
252: // resultpanel.add(search,BorderLayout.SOUTH);
253: tip.setEnabled(true);
254: resultpanel.revalidate();
255: srchclz.addActionListener(new ActionListener() {
256: public void actionPerformed(ActionEvent ae) {
257: final java.lang.String f = clname.getText();
258: final java.lang.String s = cpath.getText();
259: Thread t1 = new Thread() {
260: public void run() {
261: boolean found = false;
262: UILauncher.getUIutil().setSearch(true);
263: for (int z = 0; z < searchJars.size(); z++) {
264: java.lang.String s1 = (java.lang.String) searchJars
265: .get(z);
266: found = checkJarForClass(s1.trim(), f
267: .trim());
268: if (found) {
269: UILauncher.getUIutil().setSearch(
270: false);
271: reqdJar = s1.trim();
272: break;
273: }
274: }
275: if (!found && searchJars.size() == 0
276: && new File(s.trim()).exists()) // Folder
277: {
278: StringBuffer sb = new StringBuffer("");
279: found = checkForClassNameInFolder(s
280: .trim(), f.trim(), sb);
281: if (found) {
282: UILauncher.getUIutil().setSearch(
283: false);
284: reqdJar = sb.toString();
285:
286: }
287:
288: }
289: UILauncher.getUIutil().setSearch(false);
290: if (!found) {
291: JOptionPane
292: .showMessageDialog(
293: JarPreviewFrame.this ,
294: "Could Not Find Class In Search Path");
295: } else {
296: JOptionPane
297: .showMessageDialog(
298: JarPreviewFrame.this ,
299: "Class Found in Jar "
300: + reqdJar);
301: }
302:
303: }
304:
305: };
306: Thread t2 = new Thread() {
307: public void run() {
308: JFrame frame = LaunchProgressBarFrame();
309: frame.setVisible(true);
310: while (UILauncher.getUIutil()
311: .searchForClassName()) {
312: try {
313: Thread.sleep(1000);
314: } catch (Exception e) {
315:
316: }
317:
318: }
319: frame.setVisible(false);
320:
321: }
322:
323: };
324: try {
325: boolean ok = continueSearch(f, cpath.getText());
326: if (ok) {
327: //if(searchJars.size() > 0)
328: //{
329: t1.setPriority(Thread.MAX_PRIORITY);
330: t2.setPriority(Thread.NORM_PRIORITY);
331: UILauncher.getUIutil().setSearch(true);
332: t2.start();
333: t1.start();
334:
335: //}
336: /*else
337: {
338: JOptionPane.showMessageDialog(JarPreviewFrame.this,"Error while regsitering Jars Entered As Search Path");
339: } */
340: }
341: } catch (Exception e) {
342:
343: JOptionPane.showMessageDialog(
344: JarPreviewFrame.this , e.getMessage());
345: }
346:
347: }
348: });
349:
350: } catch (Exception e) {
351: JOptionPane.showMessageDialog(null, e.getMessage());
352: }
353:
354: }
355:
356: private String reqdJar = null;
357:
358: private boolean continueSearch(java.lang.String f,
359: java.lang.String cpath) throws Exception {
360: boolean b = true;
361: if (cpath == null) {
362: throw new Exception("Search Path Cannot Be Empty");
363: }
364: if (f == null) {
365: throw new Exception("ClassName(QName) Cannot Be Empty");
366: }
367: File file = new File(cpath);
368:
369: if (file.exists() && file.isDirectory() && f != null
370: && f.length() > 0) {
371: return b;
372: }
373: if (file.exists() && !file.isDirectory()
374: && file.getAbsolutePath().endsWith("jar") == false)// && f!=null && f.length() > 0)
375:
376: {
377: throw new Exception(
378: "Search Path is neither a Folder nor a Jar");
379: }
380: if (!file.exists()) {
381: StringTokenizer tokens = new StringTokenizer(cpath, ",");
382: while (tokens.hasMoreTokens()) {
383: String tmp = tokens.nextElement().toString().trim();
384: if (tmp.endsWith("jar"))
385: searchJars.add(tmp);
386: }
387: }
388: return b;
389: }
390:
391: private ArrayList searchJars = new ArrayList();
392: static private L Lref = null;
393:
394: static class L implements ActionListener {
395:
396: private L() {
397:
398: }
399:
400: public static L getL() {
401: if (Lref != null) {
402: return Lref;
403: } else {
404: Lref = new L();
405: return Lref;
406: }
407: }
408:
409: public void actionPerformed(ActionEvent a) {
410: String s = "Search Path can include comma separated Jar List";
411: s += "\nUser can specify a folder also(Single)";
412: JOptionPane.showMessageDialog(parent, s,
413: "How to Specify Path",
414: JOptionPane.INFORMATION_MESSAGE);
415: }
416:
417: }
418:
419: public static JFrame LaunchProgressBarFrame() {
420: JFrame f = new JFrame("Jdec Progress...");
421: srchprog = f;
422:
423: JLabel statusLabel = new JLabel();
424: statusLabel
425: .setText("Please Wait for Jdec to finish executing the current task....");
426: Dimension d = UILauncher.getMainFrame().getToolkit()
427: .getScreenSize();
428: f.setBounds((int) d.getWidth() / 2 - 130,
429: (int) d.getHeight() / 2 - 80, 380, 85);
430: JProgressBar progressBar = new JProgressBar();
431: progressBar.setIndeterminate(true);
432: JPanel contents = (JPanel) f.getContentPane();
433: contents.setSize(400, 150);
434: contents.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
435: 10));
436: contents.add(statusLabel, BorderLayout.NORTH);
437: contents.add(progressBar);
438: f.getContentPane().add(progressBar);
439: f.addWindowListener(wndCloser);
440: return f;
441:
442: }
443:
444: static JFrame srchprog = null;
445:
446: static WindowListener wndCloser = new WindowAdapter() {
447: public void windowClosing(WindowEvent e) {
448: srchprog.setVisible(false);
449: srchprog.dispose();
450: srchprog = null;
451: }
452: };
453:
454: private boolean checkJarForClass(java.lang.String jar,
455: java.lang.String qname) {
456: File file = new File(jar);
457: if (!file.exists()) {
458: return false;
459:
460: } else {
461: try {
462: JarFile jarf = new JarFile(jar);
463: Enumeration en = jarf.entries();
464: while (en.hasMoreElements()) {
465: ZipEntry zipe = (ZipEntry) en.nextElement();
466: if (zipe != null) {
467: String name = zipe.getName();
468: if (name.indexOf("/") != -1)
469: name = name.replaceAll("/", ".");
470: if (name.indexOf(qname) != -1) {
471: return true;
472: }
473: }
474: }
475: } catch (Exception e) {
476: return false;
477: }
478:
479: }
480:
481: return false;
482:
483: }
484:
485: private boolean checkForClassNameInFolder(String folder,
486: String qname, StringBuffer sb) {
487: boolean ok = false;
488: File f = new File(folder);
489: String list[] = f.list();
490: for (int z = 0; z < list.length; z++) {
491: File d = new File(folder + File.separator + list[z]);
492: if (!d.isDirectory() && d.getAbsolutePath().endsWith("jar")) {
493: ok = checkJarForClass(d.getAbsolutePath(), qname);
494:
495: if (ok) {
496: sb.append(d.getAbsolutePath());
497: return ok;
498: }
499: }
500: if (d.isDirectory()) {
501:
502: boolean b1 = checkForClassNameInFolder(d
503: .getAbsolutePath(), qname, sb);
504: if (b1) {
505: return b1;
506: }
507: }
508:
509: }
510: return ok;
511: }
512:
513: }
|