From 82e3bb8888849e8a47acf5676f5baa63875550c6 Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Fri, 21 Feb 2025 13:29:09 +0800 Subject: Clear code of clang-tidy, cppcheck --- src/cppcheckplus/control/Main.java | 300 ++++---- src/cppcheckplus/control/MyContorlUtil.java | 1076 +++++++++++++-------------- src/cppcheckplus/control/settings.xml | 4 +- src/cppcheckplus/shortcut/MyTree.java | 6 +- src/cppcheckplus/tab/MyTabPanel1.java | 154 ++-- src/cppcheckplus/text/MyTextPanel1.java | 5 +- 6 files changed, 762 insertions(+), 783 deletions(-) diff --git a/src/cppcheckplus/control/Main.java b/src/cppcheckplus/control/Main.java index 2759e2f..19d33cc 100644 --- a/src/cppcheckplus/control/Main.java +++ b/src/cppcheckplus/control/Main.java @@ -65,7 +65,7 @@ public class Main extends JFrame { private final JFileChooser jfc = new JFileChooser();// 文件选择器 private final JFileChooser jfsettings = new JFileChooser();// 配置设定 private final String projectPath; - public MyTabPanel tabPanel; + public MyTabPanel centerTabPanel; // 编辑代码的面板 public MyTextPanel testCode; public String src = ""; // 目标源文件或目录的路径 @@ -73,9 +73,11 @@ public class Main extends JFrame { public String flawfinderPath = null; public String paracppcheck = null; public String paraflawfinder = null; - public creatTable ct = new creatTable(); + public String clangtidyPath = null; + public String paraclangtidy = null; + public creatTable ct = new creatTable(); // 生成表格 private MyOutlookPanel outlookPanel; - private MyTabPanel1 tabPanel2; + private MyTabPanel1 footTabPanel; private MyShortcutPanel shortcutPanel; private MyTree tree; // 标记是否修改了工具路径 @@ -136,9 +138,9 @@ public class Main extends JFrame { // 读取文件内容并显示到代码区 textCode.setText(FileTools.readFile(result)); // 如果代码标签页不存在,则创建一个 - if (!tabPanel.isSelectTabComponents("代码")) { - tabPanel.addTab("代码", testCode); - tabPanel.isSelectTabComponents("代码"); + if (!centerTabPanel.isSelectTabComponents("代码")) { + centerTabPanel.addTab("代码", testCode); + centerTabPanel.isSelectTabComponents("代码"); } break; case "onMenuLoad": @@ -243,10 +245,10 @@ public class Main extends JFrame { // 按类型排序:生成表格并按类型排序 ct.makeTable(); ct.ord(2); - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { viewPanel = new ViewPanel(ct.results); - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { viewPanel.update(ct.results); } @@ -255,10 +257,10 @@ public class Main extends JFrame { // 按级别排序 ct.makeTable(); ct.ord(3); - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { viewPanel = new ViewPanel(ct.results); - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { viewPanel.update(ct.results); } @@ -267,20 +269,19 @@ public class Main extends JFrame { // 按文件排序 ct.makeTable(); ct.ord(1); - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { viewPanel = new ViewPanel(ct.results); - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { viewPanel.update(ct.results); } break; case "onSetCppcheckRoad": // cppcheck路径 - jfsettings.setFileSelectionMode(0);// 设定只能选择到文件 -// int state = jfsettings.showOpenDialog(null); - if (jfsettings.showOpenDialog(null) == 1) { - return; // 撤销则返回 + jfsettings.setFileSelectionMode(JFileChooser.FILES_ONLY); + if (jfsettings.showOpenDialog(null) == JFileChooser.CANCEL_OPTION) { + return; } else { File cppcheckFile = jfsettings.getSelectedFile(); String cppcheckPathTemp = @@ -293,8 +294,7 @@ public class Main extends JFrame { case "onSetFlawfinderRoad": // flawfinder路径 jfsettings.setFileSelectionMode( - JFileChooser.FILES_ONLY);// 设定只能选择到文件 -// int state = jfsettings.showOpenDialog(null); + JFileChooser.FILES_ONLY); if (jfsettings.showOpenDialog(null) == JFileChooser.CANCEL_OPTION) { return;// 撤销则返回 @@ -320,7 +320,6 @@ public class Main extends JFrame { } public static void main(String[] args) throws ParserConfigurationException { - SwingUtilities.invokeLater(new Runnable() { public void run() { MyContorlUtil.setupLookAndFeel(); @@ -331,7 +330,7 @@ public class Main extends JFrame { } private void execute(String src, String para) { - System.out.println("cppcheckPath:" + cppcheckPath); + // FIXME:这段代码原因不详,十分令人疑惑 if (changedpathcpp) { MyContorlUtil.loadToolsRoad(settingsXML, this); changedpathcpp = false; @@ -342,49 +341,44 @@ public class Main extends JFrame { return; } - Runtime runtime = Runtime.getRuntime(); - Process p = null; - Runtime runtime1 = Runtime.getRuntime(); - Process p1 = null; + Runtime[] runtime = {Runtime.getRuntime(), Runtime.getRuntime()}; + Process[] p = new Process[2]; try { - // 指定模板执行cppcheck,格式化输出信息 - p = runtime.exec(String.format(cppcheckPath + + System.out.printf(cppcheckPath + " %s %s%n", para, src); + // 指定模板执行cppcheck,用于生成“结果”表格 + p[0] = runtime[0].exec(String.format(cppcheckPath + " --template=\"cppcheck${file}${line}${severity}${id}${message}${cwe}\" %s %s", para, src)); - final InputStream is1 = p.getInputStream(); - final InputStream is2 = p.getErrorStream(); + // 不使用模板,在底部标签页显示纯文本结果 + p[1] = runtime[1].exec( + String.format(cppcheckPath + " %s %s", para, src)); - // 不使用模板,捕获错误信息 - p1 = - runtime.exec(String.format(cppcheckPath + " %s %s", para, src)); - System.out.printf(cppcheckPath + " %s %s%n", para, src); - final InputStream is11 = p1.getInputStream(); - final InputStream is21 = p1.getErrorStream(); + final InputStream[] stdout = + {p[0].getInputStream(), p[1].getInputStream()}; + final InputStream[] stderr = + {p[0].getErrorStream(), p[1].getErrorStream()}; - // 处理标准输出 + // 模板标准输出 new Thread() { @SuppressWarnings("resource") public void run() { - BufferedReader br1 = - new BufferedReader(new InputStreamReader(is1)); - //StringBuilder l1 = new StringBuilder(); + new BufferedReader(new InputStreamReader(stdout[0])); + StringBuilder l1 = new StringBuilder(); try { - String line1 = null; int i = 0; textCheck.setText(""); while ((line1 = br1.readLine()) != null) { - i++; textCheck.append(i + " " + line1 + ENTERWindows); + i++; } } catch (Exception e) { e.printStackTrace(); System.out.println("inputError"); } finally { try { - //textCheck.setText(new String(l1.toString().getBytes(), "utf-8")); - is1.close(); + stdout[0].close(); } catch (IOException e) { e.printStackTrace(); } @@ -392,22 +386,28 @@ public class Main extends JFrame { } }.start(); - // 处理错误流 + // 模板错误流,用于生成结果表格 new Thread() { public void run() { BufferedReader br2 = - new BufferedReader(new InputStreamReader(is2)); + new BufferedReader(new InputStreamReader(stderr[0])); StringBuilder l2 = new StringBuilder(); try { String line2 = null; + int i = 0; while ((line2 = br2.readLine()) != null) { ct.toRowsOfCppcheckResult(line2); + l2.append(i + " " + line2 + ENTERWindows); + i++; } } catch (Exception e) { e.printStackTrace(); } finally { try { - is2.close(); + cppcheckResult.setText( + new String(l2.toString().getBytes(), + StandardCharsets.UTF_8)); + stderr[0].close(); } catch (IOException e) { e.printStackTrace(); } @@ -415,11 +415,11 @@ public class Main extends JFrame { } }.start(); - // 第二次的错误流 + // 纯文本的错误流,用于在底部标签页显示 new Thread() { public void run() { BufferedReader br2 = - new BufferedReader(new InputStreamReader(is21)); + new BufferedReader(new InputStreamReader(stderr[1])); StringBuilder l2 = new StringBuilder(); cppcheckResult.setText(""); try { @@ -436,7 +436,7 @@ public class Main extends JFrame { cppcheckResult.setText( new String(l2.toString().getBytes(), StandardCharsets.UTF_8)); - is21.close(); + stderr[1].close(); } catch (IOException e) { e.printStackTrace(); } @@ -445,29 +445,29 @@ public class Main extends JFrame { }.start(); // 等待两个进程结束 - int exitCode = p.waitFor(); - int exitCode1 = p1.waitFor(); + int exitCode = p[0].waitFor(); + int exitCode1 = p[1].waitFor(); if (exitCode == SUCCESS) { System.out.println(SUCCESS_MESSAGE); - } else { System.err.println(ERROR_MESSAGE + exitCode); } - p.destroy(); - p1.destroy(); + p[0].destroy(); + p[1].destroy(); } catch (Exception e) { try { // 出现异常则关闭相关流 - p.getErrorStream().close(); - p.getInputStream().close(); - p.getOutputStream().close(); - p1.getErrorStream().close(); - p1.getInputStream().close(); - p1.getOutputStream().close(); + p[0].getErrorStream().close(); + p[0].getInputStream().close(); + p[0].getOutputStream().close(); + p[1].getErrorStream().close(); + p[1].getInputStream().close(); + p[1].getOutputStream().close(); } catch (Exception ee) { } } - if (!tabPanel.isSelectTabComponents("结果")) { + + if (!centerTabPanel.isSelectTabComponents("结果")) { try { ct.makeTable(); viewPanel = new ViewPanel(ct.results); @@ -475,8 +475,8 @@ public class Main extends JFrame { e1.printStackTrace(); System.out.println("图表生成错误"); } - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { try { ct.makeTable(); @@ -486,59 +486,65 @@ public class Main extends JFrame { System.out.println("图表生成错误"); } } + + // 底部标签页切换到cppcheck + if (!footTabPanel.isSelectTabComponents("cppcheck")) { + footTabPanel.selectTabByOId("cppcheck"); + } } private void execute2(String src) { - // clamh-tidy 检查 + // clang-tidy 检查 Runtime runtime = Runtime.getRuntime(); Process p = null; try { // 修改为执行 clang-tidy - String path = "C:\\MinGW\\include"; // FIXME: 需要修改为自己的路径 Path srcPath = Paths.get(src); String execable; + // FIXME: + // 1.这里没能实现递归检查 + // 2.如何指定特定的检查规则? + // 3.如何指定特定的头文件路径? + // 4.是否需要指定代码标准 if (Files.isDirectory(srcPath)) { // 是目录,则检查这个目录下所有cpp文件 execable = String.format( - "clang-tidy %s\\*.cpp --checks=* --header-filter=%s -- -std=c++17 -I %s", - src, src, path); + "%s %s\\*.cpp --checks=* --header-filter=%s -- %s", + clangtidyPath, src, src, paraclangtidy); } else { // 是文件,直接执行 - execable = String.format( - "clang-tidy %s --checks=* --header-filter=%s -- -std=c++17 -I %s", - src, src, path); + execable = + String.format("%s %s --checks=* --header-filter=%s -- %s", + clangtidyPath, src, src, paraclangtidy); } - //String execable1 = String.format("clang-tidy %s --checks=* --header-filter=%s -- -std=c++17 -I %s", src, src, path); - //String execable2 = String.format("clang-tidy %s\\*.cpp --checks=* --header-filter=%s -- -std=c++17 -I %s", src,src, path); //String execable = String.format("clang-tidy %s\\*.cpp --checks='*,misc-no-delete-this,-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type' -- -std=c++17 -I %s", src, path); System.out.println(execable); // 执行 - p = runtime.exec(execable); // -- 表示之后是源文件 - final InputStream is1 = p.getInputStream(); // 标准输出流 - final InputStream is2 = p.getErrorStream(); // clang-tidy 的错误流 + p = runtime.exec(execable); + final InputStream stdout = p.getInputStream(); + final InputStream stderr = p.getErrorStream(); + + clangResult.setText(""); // 处理标准输出 new Thread() { public void run() { BufferedReader br1 = - new BufferedReader(new InputStreamReader(is1)); + new BufferedReader(new InputStreamReader(stdout)); StringBuilder l1 = new StringBuilder(); try { String line1 = null; while ((line1 = br1.readLine()) != null) { - // 只处理包含源文件路径的行 - if (line1.contains(src)) { - l1.append(line1).append(ENTERWindows); - // 如果有需要,可以将标准输出进行处理 - ct.toRowsOfClang(line1); - } + l1.append(line1 + ENTERWindows); + ct.toRowsOfClang(line1); } } catch (Exception e) { e.printStackTrace(); } finally { try { - is1.close(); + clangResult.append(l1.toString()); + stdout.close(); } catch (IOException e) { e.printStackTrace(); } @@ -546,46 +552,40 @@ public class Main extends JFrame { } }.start(); - // FIXME: 这一部分是否必要? - //new Thread() { - // - // - // public void run() { - // BufferedReader br2 = new BufferedReader(new InputStreamReader(is2)); - // StringBuilder l2 = new StringBuilder(); - // try { - // String line2 = null; - // int i = 0; - // clangResult.setText(""); - // while ((line2 = br2.readLine()) != null) { - // l2.append(i + " " + line2 + ENTERWindows); - // ct.toRowsOfClang(line2); // 可能需要根据 clang-tidy 的输出格式进行调整 - // i++; - // } - // } catch (Exception e) { - // e.printStackTrace(); - // System.out.println("inputError"); - // } finally { - // try { - // clangResult.setText(l2.substring(0, l2.length())); - // is2.close(); - // } catch (IOException e) { - // e.printStackTrace(); - // } - // } - // } - //}.start(); + new Thread() { + public void run() { + BufferedReader br2 = + new BufferedReader(new InputStreamReader(stderr)); + StringBuilder l2 = new StringBuilder(); + try { + String line2 = null; + int i = 0; + l2.append(ENTERWindows + ENTERWindows); + while ((line2 = br2.readLine()) != null) { + l2.append(line2 + ENTERWindows); + ct.toRowsOfClang(line2); + i++; + } + } catch (Exception e) { + e.printStackTrace(); + System.out.println("inputError"); + } finally { + try { + clangResult.append(l2.toString()); + stderr.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }.start(); int exitCode = p.waitFor(); if (exitCode == SUCCESS) { System.out.println(SUCCESS_MESSAGE); - } else { System.err.println(ERROR_MESSAGE + exitCode); } - - // 为什么需要关闭两次? - p.destroy(); p.destroy(); } catch (Exception e) { try { @@ -597,7 +597,7 @@ public class Main extends JFrame { } // 更新或者创建结果标签页,显示结果 - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { try { ct.makeTable(); viewPanel = new ViewPanel(ct.results); @@ -605,8 +605,8 @@ public class Main extends JFrame { e1.printStackTrace(); System.out.println("图表生成错误"); } - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { try { ct.makeTable(); @@ -616,10 +616,13 @@ public class Main extends JFrame { System.out.println("图表生成错误"); } } + + if (!footTabPanel.isSelectTabComponents("Clang")) { + footTabPanel.selectTabByOId("Clang"); + } } private void execute3(String src, String para, String tmpFileName) { - System.out.println("flawfinderPath:" + flawfinderPath); // flawfinder if (changedpathflaw) { MyContorlUtil.loadToolsRoad(settingsXML, this); @@ -716,7 +719,7 @@ public class Main extends JFrame { } catch (Exception ee) { } } - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { try { ct.makeTable(); viewPanel = new ViewPanel(ct.results); @@ -724,8 +727,8 @@ public class Main extends JFrame { e1.printStackTrace(); System.out.println("图表生成错误"); } - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } else { try { ct.makeTable(); @@ -736,6 +739,9 @@ public class Main extends JFrame { } } + if (!footTabPanel.isSelectTabComponents("flawfinder")) { + footTabPanel.selectTabByOId("flawfinder"); + } } private void initSwing() throws UnsupportedEncodingException { @@ -774,11 +780,11 @@ public class Main extends JFrame { // 面板加入布局 centerPane.add(outlookPanel, "West"); - centerPane.add(tabPanel, "Center"); + centerPane.add(centerTabPanel, "Center"); centerPane.add(shortcutPanel, "East"); // 底部添加第二个标签页面板,用于显示检查结果 - contentPanel.add(tabPanel2, BorderLayout.SOUTH); + contentPanel.add(footTabPanel, BorderLayout.SOUTH); } private void initTree() { @@ -806,7 +812,7 @@ public class Main extends JFrame { String command = item.getActionCommand(); if (command.equals("Chart2")) { // 如果TabPanel不存在就create,否则就切换 - if (!tabPanel.isSelectTabComponents("结果")) { + if (!centerTabPanel.isSelectTabComponents("结果")) { try { ct.makeTable(); viewPanel = new ViewPanel(ct.results); @@ -814,20 +820,20 @@ public class Main extends JFrame { e1.printStackTrace(); System.out.println("图表生成错误"); } - tabPanel.addTab("结果", createPage(viewPanel)); - tabPanel.isSelectTabComponents("结果"); + centerTabPanel.addTab("结果", createPage(viewPanel)); + centerTabPanel.isSelectTabComponents("结果"); } } else if (command.equals("Map")) { // 代码标签页 - if (!tabPanel.isSelectTabComponents("代码")) { - tabPanel.addTab("代码", testCode); - tabPanel.isSelectTabComponents("代码"); + if (!centerTabPanel.isSelectTabComponents("代码")) { + centerTabPanel.addTab("代码", testCode); + centerTabPanel.isSelectTabComponents("代码"); } } else if (command.equals("Process")) { // 进度 - if (!tabPanel.isSelectTabComponents("进度")) { - tabPanel.addTab("进度", textCheck); - tabPanel.isSelectTabComponents("进度"); + if (!centerTabPanel.isSelectTabComponents("进度")) { + centerTabPanel.addTab("进度", textCheck); + centerTabPanel.isSelectTabComponents("进度"); } } } @@ -839,17 +845,17 @@ public class Main extends JFrame { } private void initTab() { - tabPanel = new MyTabPanel(); - tabPanel.addMouseListener(new MouseAdapter() { + centerTabPanel = new MyTabPanel(); + centerTabPanel.addMouseListener(new MouseAdapter() { private boolean isMaximized() { return outlookPanel.isShrinked(); } public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 1) { - TabbedPaneUI ui = tabPanel.getUI(); + TabbedPaneUI ui = centerTabPanel.getUI(); int tabIndex = - ui.tabForCoordinate(tabPanel, e.getX(), e.getY()); + ui.tabForCoordinate(centerTabPanel, e.getX(), e.getY()); if (tabIndex != -1) { boolean maxed = isMaximized(); outlookPanel.setShrink(!maxed); @@ -867,17 +873,17 @@ public class Main extends JFrame { } private void initTab2() { - tabPanel2 = new MyTabPanel1(); - tabPanel2.addMouseListener(new MouseAdapter() { + footTabPanel = new MyTabPanel1(); + footTabPanel.addMouseListener(new MouseAdapter() { private boolean isMaximized() { return outlookPanel.isShrinked(); } public void mouseClicked(MouseEvent e) { if (e.getClickCount() > 1) { - TabbedPaneUI ui = tabPanel2.getUI(); + TabbedPaneUI ui = footTabPanel.getUI(); int tabIndex = - ui.tabForCoordinate(tabPanel2, e.getX(), e.getY()); + ui.tabForCoordinate(footTabPanel, e.getX(), e.getY()); if (tabIndex != -1) { boolean maxed = isMaximized(); outlookPanel.setShrink(!maxed); @@ -894,9 +900,9 @@ public class Main extends JFrame { cppcheckResult = cppcheckPanel.getTextArea(); clangResult = clangPanel.getTextArea(); - tabPanel2.addTab("cppcheck", createPage1(cppcheckPanel)); - tabPanel2.addTab("Clang", createPage1(clangPanel)); - tabPanel2.addTab("flawfinder", createPage1(flawfinderPanel)); + footTabPanel.addTab("cppcheck", createPage1(cppcheckPanel)); + footTabPanel.addTab("Clang", createPage1(clangPanel)); + footTabPanel.addTab("flawfinder", createPage1(flawfinderPanel)); } private MyTabPage1 createPage1(JComponent pageContent) { diff --git a/src/cppcheckplus/control/MyContorlUtil.java b/src/cppcheckplus/control/MyContorlUtil.java index 72716de..acdc9ba 100644 --- a/src/cppcheckplus/control/MyContorlUtil.java +++ b/src/cppcheckplus/control/MyContorlUtil.java @@ -1,6 +1,15 @@ - package cppcheckplus.control; +import com.jgoodies.looks.plastic.PlasticLookAndFeel; +import com.jgoodies.looks.plastic.theme.ExperienceGreen; +import cppcheckplus.menu.MyMenu; +import cppcheckplus.menu.MyMenuBar; +import cppcheckplus.menu.MyMenuItem; +import cppcheckplus.menu.MyRootMenu; +import cppcheckplus.outlookpanel.MyOutlookBar; +import cppcheckplus.outlookpanel.MyOutlookHeader; +import cppcheckplus.outlookpanel.MyOutlookPanel; +import cppcheckplus.outlookpanel.MyOutlookPanelListItem; import java.awt.Color; import java.awt.Component; import java.awt.Font; @@ -19,7 +28,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; - import javax.imageio.ImageIO; import javax.swing.BorderFactory; import javax.swing.Box; @@ -35,563 +43,529 @@ import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import com.jgoodies.looks.plastic.PlasticLookAndFeel; -import com.jgoodies.looks.plastic.theme.ExperienceGreen; - -import cppcheckplus.menu.MyMenu; -import cppcheckplus.menu.MyMenuBar; -import cppcheckplus.menu.MyMenuItem; -import cppcheckplus.menu.MyRootMenu; -import cppcheckplus.outlookpanel.MyOutlookBar; -import cppcheckplus.outlookpanel.MyOutlookHeader; -import cppcheckplus.outlookpanel.MyOutlookPanel; -import cppcheckplus.outlookpanel.MyOutlookPanelListItem; - /** * @ClassName: ContorlUtil - * @Description: TODO(控件工具,皮肤信息和基本操作) - * + * @Description: TODO(控件工具 , 皮肤信息和基本操作) */ -public class MyContorlUtil -{ - //文本 - public static final Color DEFAULT_TEXT_COLOR = new Color(37, 81, 54); - public static final Font FONT_14_BOLD = new Font("微软雅黑", 1, 14); - public static final Font FONT_12_BOLD = new Font("微软雅黑", 1, 12); - public static final Font FONT_14_PLAIN = new Font("微软雅黑", 0, 14); - public static final Font FONT_12_PLAIN = new Font("微软雅黑", 0, 12); -// public static final Font FONT_12_PLAIN = new Font("Dialog", 0, 12); +public class MyContorlUtil { + //文本 + public static final Color DEFAULT_TEXT_COLOR = new Color(37, 81, 54); + public static final Font FONT_14_BOLD = new Font("微软雅黑", 1, 14); + public static final Font FONT_12_BOLD = new Font("微软雅黑", 1, 12); + public static final Font FONT_14_PLAIN = new Font("微软雅黑", 0, 14); + public static final Font FONT_12_PLAIN = new Font("微软雅黑", 0, 12); + // public static final Font FONT_12_PLAIN = new Font("Dialog", 0, 12); //public static final Font FONT_12_PLAIN = new Font("Calibri", 0, 12); - //菜单 - public static final Color MENUITEM_SELECTED_BACKGROUND = new Color(166, 188, 140); - public static final Color MENUITEM_BACKGROUND = new Color(228, 235, 218); - //主面板 - public static final Color CONTENT_PANE_BACKGROUND = new Color(92, 153, 45); - public static final Color CONTENT_PANE_BACKGROUND2 = new Color(78, 238, 148); - public static final Color CONTENT_PANE_BACKGROUND3 = new Color(238, 220, 130); - //工具栏按钮 - public static final Color BUTTON_ROVER_COLOR = new Color(196, 196, 197); - //OutLook面板 - public static final Color OUTLOOK_TEXT_COLOR = new Color(120, 120, 125); - public static final Color OUTLOOK_SPLIT_COLOR = new Color(174, 171, 162); - public static final Color OUTLOOK_CONTAINER_COLOR = new Color(217, 218, 219) ;//容器的背景颜色,灰色 - //多视图Tab - public static final Color TAB_BOTTOM_LINE_COLOR = new Color(167, 173, 175); - //快捷菜单面板 - public static final Color LIST_SPLIT_COLOR = new Color(105, 113, 120); - public static final Color LIST_BACKGROUND = new Color(175, 174, 176); - public static final Color LIST_TEXT_COLOR = new Color(49, 52, 58); - //图形 - public static Font CHART_AXIS_FONT = new Font("微软雅黑",Font.PLAIN,12) ;// 坐标轴字体 - public static Font CHART_TITLE_FONT = new Font("微软雅黑",Font.PLAIN,14);//标题字体 - public static Font CHART_LEGEND_FONT = new Font("微软雅黑",Font.PLAIN,12); //图例字体 - - //其他 - public static final Color NO_COLOR = new Color(0, 0, 0, 0); - public static final Insets ZERO_INSETS = new Insets(0, 0, 0, 0); - /** - * ClassLoader获得BufferedImage,注意和getClass().getResource()的路径不同 - * 最前面不带/ - * @param @param imgPath - * @param @return 参数 - * @return Image 返回类型 - * @throws - */ - public static Image getImage(String imgPath) - { - BufferedImage img = null; - try - { - //System.out.println("图片路径: "+imgPath); - img = ImageIO.read(ClassLoader.getSystemResource(imgPath)); - } catch (IOException e) - { - System.out.println("图片路径找不到: "+imgPath); - //e.printStackTrace(); - } - return img; - } - public static ImageIcon getImageIcon(String imgPath) - { - ImageIcon icon = null; - try - { - icon = new ImageIcon(ClassLoader.getSystemResource(imgPath)); - return icon; - } - catch (Exception e) - { - System.out.println("找不到图片: " +imgPath ); - } - return icon; - } - public static Image iconToImage(Icon icon) - { - if (icon instanceof ImageIcon) - { - return ((ImageIcon) icon).getImage(); - } - else - { - int w = icon.getIconWidth(); - int h = icon.getIconHeight(); - BufferedImage image = new BufferedImage(w, h, 2); - Graphics2D g = image.createGraphics(); - icon.paintIcon(null, g, 0, 0); - g.dispose(); - return image; - } - } - public static ImageIcon createDyedIcon(ImageIcon icon, Color color) - { - if (color == null) - { - return icon; - } - else - { - int iconWidth = icon.getIconWidth(); - int iconHeight = icon.getIconHeight(); - BufferedImage bi = new BufferedImage(iconWidth, iconHeight, 2); - Graphics2D g2d = bi.createGraphics(); - icon.paintIcon(null, g2d, 0, 0); - g2d.dispose(); - Image dyedImage = createDyedImage(bi, color); - return new ImageIcon(dyedImage); - } - } - public static Image createDyedImage(Image image, Color color) - { - if (color == null) - return image; - if (image != null) - { - int w = image.getWidth(null); - int h = image.getHeight(null); - int pixels[] = new int[w * h]; - PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w); - try - { - pg.grabPixels(); - } - catch (InterruptedException ex) - { - ex.printStackTrace(); - return null; - } - BufferedImage bi = new BufferedImage(w <= 1 ? 1 : w, - h <= 1 ? 1 : h, 2); - for (int i = 0; i < pixels.length; i++) - { - int pixel = pixels[i]; - int row = i / w; - int col = i % w; - if (color != null && pixel != 0) - pixel = color.getRGB(); - bi.setRGB(col, row, pixel); - } - return bi; - } - else - { - return null; - } - } - public static Icon createMovedIcon(Icon icon) - { - return createMovedIcon(icon, 1, 1); - } - public static Icon createMovedIcon(final Icon icon, final int offsetX, - final int offsetY) - { - return new Icon() - { - public void paintIcon(Component c, Graphics g, int x, int y) - { - icon.paintIcon(c, g, x + offsetX, y + offsetY); - } - public int getIconWidth() - { - return icon.getIconWidth(); - } - public int getIconHeight() - { - return icon.getIconHeight(); - } - }; - } - - public static TexturePaint createTexturePaint(String imgPath) - { - ImageIcon icon = getImageIcon(imgPath); - int imageWidth = icon.getIconWidth(); - int imageHeight = icon.getIconHeight(); - BufferedImage bi = new BufferedImage(imageWidth, imageHeight, 2); - Graphics2D g2d = bi.createGraphics(); - g2d.drawImage(icon.getImage(), 0, 0, null); - g2d.dispose(); - return new TexturePaint(bi, - new Rectangle(0, 0, imageWidth, imageHeight)); - } - - public static Color getColor(String color) { - Color convertedColor = Color.ORANGE; - try { - convertedColor = new Color(Integer.parseInt(color, 16)); - } catch(NumberFormatException e) { - String error = String.format("颜色转化出错:s%",color); - System.out.println(error); - } - return convertedColor; - } - - public static InputStream getXMLFile(String filePath) - { - return ClassLoader.getSystemResourceAsStream(filePath); - } - - - public static String getStringAttribute(org.w3c.dom.Node node, String name) - { - org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name); - if (attribute != null) - return attribute.getNodeValue(); - else - return null; - } - - private static Icon getIconAttribute(org.w3c.dom.Node node, String name) - { - String iconURL = getStringAttribute(node, name); - if (iconURL != null && !iconURL.isEmpty()) - return getImageIcon(iconURL); - else - return null; - } - - private static int getIntAttribute(org.w3c.dom.Node node, String name) - { - String value = getStringAttribute(node, name); - if (value != null && !value.isEmpty()) - return Integer.valueOf(value).intValue(); - else - return 0; - } - - public static MyMenuBar loadMenuBar(String xml, ActionListener action) - { - MyMenuBar menuBar = null; - try - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList rootMenus = root.getChildNodes(); - if (rootMenus != null) - { - menuBar = new MyMenuBar(); - for (int i = 0; i < rootMenus.getLength(); i++) - { - org.w3c.dom.Node menu = rootMenus.item(i); - if (menu.getNodeType() == Node.ELEMENT_NODE) - { - if (menu.getNodeName().equalsIgnoreCase("menu")) - { - String text = MyContorlUtil.getStringAttribute(menu, "text"); - MyRootMenu rootMenu = new MyRootMenu(); - rootMenu.setText(text); - menuBar.add(rootMenu); - processMenuItem(menu, rootMenu, action); - } - if (menu.getNodeName().equalsIgnoreCase("logo")) - { - String tooltip = MyContorlUtil.getStringAttribute(menu, "tooltip"); - String imageURL = MyContorlUtil.getStringAttribute(menu, "image"); - menuBar.add(Box.createGlue()); - JLabel label = new JLabel(MyContorlUtil.getImageIcon(imageURL)); - label.setBorder(BorderFactory.createEmptyBorder(0,5, 0, 5)); - label.setToolTipText(tooltip); - menuBar.add(label); - } - } - } - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - return menuBar; - } - - - public static void fixSettingPath(String xml, String tool, String newPath) - { - try - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList tools = root.getChildNodes(); - if (tools != null) - { - for (int i = 0; i < tools.getLength(); i++) - { - org.w3c.dom.Node menu = tools.item(i); - if (menu.getNodeType() == Node.ELEMENT_NODE) - { - if (menu.getNodeName().equalsIgnoreCase("tool")) - { - org.w3c.dom.Node att = menu.getAttributes().getNamedItem("id"); - String toolName = MyContorlUtil.getStringAttribute(menu, "toolName"); - if(tool.equals("cppcheck") && toolName.equals("cppcheck") ){ - org.w3c.dom.Node attribute = menu.getAttributes().getNamedItem("filePath"); - if (attribute != null) - attribute.setNodeValue(newPath.replaceAll("\\\\","\\/")); - } - else if(tool.equals("flawfinder") && toolName.equals("flawfinder")){ - org.w3c.dom.Node attribute = menu.getAttributes().getNamedItem("filePath"); - if (attribute != null) - attribute.setNodeValue(newPath.replaceAll("\\\\","\\/")); - } - } - } - } - } - - Transformer transformer=TransformerFactory.newInstance().newTransformer(); - DOMSource source=new DOMSource(doc); - File file =new File(ClassLoader.getSystemResource(xml).toURI()); - FileOutputStream outstream =new FileOutputStream("src/cppcheckplus/control/settings.xml"); - StreamResult reslut=new StreamResult(outstream); - transformer.transform(source, reslut); - outstream.close(); - - } - catch (Exception ex) - { - ex.printStackTrace(); - } - - } - - public static void loadToolsRoad(String xml, Main main) - { - try - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList tools = root.getChildNodes(); - if (tools != null) - { - for (int i = 0; i < tools.getLength(); i++) - { - org.w3c.dom.Node menu = tools.item(i); - if (menu.getNodeType() == Node.ELEMENT_NODE) - { - if (menu.getNodeName().equalsIgnoreCase("tool")) - { - String toolName = MyContorlUtil.getStringAttribute(menu, "toolName"); - String filePath = MyContorlUtil.getStringAttribute(menu, "filePath"); - String para= MyContorlUtil.getStringAttribute(menu, "para"); - if(toolName.equals("cppcheck")){ - main.cppcheckPath=filePath; - main.paracppcheck=para; - } - else if(toolName.equals("flawfinder")){ - main.flawfinderPath=filePath; - main.paraflawfinder=para; - } - } - } - } - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - - - - private static void processMenuItem(org.w3c.dom.Node menu, - JMenuItem parentMenu, ActionListener action) - { - NodeList children = menu.getChildNodes(); - if (children != null) - { - for (int j = 0; j < children.getLength(); j++) - { - org.w3c.dom.Node itemNode = children.item(j); - if (itemNode.getNodeType() != Node.ELEMENT_NODE) - continue; - boolean isMenuItem = itemNode.getNodeName().equalsIgnoreCase( - "menuitem"); - boolean isMenu = itemNode.getNodeName() - .equalsIgnoreCase("menu"); - if (!isMenuItem && !isMenu) - continue; - String text = getStringAttribute(itemNode, "text"); - String tooltip = getStringAttribute(itemNode, "tooltip"); - Icon icon = getIconAttribute(itemNode, "icon"); - String command = getStringAttribute(itemNode, "action"); - JMenuItem menuItem = null; - if (isMenu) - { - menuItem = new MyMenu(); - } - else - { - menuItem = new MyMenuItem(); - menuItem.addActionListener(action); - } - menuItem.setText(text); - menuItem.setToolTipText(tooltip); - menuItem.setActionCommand(command); - menuItem.setIcon(icon); - parentMenu.add(menuItem); - if (isMenu) - processMenuItem(itemNode, menuItem, action); - } - } - } - - //加载OutloolPanel上的最上面的快捷工具栏OutlookHeader - public static void loadOutlookToolBar(String xml, MyOutlookHeader header, - ActionListener action) - { - try - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList buttons = root.getChildNodes(); - if (buttons != null) - { - for (int i = 0; i < buttons.getLength(); i++) - { - org.w3c.dom.Node buttonNode = buttons.item(i); - if (buttonNode.getNodeType() == Node.ELEMENT_NODE) - { - if (buttonNode.getNodeName().equalsIgnoreCase("button")) - { - String tooltip = getStringAttribute(buttonNode, - "tooltip"); - Icon icon = getIconAttribute(buttonNode, "icon"); - String command = getStringAttribute(buttonNode, - "action"); - header.addButton(icon, tooltip, action, command); - } - if (buttonNode.getNodeName().equalsIgnoreCase( - "separator")) - header.addSeparator(); - } - } - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - - public static void loadOutlookPanel(String xml, MyOutlookPanel outlookPane) - { - try - { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db = dbf.newDocumentBuilder(); - Document doc = db.parse(getXMLFile(xml)); - Element root = doc.getDocumentElement(); - NodeList modules = root.getChildNodes(); - if (modules != null) - { - for (int i = 0; i < modules.getLength(); i++) - { - org.w3c.dom.Node moduleNode = modules.item(i); - if (moduleNode.getNodeType() == Node.ELEMENT_NODE - && moduleNode.getNodeName().equalsIgnoreCase( - "module")) - { - String text = getStringAttribute(moduleNode, "text"); - Icon icon = getIconAttribute(moduleNode, "icon"); - Icon iconSelected = getIconAttribute(moduleNode, - "selected_icon"); - MyOutlookBar bar = outlookPane.addBar(text, icon, - iconSelected,getOutlookPanelSubModules(moduleNode)); - } - } - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - - private static MyOutlookPanelListItem[] getOutlookPanelSubModules(Node moduleNode) - { - List listItemList = new ArrayList(); - if(moduleNode != null) - { - NodeList subModules = moduleNode.getChildNodes(); - for (int i = 0; i < subModules.getLength(); i++) - { - Node subNode = subModules.item(i); - if( (subNode.getNodeType() == Node.ELEMENT_NODE) && (subNode.getNodeName().equalsIgnoreCase("submodule")) ) - { - MyOutlookPanelListItem item = new MyOutlookPanelListItem(); - item.setIcon(getIconAttribute(subNode, "icon")); - item.setText(getStringAttribute(subNode,"text")); - item.setToolTip(getStringAttribute(subNode, "tooltip")); - item.setActionCommand(getStringAttribute(subNode, "action")); - listItemList.add(item); - } - } - } - MyOutlookPanelListItem[] items = new MyOutlookPanelListItem[listItemList.size()]; - return listItemList.toArray(items); - } - - public static void setupLookAndFeel() - { - //Locale.setDefault(new Locale("EN_US")); - com.jgoodies.looks.plastic.PlasticTheme theme = new ExperienceGreen() - { - public FontUIResource getControlTextFont() - { - return new FontUIResource(new Font("微软雅黑", 0, 11)); - } - }; - PlasticLookAndFeel.setPlasticTheme(theme); - try - { - UIManager.setLookAndFeel("com.jgoodies.looks.plastic.Plastic3DLookAndFeel"); - } - catch (Exception ex) - { - ex.printStackTrace(); - } - UIManager.put("Menu.selectionBackground", MyContorlUtil.NO_COLOR); - UIManager.put("MenuItem.selectionBackground",MyContorlUtil.MENUITEM_SELECTED_BACKGROUND); - // UIManager.put("PopupMenu.border", new FreePopupMenuBorder()); - UIManager.put("ToolTip.font", MyContorlUtil.FONT_14_BOLD); - UIManager.put("TabbedPane.contentBorderInsets", MyContorlUtil.ZERO_INSETS); - UIManager.put("TabbedPane.tabInsets", MyContorlUtil.ZERO_INSETS); - UIManager.put("TabbedPane.selectedTabPadInsets", MyContorlUtil.ZERO_INSETS); - UIManager.put("TabbedPane.tabAreaInsets", MyContorlUtil.ZERO_INSETS); - } + //菜单 + public static final Color MENUITEM_SELECTED_BACKGROUND = + new Color(166, 188, 140); + public static final Color MENUITEM_BACKGROUND = new Color(228, 235, 218); + //主面板 + public static final Color CONTENT_PANE_BACKGROUND = new Color(92, 153, 45); + public static final Color CONTENT_PANE_BACKGROUND2 = + new Color(78, 238, 148); + public static final Color CONTENT_PANE_BACKGROUND3 = + new Color(238, 220, 130); + //工具栏按钮 + public static final Color BUTTON_ROVER_COLOR = new Color(196, 196, 197); + //OutLook面板 + public static final Color OUTLOOK_TEXT_COLOR = new Color(120, 120, 125); + public static final Color OUTLOOK_SPLIT_COLOR = new Color(174, 171, 162); + public static final Color OUTLOOK_CONTAINER_COLOR = + new Color(217, 218, 219);//容器的背景颜色,灰色 + //多视图Tab + public static final Color TAB_BOTTOM_LINE_COLOR = new Color(167, 173, 175); + //快捷菜单面板 + public static final Color LIST_SPLIT_COLOR = new Color(105, 113, 120); + public static final Color LIST_BACKGROUND = new Color(175, 174, 176); + public static final Color LIST_TEXT_COLOR = new Color(49, 52, 58); + //其他 + public static final Color NO_COLOR = new Color(0, 0, 0, 0); + public static final Insets ZERO_INSETS = new Insets(0, 0, 0, 0); + //图形 + public static Font CHART_AXIS_FONT = new Font("微软雅黑", Font.PLAIN, 12); + //图例字体 +// 坐标轴字体 + public static Font CHART_TITLE_FONT = new Font("微软雅黑", Font.PLAIN, 14); + //标题字体 + public static Font CHART_LEGEND_FONT = new Font("微软雅黑", Font.PLAIN, 12); + + /** + * ClassLoader获得BufferedImage,注意和getClass().getResource()的路径不同 + * 最前面不带/ + * + * @param @param imgPath + * @param @return 参数 + * @return Image 返回类型 + * @throws + */ + public static Image getImage(String imgPath) { + BufferedImage img = null; + try { + //System.out.println("图片路径: "+imgPath); + img = ImageIO.read(ClassLoader.getSystemResource(imgPath)); + } catch (IOException e) { + System.out.println("图片路径找不到: " + imgPath); + //e.printStackTrace(); + } + return img; + } + + public static ImageIcon getImageIcon(String imgPath) { + ImageIcon icon = null; + try { + icon = new ImageIcon(ClassLoader.getSystemResource(imgPath)); + return icon; + } catch (Exception e) { + System.out.println("找不到图片: " + imgPath); + } + return icon; + } + + public static Image iconToImage(Icon icon) { + if (icon instanceof ImageIcon) { + return ((ImageIcon) icon).getImage(); + } else { + int w = icon.getIconWidth(); + int h = icon.getIconHeight(); + BufferedImage image = new BufferedImage(w, h, 2); + Graphics2D g = image.createGraphics(); + icon.paintIcon(null, g, 0, 0); + g.dispose(); + return image; + } + } + + public static ImageIcon createDyedIcon(ImageIcon icon, Color color) { + if (color == null) { + return icon; + } else { + int iconWidth = icon.getIconWidth(); + int iconHeight = icon.getIconHeight(); + BufferedImage bi = new BufferedImage(iconWidth, iconHeight, 2); + Graphics2D g2d = bi.createGraphics(); + icon.paintIcon(null, g2d, 0, 0); + g2d.dispose(); + Image dyedImage = createDyedImage(bi, color); + return new ImageIcon(dyedImage); + } + } + + public static Image createDyedImage(Image image, Color color) { + if (color == null) { + return image; + } + if (image != null) { + int w = image.getWidth(null); + int h = image.getHeight(null); + int[] pixels = new int[w * h]; + PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w); + try { + pg.grabPixels(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + return null; + } + BufferedImage bi = + new BufferedImage(w <= 1 ? 1 : w, h <= 1 ? 1 : h, 2); + for (int i = 0; i < pixels.length; i++) { + int pixel = pixels[i]; + int row = i / w; + int col = i % w; + if (color != null && pixel != 0) { + pixel = color.getRGB(); + } + bi.setRGB(col, row, pixel); + } + return bi; + } else { + return null; + } + } + + public static Icon createMovedIcon(Icon icon) { + return createMovedIcon(icon, 1, 1); + } + + public static Icon createMovedIcon(final Icon icon, final int offsetX, + final int offsetY) { + return new Icon() { + public void paintIcon(Component c, Graphics g, int x, int y) { + icon.paintIcon(c, g, x + offsetX, y + offsetY); + } + + public int getIconWidth() { + return icon.getIconWidth(); + } + + public int getIconHeight() { + return icon.getIconHeight(); + } + }; + } + + public static TexturePaint createTexturePaint(String imgPath) { + ImageIcon icon = getImageIcon(imgPath); + int imageWidth = icon.getIconWidth(); + int imageHeight = icon.getIconHeight(); + BufferedImage bi = new BufferedImage(imageWidth, imageHeight, 2); + Graphics2D g2d = bi.createGraphics(); + g2d.drawImage(icon.getImage(), 0, 0, null); + g2d.dispose(); + return new TexturePaint(bi, + new Rectangle(0, 0, imageWidth, imageHeight)); + } + + public static Color getColor(String color) { + Color convertedColor = Color.ORANGE; + try { + convertedColor = new Color(Integer.parseInt(color, 16)); + } catch (NumberFormatException e) { + String error = String.format("颜色转化出错:s%", color); + System.out.println(error); + } + return convertedColor; + } + + public static InputStream getXMLFile(String filePath) { + return ClassLoader.getSystemResourceAsStream(filePath); + } + + + public static String getStringAttribute(org.w3c.dom.Node node, + String name) { + org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name); + if (attribute != null) { + return attribute.getNodeValue(); + } else { + return null; + } + } + + private static Icon getIconAttribute(org.w3c.dom.Node node, String name) { + String iconURL = getStringAttribute(node, name); + if (iconURL != null && !iconURL.isEmpty()) { + return getImageIcon(iconURL); + } else { + return null; + } + } + + private static int getIntAttribute(org.w3c.dom.Node node, String name) { + String value = getStringAttribute(node, name); + if (value != null && !value.isEmpty()) { + return Integer.valueOf(value).intValue(); + } else { + return 0; + } + } + + public static MyMenuBar loadMenuBar(String xml, ActionListener action) { + MyMenuBar menuBar = null; + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); + Element root = doc.getDocumentElement(); + NodeList rootMenus = root.getChildNodes(); + if (rootMenus != null) { + menuBar = new MyMenuBar(); + for (int i = 0; i < rootMenus.getLength(); i++) { + org.w3c.dom.Node menu = rootMenus.item(i); + if (menu.getNodeType() == Node.ELEMENT_NODE) { + if (menu.getNodeName().equalsIgnoreCase("menu")) { + String text = + MyContorlUtil.getStringAttribute(menu, "text"); + MyRootMenu rootMenu = new MyRootMenu(); + rootMenu.setText(text); + menuBar.add(rootMenu); + processMenuItem(menu, rootMenu, action); + } + if (menu.getNodeName().equalsIgnoreCase("logo")) { + String tooltip = + MyContorlUtil.getStringAttribute(menu, + "tooltip"); + String imageURL = + MyContorlUtil.getStringAttribute(menu, "image"); + menuBar.add(Box.createGlue()); + JLabel label = new JLabel( + MyContorlUtil.getImageIcon(imageURL)); + label.setBorder( + BorderFactory.createEmptyBorder(0, 5, 0, 5)); + label.setToolTipText(tooltip); + menuBar.add(label); + } + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return menuBar; + } + + + public static void fixSettingPath(String xml, String tool, String newPath) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); + Element root = doc.getDocumentElement(); + NodeList tools = root.getChildNodes(); + if (tools != null) { + for (int i = 0; i < tools.getLength(); i++) { + org.w3c.dom.Node menu = tools.item(i); + if (menu.getNodeType() == Node.ELEMENT_NODE) { + if (menu.getNodeName().equalsIgnoreCase("tool")) { + org.w3c.dom.Node att = + menu.getAttributes().getNamedItem("id"); + String toolName = + MyContorlUtil.getStringAttribute(menu, + "toolName"); + if (tool.equals("cppcheck") && + toolName.equals("cppcheck")) { + org.w3c.dom.Node attribute = + menu.getAttributes() + .getNamedItem("filePath"); + if (attribute != null) { + attribute.setNodeValue( + newPath.replaceAll("\\\\", "\\/")); + } + } else if (tool.equals("flawfinder") && + toolName.equals("flawfinder")) { + org.w3c.dom.Node attribute = + menu.getAttributes() + .getNamedItem("filePath"); + if (attribute != null) { + attribute.setNodeValue( + newPath.replaceAll("\\\\", "\\/")); + } + } + } + } + } + } + + Transformer transformer = + TransformerFactory.newInstance().newTransformer(); + DOMSource source = new DOMSource(doc); + File file = new File(ClassLoader.getSystemResource(xml).toURI()); + FileOutputStream outstream = + new FileOutputStream("src/cppcheckplus/control/settings.xml"); + StreamResult reslut = new StreamResult(outstream); + transformer.transform(source, reslut); + outstream.close(); + + } catch (Exception ex) { + ex.printStackTrace(); + } + + } + + public static void loadToolsRoad(String xml, Main main) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); + Element root = doc.getDocumentElement(); + NodeList tools = root.getChildNodes(); + if (tools != null) { + for (int i = 0; i < tools.getLength(); i++) { + org.w3c.dom.Node menu = tools.item(i); + if (menu.getNodeType() == Node.ELEMENT_NODE) { + if (menu.getNodeName().equalsIgnoreCase("tool")) { + String toolName = + MyContorlUtil.getStringAttribute(menu, + "toolName"); + String filePath = + MyContorlUtil.getStringAttribute(menu, + "filePath"); + String para = + MyContorlUtil.getStringAttribute(menu, "para"); + + switch (toolName) { + case "cppcheck": + main.cppcheckPath = filePath; + main.paracppcheck = para; + break; + case "flawfinder": + main.flawfinderPath = filePath; + main.paraflawfinder = para; + break; + case "clang-tidy": + main.clangtidyPath = filePath; + main.paraclangtidy = para; + break; + default: + break; + } + } + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + + private static void processMenuItem(org.w3c.dom.Node menu, + JMenuItem parentMenu, + ActionListener action) { + NodeList children = menu.getChildNodes(); + if (children != null) { + for (int j = 0; j < children.getLength(); j++) { + org.w3c.dom.Node itemNode = children.item(j); + if (itemNode.getNodeType() != Node.ELEMENT_NODE) { + continue; + } + boolean isMenuItem = + itemNode.getNodeName().equalsIgnoreCase("menuitem"); + boolean isMenu = + itemNode.getNodeName().equalsIgnoreCase("menu"); + if (!isMenuItem && !isMenu) { + continue; + } + String text = getStringAttribute(itemNode, "text"); + String tooltip = getStringAttribute(itemNode, "tooltip"); + Icon icon = getIconAttribute(itemNode, "icon"); + String command = getStringAttribute(itemNode, "action"); + JMenuItem menuItem = null; + if (isMenu) { + menuItem = new MyMenu(); + } else { + menuItem = new MyMenuItem(); + menuItem.addActionListener(action); + } + menuItem.setText(text); + menuItem.setToolTipText(tooltip); + menuItem.setActionCommand(command); + menuItem.setIcon(icon); + parentMenu.add(menuItem); + if (isMenu) { + processMenuItem(itemNode, menuItem, action); + } + } + } + } + + //加载OutloolPanel上的最上面的快捷工具栏OutlookHeader + public static void loadOutlookToolBar(String xml, MyOutlookHeader header, + ActionListener action) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(getXMLFile(xml)); + Element root = doc.getDocumentElement(); + NodeList buttons = root.getChildNodes(); + if (buttons != null) { + for (int i = 0; i < buttons.getLength(); i++) { + org.w3c.dom.Node buttonNode = buttons.item(i); + if (buttonNode.getNodeType() == Node.ELEMENT_NODE) { + if (buttonNode.getNodeName() + .equalsIgnoreCase("button")) { + String tooltip = + getStringAttribute(buttonNode, "tooltip"); + Icon icon = getIconAttribute(buttonNode, "icon"); + String command = + getStringAttribute(buttonNode, "action"); + header.addButton(icon, tooltip, action, command); + } + if (buttonNode.getNodeName() + .equalsIgnoreCase("separator")) { + header.addSeparator(); + } + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public static void loadOutlookPanel(String xml, + MyOutlookPanel outlookPane) { + try { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = db.parse(getXMLFile(xml)); + Element root = doc.getDocumentElement(); + NodeList modules = root.getChildNodes(); + if (modules != null) { + for (int i = 0; i < modules.getLength(); i++) { + org.w3c.dom.Node moduleNode = modules.item(i); + if (moduleNode.getNodeType() == Node.ELEMENT_NODE && + moduleNode.getNodeName().equalsIgnoreCase("module")) { + String text = getStringAttribute(moduleNode, "text"); + Icon icon = getIconAttribute(moduleNode, "icon"); + Icon iconSelected = + getIconAttribute(moduleNode, "selected_icon"); + MyOutlookBar bar = + outlookPane.addBar(text, icon, iconSelected, + getOutlookPanelSubModules(moduleNode)); + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + private static MyOutlookPanelListItem[] getOutlookPanelSubModules( + Node moduleNode) { + List listItemList = + new ArrayList(); + if (moduleNode != null) { + NodeList subModules = moduleNode.getChildNodes(); + for (int i = 0; i < subModules.getLength(); i++) { + Node subNode = subModules.item(i); + if ((subNode.getNodeType() == Node.ELEMENT_NODE) && + (subNode.getNodeName().equalsIgnoreCase("submodule"))) { + MyOutlookPanelListItem item = new MyOutlookPanelListItem(); + item.setIcon(getIconAttribute(subNode, "icon")); + item.setText(getStringAttribute(subNode, "text")); + item.setToolTip(getStringAttribute(subNode, "tooltip")); + item.setActionCommand( + getStringAttribute(subNode, "action")); + listItemList.add(item); + } + } + } + MyOutlookPanelListItem[] items = + new MyOutlookPanelListItem[listItemList.size()]; + return listItemList.toArray(items); + } + + public static void setupLookAndFeel() { + //Locale.setDefault(new Locale("EN_US")); + com.jgoodies.looks.plastic.PlasticTheme theme = new ExperienceGreen() { + public FontUIResource getControlTextFont() { + return new FontUIResource(new Font("微软雅黑", 0, 11)); + } + }; + PlasticLookAndFeel.setPlasticTheme(theme); + try { + UIManager.setLookAndFeel( + "com.jgoodies.looks.plastic.Plastic3DLookAndFeel"); + } catch (Exception ex) { + ex.printStackTrace(); + } + UIManager.put("Menu.selectionBackground", MyContorlUtil.NO_COLOR); + UIManager.put("MenuItem.selectionBackground", + MyContorlUtil.MENUITEM_SELECTED_BACKGROUND); + // UIManager.put("PopupMenu.border", new FreePopupMenuBorder()); + UIManager.put("ToolTip.font", MyContorlUtil.FONT_14_BOLD); + UIManager.put("TabbedPane.contentBorderInsets", + MyContorlUtil.ZERO_INSETS); + UIManager.put("TabbedPane.tabInsets", MyContorlUtil.ZERO_INSETS); + UIManager.put("TabbedPane.selectedTabPadInsets", + MyContorlUtil.ZERO_INSETS); + UIManager.put("TabbedPane.tabAreaInsets", MyContorlUtil.ZERO_INSETS); + } } diff --git a/src/cppcheckplus/control/settings.xml b/src/cppcheckplus/control/settings.xml index 43efee0..078103a 100644 --- a/src/cppcheckplus/control/settings.xml +++ b/src/cppcheckplus/control/settings.xml @@ -1,6 +1,8 @@ - + + + \ No newline at end of file diff --git a/src/cppcheckplus/shortcut/MyTree.java b/src/cppcheckplus/shortcut/MyTree.java index 74485b3..775e3a2 100644 --- a/src/cppcheckplus/shortcut/MyTree.java +++ b/src/cppcheckplus/shortcut/MyTree.java @@ -71,9 +71,9 @@ public class MyTree extends JPanel { if(selectedNode!=null && selectedNode.isLeaf()){ result = ((FileObject) selectedNode.getUserObject()).file; jf.src = result.getAbsolutePath(); - if (!jf.tabPanel.isSelectTabComponents("代码")) { - jf.tabPanel.addTab("代码", jf.testCode); - jf.tabPanel.isSelectTabComponents("代码"); + if (!jf.centerTabPanel.isSelectTabComponents("代码")) { + jf.centerTabPanel.addTab("代码", jf.testCode); + jf.centerTabPanel.isSelectTabComponents("代码"); } textCode.setText(FileTools.readFile(result)); } diff --git a/src/cppcheckplus/tab/MyTabPanel1.java b/src/cppcheckplus/tab/MyTabPanel1.java index ace4d72..df3da4d 100644 --- a/src/cppcheckplus/tab/MyTabPanel1.java +++ b/src/cppcheckplus/tab/MyTabPanel1.java @@ -1,96 +1,92 @@ package cppcheckplus.tab; +import cppcheckplus.control.MyContorlUtil; import java.awt.Component; import javax.swing.JTabbedPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import cppcheckplus.control.MyContorlUtil; +public class MyTabPanel1 extends JTabbedPane { + private final int preferredUnselectedTabWidth; + private final int preferredTabHeight; + + public MyTabPanel1() { + preferredUnselectedTabWidth = 80; + preferredTabHeight = MyContorlUtil.getImageIcon( + "control/images/tab_header_background.png").getIconHeight(); + init(); + } -public class MyTabPanel1 extends JTabbedPane -{ - private int preferredUnselectedTabWidth; - private int preferredTabHeight; + private void init() { + setFont(MyContorlUtil.FONT_12_BOLD); + setForeground(MyContorlUtil.DEFAULT_TEXT_COLOR); + setBorder(null); + setFocusable(false); + setTabLayoutPolicy(1); + setOpaque(false); + setUI(new MyTabPanelUI1(this)); + addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + updateTabComponents(); + } + }); + } - public MyTabPanel1() - { - preferredUnselectedTabWidth = 80; - preferredTabHeight = MyContorlUtil.getImageIcon("control/images/tab_header_background.png").getIconHeight(); - init(); - } - private void init() - { - setFont(MyContorlUtil.FONT_12_BOLD); - setForeground(MyContorlUtil.DEFAULT_TEXT_COLOR); - setBorder(null); - setFocusable(false); - setTabLayoutPolicy(1); - setOpaque(false); - setUI(new MyTabPanelUI1(this)); - addChangeListener(new ChangeListener() { - public void stateChanged(ChangeEvent e) - { - updateTabComponents(); - } - }); - } + @Override + public void addTab(String title, Component component) { + super.addTab(title, component); + int index = getTabCount() - 1; + MyTabComponent1 tabComponent = new MyTabComponent1(title, this); + tabComponent.setTitle(title); + setTabComponentAt(index, tabComponent); + setToolTipTextAt(index, title); + updateTabComponents(); + } - - @Override - public void addTab(String title, Component component) - { - super.addTab(title, component); - int index = getTabCount() - 1; - MyTabComponent1 tabComponent = new MyTabComponent1(title, this); - tabComponent.setTitle(title); - setTabComponentAt(index, tabComponent); - setToolTipTextAt(index, title); - updateTabComponents(); - } - - public boolean isSelectTabComponents(String oId) - { - for(int i=0;i