Skip to content

Commit

Permalink
add support for using mac menubar, further improve menubar code, vari…
Browse files Browse the repository at this point in the history
…ous fixes and cleanups
  • Loading branch information
ESIC-DA committed Jun 6, 2019
1 parent c35f07e commit c50b873
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 104 deletions.
54 changes: 22 additions & 32 deletions src/esic/AHAGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AHAGUI extends javax.swing.JFrame implements org.graphstream.ui.vie
protected AHAModel m_model=null;

protected javax.swing.JLabel m_btmPnlSearchStatus=new javax.swing.JLabel("");
protected javax.swing.JCheckBoxMenuItem m_btmPnlChangeOnMouseOver=new javax.swing.JCheckBoxMenuItem("Update on MouseOver",false), m_infoPnlShowOnlyMatchedMetrics=new javax.swing.JCheckBoxMenuItem("Show Only Matched Metrics",true), m_infoPnlShowScoringSpecifics=new javax.swing.JCheckBoxMenuItem("Show Score Metric Specifics",false);
protected javax.swing.JCheckBoxMenuItem m_infoPnlUpdateOnMouseover=new javax.swing.JCheckBoxMenuItem("Update on MouseOver",false), m_infoPnlShowOnlyMatchedMetrics=new javax.swing.JCheckBoxMenuItem("Show Only Matched Metrics",true), m_infoPnlShowScoringSpecifics=new javax.swing.JCheckBoxMenuItem("Show Score Metric Specifics",false);
protected javax.swing.JTextField m_btmPnlSearch=new javax.swing.JTextField("Search...");
protected javax.swing.JScrollPane m_inspectorPanel=null;
protected final String[][] m_infoPnlColumnHeaders={{"Info"},{"Open Internal Port", "Proto"},{"Open External Port", "Proto"},{"Connected Process", "PID"}, {"Score Metric", "Value"}}; //right now things would break if the number of these ever got changed at runtime, so made static.
Expand Down Expand Up @@ -92,28 +92,26 @@ public AHAGUI(AHAModel model, boolean useMultiLineGraph)

// -- begin file menu --
AHAGUIHelpers.createMenuItem(new javax.swing.JMenuItem("Open..."), this, "openNewFile", "Open a new file", fileMenu); //openMenuItem.setMnemonic(KeyEvent.VK_N);


fileMenu.addSeparator();
AHAGUIHelpers.createMenuItem(new javax.swing.JMenuItem("Exit"), this, "exit", "Exit AHA-GUI", fileMenu);


// -- begin view menu --
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Hide Windows Operating System Processes"), this, "hideOSProcs", "Hides the usual Windows™ operating system processes, while interesting these processes can get in the way of other analysis.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Hide External Node"), this, "hideExtNode", "Hides the main 'External' node from the graph that all nodes which listen on externally accessible addresses connect to.",viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show External Node", true), this, "processpath==external", "Shows the main 'External' node from the graph that all nodes which listen on externally accessible addresses connect to.",viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Use DNS Names"), this, "showFQDN", "Show the DNS names of external nodes rather than IPs.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Use Custom ScoreFile", m_model.m_useCustomOverlayScoreFile), this, "useCustom", "If a custom score file was loaded, this option will apply those custom directives to the graph view.", viewMenu);

viewMenu.addSeparator();
AHAGUIHelpers.createMenuItem(m_btmPnlChangeOnMouseOver, this, "refreshInfoPanel", "Enable change of the inspector above on hovering over nodes in addition to clicking.", viewMenu);
AHAGUIHelpers.createMenuItem(m_infoPnlShowOnlyMatchedMetrics, this, "refreshInfoPanel", "Only displays metrics which were matched, for example if ASLR was true.", viewMenu);
AHAGUIHelpers.createMenuItem(m_infoPnlShowScoringSpecifics, this, "refreshInfoPanel", "Shows the specific metric in the inspector above that matched.", viewMenu);
AHAGUIHelpers.createMenuItem(m_infoPnlUpdateOnMouseover, this, "refreshInfoPanel", "Enable change of the inspector above on hovering over nodes in addition to clicking.", viewMenu);

viewMenu.addSeparator();
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show Connectionless Nodes", true), this, "protocol==none", "Show / Hide nodes with no protocol in the graph.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show Pipe", true), this, "protocol==pipe", "Show / Hide Pipe protocol nodes in the graph.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show TCP", true), this, "protocol==tcp", "Show / Hide TCP protocol nodes in the graph.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show UDP", true), this, "protocol==udp", "Show / Hide UDP protocol nodes in the graph.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show Pipe", true), this, "protocol==pipe", "Show / Hide Pipe protocol nodes in the graph.", viewMenu);
AHAGUIHelpers.createMenuItem(new javax.swing.JCheckBoxMenuItem("Show Connectionless Nodes", true), this, "protocol==none", "Show / Hide nodes with no protocol in the graph.", viewMenu);

viewMenu.addSeparator();
javax.swing.ButtonGroup buttonGroup=new javax.swing.ButtonGroup();
Expand Down Expand Up @@ -259,14 +257,14 @@ public void actionPerformed(java.awt.event.ActionEvent e) //swing actions go to
String actionCommand=e.getActionCommand();
if (actionCommand.equals("dataView")) { showDataView(this); }
else if (actionCommand.equals("hideOSProcs")) { m_model.hideOSProcs(m_model.m_graph, ((javax.swing.JCheckBoxMenuItem)source).isSelected()); }
else if (actionCommand.equals("hideExtNode")) { m_model.hideFalseExternalNode(m_model.m_graph, ((javax.swing.JCheckBoxMenuItem)source).isSelected()); }
//else if (actionCommand.equals("hideExtNode")) { m_model.hideFalseExternalNode(m_model.m_graph, ((javax.swing.JCheckBoxMenuItem)source).isSelected()); }
else if (actionCommand.equals("showFQDN")) { m_model.useFQDNLabels(m_model.m_graph, ((javax.swing.JCheckBoxMenuItem)source).isSelected()); }
else if (actionCommand.equals("resetZoom")) { m_graphViewPanel.getCamera().resetView(); }
else if (actionCommand.equals("exit")) { terminateGUI(false, false); } //close window
else if (actionCommand.equals("openNewFile")) { s_userWantsToRelaunch.set(true); terminateGUI(false, false); } //close window
else if (actionCommand.equals("refreshInfoPanel")) { updateInfoDisplays(m_currentlyDisplayedNode.get(), false, m_model); } //update info display because a checkbox somewhere changed
else if (actionCommand.equals("search")) { m_btmPnlSearchStatus.setText(m_model.handleSearch(m_btmPnlSearch.getText())); }
else if (actionCommand.contains("protocol=="))
else if (actionCommand.contains("protocol==") || actionCommand.contains("processpath=="))
{
boolean hide=!((javax.swing.JCheckBoxMenuItem)e.getSource()).isSelected();
m_model.genericHideUnhideNodes( actionCommand,hide );
Expand Down Expand Up @@ -375,7 +373,7 @@ private void showDataView(AHAGUI parent) //shows the window that lists the liste

protected void updateInfoDisplays(Node node, boolean occuredFromMouseOver, AHAModel model)
{
if ( node==null || (occuredFromMouseOver && !m_btmPnlChangeOnMouseOver.isSelected()) ) { return; } //if element is null, or triggered from mosueover and we're presently supposed to ignore that, return
if ( node==null || (occuredFromMouseOver && !m_infoPnlUpdateOnMouseover.isSelected()) ) { return; } //if element is null, or triggered from mosueover and we're presently supposed to ignore that, return
m_currentlyDisplayedNode.set(node);
//final String bottomAreaConnnection="Connections: "+getNodeConnectionString(node), bottomAreaName=getNameString(node," "), bottomAreaScoreInfo="Score: "+getNodeScoreReasonString(node, false);
Object[][] infoData=null, intPorts=null, extPorts=null, connectionData=null, scoreReasons=null;
Expand Down Expand Up @@ -473,10 +471,6 @@ protected void updateInfoDisplays(Node node, boolean occuredFromMouseOver, AHAMo
if (!isNegativeScore) { scoreValue="+"+scoreValue; } //scoreReasons[j][0]=scoreString+" (+"+scoreValue+")";
String output=scoreString+" ("+scoreValue+")";
if (isNegativeScore) { output="<html><font color=red>"+output+"</font></html>"; }
//if (isNegativeScore) { output="<html><p style='color:red;overflow:elipsis'>"+output+"</p></html>"; }

// java.text.AttributedString s=new java.text.AttributedString(scoreString+" ("+scoreValue+")");
// if (isNegativeScore) { s.setAttribute(java.awt.font.TextAttribute.FOREGROUND, java.awt.Color.red); }

scoreReasons[j][0]=output;
}
Expand Down Expand Up @@ -617,19 +611,8 @@ public synchronized void startedHoveringOverElement(org.graphstream.ui.graphicGr
updateInfoDisplays(node, true, m_model);
}

@Override
public void mouseOver(String id)
{
// TODO Auto-generated method stub

}
@Override
public void mouseLeft(String id)
{
// TODO Auto-generated method stub

}
//END graph interaction handlers
public void mouseOver(String id) {}
public void mouseLeft(String id) {}

public void windowOpened(java.awt.event.WindowEvent e) {} //Window listener related events
public void windowIconified(java.awt.event.WindowEvent e) {}
Expand Down Expand Up @@ -663,8 +646,9 @@ protected void terminateGUI (boolean reOpenSameFile, boolean triggeredFromWindow

public static void main(String args[])
{

boolean debug=false, verbose=false, useMultiLineGraph=true, useOverlayScoreFile=false, applyTheme=true, force100percentScale=true;
String scoreFileName="", inputFileName="";
String scoreFileName="", inputFileName="", useAppleTopOfScreenMenubarIfApplicable="true";
java.awt.Font uiFont=new java.awt.Font(java.awt.Font.MONOSPACED,java.awt.Font.PLAIN,12);
for (String s : args)
{
Expand All @@ -675,26 +659,32 @@ public static void main(String args[])
if (argTokens[0].equalsIgnoreCase("--debug")) { debug=true; } //print more debugs while running
if (argTokens[0].equalsIgnoreCase("--verbose")) { verbose=true; } //print more debugs while running
if (argTokens[0].equalsIgnoreCase("--single")) { useMultiLineGraph=false; } //draw single lines between nodes
if (argTokens[0].equalsIgnoreCase("--bigfont")) { uiFont=new java.awt.Font(java.awt.Font.MONOSPACED,java.awt.Font.PLAIN,18); } //use 18pt font instead of default

if (argTokens[0].equalsIgnoreCase("--forceJMenu")) { useAppleTopOfScreenMenubarIfApplicable="false"; } //draw single lines between nodes
if (argTokens[0].equalsIgnoreCase("--notheme")) { applyTheme=false; } //draw single lines between nodes
if (argTokens[0].equalsIgnoreCase("--noforcescale")) { force100percentScale=false; } //draw single lines between nodes
if (argTokens[0].equalsIgnoreCase("--bigfont")) { uiFont=new java.awt.Font(java.awt.Font.MONOSPACED,java.awt.Font.PLAIN,18); } //use 18pt font instead of default
if (argTokens[0].equalsIgnoreCase("scorefile")) { scoreFileName=argTokens[1]; useOverlayScoreFile=true; } //path to custom score file, and enable since...that makes sense in this case
if (argTokens[0].equalsIgnoreCase("inputFile")) { inputFileName=argTokens[1]; } //path to input file. ignore CLI filename on second time through here (means user asked to open a new file)
if (argTokens[0].equals("help")||argTokens[0].equals("?"))
if (argTokens[0].toLowerCase().contains("help")||argTokens[0].equals("?"))
{
System.out.println
(
"Arguments are as follows:"+
"--debug : print additional information to console while running\n"+
"Possible arguments are as follows:\n"+
"--debug : print tons of additional information for debugging use to console while running\n"+
"--verbose : print additional information to console while running (but less than debug)\n"+
"--single : use single lines between nodes with multiple connections\n"+
"--bigfont : use 18pt font instead of the default 12pt font (good for demos). Will have no effect if used with --notheme\n"+
"--forceJMenu : on macs, force use of normal JMenu rather than using the traditional macOS menubar at the top of the screen\n"+
"--notheme : attempt to minimize theming information set on gui components so that the OS theme will be used. Unsupported / components may be oddly sized.\n"+
"--noforcescale : ignore the app attempting to force the scale down to 100% to avoid several existing graphstream bugs. \n"+
"scorefile=scorefile.csv : use the scorefile specified after the equals sign\n"+
"inputFile=inputFile.csv : use the inputFile specified after the equals sign\n"
); return;
}
} catch (Exception e) { e.printStackTrace(); }
}
System.setProperty("apple.laf.useScreenMenuBar", useAppleTopOfScreenMenubarIfApplicable);
if (force100percentScale) { System.setProperty("sun.java2d.uiScale", "100%"); } //sad little hack to work around current issues on HiDPI machines
if (applyTheme) { AHAGUIHelpers.applyTheme(uiFont); }

Expand Down
68 changes: 7 additions & 61 deletions src/esic/AHAGUIHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ public static class AHASortIcon implements javax.swing.Icon
public AHASortIcon(boolean icon) { thingToDraw=icon; }
public void paintIcon(Component c, java.awt.Graphics g, int x, int y)
{
java.awt.Graphics2D g2 = (java.awt.Graphics2D)g;
//System.out.printf("paintIcon called x=%d y=%d\n",x,y);
java.awt.Graphics2D g2 = (java.awt.Graphics2D)g; //System.out.printf("paintIcon called x=%d y=%d\n",x,y);
g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(java.awt.Color.GREEN);
if (thingToDraw==true) { g2.fillPolygon(new int[]{x,x+6,x+3}, new int[]{y+5,y+5,y}, 3); }
Expand All @@ -192,7 +191,11 @@ public static javax.swing.JMenuItem createMenuItem(javax.swing.JMenuItem item, j
{
item.setActionCommand(actionCommand);
item.addActionListener(listener);
item.setToolTipText(AHAGUIHelpers.styleToolTipText(tooltip));
try
{
if (System.getProperty("os.name").toLowerCase().contains("mac") && System.getProperty("apple.laf.useScreenMenuBar")!=null && System.getProperty("apple.laf.useScreenMenuBar").equalsIgnoreCase("false")) { tooltip=AHAGUIHelpers.styleToolTipText(tooltip); }
} catch (Exception e) { System.err.print("Failed to style tooltip text...possibly due to inability to assess platform. error: ");e.printStackTrace(); }
item.setToolTipText(tooltip);
menu.add(item);
return item;
}
Expand Down Expand Up @@ -258,57 +261,6 @@ public static String styleToolTipText(String s) //format all tool tip texts by m
return "<html><p style='font-style:bold;color:black;background:white;'>"+s+"</p></html>";
}

// public static class AttributedLabel extends javax.swing.JLabel {
//
// java.text.AttributedString str=null;
// int length=0;
// public AttributedLabel(Object o)
// {
// if (o instanceof String) { str = new java.text.AttributedString((String)o); }
// if (o instanceof java.text.AttributedString) { str = (java.text.AttributedString)o; }
// if (str!=null)
// {
// length=str.getIterator().getEndIndex();
// }
// }
// public void paintComponent(java.awt.Graphics g){
// // Render the string
// if (str==null || length < 1) { return; }
// java.awt.Graphics2D g2=(java.awt.Graphics2D)g;
// java.awt.Rectangle r=g.getClipBounds();
// //System.out.println("height="+r.height);
// java.awt.Font uiFont=(java.awt.Font)javax.swing.UIManager.get("Label.font");
// if (uiFont!=null)
// {
// g2.setFont(uiFont);
// str.addAttribute(java.awt.font.TextAttribute.FONT, uiFont);
// }
//
// g2.setRenderingHint(
// java.awt.RenderingHints.KEY_TEXT_ANTIALIASING,
// java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//
// g2.drawString(str.getIterator(), 1, r.height-3);
//// java.text.AttributedCharacterIterator it = str.getIterator();
//// StringBuilder stringBuilder = new StringBuilder();
////
//// char ch = it.current();
//// while( ch != java.text.CharacterIterator.DONE)
//// {
//// stringBuilder.append( ch);
//// ch = it.next();
//// }
//// System.out.println(stringBuilder.toString());
// }
//
//}

// public static class AttributedLabelRenderer extends javax.swing.table.DefaultTableCellRenderer {
// public Component getTableCellRendererComponent(javax.swing.JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
// {
// return new AttributedLabel(value);
// }
//}

public static javax.swing.JScrollPane createTablesInScrollPane(String[][] columnHeaders, String[][] columnTooltips, Object[][][] initialData, javax.swing.JTable[] tableRefs, int[] columnWidths)
{
Expand Down Expand Up @@ -352,16 +304,13 @@ public String getToolTipText(java.awt.event.MouseEvent e)
}
};
}

// tableRefs[i].setDefaultRenderer(java.text.AttributedString.class, new AttributedLabelRenderer());
// tableRefs[i].setDefaultRenderer(String.class, new AttributedLabelRenderer());
tableRefs[i].setModel( new javax.swing.table.DefaultTableModel(initialData[i], columnHeaders[i])
{
public Class<?> getColumnClass(int column) //makes it so row sorters work properly
{
try
{
Object o=null; //System.out.println("ColDetectorCalled for column="+column); //lazy hack but seems to work so shrug
Object o=null;
for (int row=0;row<getRowCount();row++)
{
o=getValueAt(row, column);
Expand All @@ -372,14 +321,11 @@ public Class<?> getColumnClass(int column) //makes it so row sorters work proper
if (o instanceof Double) { return Double.class; }
if (o instanceof Float) { return Float.class; }
if (o instanceof Long) { return Long.class; }
//if (o instanceof java.text.AttributedString) { return java.text.AttributedString.class; }
} catch (Exception e) { e.printStackTrace(); }
return Object.class;
}
});
tableRefs[i].setDefaultRenderer(Integer.class, tcLeftAlignRenderer);
//tableRefs[i].getTableHeader().setBorder(null);
//tableRefs[i].setBorder(null);
tableRefs[i].setPreferredScrollableViewportSize(tableRefs[i].getPreferredSize());
tableRefs[i].setAlignmentY(javax.swing.JTable.TOP_ALIGNMENT);
tableRefs[i].getTableHeader().setAlignmentY(javax.swing.JTable.TOP_ALIGNMENT);
Expand Down
14 changes: 3 additions & 11 deletions src/esic/AHAModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,7 @@ protected void hideOSProcs(Graph g, boolean hide)
genericHideUnhideNodes( "processpath=="+s,hide );
}
}

protected void hideFalseExternalNode(Graph g, boolean hide)
{
genericHideUnhideNodes( "processpath==external",hide );
}
protected void hideFalseExternalNode(Graph g, boolean hide) { genericHideUnhideNodes( "processpath==external",hide ); }

protected int genericHideUnhideNodes( String criteria, boolean hide )
{
Expand All @@ -899,8 +895,7 @@ protected int genericHideUnhideNodes( String criteria, boolean hide )
String attribute=args[0].toLowerCase();
String seeking="";
if (args.length >1 && args[1]!=null) { seeking=args[1].toLowerCase(); }//important to do every loop since seeking may be modified if it is inverted
//System.out.println("attr='"+attribute+"' seeking='"+seeking+"'");
for (Node node:m_graph)
for (Node node:m_graph) //System.out.println("attr='"+attribute+"' seeking='"+seeking+"'");
{
String attrValue=(String)node.getAttribute(attribute);
if (attrValue!=null && seeking!=null && notInverseSearch==attrValue.equals(seeking))
Expand Down Expand Up @@ -984,7 +979,6 @@ protected int genericEmphasizeNodes( String criteria, boolean emphasize ) //TODO
nodeClass=nodeClass.replaceAll("emphasize, ", "");
if (m_verbose) { System.out.println("unEmphasize node="+node.getId()); }
}
//System.out.println("Writing node class=|"+nodeClass+"|");
node.setAttribute("ui.class", nodeClass);

java.util.Iterator<Edge> it=node.iterator(); //TODO rewrite in gs2.0 parlance
Expand Down Expand Up @@ -1236,9 +1230,7 @@ protected TableDataHolder generateReport()
data[j++]=strAsInt((String)n.getAttribute(AHAModel.scrMethdAttr(ScoreMethod.Normal)));
data[j++]=strAsInt((String)n.getAttribute(AHAModel.scrMethdAttr(ScoreMethod.WorstCommonProcBETA)));
// RelativeScore CODE //
data[j++]=strAsDouble((String)n.getAttribute(scrMethdAttr(ScoreMethod.RelativeScoreBETA)));
//data[j++]=n.getAttribute("parents");
//data[j++]=n.getAttribute("sibling");
data[j++]=strAsDouble((String)n.getAttribute(scrMethdAttr(ScoreMethod.RelativeScoreBETA))); //data[j++]=n.getAttribute("parents"); //data[j++]=n.getAttribute("sibling");
// END RelativeScore CODE //
tableData[tableNumber][i++]=data;
}
Expand Down

0 comments on commit c50b873

Please sign in to comment.