1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
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;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.FileOutputStream;
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;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
/**
* @ClassName: ContorlUtil
* @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 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 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<MyOutlookPanelListItem> listItemList =
new ArrayList<MyOutlookPanelListItem>();
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);
}
}
|