/plugins/Calculator/tags/1.2.0/src/ise/calculator/ConstantsEditor.java

# · Java · 141 lines · 118 code · 17 blank · 6 comment · 15 complexity · 4eb1c3bc0fe9659466854787dddd6255 MD5 · raw file

  1. /*
  2. This file, unless otherwise noted, is wholly the work of Dale Anson,
  3. danson@grafidog.com. The complete contents of this file is hereby
  4. released into the public domain, with no rights reserved. For questions,
  5. concerns, or comments, please email the author.
  6. */
  7. package ise.calculator;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.*;
  11. import javax.swing.*;
  12. import javax.swing.event.*;
  13. public class ConstantsEditor extends JDialog {
  14. private String LS = System.getProperty( "line.separator" );
  15. public ConstantsEditor( JDialog parent, File file ) {
  16. this( parent, "Edit Constant", file );
  17. }
  18. public ConstantsEditor( final JDialog parent, String constant_title, final File constant_file ) {
  19. super( parent, constant_title, true );
  20. JPanel contents = new JPanel( new LambdaLayout() );
  21. try {
  22. String name = "";
  23. String value = "";
  24. String uncertainty = "";
  25. String units = "";
  26. if ( constant_file.exists() && constant_file.length() > 0) {
  27. FunctionReader fr = new FunctionReader(constant_file);
  28. String desc = fr.getDescription();
  29. String[] split = desc.split( "<br>" );
  30. name = split[ 0 ].substring( split[ 0 ].indexOf( " " ) ).trim();
  31. value = split[ 1 ].substring( split[ 1 ].indexOf( " " ) ).trim();
  32. uncertainty = split[ 2 ].substring( split[ 2 ].indexOf( " " ) ).trim();
  33. units = split[ 3 ].substring( split[ 3 ].indexOf( " " ) ).trim();
  34. }
  35. final JTextField name_field = new JTextField( name );
  36. final JButton name_view = new JButton( "View" );
  37. final JTextField value_field = new JTextField( );
  38. value_field.setDocument( new RegisterDocument() );
  39. value_field.setText( value );
  40. final JTextField uncertainty_field = new JTextField( uncertainty );
  41. final JButton uncertainty_view = new JButton( "View" );
  42. final JTextField units_field = new JTextField( units );
  43. final JButton units_view = new JButton( "View" );
  44. ActionListener view_listener = new ActionListener() {
  45. public void actionPerformed( ActionEvent ae ) {
  46. JButton btn = ( JButton ) ae.getSource();
  47. String text = "";
  48. if ( btn.equals( name_view ) ) {
  49. text = name_field.getText();
  50. }
  51. else if ( btn.equals( uncertainty_view ) ) {
  52. text = uncertainty_field.getText();
  53. }
  54. else if ( btn.equals( units_view ) ) {
  55. text = units_field.getText();
  56. }
  57. JOptionPane.showMessageDialog( ConstantsEditor.this, "<html>" + text, "View", JOptionPane.PLAIN_MESSAGE );
  58. }
  59. };
  60. name_view.addActionListener( view_listener );
  61. uncertainty_view.addActionListener( view_listener );
  62. units_view.addActionListener( view_listener );
  63. KappaLayout btn_layout = new KappaLayout();
  64. JPanel btn_panel = new JPanel( btn_layout );
  65. JButton save_btn = new JButton( "Save" );
  66. JButton cancel_btn = new JButton( "Cancel" );
  67. btn_panel.add( save_btn, "0, 0, 1, 1, E, w,3" );
  68. btn_panel.add( cancel_btn, "1, 0, 1, 1, W, w,3" );
  69. btn_layout.makeColumnsSameWidth( 0, 1 );
  70. contents.add( new JLabel( "Name:" ), "0, 0, 1, 1, 0, w, 3" );
  71. contents.add( name_field, "1, 0, 5, 1, 0, w, 3" );
  72. contents.add( name_view, "6, 0, 1, 1, 0, w, 3" );
  73. contents.add( new JLabel( "Value:" ), "0, 1, 1, 1, 0, w, 3" );
  74. contents.add( value_field, "1, 1, 6, 1, 0, w, 3" );
  75. contents.add( new JLabel( "Uncertainty:" ), "0, 2, 1, 1, 0, w, 3" );
  76. contents.add( uncertainty_field, "1, 2, 5, 1, 0, wh, 3" );
  77. contents.add( uncertainty_view, "6, 2, 1, 1, 0, w, 3" );
  78. contents.add( new JLabel( "Units:" ), "0, 3, 1, 1, 0, w, 3" );
  79. contents.add( units_field, "1, 3, 5, 1, 0, wh, 3" );
  80. contents.add( units_view, "6, 3, 1, 1, 0, w, 3" );
  81. contents.add( btn_panel, "0, 4, 7, 1, 0, , 6" );
  82. save_btn.addActionListener( new ActionListener() {
  83. public void actionPerformed( ActionEvent ae ) {
  84. try {
  85. String name = name_field.getText();
  86. if (name == null || name.trim().length() == 0)
  87. throw new Exception("Constant name must be entered.");
  88. String value = value_field.getText();
  89. if (value == null || value.trim().length() == 0)
  90. throw new Exception("Value must be entered.");
  91. String desc = "<html>Constant: " + name + "<br>Value: " + value + "<br>Uncertainty: " + uncertainty_field.getText().trim() + "<br>Units: " + units_field.getText().trim();
  92. FunctionWriter fw = new FunctionWriter(constant_file );
  93. fw.setConstant(true);
  94. fw.write(name, desc, value);
  95. }
  96. catch ( Exception e ) {
  97. JOptionPane.showMessageDialog( parent, "Unable to save: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
  98. }
  99. setVisible(false);;
  100. dispose();
  101. }
  102. }
  103. );
  104. cancel_btn.addActionListener( new ActionListener() {
  105. public void actionPerformed( ActionEvent ae ) {
  106. setVisible(false);
  107. dispose();
  108. }
  109. }
  110. );
  111. setContentPane( contents );
  112. pack();
  113. GUIUtils.center( parent, this );
  114. setVisible( true );
  115. }
  116. catch ( Exception e ) {
  117. JOptionPane.showMessageDialog( parent, "Error: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
  118. e.printStackTrace();
  119. }
  120. }
  121. public static void main ( String[] args ) {
  122. new ConstantsEditor( new JDialog(), "test", new File( "c:/Documents and Settings/danson/.calc/const7543.calc" ) );
  123. }
  124. }