KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.ca.directory.jxplorer.editor;
2
3 import com.ca.commons.cbutil.*;
4 import com.ca.directory.jxplorer.JXplorer;
5 import com.ca.directory.jxplorer.HelpIDs;
6 import com.ca.commons.naming.DN;
7
8 import javax.swing.*;
9 import javax.swing.border.TitledBorder JavaDoc;
10 import java.awt.*;
11 import java.io.*;
12 import java.util.logging.Logger JavaDoc;
13 import java.util.logging.Level JavaDoc;
14
15 /**
16  * This class extends JFileChooser so that we can add an accessory
17  * object to it. This accessory is a panel with two radio buttons on it:
18  * save and load. They are basically flags that tell JX if the user wants
19  * to load a new file in or save the current one to an external file.<br>
20  * This accessory panel also had a Help button.<br>
21  * This class can be extended if you want to add another button for example.
22  * This is done by over writing the addComponent method.
23  * @author Trudi.
24  */

25 public class defaultbinaryeditor extends JFileChooser
26         implements abstractbinaryeditor
27 {
28     /**
29      * The DN of the current entry.
30      */

31     protected DN currentDN = null;
32
33     /**
34      * The data from the directory.
35      */

36     protected editablebinary editMe = null;
37
38     /**
39      * Global variable for the entry that is being either loaded or saved
40      * it is also used as the source for setting the value in the table
41      * editor if the user is not in edit mode (this would mean the value is unchanged).
42      */

43     protected byte[] bytes;
44
45     /**
46      * The accessory object that is added to this JFileChooser.
47      */

48     protected JFileChooserAccessory accessory = new JFileChooserAccessory();
49
50     /**
51      * The handle to the application frame, used to dsiplay dialog boxes.
52      */

53     protected Frame owner;
54
55     private static Logger JavaDoc log = Logger.getLogger(defaultbinaryeditor.class.getName());
56
57     /**
58      * Constructor that calls the parent constructor with the file directory to open.
59      * It also sets the accessory, the approve button tool tip and the dialog title.
60      * @param owner handle to the application frame, used to display dialog boxes.
61      */

62     public defaultbinaryeditor(Frame owner)
63     {
64         super(JXplorer.getProperty("binary.homeDir"));
65         this.owner = owner;
66
67         setAccessory(accessory);
68
69         setApproveButtonToolTipText(CBIntText.get("Click here to either load or save the file depending on the option selected."));
70         setDialogTitle(CBIntText.get("Binary Data"));
71     }
72
73     /**
74      * Opens the dialog. If a value exists the save radio button
75      * is enabled and selected otherwise it is disabled and the load
76      * radio button is selected.<br><br>
77      * When the dialog is closed, this method determines if the cancel
78      * button was clicked where by doing nothing. Otherwise if the OK
79      * button was clicked, it calls the save method if the save radio
80      * button is checked, or the load method if the load radio button is
81      * checked.
82      */

83     public void showDialog()
84     {
85         if (bytes == null || bytes.length == 0)
86         {
87             accessory.setSaveEnabled(false);
88             accessory.setSaveRadioSelected(false);
89         }
90         else
91         {
92             accessory.setSaveEnabled(true);
93             accessory.setSaveRadioSelected(true);
94         }
95
96         if (showDialog(owner, CBIntText.get("OK")) != JFileChooser.APPROVE_OPTION)
97             return;
98
99         if (accessory.isSaveSelected())
100             save();
101         else if (accessory.isLoadSelected())
102             load();
103
104         quit();
105     }
106
107     /**
108      * Save binary data to the file.
109      */

110     public void save()
111     {
112         File file = getSelectedFile();
113         JXplorer.setProperty("binary.homeDir", getSelectedFile().getParent());
114
115         try
116         {
117             FileOutputStream output = new FileOutputStream(file);
118             output.write(bytes);
119
120             output.close();
121         }
122         catch (IOException e)
123         {
124             log.log(Level.WARNING, "Error writing to the file!", e);
125             return;
126         }
127
128         JOptionPane.showMessageDialog(owner, CBIntText.get("File ''{0}'' was successfully saved.", new String JavaDoc[] {file.getName()}), CBIntText.get("File Saved"), JOptionPane.INFORMATION_MESSAGE);
129     }
130
131     /**
132      * Loads binary data from file.
133      */

134     protected void load()
135     {
136         CBCache.cleanCache(currentDN.toString()); //TE: delete any temporary files associates with this entry.
137

138         File file = getSelectedFile();
139         JXplorer.setProperty("binary.homeDir", getSelectedFile().getParent());
140
141         try
142         {
143             FileInputStream input = new FileInputStream(file);
144
145             int length = (int) file.length();
146             if (length > 0)
147             {
148
149                 bytes = new byte[length];
150                 int read = input.read(bytes);
151                 editMe.setValue(bytes);
152             }
153             input.close();
154         }
155         catch (IOException e)
156         {
157             log.log(Level.WARNING,"Error opening the file!", e);
158             return;
159         }
160
161         JOptionPane.showMessageDialog(owner, CBIntText.get("File ''{0}'' was successfully loaded. Don't forget to click Submit in the Table Editor to save the data to the DSA.", new String JavaDoc[] {file.getName()}), CBIntText.get("File Loaded"), JOptionPane.INFORMATION_MESSAGE);
162     }
163
164     /**
165      * Does nothing. Can be over written.
166      */

167     public void quit()
168     {
169         //TE: Extend this...odDocumentDOCEditor & odSpreadSheetXLSEditor.
170
}
171
172     /**
173      * Sets the value to display in the editor. If there is no value or if it is
174      * null, "No data available" is displayed in the editor. Otherwise the value
175      * is shortened (if needed) to 1000 characters then converted to a hex string and
176      * displayed in the text area.
177      * @param editMe a value from the dsa that is to be displayed in the editor.
178      */

179     public void setValue(editablebinary editMe)
180     {
181         this.editMe = editMe;
182         bytes = editMe.getValue();
183     }
184
185     /**
186      * Sets the dn of the entry being modified.
187      * @param dn the DN of the entry being modified.
188      */

189
190     public void setDN(DN dn)
191     {
192         currentDN = dn;
193     }
194
195     /**
196      * Returns null. Can be over written.
197      * @return null.
198      */

199     public JComponent addComponent()
200     {
201         return null;
202     }
203
204     /**
205      * Creats a panel with two radio buttons and a help button.
206      * Calls the addComponent method incase this class has been
207      * extended and the extending class wants to add an extra component.
208      * @author Trudi.
209      */

210     public class JFileChooserAccessory extends JPanel
211     {
212         protected JLabel label;
213         protected CBButton helpButton, btnCustom = null;
214
215         protected JRadioButton saveRadio = new JRadioButton(CBIntText.get("Save"));
216         protected JRadioButton loadRadio = new JRadioButton(CBIntText.get("Load"));
217
218         /**
219          * Sets up the panel.
220          */

221         public JFileChooserAccessory()
222         {
223             CBPanel mainPanel = new CBPanel();
224
225             helpButton = new CBButton(CBIntText.get("Help"), CBIntText.get("Click here for Help."));
226             CBHelpSystem.useDefaultHelp(helpButton, HelpIDs.ATTR_BINARY);
227
228             saveRadio.setToolTipText(CBIntText.get("To save to an external file, select this option then click OK."));
229             loadRadio.setToolTipText(CBIntText.get("To load from an external file, select this option then click OK."));
230             loadRadio.setSelected(true);
231
232             ButtonGroup radioGroup = new ButtonGroup();
233             radioGroup.add(loadRadio);
234             radioGroup.add(saveRadio);
235
236             CBPanel radioPanel = new CBPanel();
237             radioPanel.setBorder(new TitledBorder JavaDoc(CBIntText.get(" Options ")));
238             radioPanel.addln(saveRadio);
239             radioPanel.addln(loadRadio);
240
241             mainPanel.addln(radioPanel);
242
243             JComponent c = addComponent();
244             if (c != null)
245                 mainPanel.addln(c);
246
247             mainPanel.addln(helpButton);
248             add(mainPanel, BorderLayout.CENTER);
249         }
250
251         /**
252          * Sets the enabled state of the saveRadio button.
253          * @param b the enabled state.
254          */

255         public void setSaveEnabled(boolean b)
256         {
257             saveRadio.setEnabled(b);
258         }
259
260         /**
261          * Sets whether the saveRadio is selected.
262          * @param b true to select the saveRadio, false otherwise.
263          */

264         public void setSaveRadioSelected(boolean b)
265         {
266             saveRadio.setSelected(b);
267         }
268
269         /**
270          * @return true if loadRadio is selected.
271          */

272         public boolean isLoadSelected()
273         {
274             return loadRadio.isSelected();
275         }
276
277         /**
278          * @return true if saveRadio is selected.
279          */

280         public boolean isSaveSelected()
281         {
282             return saveRadio.isSelected();
283         }
284     }
285 }
Popular Tags