001: package tide.exttools.checkstyle;
002:
003: import java.net.URL;
004: import tide.exttools.ToolsAutoApplyManager;
005: import tide.project.*;
006: import tide.editor.*;
007: import javax.swing.*;
008: import java.awt.*;
009: import java.awt.event.*;
010: import javax.swing.border.*;
011: import java.util.*;
012: import java.io.*;
013: import snow.utils.*;
014: import snow.utils.storage.*;
015: import snow.utils.gui.*;
016:
017: public class CheckStyleSettingsDialog extends JDialog {
018: private final FileField csPath = new FileField("", false,
019: "Give the checkstyle jar path", JFileChooser.FILES_ONLY);
020: final private JButton viewOpts = new JButton("available options");
021: final JTextField argsField;
022:
023: public static String defaultCSLocation = "c:/java/tools/checkstyle-4.4/checkstyle-all-4.4.jar";
024: private final JComboBox messagesLanguage = new JComboBox(
025: new String[] { "en", "fr", "de", "es", "fi", "pt" });
026: public final static String homepage = "http://checkstyle.sourceforge.net/";
027: public final static String directDownloadLink44 = "http://surfnet.dl.sourceforge.net/sourceforge/checkstyle/checkstyle-4.4.zip";
028: // = "http://heanet.dl.sourceforge.net/sourceforge/checkstyle/checkstyle-4.3.zip";
029: // http://surfnet.dl.sourceforge.net/sourceforge/checkstyle/checkstyle-4.3.zip
030: public boolean dialogWasAccepted = false;
031:
032: public CheckStyleSettingsDialog(final JFrame parent,
033: final ProjectSettings settings) {
034: super (parent, "Checkstyle source checker", true);
035:
036: JComponent ta = GUIUtils
037: .createReadOnlyDescriptionArea("CheckStyle is an external style checking tool ("
038: + homepage
039: + ")."
040: + "\r\nIt checks source files for common problems."
041: + "\n\nREMARK: For correct relevance categorisation, the english messages must be used."
042: + "\n\nUSAGE: launch CheckStyle from the context menu of the sources tree, for single files or branches.");
043: add(ta, BorderLayout.NORTH);
044:
045: JPanel inputPanel = new JPanel();
046: add(inputPanel, BorderLayout.CENTER);
047: GridLayout3 gl3 = new GridLayout3(2, inputPanel);
048: gl3.addSeparator();
049:
050: JButton help = new JButton("Help", Icons.createHelpIcon(16,
051: true));
052: help.setMargin(new Insets(0, 2, 0, 2));
053: help.setFocusPainted(false);
054: gl3.add(help);
055: //gl3.addSeparator();
056: help.addActionListener(new ActionListener() {
057: public void actionPerformed(ActionEvent ae) {
058: if (csPath.getPath() != null) {
059: try {
060: File local = new File(csPath.getPath()
061: .getParentFile(), "docs/index.html");
062: if (local.exists()) {
063: SysUtils.openBrowser("file://" + local);
064: } else {
065: System.out.println("No local help: "
066: + local);
067: SysUtils.openBrowser(homepage);
068: }
069: } catch (Exception e) {
070: JOptionPane.showMessageDialog(null,
071: "Can't open browser\nerr:"
072: + e.getMessage(), "Error",
073: JOptionPane.ERROR_MESSAGE);
074: }
075: }
076: }
077: });
078:
079: JButton down = new JButton("Download and install version 4.4",
080: Icons.sharedDownArrow);
081: down.setMargin(new Insets(0, 2, 0, 2));
082: down.setFocusPainted(false);
083: gl3.add(down);
084: //gl3.addSeparator();
085: down.addActionListener(new ActionListener() {
086: public void actionPerformed(ActionEvent ae) {
087: final long[] ds;
088: final URL url;
089: try {
090: url = new URL(directDownloadLink44);
091: ds = NetUtils.getDateAndSizeOnServer(url);
092: } catch (Exception ex) {
093: JOptionPane.showMessageDialog(null,
094: "Cannot download Checkstyle (are you online?):\n "
095: + ex.getMessage(), "Error",
096: JOptionPane.ERROR_MESSAGE);
097: return;
098: }
099:
100: final JFileChooser fs = new JFileChooser();
101: fs
102: .setDialogTitle("Choose the folder to install Checkstyle in");
103: fs.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
104: fs
105: .setApproveButtonText("Download and install Checkstyle in this folder");
106: int rep = fs
107: .showOpenDialog(CheckStyleSettingsDialog.this );
108: if (rep != fs.APPROVE_OPTION)
109: return;
110:
111: final File destFolder = fs.getSelectedFile();
112:
113: final ProgressModalDialog pmd = new ProgressModalDialog(
114: CheckStyleSettingsDialog.this ,
115: "Download Checkstyle", true);
116: pmd.setProgressBounds(100);
117: pmd.start();
118: final File tempDest = new File(destFolder,
119: "checkstyle-4.4.zip");
120: Thread t = new Thread() {
121: @Override
122: public void run() {
123: try {
124: pmd
125: .setProgressSection("Downloading Checkstyle from sourceforge");
126: pmd
127: .setProgressComment("checkstyle-4.4.zip (3MB)");
128: NetUtils
129: .downloadFileFromServer(url,
130: tempDest,
131: "checkstyle-4.4.zip", pmd);
132:
133: // ok, => install
134: pmd
135: .setProgressSection("Unpacking Checkstyle locally");
136: FileUtils.extractZipFile(tempDest,
137: destFolder, pmd);
138:
139: // link the jar
140: File pf = new File(destFolder,
141: "checkstyle-4.4/checkstyle-all-4.4.jar");
142:
143: csPath.setPath(pf);
144: // todo: look if exist !!
145:
146: if (argsField.getText().length() == 0) {
147: argsField.setText("-c sun_checks.xml");
148: }
149: } catch (Exception ex) {
150: JOptionPane.showMessageDialog(null,
151: "Cannot download Checkstyle:\n "
152: + ex.getMessage(), "Error",
153: JOptionPane.ERROR_MESSAGE);
154: ex.printStackTrace();
155: } finally {
156: pmd.closeDialog();
157: }
158: }
159: };
160: t.start();
161:
162: //NetUtils.download()
163: }
164: });
165:
166: gl3.addSeparator();
167:
168: gl3.add("CheckStyle jar");
169: gl3.add(csPath);
170: csPath.setPath(settings.getProperty("CheckStyle_path",
171: defaultCSLocation));
172: if (MainEditorFrame.instance != null)
173: csPath.offerRememberedGlobalCompletion(
174: MainEditorFrame.instance.globalProperties,
175: "knownCheckStyles");
176: csPath.setComponentWidth(350);
177: csPath.setAutoColorized();
178: csPath.allowedExtensions.add("jar");
179: csPath.fileTypeDescription = "Checkstyle executable jar";
180:
181: gl3.add("Options");
182: JPanel argumentsPanel = new JPanel(new FlowLayout(
183: FlowLayout.LEFT, 0, 0));
184: viewOpts.setMargin(new Insets(0, 2, 0, 2));
185: viewOpts.setFocusPainted(false);
186: argsField = new JTextField(settings.getProperty(
187: "CheckStyle_Options", "-c sun_checks.xml"), 25);
188: gl3.add(argumentsPanel);
189: argumentsPanel.add(argsField);
190: argumentsPanel.add(viewOpts);
191: viewOpts.addActionListener(new ActionListener() {
192: public void actionPerformed(ActionEvent ae) {
193: displayOptions();
194: }
195: });
196:
197: gl3.add("");
198: final JCheckBox autoApplyOnChanges = new JCheckBox(
199: "Auto apply on changed sources", settings
200: .getBooleanProperty(
201: "CheckStyle_autoApplyOnChanges", false));
202: gl3.add(autoApplyOnChanges);
203:
204: gl3.add("");
205: final JCheckBox autoRemIrrelevant = new JCheckBox(
206: "Auto remove messages declared as irrelevant", settings
207: .getBooleanProperty(
208: "CheckStyle_ignoreIrrelevant", false));
209: gl3.add(autoRemIrrelevant);
210:
211: gl3.add("language");
212: gl3.add(messagesLanguage);
213: messagesLanguage.setEditable(true); // if not existing, english!
214: messagesLanguage.setSelectedItem(settings.getProperty(
215: "CheckStyle_messagesLanguage", "en"));
216: //System.getProperty("user.language","en")));
217:
218: CloseControlPanel ccp = new CloseControlPanel(this , true, true,
219: "Ok");
220: add(ccp, BorderLayout.SOUTH);
221:
222: pack();
223: this .setLocationRelativeTo(parent);
224: this .setVisible(true); // MODAL => waits
225:
226: if (ccp.getWasCancelled())
227: return;
228:
229: dialogWasAccepted = true;
230:
231: // save settings
232: settings.setProperty("CheckStyle_path", csPath.getPath()
233: .getAbsolutePath());
234: settings.setProperty("CheckStyle_Options", argsField.getText());
235: settings.setProperty("CheckStyle_messagesLanguage", ""
236: + messagesLanguage.getSelectedItem());
237: settings.setBooleanProperty("CheckStyle_autoApplyOnChanges",
238: autoApplyOnChanges.isSelected());
239: settings.setBooleanProperty("CheckStyle_ignoreIrrelevant",
240: autoRemIrrelevant.isSelected());
241:
242: //CheckStyleLauncher.getInstance().configureAutoScan();
243: ToolsAutoApplyManager.getInstance();
244:
245: if (MainEditorFrame.instance != null) {
246: csPath.rememberPathForGlobalCompletion(
247: MainEditorFrame.instance.globalProperties,
248: "knownCheckStyles");
249: }
250: } // Constructor
251:
252: /** @return true if findbugs jar is present.
253: Used to detect if a setup dialog should be shown when starting the tool (if not found).
254: */
255: public static boolean isConfigured() {
256: ProjectSettings actualProject = MainEditorFrame.instance
257: .getActualProject();
258: return new File(actualProject.getProperty("CheckStyle_path",
259: defaultCSLocation)).exists();
260: }
261:
262: private void displayOptions() {
263: String javaExe = "";
264: if (MainEditorFrame.instance != null) {
265: ProjectSettings actualProject = MainEditorFrame.instance
266: .getActualProject();
267: javaExe = actualProject.getJava_TOOL().getAbsolutePath();
268: } else {
269: javaExe = new File(System.getProperty("java.home"),
270: "bin/java.exe").getAbsolutePath();
271: }
272: StringBuilder comp = new StringBuilder();
273: try {
274: Vector<String> args = new Vector<String>();
275: args.add(javaExe);
276: args.add("-jar");
277: args.add(csPath.getPath().getAbsolutePath());
278: comp.append(ProcessUtils.readWholeProcessStack(args));
279: } catch (Exception e) {
280: comp.append("\r\nError: " + e.getMessage());
281: e.printStackTrace();
282: }
283:
284: JDialog optionsDialog = new JDialog(this ,
285: "Available CheckStyle options", false);
286: JTextPane tp = new JTextPane();
287: tp.setFont(MainEditorFrame.fixedWidthFontForProcesses);
288: tp.setText(comp.toString().trim());
289: optionsDialog.add(new JScrollPane(tp), BorderLayout.CENTER);
290: CloseControlPanel ccp = new CloseControlPanel(optionsDialog,
291: false, true, "Close");
292: optionsDialog.add(ccp, BorderLayout.SOUTH);
293: tp.setEditable(false);
294: tp.setCaretPosition(0);
295: optionsDialog.setSize(550, 310);
296: optionsDialog.setLocationRelativeTo(this );
297: optionsDialog.setVisible(true);
298: }
299:
300: public static void main(String[] aa) {
301: JFrame f = new JFrame("Test");
302: f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
303: ProjectSettings props = new ProjectSettings();
304: new CheckStyleSettingsDialog(f, props);
305: System.exit(0);
306: }
307: }
|