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.ItemEvent; 026import java.awt.event.ItemListener; 027import java.awt.event.KeyEvent; 028import java.util.Iterator; 029import java.util.List; 030import java.util.Vector; 031 032import javax.swing.BorderFactory; 033import javax.swing.ButtonGroup; 034import javax.swing.JButton; 035import javax.swing.JComboBox; 036import javax.swing.JDialog; 037import javax.swing.JLabel; 038import javax.swing.JOptionPane; 039import javax.swing.JPanel; 040import javax.swing.JRadioButton; 041import javax.swing.JTextField; 042import javax.swing.border.TitledBorder; 043 044import hdf.object.DataFormat; 045import hdf.object.Dataset; 046import hdf.object.Datatype; 047import hdf.object.FileFormat; 048import hdf.object.Group; 049import hdf.object.HObject; 050import hdf.object.ScalarDS; 051 052/** 053 * NewImageDialog shows a message dialog requesting user input for creating a 054 * new HDF4/5 Image. 055 * 056 * @author Peter X. Cao 057 * @version 2.4 9/6/2007 058 */ 059public class NewImageDialog extends JDialog implements ActionListener, 060 ItemListener { 061 private static final long serialVersionUID = 6204900461720887966L; 062 063 private JTextField nameField, widthField, heightField; 064 065 @SuppressWarnings("rawtypes") 066 private JComboBox parentChoice; 067 068 private JRadioButton checkIndex, checkTrueColor, checkInterlacePixel, 069 checkInterlacePlane; 070 071 /** a list of current groups */ 072 private List<Object> groupList; 073 074 private boolean isH5; 075 076 private HObject newObject; 077 078 private FileFormat fileFormat; 079 080 private final Toolkit toolkit; 081 082 /** 083 * Constructs NewImageDialog with specified list of possible parent groups. 084 * 085 * @param owner 086 * the owner of the input 087 * @param pGroup 088 * the parent group which the new group is added to. 089 * @param objs 090 * the list of all objects. 091 */ 092 @SuppressWarnings({ "rawtypes", "unchecked" }) 093 public NewImageDialog(Frame owner, Group pGroup, List<?> objs) { 094 super(owner, "New HDF Image...", true); 095 096 newObject = null; 097 098 isH5 = pGroup.getFileFormat().isThisType( 099 FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5)); 100 fileFormat = pGroup.getFileFormat(); 101 toolkit = Toolkit.getDefaultToolkit(); 102 103 parentChoice = new JComboBox(); 104 groupList = new Vector<Object>(); 105 Object obj = null; 106 Iterator<?> iterator = objs.iterator(); 107 while (iterator.hasNext()) { 108 obj = iterator.next(); 109 if (obj instanceof Group) { 110 groupList.add(obj); 111 Group g = (Group) obj; 112 if (g.isRoot()) { 113 parentChoice.addItem(HObject.separator); 114 } 115 else { 116 parentChoice.addItem(g.getPath() + g.getName() 117 + HObject.separator); 118 } 119 } 120 } 121 122 if (pGroup.isRoot()) { 123 parentChoice.setSelectedItem(HObject.separator); 124 } 125 else { 126 parentChoice.setSelectedItem(pGroup.getPath() + pGroup.getName() 127 + HObject.separator); 128 } 129 130 JPanel contentPane = (JPanel) getContentPane(); 131 contentPane.setLayout(new BorderLayout(5, 5)); 132 contentPane.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 5)); 133 int w = 400 + (ViewProperties.getFontSize() - 12) * 15; 134 int h = 250 + (ViewProperties.getFontSize() - 12) * 10; 135 contentPane.setPreferredSize(new Dimension(w, h)); 136 137 JButton okButton = new JButton(" Ok "); 138 okButton.setActionCommand("Ok"); 139 okButton.setMnemonic(KeyEvent.VK_O); 140 okButton.addActionListener(this); 141 142 JButton cancelButton = new JButton("Cancel"); 143 cancelButton.setMnemonic(KeyEvent.VK_C); 144 cancelButton.setActionCommand("Cancel"); 145 cancelButton.addActionListener(this); 146 147 // set OK and CANCEL buttons 148 JPanel buttonPanel = new JPanel(); 149 buttonPanel.add(okButton); 150 buttonPanel.add(cancelButton); 151 contentPane.add(buttonPanel, BorderLayout.SOUTH); 152 153 // set name, parent, width and height panel 154 JPanel centerP = new JPanel(); 155 centerP.setLayout(new BorderLayout(5, 5)); 156 JPanel tmpP = new JPanel(); 157 tmpP.setLayout(new GridLayout(6, 1, 5, 5)); 158 tmpP.add(new JLabel("Image name: ")); 159 tmpP.add(new JLabel("Parent group: ")); 160 tmpP.add(new JLabel("Height: ")); 161 tmpP.add(new JLabel("Width: ")); 162 tmpP.add(new JLabel("Image type: ")); 163 tmpP.add(new JLabel("Data layout: ")); 164 centerP.add(tmpP, BorderLayout.WEST); 165 166 tmpP = new JPanel(); 167 tmpP.setLayout(new GridLayout(6, 1, 5, 5)); 168 tmpP.add(nameField = new JTextField()); 169 tmpP.add(parentChoice); 170 tmpP.add(heightField = new JTextField()); 171 tmpP.add(widthField = new JTextField()); 172 173 JPanel tmpP0 = new JPanel(); 174 tmpP0.setLayout(new GridLayout(1, 2)); 175 tmpP0.add(checkIndex = new JRadioButton("Indexed colormap", true)); 176 tmpP0.add(checkTrueColor = new JRadioButton("24-bit truecolor")); 177 tmpP0.setBorder(new TitledBorder("")); 178 tmpP.add(tmpP0); 179 180 tmpP0 = new JPanel(); 181 tmpP0.setLayout(new GridLayout(1, 2)); 182 tmpP0.add(checkInterlacePixel = new JRadioButton("Pixel interlace")); 183 tmpP0.add(checkInterlacePlane = new JRadioButton("Plane interlace")); 184 tmpP0.setBorder(new TitledBorder("")); 185 tmpP.add(tmpP0); 186 187 centerP.add(tmpP, BorderLayout.CENTER); 188 189 ButtonGroup bgroup = new ButtonGroup(); 190 bgroup.add(checkInterlacePixel); 191 bgroup.add(checkInterlacePlane); 192 bgroup = new ButtonGroup(); 193 bgroup.add(checkTrueColor); 194 bgroup.add(checkIndex); 195 checkIndex.addItemListener(this); 196 checkTrueColor.addItemListener(this); 197 checkInterlacePixel.setSelected(true); 198 checkInterlacePixel.setEnabled(false); 199 checkInterlacePlane.setEnabled(false); 200 201 centerP.setBorder(new TitledBorder("")); 202 contentPane.add(centerP, BorderLayout.CENTER); 203 204 // locate the H5Property dialog 205 Point l = owner.getLocation(); 206 l.x += 250; 207 l.y += 80; 208 setLocation(l); 209 validate(); 210 pack(); 211 } 212 213 public void actionPerformed(ActionEvent e) { 214 Object source = e.getSource(); 215 String cmd = e.getActionCommand(); 216 217 if (cmd.equals("Ok")) { 218 newObject = createHDFimage(); 219 if (newObject != null) { 220 dispose(); 221 } 222 } 223 if (cmd.equals("Cancel")) { 224 newObject = null; 225 dispose(); 226 ((Vector<Object>) groupList).setSize(0); 227 } 228 } 229 230 public void itemStateChanged(ItemEvent e) { 231 Object source = e.getSource(); 232 233 if (source.equals(checkIndex)) { 234 checkInterlacePixel.setSelected(true); 235 checkInterlacePixel.setEnabled(false); 236 checkInterlacePlane.setEnabled(false); 237 } 238 else if (source.equals(checkTrueColor)) { 239 checkInterlacePixel.setEnabled(true); 240 checkInterlacePlane.setEnabled(true); 241 } 242 } 243 244 private Dataset createHDFimage() { 245 Dataset dataset = null; 246 247 String name = nameField.getText(); 248 if (name != null) { 249 name = name.trim(); 250 } 251 if ((name == null) || (name.length() <= 0)) { 252 toolkit.beep(); 253 JOptionPane.showMessageDialog(this, 254 "Dataset name is not specified.", getTitle(), 255 JOptionPane.ERROR_MESSAGE); 256 return null; 257 } 258 259 if (name.indexOf(HObject.separator) >= 0) { 260 toolkit.beep(); 261 JOptionPane.showMessageDialog(this, 262 "Dataset name cannot contain path.", getTitle(), 263 JOptionPane.ERROR_MESSAGE); 264 return null; 265 } 266 267 Group pgroup = (Group) groupList.get(parentChoice.getSelectedIndex()); 268 if (pgroup == null) { 269 toolkit.beep(); 270 JOptionPane.showMessageDialog(this, "Select a parent group.", 271 getTitle(), JOptionPane.ERROR_MESSAGE); 272 return null; 273 } 274 275 int w = 0, h = 0; 276 try { 277 w = Integer.parseInt(widthField.getText()); 278 h = Integer.parseInt(heightField.getText()); 279 } 280 catch (Exception ex) { 281 toolkit.beep(); 282 JOptionPane.showMessageDialog(this, ex.getMessage(), getTitle(), 283 JOptionPane.ERROR_MESSAGE); 284 return null; 285 } 286 287 long[] dims = null; 288 int tclass = Datatype.CLASS_CHAR; 289 int tsign = Datatype.SIGN_NONE; 290 int tsize = 1; 291 int torder = Datatype.NATIVE; 292 int interlace = ScalarDS.INTERLACE_PIXEL; 293 int ncomp = 2; 294 295 if (checkIndex.isSelected()) { 296 // indexed colormap 297 if (isH5) { 298 long[] tmpdims = { h, w }; 299 dims = tmpdims; 300 } 301 else { 302 long[] tmpdims = { w, h }; 303 dims = tmpdims; 304 } 305 } 306 else { 307 // true color image 308 if (isH5) { 309 // HDF5 true color image 310 if (checkInterlacePixel.isSelected()) { 311 long[] tmpdims = { h, w, 3 }; 312 dims = tmpdims; 313 } 314 else { 315 interlace = ScalarDS.INTERLACE_PLANE; 316 long[] tmpdims = { 3, h, w }; 317 dims = tmpdims; 318 } 319 } 320 else { 321 // HDF4 true color image 322 ncomp = 3; 323 long[] tmpdims = { w, h }; 324 dims = tmpdims; 325 if (checkInterlacePlane.isSelected()) { 326 interlace = ScalarDS.INTERLACE_PLANE; 327 } 328 } 329 } 330 331 try { 332 333 Datatype datatype = fileFormat.createDatatype(tclass, tsize, 334 torder, tsign); 335 dataset = fileFormat.createImage(name, pgroup, datatype, dims, 336 dims, null, -1, ncomp, interlace, null); 337 dataset.init(); 338 } 339 catch (Exception ex) { 340 toolkit.beep(); 341 JOptionPane.showMessageDialog(this, ex.getMessage(), getTitle(), 342 JOptionPane.ERROR_MESSAGE); 343 return null; 344 } 345 346 return dataset; 347 } 348 349 /** 350 * Returns the new dataset created. 351 * 352 * @return The new Dataset created 353 */ 354 public DataFormat getObject() { 355 return newObject; 356 } 357 358 /** 359 * Returns the parent group of the new dataset. 360 * 361 * @return The parent group of the new Dataset 362 */ 363 public Group getParentGroup() { 364 return (Group) groupList.get(parentChoice.getSelectedIndex()); 365 } 366}