001: package tide.exttools.AStyle;
002:
003: // Class provided by Mike K.
004: //
005: import tide.project.*;
006: import tide.editor.*;
007: import javax.swing.*;
008: import java.awt.BorderLayout;
009: import java.awt.FlowLayout;
010: import java.awt.Insets;
011: import java.awt.event.*;
012: import javax.swing.border.*;
013: import java.util.*;
014: import java.io.*;
015: import snow.utils.*;
016: import snow.utils.storage.*;
017: import snow.utils.gui.*;
018:
019: /** The settings of AStyle
020: */
021: public class AStyleSettingsDialog extends JDialog {
022: private final FileField AStylePath = new FileField("", false,
023: "Give the AStyle executable path", JFileChooser.FILES_ONLY);
024: final private JButton viewOpts = new JButton("available options");
025: private final ProgressModalDialog pmd; // NOPMD
026:
027: public boolean dialogWasAccepted = false;
028:
029: public final static String defaultAStyleLocation = "c:/astyle/bin/astyle.exe";
030: public final static String defaultOptions = "--style=java";
031:
032: public final static String homepage = "http://astyle.sourceforge.net/";
033: public final static String directDownloadLink = "http://mesh.dl.sourceforge.net/sourceforge/astyle/astyle_1.21__windows.zip";
034:
035: public AStyleSettingsDialog(final JFrame parent,
036: final ProjectSettings settings) {
037: super (parent, "AStyle sourceode formatter", true);
038: pmd = new ProgressModalDialog(parent, "AStyle formatting");
039:
040: JComponent ta = GUIUtils
041: .createReadOnlyDescriptionArea("Astyle is an external tool available at "
042: + homepage
043: + " ."
044: + "\r\nIt formats source files according your rules.");
045: //ta.setEditable(false);
046: //ta.setBackground(getBackground());
047: //ta.setBorder(BorderFactory.createEmptyBorder(5,10,10,5));
048: add(ta, BorderLayout.NORTH);
049:
050: JPanel inputPanel = new JPanel();
051: add(inputPanel, BorderLayout.CENTER);
052: GridLayout3 gl3 = new GridLayout3(2, inputPanel);
053: gl3.addSeparator();
054:
055: JButton help = new JButton("Help", Icons.createHelpIcon(16,
056: true));
057: help.setMargin(new Insets(0, 2, 0, 2));
058: help.setFocusPainted(false);
059: gl3.add(help);
060: gl3.addSeparator();
061: help.addActionListener(new ActionListener() {
062: public void actionPerformed(ActionEvent ae) {
063: if (AStylePath.getPath() != null) {
064: try {
065: File local = new File(AStylePath.getPath()
066: .getParentFile().getParentFile(),
067: "doc/astyle.html");
068: if (local.exists()) {
069: SysUtils.openBrowser("file://" + local);
070: } else {
071: System.out.println("No local pg help: "
072: + local);
073: SysUtils.openBrowser(homepage);
074: }
075: } catch (Exception e) {
076:
077: }
078: }
079: }
080: });
081:
082: gl3.add("AStyle application");
083: gl3.add(AStylePath);
084: AStylePath.setPath(settings.getProperty("AStyle_path",
085: defaultAStyleLocation));
086: AStylePath.offerRememberedGlobalCompletion(
087: MainEditorFrame.instance.globalProperties,
088: "knownAStyles");
089: AStylePath.setComponentWidth(300);
090: AStylePath.setAutoColorized();
091: if (SysUtils.is_Windows_OS()) {
092: AStylePath.allowedExtensions.add("exe");
093: AStylePath.fileTypeDescription = "Windows executables";
094: }
095:
096: gl3.add("Options");
097: JPanel argumentsPanel = new JPanel(new FlowLayout(
098: FlowLayout.LEFT, 0, 0));
099: viewOpts.setMargin(new Insets(0, 2, 0, 2));
100: viewOpts.setFocusPainted(false);
101: JTextField argsField = new JTextField(settings.getProperty(
102: "AStyle_Options", defaultOptions), 20);
103: gl3.add(argumentsPanel);
104: argumentsPanel.add(argsField);
105: argumentsPanel.add(viewOpts);
106: viewOpts.addActionListener(new ActionListener() {
107: public void actionPerformed(ActionEvent ae) {
108: displayOptions();
109: }
110: });
111:
112: final CloseControlPanel ccp = new CloseControlPanel(this , true,
113: true, "Ok");
114: add(ccp, BorderLayout.SOUTH);
115:
116: pack();
117: this .setLocationRelativeTo(parent);
118: this .setVisible(true); // MODAL => waits
119:
120: //System.out.println(""+Arrays.toString(getLines(ignoredSubstringsTA.getText())));
121:
122: if (ccp.getWasCancelled())
123: return;
124: dialogWasAccepted = true;
125:
126: // save settings
127: settings.setProperty("AStyle_path", AStylePath.getPath()
128: .getAbsolutePath());
129: settings.setProperty("AStyle_Options", argsField.getText());
130:
131: AStylePath.rememberPathForGlobalCompletion(
132: MainEditorFrame.instance.globalProperties,
133: "knownAStyles");
134:
135: } // Constructor
136:
137: /** @return true if astyle path is present.
138: Used to detect if a setup dialog should be shown when starting the tool (if not found).
139: */
140: public static boolean isConfigured() {
141: ProjectSettings actualProject = MainEditorFrame.instance
142: .getActualProject();
143: return new File(actualProject.getProperty("AStyle_path",
144: defaultAStyleLocation)).exists();
145: }
146:
147: /** read the options from command line
148: */
149: private void displayOptions() {
150: //ProjectSettings actualProject = MainEditorFrame.instance.getActualProject();
151: StringBuilder comp = new StringBuilder();
152: try {
153: List<String> args = new ArrayList<String>();
154: args.add(AStylePath.getPath().getAbsolutePath());
155: args.add("-h");
156: comp.append(ProcessUtils.readWholeProcessStack(args));
157: } catch (Exception e) {
158: comp.append("\r\nError: " + e.getMessage());
159: e.printStackTrace();
160: }
161:
162: JDialog optionsDialog = new JDialog(this ,
163: "Available AStyle options", false);
164: JTextPane tp = new JTextPane();
165: tp.setFont(MainEditorFrame.fixedWidthFontForProcesses);
166: tp.setText(comp.toString().trim());
167: optionsDialog.add(new JScrollPane(tp), BorderLayout.CENTER);
168: CloseControlPanel ccp = new CloseControlPanel(optionsDialog,
169: false, true, "Close");
170: optionsDialog.add(ccp, BorderLayout.SOUTH);
171: tp.setEditable(false);
172: tp.setCaretPosition(0);
173: optionsDialog.setSize(800, 550);
174: optionsDialog.setLocationRelativeTo(viewOpts);
175: optionsDialog.setVisible(true);
176: }
177:
178: /*test public static void main(String[] aa)
179: {
180: JFrame f = new JFrame("Test");
181: f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
182: ProjectSettings props = new ProjectSettings();
183: new AStyleSettingsDialog(f, props);
184: System.exit(0);
185: }*/
186:
187: }
|