PageRenderTime 120ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jcae/mesh-algos/src/org/jcae/netbeans/mesh/ExportGroupAction.java

http://github.com/jeromerobert/jCAE
Java | 181 lines | 145 code | 17 blank | 19 comment | 7 complexity | 41f381056165c04349efcdc4bbb2a3b4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * Project Info: http://jcae.sourceforge.net
  3. *
  4. * This program is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * (C) Copyright 2007, by EADS France
  19. */
  20. package org.jcae.netbeans.mesh;
  21. import java.awt.GridBagConstraints;
  22. import java.awt.GridBagLayout;
  23. import java.awt.Insets;
  24. import java.io.*;
  25. import java.util.HashSet;
  26. import javax.swing.BorderFactory;
  27. import javax.swing.JComboBox;
  28. import javax.swing.JFileChooser;
  29. import javax.swing.JFormattedTextField;
  30. import javax.swing.JLabel;
  31. import javax.swing.JOptionPane;
  32. import javax.swing.JPanel;
  33. import javax.swing.JSeparator;
  34. import javax.xml.parsers.ParserConfigurationException;
  35. import org.jcae.mesh.xmldata.MeshExporter;
  36. import org.jcae.netbeans.DoubleFormatter;
  37. import org.openide.ErrorManager;
  38. import org.openide.filesystems.FileObject;
  39. import org.openide.filesystems.FileUtil;
  40. import org.openide.nodes.Node;
  41. import org.openide.util.HelpCtx;
  42. import org.openide.util.actions.CookieAction;
  43. import org.xml.sax.SAXException;
  44. public class ExportGroupAction extends CookieAction
  45. {
  46. public static class ChooseUnitPanel extends JPanel
  47. {
  48. private JFormattedTextField scaleField = new JFormattedTextField(1E-3);
  49. private JComboBox unitCombo = new JComboBox(new Object[]{"m", "mm"});
  50. public ChooseUnitPanel()
  51. {
  52. setLayout(new GridBagLayout());
  53. setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  54. GridBagConstraints gbc = new GridBagConstraints();
  55. gbc.anchor = GridBagConstraints.WEST;
  56. add(new JLabel("Unit:"), gbc);
  57. gbc = new GridBagConstraints();
  58. gbc.gridy = 1;
  59. add(unitCombo, gbc);
  60. gbc = new GridBagConstraints();
  61. gbc.gridy = 2;
  62. gbc.fill = GridBagConstraints.HORIZONTAL;
  63. gbc.insets = new Insets(5, 5, 5, 5);
  64. add(new JSeparator(), gbc);
  65. gbc = new GridBagConstraints();
  66. gbc.gridy = 3;
  67. gbc.anchor = GridBagConstraints.WEST;
  68. add(new JLabel("Scale:"), gbc);
  69. gbc = new GridBagConstraints();
  70. gbc.gridy = 4;
  71. gbc.anchor = GridBagConstraints.NORTH;
  72. gbc.fill = GridBagConstraints.HORIZONTAL;
  73. gbc.weightx = 1.0;
  74. gbc.weighty = 1.0;
  75. add(scaleField, gbc);
  76. scaleField.setFormatterFactory(DoubleFormatter.FACTORY);
  77. }
  78. public boolean isMeters()
  79. {
  80. return unitCombo.getSelectedIndex() == 0;
  81. }
  82. public double getScale()
  83. {
  84. return (Double)scaleField.getValue();
  85. }
  86. }
  87. protected int mode()
  88. {
  89. return CookieAction.MODE_ALL;
  90. }
  91. protected Class[] cookieClasses()
  92. {
  93. return new Class[]{GroupNode.class};
  94. }
  95. @Override
  96. protected void performAction(Node[] arg0)
  97. {
  98. try
  99. {
  100. HashSet<String> set=new HashSet<String>();
  101. AmibeDataObject amibeDataObject=null;
  102. for(int i=0; i<arg0.length; i++)
  103. {
  104. amibeDataObject=arg0[i].getParentNode().getParentNode()
  105. .getLookup().lookup(AmibeDataObject.class);
  106. set.add(amibeDataObject.getMeshDirectory());
  107. }
  108. if(set.size()>1)
  109. {
  110. JOptionPane.showMessageDialog(null,
  111. "UNV export can only work with groups from the same mesh");
  112. return;
  113. }
  114. String meshDir=set.toArray()[0].toString();
  115. FileObject meshDirFile=
  116. amibeDataObject.getPrimaryFile().getParent();
  117. JFileChooser jfc=new JFileChooser();
  118. ChooseUnitPanel unitPanel=new ChooseUnitPanel();
  119. jfc.setAccessory(unitPanel);
  120. jfc.setCurrentDirectory(FileUtil.toFile(meshDirFile));
  121. if(jfc.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
  122. {
  123. String[] ids=new String[arg0.length];
  124. for(int i=0; i<arg0.length; i++)
  125. {
  126. GroupNode n=arg0[i].getCookie(GroupNode.class);
  127. ids[i]=n.getGroup().getName();
  128. }
  129. String unvFile=jfc.getSelectedFile().getPath();
  130. if(!unvFile.endsWith(".unv"))
  131. unvFile+=".unv";
  132. PrintStream stream=new PrintStream(new BufferedOutputStream(
  133. new FileOutputStream(unvFile)));
  134. MeshExporter.UNV exporter=new MeshExporter.UNV(new File(meshDir), ids);
  135. if(unitPanel.isMeters())
  136. exporter.setUnit(MeshExporter.UNV.Unit.METER);
  137. else
  138. exporter.setUnit(MeshExporter.UNV.Unit.MM);
  139. exporter.write(stream);
  140. stream.close();
  141. }
  142. }
  143. catch(IOException ex)
  144. {
  145. ErrorManager.getDefault().notify(ex);
  146. }
  147. catch (ParserConfigurationException e)
  148. {
  149. ErrorManager.getDefault().notify(e);
  150. }
  151. catch (SAXException e)
  152. {
  153. ErrorManager.getDefault().notify(e);
  154. }
  155. }
  156. public String getName()
  157. {
  158. return "Export as UNV";
  159. }
  160. public HelpCtx getHelpCtx()
  161. {
  162. return null;
  163. }
  164. }