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 COPYING file, which can be found * 009 * at the root of the source code distribution tree, * 010 * or in https://www.hdfgroup.org/licenses. * 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.dialog; 016 017import java.io.File; 018import java.util.Iterator; 019import java.util.List; 020 021import hdf.object.FileFormat; 022import hdf.view.DefaultFileFilter; 023import hdf.view.Tools; 024import hdf.view.ViewProperties; 025 026import org.eclipse.swt.SWT; 027import org.eclipse.swt.events.DisposeEvent; 028import org.eclipse.swt.events.DisposeListener; 029import org.eclipse.swt.events.SelectionAdapter; 030import org.eclipse.swt.events.SelectionEvent; 031import org.eclipse.swt.graphics.Font; 032import org.eclipse.swt.graphics.Point; 033import org.eclipse.swt.graphics.Rectangle; 034import org.eclipse.swt.layout.GridData; 035import org.eclipse.swt.layout.GridLayout; 036import org.eclipse.swt.widgets.Button; 037import org.eclipse.swt.widgets.Composite; 038import org.eclipse.swt.widgets.Dialog; 039import org.eclipse.swt.widgets.Display; 040import org.eclipse.swt.widgets.FileDialog; 041import org.eclipse.swt.widgets.Label; 042import org.eclipse.swt.widgets.Shell; 043import org.eclipse.swt.widgets.Text; 044 045/** 046 * ImageConversionDialog shows a message dialog requesting user input for 047 * converting files. 048 * 049 * @author Jordan T. Henderson 050 * @version 2.4 1/28/2016 051 */ 052public class ImageConversionDialog extends Dialog { 053 private Shell shell; 054 055 private Font curFont; 056 057 private String fileTypeFrom, fileTypeTo; 058 059 private Text srcFileField, dstFileField; 060 061 private boolean isConverted; 062 063 private boolean isConvertedFromImage; 064 065 private String convertedFile; 066 067 private String toFileExtension; 068 069 private List<FileFormat> fileList; 070 071 private String currentDir; 072 073 /** 074 * Constructs a FileConversionDialog 075 * 076 * @param parent 077 * The parent shell of the dialog. 078 * @param typeFrom 079 * source file type 080 * @param typeTo 081 * destination file type 082 * @param dir 083 * current file directory 084 * @param openFiles 085 * The list of currently open files 086 */ 087 public ImageConversionDialog(Shell parent, String typeFrom, String typeTo, String dir, 088 List<FileFormat> openFiles) 089 { 090 super(parent, SWT.APPLICATION_MODAL); 091 092 try { 093 curFont = new Font(Display.getCurrent(), ViewProperties.getFontType(), 094 ViewProperties.getFontSize(), SWT.NORMAL); 095 } 096 catch (Exception ex) { 097 curFont = null; 098 } 099 100 fileTypeFrom = typeFrom; 101 fileTypeTo = typeTo; 102 isConverted = false; 103 isConvertedFromImage = false; 104 fileList = openFiles; 105 toFileExtension = ""; 106 currentDir = dir; 107 } 108 109 /** 110 * Open the ImageConversionDialog for converting images. 111 */ 112 public void open() 113 { 114 Shell parent = getParent(); 115 shell = new Shell(parent, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL); 116 shell.setFont(curFont); 117 shell.setText(parent.getText()); 118 shell.setImages(ViewProperties.getHdfIcons()); 119 shell.setLayout(new GridLayout(1, true)); 120 121 if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF5)) { 122 toFileExtension = ".h5"; 123 shell.setText("Convert Image to HDF5 ..."); 124 isConvertedFromImage = true; 125 } 126 else if (fileTypeTo.equals(FileFormat.FILE_TYPE_HDF4)) { 127 toFileExtension = ".hdf"; 128 shell.setText("Convert Image to HDF4 ..."); 129 isConvertedFromImage = true; 130 } 131 132 // Create content region 133 Composite contentComposite = new Composite(shell, SWT.NONE); 134 contentComposite.setLayout(new GridLayout(3, false)); 135 contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 136 137 Label label = new Label(contentComposite, SWT.RIGHT); 138 label.setFont(curFont); 139 label.setText("IMAGE File: "); 140 141 srcFileField = new Text(contentComposite, SWT.SINGLE | SWT.BORDER); 142 srcFileField.setFont(curFont); 143 GridData fieldData = new GridData(SWT.FILL, SWT.FILL, true, false); 144 fieldData.minimumWidth = 350; 145 srcFileField.setLayoutData(fieldData); 146 147 Button browseButton = new Button(contentComposite, SWT.PUSH); 148 browseButton.setFont(curFont); 149 browseButton.setText("Browse..."); 150 browseButton.addSelectionListener(new SelectionAdapter() { 151 public void widgetSelected(SelectionEvent e) 152 { 153 FileDialog fChooser = new FileDialog(shell, SWT.OPEN); 154 fChooser.setFilterPath(currentDir); 155 156 if (isConvertedFromImage) { 157 DefaultFileFilter filter = DefaultFileFilter.getImageFileFilter(); 158 fChooser.setFilterExtensions(new String[] {"*", filter.getExtensions()}); 159 fChooser.setFilterNames(new String[] {"All Files", filter.getDescription()}); 160 fChooser.setFilterIndex(1); 161 } 162 else { 163 fChooser.setFilterExtensions(new String[] {"*"}); 164 fChooser.setFilterNames(new String[] {"All Files"}); 165 fChooser.setFilterIndex(0); 166 } 167 168 String filename = fChooser.open(); 169 170 if (filename == null) { 171 return; 172 } 173 174 File chosenFile = new File(filename); 175 176 currentDir = chosenFile.getParent(); 177 srcFileField.setText(chosenFile.getAbsolutePath()); 178 dstFileField.setText(chosenFile.getAbsolutePath() + toFileExtension); 179 } 180 }); 181 182 label = new Label(contentComposite, SWT.RIGHT); 183 label.setFont(curFont); 184 label.setText("HDF File: "); 185 186 dstFileField = new Text(contentComposite, SWT.SINGLE | SWT.BORDER); 187 dstFileField.setFont(curFont); 188 dstFileField.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 189 190 browseButton = new Button(contentComposite, SWT.PUSH); 191 browseButton.setFont(curFont); 192 browseButton.setText("Browse..."); 193 browseButton.addSelectionListener(new SelectionAdapter() { 194 public void widgetSelected(SelectionEvent e) 195 { 196 FileDialog fChooser = new FileDialog(shell, SWT.OPEN); 197 198 fChooser.setFilterExtensions(new String[] {"*"}); 199 fChooser.setFilterNames(new String[] {"All Files"}); 200 fChooser.setFilterIndex(0); 201 202 String filename = fChooser.open(); 203 204 if (filename == null) { 205 return; 206 } 207 208 dstFileField.setText(filename); 209 } 210 }); 211 212 // Dummy label to fill space as dialog is resized 213 new Label(shell, SWT.NONE).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 214 215 // Create Ok/Cancel button 216 Composite buttonComposite = new Composite(shell, SWT.NONE); 217 buttonComposite.setLayout(new GridLayout(2, true)); 218 buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1)); 219 220 Button okButton = new Button(buttonComposite, SWT.PUSH); 221 okButton.setFont(curFont); 222 okButton.setText(" &OK "); 223 okButton.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false)); 224 okButton.addSelectionListener(new SelectionAdapter() { 225 public void widgetSelected(SelectionEvent e) 226 { 227 isConverted = convert(); 228 229 if (isConverted) { 230 shell.dispose(); 231 } 232 } 233 }); 234 235 Button cancelButton = new Button(buttonComposite, SWT.PUSH); 236 cancelButton.setFont(curFont); 237 cancelButton.setText(" &Cancel "); 238 cancelButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false)); 239 cancelButton.addSelectionListener(new SelectionAdapter() { 240 public void widgetSelected(SelectionEvent e) 241 { 242 isConverted = false; 243 convertedFile = null; 244 shell.dispose(); 245 } 246 }); 247 248 shell.pack(); 249 250 shell.addDisposeListener(new DisposeListener() { 251 public void widgetDisposed(DisposeEvent e) 252 { 253 if (curFont != null) 254 curFont.dispose(); 255 } 256 }); 257 258 shell.setMinimumSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 259 260 Rectangle parentBounds = parent.getBounds(); 261 Point shellSize = shell.getSize(); 262 shell.setLocation((parentBounds.x + (parentBounds.width / 2)) - (shellSize.x / 2), 263 (parentBounds.y + (parentBounds.height / 2)) - (shellSize.y / 2)); 264 265 shell.open(); 266 267 Display display = parent.getDisplay(); 268 while (!shell.isDisposed()) { 269 if (!display.readAndDispatch()) 270 display.sleep(); 271 } 272 } 273 274 /** Convert file */ 275 private boolean convert() 276 { 277 boolean converted = false; 278 String srcFile = srcFileField.getText(); 279 String dstFile = dstFileField.getText(); 280 281 if ((srcFile == null) || (dstFile == null)) { 282 return false; 283 } 284 285 srcFile = srcFile.trim(); 286 dstFile = dstFile.trim(); 287 if ((srcFile == null) || (srcFile.length() <= 0) || (dstFile == null) || (dstFile.length() <= 0)) { 288 return false; 289 } 290 291 // verify the source file 292 File f = new File(srcFile); 293 if (!f.exists()) { 294 shell.getDisplay().beep(); 295 Tools.showError(shell, "Convert", "Source file does not exist."); 296 return false; 297 } 298 else if (f.isDirectory()) { 299 shell.getDisplay().beep(); 300 Tools.showError(shell, "Convert", "Source file is a directory."); 301 return false; 302 } 303 304 // verify target file 305 String srcPath = f.getParent(); 306 f = new File(dstFile); 307 File pfile = f.getParentFile(); 308 if (pfile == null) { 309 dstFile = srcPath + File.separator + dstFile; 310 f = new File(dstFile); 311 } 312 else if (!pfile.exists()) { 313 shell.getDisplay().beep(); 314 Tools.showError(shell, "Convert", "Destination file path does not exist at\n" + pfile.getPath()); 315 return false; 316 } 317 318 // check if the file is in use 319 if (fileList != null) { 320 FileFormat theFile = null; 321 Iterator<FileFormat> iterator = fileList.iterator(); 322 while (iterator.hasNext()) { 323 theFile = (FileFormat)iterator.next(); 324 if (theFile.getFilePath().equals(dstFile)) { 325 shell.getDisplay().beep(); 326 Tools.showError(shell, "Convert", "The destination file is being used."); 327 return false; 328 } 329 } 330 } 331 332 if (f.exists()) { 333 if (!Tools.showConfirm(shell, "Convert", "Destination file exists. Do you want to replace it ?")) 334 return false; 335 } 336 337 try { 338 Tools.convertImageToHDF(srcFile, dstFile, fileTypeFrom, fileTypeTo); 339 convertedFile = dstFile; 340 converted = true; 341 } 342 catch (Exception ex) { 343 convertedFile = null; 344 converted = false; 345 shell.getDisplay().beep(); 346 Tools.showError(shell, "Convert", ex.getMessage()); 347 return false; 348 } 349 350 return converted; 351 } 352 353 /** 354 * if an image file has been converted. 355 * 356 * @return the state of conversion 357 */ 358 public boolean isFileConverted() { return isConverted; } 359 360 /** 361 * get the file of an image file that has been converted. 362 * 363 * @return the name of the converted file 364 */ 365 public String getConvertedFile() { return convertedFile; } 366}