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 ; 10 import java.awt.*; 11 import java.io.*; 12 import java.util.logging.Logger ; 13 import java.util.logging.Level ; 14 15 25 public class defaultbinaryeditor extends JFileChooser 26 implements abstractbinaryeditor 27 { 28 31 protected DN currentDN = null; 32 33 36 protected editablebinary editMe = null; 37 38 43 protected byte[] bytes; 44 45 48 protected JFileChooserAccessory accessory = new JFileChooserAccessory(); 49 50 53 protected Frame owner; 54 55 private static Logger log = Logger.getLogger(defaultbinaryeditor.class.getName()); 56 57 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 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 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 [] {file.getName()}), CBIntText.get("File Saved"), JOptionPane.INFORMATION_MESSAGE); 129 } 130 131 134 protected void load() 135 { 136 CBCache.cleanCache(currentDN.toString()); 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 [] {file.getName()}), CBIntText.get("File Loaded"), JOptionPane.INFORMATION_MESSAGE); 162 } 163 164 167 public void quit() 168 { 169 } 171 172 179 public void setValue(editablebinary editMe) 180 { 181 this.editMe = editMe; 182 bytes = editMe.getValue(); 183 } 184 185 189 190 public void setDN(DN dn) 191 { 192 currentDN = dn; 193 } 194 195 199 public JComponent addComponent() 200 { 201 return null; 202 } 203 204 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 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 (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 255 public void setSaveEnabled(boolean b) 256 { 257 saveRadio.setEnabled(b); 258 } 259 260 264 public void setSaveRadioSelected(boolean b) 265 { 266 saveRadio.setSelected(b); 267 } 268 269 272 public boolean isLoadSelected() 273 { 274 return loadRadio.isSelected(); 275 } 276 277 280 public boolean isSaveSelected() 281 { 282 return saveRadio.isSelected(); 283 } 284 } 285 } | Popular Tags |