001/***************************************************************************** 002 * Copyright by The HDF Group. * 003 * Copyright by the Board of Trustees of the University of Illinois. * 004 * All rights reserved. * 005 * * 006 * This file is part of the HDF Java Products distribution. * 007 * The full copyright notice, including terms governing use, modification, * 008 * and redistribution, is contained in the files COPYING and Copyright.html. * 009 * COPYING can be found at the root of the source code distribution tree. * 010 * Or, see http://hdfgroup.org/products/hdf-java/doc/Copyright.html. * 011 * If you do not have access to either file, you may request a copy from * 012 * help@hdfgroup.org. * 013 ****************************************************************************/ 014 015package hdf.view; 016 017import java.awt.BorderLayout; 018import java.awt.Dimension; 019import java.awt.Frame; 020import java.awt.GridLayout; 021import java.awt.Point; 022import java.awt.Toolkit; 023import java.awt.event.ActionEvent; 024import java.awt.event.ActionListener; 025import java.awt.event.KeyEvent; 026import java.io.File; 027import java.util.Iterator; 028import java.util.List; 029 030import javax.swing.BorderFactory; 031import javax.swing.JButton; 032import javax.swing.JDialog; 033import javax.swing.JFileChooser; 034import javax.swing.JLabel; 035import javax.swing.JOptionPane; 036import javax.swing.JPanel; 037import javax.swing.JTextField; 038 039import hdf.object.FileFormat; 040 041/** 042 * FileConversionDialog shows a message dialog requesting user input for 043 * converting files. 044 * 045 * @author Peter X. Cao 046 * @version 2.4 9/6/2007 047 */ 048public class FileConversionDialog extends JDialog implements ActionListener { 049 private static final long serialVersionUID = 2645021913986116744L; 050 051 private String fileTypeFrom, fileTypeTo; 052 053 private JTextField srcFileField, dstFileField; 054 055 private boolean isConverted; 056 057 private boolean isConvertedFromImage; 058 059 private String convertedFile; 060 061 private String toFileExtension; 062 063 private List fileList; 064 065 private String currentDir; 066 067 private final Toolkit toolkit; 068 069 /** 070 * Constructs a FileConversionDialog 071 * 072 * @param owner 073 * The owner of the dialog. 074 * @param typeFrom 075 * source file type 076 * @param typeTo 077 * destinatin file type 078 * @param dir 079 * current file directory 080 * @param openFiles 081 * The list of current open files 082 */ 083 public FileConversionDialog(Frame owner, String typeFrom, String typeTo, 084 String dir, List openFiles) { 085 super(owner, "Convert File...", true); 086 087 fileTypeFrom = typeFrom; 088 fileTypeTo = typeTo; 089 isConverted = false; 090 isConvertedFromImage = false; 091 fileList = openFiles; 092 toFileExtension = ""; 093 currentDir = dir; 094 toolkit = Toolkit.getDefaultToolkit(); 095 096 String fromName = "Source"; 097 if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF5)) { 098 toFileExtension = ".h5"; 099 setTitle("Convert Image to HDF5 ..."); 100 fromName = "IMAGE"; 101 isConvertedFromImage = true; 102 } 103 else if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF4)) { 104 toFileExtension = ".hdf"; 105 setTitle("Convert Image to HDF4 ..."); 106 fromName = "IMAGE"; 107 isConvertedFromImage = true; 108 } 109 110 // layout the components 111 JPanel contentPane = (JPanel) getContentPane(); 112 contentPane.setLayout(new BorderLayout(5, 5)); 113 contentPane.setBorder(BorderFactory.createEmptyBorder(15, 5, 5, 5)); 114 int w = 450 + (ViewProperties.getFontSize() - 12) * 15; 115 int h = 120 + (ViewProperties.getFontSize() - 12) * 10; 116 contentPane.setPreferredSize(new Dimension(w, h)); 117 118 // add the top panel for enter file name 119 JPanel p = new JPanel(); 120 p.setLayout(new BorderLayout(5, 5)); 121 122 JPanel p0 = new JPanel(); 123 p0.setLayout(new GridLayout(2, 1, 5, 5)); 124 p0.add(new JLabel(fromName + " File: ")); 125 p0.add(new JLabel("HDF File: ")); 126 p.add(p0, BorderLayout.WEST); 127 128 p0 = new JPanel(); 129 p0.setLayout(new GridLayout(2, 1, 5, 5)); 130 p0.add(srcFileField = new JTextField()); 131 p0.add(dstFileField = new JTextField()); 132 p.add(p0, BorderLayout.CENTER); 133 134 p0 = new JPanel(); 135 p0.setLayout(new GridLayout(2, 1, 5, 5)); 136 JButton jButton = new JButton("Browse..."); 137 jButton.setActionCommand("Browse source file"); 138 jButton.addActionListener(this); 139 jButton.setName("sourcefilebutton"); 140 p0.add(jButton); 141 jButton = new JButton("Browse..."); 142 jButton.setActionCommand("Browse target file"); 143 jButton.addActionListener(this); 144 p0.add(jButton); 145 p.add(p0, BorderLayout.EAST); 146 147 contentPane.add(p, BorderLayout.CENTER); 148 149 JButton okButton = new JButton(" Ok "); 150 okButton.setMnemonic(KeyEvent.VK_O); 151 okButton.setActionCommand("Ok"); 152 okButton.addActionListener(this); 153 okButton.setName("okbutton"); 154 155 JButton cancelButton = new JButton("Cancel"); 156 cancelButton.setMnemonic(KeyEvent.VK_C); 157 cancelButton.setActionCommand("Cancel"); 158 cancelButton.addActionListener(this); 159 160 p = new JPanel(); 161 p.add(okButton); 162 p.add(cancelButton); 163 164 contentPane.add(p, BorderLayout.SOUTH); 165 166 Point l = owner.getLocation(); 167 l.x += 250; 168 l.y += 80; 169 setLocation(l); 170 pack(); 171 } 172 173 public void actionPerformed(ActionEvent e) { 174 Object source = e.getSource(); 175 String cmd = e.getActionCommand(); 176 177 if (cmd.equals("Ok")) { 178 isConverted = convert(); 179 180 if (isConverted) { 181 dispose(); 182 } 183 } 184 else if (cmd.equals("Cancel")) { 185 isConverted = false; 186 convertedFile = null; 187 dispose(); 188 } 189 else if (cmd.equals("Browse source file")) { 190 JFileChooser fchooser = new JFileChooser(currentDir); 191 if (isConvertedFromImage) 192 fchooser.setFileFilter(DefaultFileFilter.getImageFileFilter()); 193 194 int returnVal = fchooser.showOpenDialog(this); 195 196 if (returnVal != JFileChooser.APPROVE_OPTION) { 197 return; 198 } 199 200 File choosedFile = fchooser.getSelectedFile(); 201 if (choosedFile == null) { 202 return; 203 } 204 205 String fname = choosedFile.getAbsolutePath(); 206 207 if (fname == null) { 208 return; 209 } 210 211 currentDir = choosedFile.getParent(); 212 srcFileField.setText(fname); 213 dstFileField.setText(fname + toFileExtension); 214 } 215 else if (cmd.equals("Browse target file")) { 216 JFileChooser fchooser = new JFileChooser(); 217 int returnVal = fchooser.showOpenDialog(this); 218 219 if (returnVal != JFileChooser.APPROVE_OPTION) { 220 return; 221 } 222 223 File choosedFile = fchooser.getSelectedFile(); 224 if (choosedFile == null) { 225 return; 226 } 227 228 String fname = choosedFile.getAbsolutePath(); 229 230 if (fname == null) { 231 return; 232 } 233 234 dstFileField.setText(fname); 235 } 236 } 237 238 /** convert file */ 239 private boolean convert() { 240 boolean converted = false; 241 String srcFile = srcFileField.getText(); 242 String dstFile = dstFileField.getText(); 243 244 if ((srcFile == null) || (dstFile == null)) { 245 return false; 246 } 247 248 srcFile = srcFile.trim(); 249 dstFile = dstFile.trim(); 250 if ((srcFile == null) || (srcFile.length() <= 0) || (dstFile == null) 251 || (dstFile.length() <= 0)) { 252 return false; 253 } 254 255 // verify the source file 256 File f = new File(srcFile); 257 if (!f.exists()) { 258 toolkit.beep(); 259 JOptionPane.showMessageDialog(this, "Source file does not exist.", 260 this.getTitle(), JOptionPane.ERROR_MESSAGE); 261 return false; 262 } 263 else if (f.isDirectory()) { 264 toolkit.beep(); 265 JOptionPane.showMessageDialog(this, "Source file is a directory.", 266 this.getTitle(), JOptionPane.ERROR_MESSAGE); 267 return false; 268 } 269 270 // verify target file 271 String srcPath = f.getParent(); 272 f = new File(dstFile); 273 File pfile = f.getParentFile(); 274 if (pfile == null) { 275 dstFile = srcPath + File.separator + dstFile; 276 f = new File(dstFile); 277 } 278 else if (!pfile.exists()) { 279 toolkit.beep(); 280 JOptionPane.showMessageDialog(this, 281 "Destination file path does not exist at\n" 282 + pfile.getPath(), this.getTitle(), 283 JOptionPane.ERROR_MESSAGE); 284 return false; 285 } 286 287 // check if the file is in use 288 if (fileList != null) { 289 FileFormat theFile = null; 290 Iterator iterator = fileList.iterator(); 291 while (iterator.hasNext()) { 292 theFile = (FileFormat) iterator.next(); 293 if (theFile.getFilePath().equals(dstFile)) { 294 toolkit.beep(); 295 JOptionPane.showMessageDialog(this, 296 "The destination file is being used.", getTitle(), 297 JOptionPane.ERROR_MESSAGE); 298 return false; 299 } 300 } 301 } 302 303 int newFileFlag = -1; 304 if (f.exists()) { 305 newFileFlag = JOptionPane.showConfirmDialog(this, 306 "Destination file exists. Do you want to replace it ?", 307 this.getTitle(), JOptionPane.YES_NO_OPTION); 308 if (newFileFlag == JOptionPane.NO_OPTION) { 309 return false; 310 } 311 } 312 313 try { 314 Tools.convertImageToHDF(srcFile, dstFile, fileTypeFrom, fileTypeTo); 315 convertedFile = dstFile; 316 converted = true; 317 } 318 catch (Exception ex) { 319 convertedFile = null; 320 converted = false; 321 toolkit.beep(); 322 JOptionPane.showMessageDialog(this, ex.getMessage(), this 323 .getTitle(), JOptionPane.ERROR_MESSAGE); 324 return false; 325 } 326 327 return converted; 328 } 329 330 public boolean isFileConverted() { 331 return isConverted; 332 } 333 334 public String getConvertedFile() { 335 return convertedFile; 336 } 337}