1 /*
2 * Created on 24.06.2003
3 *
4 * To change the template for this generated file go to
5 * Window>Preferences>Java>Code Generation>Code and Comments
6 */
7 package net.sf.bloof.browser;
8
9 import java.awt.BorderLayout;
10 import java.awt.Dimension;
11 import java.awt.Frame;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.io.File;
15 import java.io.FileWriter;
16 import java.io.IOException;
17
18 import javax.swing.Box;
19 import javax.swing.BoxLayout;
20 import javax.swing.JButton;
21 import javax.swing.JComponent;
22 import javax.swing.JDialog;
23 import javax.swing.JFileChooser;
24 import javax.swing.JOptionPane;
25 import javax.swing.JPanel;
26 import javax.swing.JTextField;
27
28 import net.n3.nanoxml.XMLWriter;
29 import net.sf.bloof.browser.intl.Messages;
30 import net.sf.bloof.browser.intl.Text;
31 import net.sf.bloof.metrics.Metric;
32
33 /**
34 *
35 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
36 * @version $Id: DialogExportResult.java,v 1.2 2003/06/28 13:41:25 pekacki Exp $
37 */
38 public class DialogExportResult extends JDialog {
39
40 public DialogExportResult(Frame aFrame, String aTitle, boolean aModal, Metric aMetric) {
41 super(aFrame, aTitle, aModal);
42 mMetric = aMetric;
43 try {
44 panelInit();
45 pack();
46 } catch (Exception ex) {
47 ex.printStackTrace();
48 }
49 }
50
51 private void buttonCancelAction(ActionEvent aE) {
52 Main.getGuiControl().processUselessEvent(aE);
53 this.dispose();
54 }
55
56 private void buttonOkAction(ActionEvent aE) {
57 Main.getGuiControl().processUselessEvent(aE);
58 File f = new File(mFileName.getText());
59 if (!f.exists()) {
60 try {
61 FileWriter fw = new FileWriter(f);
62 XMLWriter xw = new XMLWriter(fw);
63 fw.write(XML_HEAD);
64 xw.write(mMetric.getResultXml(), true, 4);
65 fw.flush();
66 fw.close();
67 } catch (IOException e) {
68 JOptionPane.showMessageDialog(
69 null,
70 "Could not export file." + f.getAbsolutePath() + " Reason:" + e.toString(),
71 "Error occured",
72 JOptionPane.ERROR_MESSAGE);
73 }
74 } else {
75 JOptionPane.showMessageDialog(
76 null,
77 "File does already exist." + f.getAbsolutePath(),
78 "Error occured",
79 JOptionPane.ERROR_MESSAGE);
80 }
81 this.dispose();
82 }
83 /**
84 * @return
85 */
86 private JComponent initFilePanel() {
87 /* init cvs local file pane */
88 JPanel filePanel = new JPanel();
89 filePanel.setLayout(new BorderLayout());
90 JPanel contentPanel = new JPanel();
91 contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
92 JButton chooseFile = new JButton(Messages.getString(Text.CHOOSE_FILE));
93 chooseFile.addActionListener(new ActionListener() {
94 /**
95 * @see java.awt.event.ActionListener#actionPerformed( ActionEvent )
96 */
97 public void actionPerformed(ActionEvent aE) {
98 Main.getGuiControl().processUselessEvent(aE);
99 JFileChooser chooser = new JFileChooser();
100 int returnVal = chooser.showOpenDialog(mRootPanel);
101 if (returnVal == JFileChooser.APPROVE_OPTION) {
102 mFileName.setText(chooser.getSelectedFile().getAbsolutePath());
103 }
104
105 }
106 });
107 /* construct cvs local file log pange*/
108 contentPanel.add(getFiller(FILLER_HEIGHT, FILLER_WIDTH));
109 contentPanel.add(
110 Main.getGuiControl().createLabel(Messages.getString(Text.LOGFILE_LOCATION)));
111 contentPanel.add(mFileName);
112 contentPanel.add(chooseFile);
113 contentPanel.add(getFiller(FILLER_HEIGHT, FILLER_WIDTH));
114 filePanel.add(
115 Main.getGuiControl().createLabel(Messages.getString(Text.SAVE_TO_XML_FILE)),
116 BorderLayout.NORTH);
117 filePanel.add(getFiller(FILLER_HEIGHT, FILLER_WIDTH), BorderLayout.EAST);
118 filePanel.add(getFiller(FILLER_HEIGHT, FILLER_WIDTH), BorderLayout.WEST);
119 filePanel.add(contentPanel, BorderLayout.CENTER);
120
121 return filePanel;
122 }
123
124 /**
125 * Returns the Metric that was specified in the dialog
126 * @return the Metric that was specified in the dialog
127 */
128 public Metric getMetric() {
129 return mMetric;
130 }
131
132 /**
133 *
134 */
135 private JComponent initButtons() {
136 JPanel buttonsPanel = new JPanel();
137
138 mCancelButton.setText("Cancel");
139 mCancelButton.addActionListener(new java.awt.event.ActionListener() {
140 public void actionPerformed(ActionEvent aE) {
141 buttonCancelAction(aE);
142 }
143 });
144 mOkButton.setText("OK");
145 mOkButton.addActionListener(new java.awt.event.ActionListener() {
146 public void actionPerformed(ActionEvent aE) {
147 buttonOkAction(aE);
148 }
149 });
150 buttonsPanel.add(mOkButton);
151 buttonsPanel.add(mCancelButton);
152 return buttonsPanel;
153
154 }
155
156 private void panelInit() throws Exception {
157 /* init root components */
158 JPanel underRoot = new JPanel();
159 underRoot.setPreferredSize(new Dimension(250, 250));
160 mRootPanel.setLayout(new BorderLayout());
161 /* init buttons */
162 JComponent buttonsPanel = initButtons();
163 JComponent filePanel = initFilePanel();
164 /* construct central pane*/
165 /* construct buttons pane*/
166 /* construct root pane */
167 mRootPanel.add(filePanel, BorderLayout.CENTER);
168 mRootPanel.add(buttonsPanel, BorderLayout.SOUTH);
169 underRoot.add(mRootPanel);
170 getContentPane().add(underRoot);
171 }
172 /* south buttons */
173 private JButton mCancelButton = new JButton();
174
175 /* Access objects created from user input */
176 private Metric mMetric = null;
177 private JButton mOkButton = new JButton();
178 private JTextField mFileName = new JTextField();
179 /* big components */
180 private JPanel mRootPanel = new JPanel();
181 private static final int FILLER_WIDTH = 40, FILLER_HEIGHT = 40;
182 private JComponent getFiller(int aWidth, int aHeight) {
183 Dimension d = new Dimension(aWidth, aHeight);
184 return new Box.Filler(d, d, d);
185 }
186 private static final String XML_HEAD ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
187 }