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