KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > editor > baseodmediaeditor


1 package com.ca.directory.jxplorer.editor;
2
3 import com.ca.commons.cbutil.*;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.ActionListener JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.io.*;
10 import java.util.logging.Logger JavaDoc;
11
12
13 /**
14  * Class that extends DefaultBinaryEditor to basically customise the display with
15  * a specified title and to add a launch button that when clicked creates
16  * a temp file and tries to launch the file in the default player.
17  * @author Trudi.
18  */

19
20 public class baseodmediaeditor extends defaultbinaryeditor
21 {
22     /**
23      * The file that gets saved when the user clicks launch.
24      */

25     protected File file = null;
26
27     /**
28      * The title of the dialog.
29      */

30     protected String JavaDoc title = "BaseODMediaEditor";
31
32     /**
33      * The name of the file that gets saved.
34      */

35     protected String JavaDoc fileName = "BaseODMediaEditor";
36
37     /**
38      * The extension of the file that gets saved.
39      */

40     protected String JavaDoc extension = "";
41
42     private static Logger JavaDoc log = Logger.getLogger(baseodmediaeditor.class.getName());
43
44     /**
45      * Calls the parent constructor to do most the set up. Sets the title & size.
46      * @param owner handle to the application frame, used to display dialog boxes.
47      */

48
49     public baseodmediaeditor(Frame owner)
50     {
51         super(owner);
52         this.setDialogTitle(title);
53     }
54
55     /**
56      * Sets the dialog title.
57      * @param title the title the dialog should display.
58      */

59     public void setDialogTitle(String JavaDoc title)
60     {
61         this.title = title;
62         super.setDialogTitle(CBIntText.get(title));
63     }
64
65     /**
66      * Sets the file name.
67      * @param fileName the name the file should be saved with.
68      */

69     public void setFileName(String JavaDoc fileName)
70     {
71         this.fileName = fileName;
72     }
73
74     /**
75      * Sets the extension of the file.
76      * @param extension the extension the file should be saved with.
77      */

78     public void setExtension(String JavaDoc extension)
79     {
80         this.extension = extension;
81         addChoosableFileFilter(new CBFileFilter(new String JavaDoc[]{extension}, fileName + " Files (*" + extension +")"));
82     }
83
84     /**
85      * Over writes the super method to add the Launch button to the dialog.
86      * @return the Launch button (a CBButton) that when clicked calls the
87      * launch method. However, null is returned for Sun systems b/c
88      * the launcher in JX doesn't work for those platforms.
89      */

90     public JComponent addComponent()
91     {
92         CBButton btnView = new CBButton(CBIntText.get("Launch"), CBIntText.get(""));
93         btnView.setToolTipText(CBIntText.get("Launch the saved file into it's default application."));
94         btnView.addActionListener(new ActionListener JavaDoc()
95         {
96             public void actionPerformed(ActionEvent JavaDoc e)
97             {
98                 launch();
99             }
100         });
101
102         //todo: disable the Launch button if nothing to launch.
103
// if (bytes == null || bytes.length == 0)
104
// btnView.setEnabled(false);
105

106         if (System.getProperty("os.name").equalsIgnoreCase("SunOS"))
107             return null;
108         else
109             return btnView;
110     }
111
112
113     /**
114      * Makes a temporary file of the odMusicMID object (odMusicMID.mid) then
115      * tries to launch the document in the default player.
116      */

117
118     public void launch()
119     {
120         if (bytes == null)
121         {
122             log.warning("No file to launch via " + fileName + ".");
123             return;
124         }
125
126         File dir = new File("temp");
127         dir.mkdir();
128         dir.deleteOnExit();
129
130         file = new File(dir, fileName + extension); //TE: save the data into a temporary file.
131
file.deleteOnExit();
132
133         try
134         {
135             FileOutputStream output = new FileOutputStream(file);
136             output.write(bytes);
137             output.close();
138         }
139         catch (IOException e)
140         {
141             CBUtility.error("Error writing to the file!" + e);
142         }
143
144         CBLauncher.launchProgram(extension, file.getPath());
145     }
146
147
148     /**
149      * Shuts the window and deletes the temp file.
150      */

151
152     public void quit()
153     {
154         if (file != null)
155             file.delete();
156     }
157 }
Popular Tags