1 package gov.nasa.pds.ltdt.gui;
2
3 import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
4 import gov.nasa.pds.ltdt.label.reformat.LabelReformatter;
5
6 import java.awt.event.ActionEvent;
7 import java.io.ByteArrayInputStream;
8 import java.io.IOException;
9 import java.util.Properties;
10
11 import javax.swing.AbstractAction;
12 import javax.swing.Icon;
13 import javax.swing.JOptionPane;
14
15 /***
16 * Reformat Action to handle template editor reformat requests
17 * @author jwang
18 *
19 */
20 public class ReformatAction extends AbstractAction {
21
22 private MainWindow window = null;
23 private Properties props;
24
25 /***
26 * Constructor
27 * @param name
28 * @param image
29 * @param desc
30 * @param mnemonic
31 * @param props
32 * @param window
33 */
34 public ReformatAction (String name, Icon image,
35 String desc, Integer mnemonic, Properties props,
36 MainWindow window) {
37 super (name, image);
38 putValue (SHORT_DESCRIPTION, desc);
39 putValue (MNEMONIC_KEY, mnemonic);
40
41 this.props = props;
42 this.window = window;
43
44
45 }
46
47 public void actionPerformed(ActionEvent ae) {
48
49 try {
50
51
52 int index = window.editorPane.jta.getCaretPosition();
53
54 ByteArrayInputStream str = new ByteArrayInputStream(window.editorPane.jta.getText().toString().getBytes());
55 LabelReformatter lr = new LabelReformatter();
56 window.editorPane.jta.setText(lr.reformat(str).toString());
57
58
59
60
61
62 int size = window.editorPane.jta.getText().length();
63 if (size<index) index=size-1;
64
65 window.editorPane.jta.setCaretPosition(index);
66 window.editorPane.jta.grabFocus();
67
68 props.setProperty(LTDTKeys.PROJECTALTERED, "true");
69 }
70 catch (IOException e) {
71 JOptionPane.showMessageDialog(window, "IOError during reformatting.",
72 "Warning!", JOptionPane.WARNING_MESSAGE);
73 }
74 }
75 }