001: /*
002: *====================================================================
003: *The JRefactory License, Version 1.0
004: *
005: *Copyright (c) 2001 JRefactory. All rights reserved.
006: *
007: *Redistribution and use in source and binary forms, with or without
008: *modification, are permitted provided that the following conditions
009: *are met:
010: *
011: *1. Redistributions of source code must retain the above copyright
012: *notice, this list of conditions and the following disclaimer.
013: *
014: *2. Redistributions in binary form must reproduce the above copyright
015: *notice, this list of conditions and the following disclaimer in
016: *the documentation and/or other materials provided with the
017: *distribution.
018: *
019: *3. The end-user documentation included with the redistribution,
020: *if any, must include the following acknowledgment:
021: *"This product includes software developed by the
022: *JRefactory (http://www.sourceforge.org/projects/jrefactory)."
023: *Alternately, this acknowledgment may appear in the software itself,
024: *if and wherever such third-party acknowledgments normally appear.
025: *
026: *4. The names "JRefactory" must not be used to endorse or promote
027: *products derived from this software without prior written
028: *permission. For written permission, please contact seguin@acm.org.
029: *
030: *5. Products derived from this software may not be called "JRefactory",
031: *nor may "JRefactory" appear in their name, without prior written
032: *permission of Chris Seguin.
033: *
034: *THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: *OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: *DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
038: *ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: *SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: *LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: *USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: *ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: *OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: *OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: *SUCH DAMAGE.
046: *====================================================================
047: *
048: *This software consists of voluntary contributions made by many
049: *individuals on behalf of JRefactory. For more information on
050: *JRefactory, please see
051: *<http://www.sourceforge.org/projects/jrefactory>.
052: */
053: package org.acm.seguin.tools.install;
054:
055: import java.awt.Font;
056: import java.awt.GridBagConstraints;
057: import java.awt.GridBagLayout;
058: import java.awt.Insets;
059: import java.awt.event.ActionEvent;
060: import java.awt.event.ActionListener;
061: import java.io.File;
062: import java.io.FileWriter;
063: import java.io.IOException;
064: import java.io.PrintWriter;
065: import java.util.Iterator;
066: import java.util.LinkedList;
067: import javax.swing.*;
068: import org.acm.seguin.ide.common.ExitOnCloseAdapter;
069: import org.acm.seguin.io.FileCopy;
070: import org.acm.seguin.tools.RefactoryInstaller;
071: import org.acm.seguin.util.FileSettings;
072: import org.acm.seguin.util.MissingSettingsException;
073: import java.awt.event.WindowAdapter;
074: import java.awt.event.WindowEvent;
075:
076: /**
077: * The GUI for configuring the pretty printer
078: *
079: * @author Chris Seguin
080: * @created September 12, 2001
081: */
082: public class PrettyPrinterConfigGUI {
083: private JTabbedPane tabs;
084: private LinkedList list;
085: private boolean isFromCommandLine;
086: private JFrame frame = null;
087: private PrettyPrintBuffer prettyPrinter;
088:
089: /**
090: * Constructor for the PrettyPrinterConfigGUI object
091: *
092: * @paramcommandLine Description of Parameter
093: */
094: public PrettyPrinterConfigGUI(boolean commandLine) {
095: list = new LinkedList();
096: isFromCommandLine = commandLine;
097: }
098:
099: /**
100: * Main processing method for the PrettyPrinterConfigGUI object
101: */
102: public void run() {
103: backup();
104: if (frame == null)
105: initializeFrame(true);
106:
107: else
108: reload();
109:
110: frame.setVisible(true);
111: }
112:
113: /** Create a backup */
114: public void backup() {
115: FileSettings bundle = FileSettings.getRefactoryPrettySettings();
116: File file = bundle.getFile();
117: File backup = new File(file.getParentFile(), file.getName()
118: + ".backup");
119:
120: (new FileCopy(file, backup, false)).run();
121: }
122:
123: /** Restore the backups */
124: public void restore() {
125: FileSettings bundle = FileSettings.getRefactoryPrettySettings();
126: File file = bundle.getFile();
127: File backup = new File(file.getParentFile(), file.getName()
128: + ".backup");
129:
130: (new FileCopy(backup, file, false)).run();
131: }
132:
133: /** Save the pretty printer stuff to a file */
134: public void save() {
135: try {
136: File file = new File(FileSettings
137: .getRefactorySettingsRoot(), "pretty.settings");
138:
139: System.out.println("Saving: " + file.getPath());
140:
141: PrintWriter output = new PrintWriter(new FileWriter(file));
142:
143: output.println("# Version");
144: output.println("version="
145: + RefactoryInstaller.PRETTY_CURRENT_VERSION);
146: output.println(" ");
147:
148: Iterator iter = list.iterator();
149:
150: while (iter.hasNext()) {
151: SettingGroup next = (SettingGroup) iter.next();
152:
153: next.generateSetting(output);
154: }
155:
156: output.close();
157: } catch (IOException ioe) {
158: ioe.printStackTrace(System.out);
159: }
160: }
161:
162: /** Reloads all the values from the file */
163: public void reload() {
164: FileSettings bundle = FileSettings.getRefactoryPrettySettings();
165:
166: bundle.setReloadNow(true);
167:
168: Iterator iter = list.iterator();
169:
170: while (iter.hasNext()) {
171: SettingGroup next = (SettingGroup) iter.next();
172:
173: next.reload();
174: }
175:
176: prettyPrinter.prettyPrintCurrentWindow();
177: }
178:
179: /**
180: * Initialize the frame
181: *
182: * @paramlive Description of the Parameter
183: */
184: protected void initializeFrame(boolean live) {
185: frame = new JFrame("Pretty Printer Configuration");
186: createTabs();
187:
188: JPanel panel = new JPanel();
189:
190: panel.setLayout(new GridBagLayout());
191:
192: GridBagConstraints constraints = new GridBagConstraints();
193:
194: constraints.gridx = 0;
195: constraints.gridy = 0;
196: constraints.gridwidth = 1;
197: constraints.gridheight = 1;
198: constraints.weightx = 1.0;
199: constraints.weighty = 1.0;
200: constraints.anchor = constraints.WEST;
201: constraints.fill = constraints.BOTH;
202: constraints.insets = new Insets(5, 5, 5, 5);
203: constraints.ipadx = 0;
204: constraints.ipady = 0;
205: panel.add(tabs, constraints);
206:
207: JEditorPane editor = new JEditorPane();
208:
209: editor.setFont(new Font("Monospaced", Font.PLAIN, 10));
210:
211: String contents = load(editor);
212: JScrollPane scrollPane = new JScrollPane(editor);
213:
214: constraints.gridx = 1;
215: panel.add(scrollPane, constraints);
216: if (live) {
217: prettyPrinter = new PrettyPrintBuffer(editor, contents);
218: prettyPrinter.prettyPrintCurrentWindow();
219: }
220:
221: frame.getContentPane().add(panel);
222:
223: JMenuBar menuBar = new JMenuBar();
224: JMenu menu = new JMenu("File");
225:
226: menuBar.add(menu);
227:
228: JMenuItem item = new JMenuItem("Save");
229:
230: item.addActionListener(new SaveAdapter());
231: menu.add(item);
232: item = new JMenuItem("Restore");
233: item.addActionListener(new RestoreAdapter());
234: menu.add(item);
235: item = new JMenuItem("Close");
236: item.addActionListener(new CloseAdapter());
237: menu.add(item);
238: frame.setJMenuBar(menuBar);
239: frame.pack();
240:
241: if (isFromCommandLine)
242: frame.addWindowListener(new ExitOnCloseAdapter());
243:
244: else
245: frame.addWindowListener(new CloseFrameAdapter());
246:
247: }
248:
249: /**
250: * Description of the Method
251: *
252: * @paramitem Description of Parameter
253: */
254: private void add(SettingGroup item) {
255: tabs.add(item.getGroupName(), new JScrollPane(item));
256: list.add(item);
257: }
258:
259: /** Description of the Method */
260: private void createTabs() {
261: tabs = new JTabbedPane();
262: add(new SpacingGroup());
263: add(new BlockGroup());
264: add(new SingleLineGroup());
265: add(new CStyleGroup());
266: add(new JavadocGroup());
267: add(new DescriptionGroup());
268: add(new MiscGroup());
269: add(new SortGroup());
270: }
271:
272: /**
273: * Loads the editor pane with the contents
274: *
275: * @parameditor the editor
276: * @return the contents of the editor
277: */
278: private String load(JEditorPane editor) {
279: StringBuffer buffer = new StringBuffer();
280:
281: buffer.append("package test;\n");
282: buffer.append("\n");
283: buffer.append("import java.io.*;\n");
284: buffer.append("import java.util.*;\n");
285: buffer.append("\n");
286: buffer.append("public class TestClass extends Object {\n");
287: buffer.append(" private String privateField;\n");
288: buffer
289: .append(" public String getField() { return privateField; }\n");
290: buffer
291: .append(" public void setField(String value) { privateField = value; }\n");
292: buffer.append(" public void run() {\n");
293: buffer.append(" int length = privateField.length();\n");
294: buffer.append(" switch (length) {\n");
295: buffer.append(" case 1:\n");
296: buffer
297: .append(" System.out.println(\"Length is one\");\n");
298: buffer.append(" break;\n");
299: buffer.append(" default:\n");
300: buffer
301: .append(" System.out.println(\"Length is not one\");\n");
302: buffer.append(" break;\n");
303: buffer.append(" }\n");
304: buffer.append(" }");
305: buffer.append(" private int getCode(int value) {");
306: buffer.append(" if (value > 0)");
307: buffer.append(" System.out.println(\"positive\");");
308: buffer.append(" else");
309: buffer.append(" System.out.println(\"negative\");");
310: buffer.append(" ");
311: buffer.append(" if (value % 2 == 0) {");
312: buffer.append(" System.out.println(\"even\");");
313: buffer.append(" }");
314: buffer.append(" else {");
315: buffer.append(" System.out.println(\"odd\");");
316: buffer.append(" }");
317: buffer.append(" ");
318: buffer.append(" try {");
319: buffer.append(" invokeOther();");
320: buffer.append(" } catch (Exception exc) {");
321: buffer.append(" exc.printStackTrace();");
322: buffer.append(" }");
323: buffer.append(" }");
324: buffer.append("}\n");
325:
326: String result = buffer.toString();
327:
328: editor.setText(result);
329: return result;
330: }
331:
332: /**
333: * The main program for the PrettyPrinterConfigGUI class
334: *
335: * @paramargs The command line arguments
336: */
337: public static void main(String[] args) {
338: (new PrettyPrinterConfigGUI(true)).run();
339: }
340:
341: /**
342: * Simple adapter that exits the application when the frame is closed
343: *
344: * @author Chris Seguin
345: * @created September 12, 2001
346: */
347: class CloseFrameAdapter extends WindowAdapter {
348: /**
349: * The window is closing
350: *
351: * @paramevt Description of Parameter
352: */
353: public void windowClosing(WindowEvent evt) {
354: frame.setVisible(false);
355: }
356: }
357:
358: /**
359: * Description of the Class
360: *
361: * @author Chris Seguin
362: * @created September 12, 2001
363: */
364: private class SaveAdapter implements ActionListener {
365: /**
366: * Description of the Method
367: *
368: * @paramevt Description of Parameter
369: */
370: public void actionPerformed(ActionEvent evt) {
371: save();
372: prettyPrinter.prettyPrintCurrentWindow();
373: }
374: }
375:
376: /**
377: * Description of the Class
378: *
379: * @author Chris Seguin
380: * @created September 12, 2001
381: */
382: private class RestoreAdapter implements ActionListener {
383: /**
384: * Description of the Method
385: *
386: * @paramevt Description of Parameter
387: */
388: public void actionPerformed(ActionEvent evt) {
389: restore();
390: }
391: }
392:
393: /**
394: * Description of the Class
395: *
396: * @author Chris Seguin
397: * @created September 12, 2001
398: */
399: private class CloseAdapter implements ActionListener {
400: /**
401: * Description of the Method
402: *
403: * @paramevt Description of Parameter
404: */
405: public void actionPerformed(ActionEvent evt) {
406: if (isFromCommandLine)
407: System.exit(0);
408:
409: else
410: frame.setVisible(false);
411:
412: }
413: }
414: }
|