00001 package cz.vutbr.fit.dudka.SGVis;
00002
00010 import java.awt.BorderLayout;
00011 import java.awt.Color;
00012 import java.awt.Component;
00013 import java.awt.Dimension;
00014 import java.awt.Font;
00015 import java.awt.GridLayout;
00016 import java.awt.event.KeyEvent;
00017 import java.net.MalformedURLException;
00018 import java.net.URL;
00019
00020 import javax.swing.BorderFactory;
00021 import javax.swing.JDialog;
00022 import javax.swing.JFrame;
00023 import javax.swing.JLabel;
00024 import javax.swing.JMenu;
00025 import javax.swing.JMenuBar;
00026 import javax.swing.JMenuItem;
00027 import javax.swing.JOptionPane;
00028 import javax.swing.JPanel;
00029 import javax.swing.JToolBar;
00030 import javax.swing.border.TitledBorder;
00031 import javax.swing.event.ChangeEvent;
00032 import javax.swing.event.ChangeListener;
00033
00034 import cz.vutbr.fit.dudka.SGVis.Data.Statistics;
00035 import cz.vutbr.fit.dudka.SGVis.Visual.GraphDisplay;
00036 import cz.vutbr.fit.dudka.SGVis.Visual.GraphView;
00037 import cz.vutbr.fit.dudka.SGVis.Visual.LookupWorker;
00038
00043 public class Main extends JFrame {
00044 private static final long serialVersionUID = 1L;
00045 private JPanel jContentPane = null;
00046 private JMenuBar jJMenuBar = null;
00047 private JMenu visMenu = null;
00048 private JMenuItem menuItemZootmToFit = null;
00049 private JMenuItem menuItemNewVis = null;
00050 private JToolBar statusBar = null;
00051 private JLabel statusText = null;
00052 final private GraphView gview;
00053 private JMenuItem menuItemStatistics = null;
00054 private JMenu helpMenu = null;
00055 private JMenuItem menuItemHelp = null;
00056
00060 private Main(GraphView gview) {
00061 super();
00062 this.gview = gview;
00063 initialize();
00064 gview.addStatusListener(new ChangeListener() {
00065 public void stateChanged(ChangeEvent e) {
00066 GraphView gv = (GraphView) e.getSource();
00067 statusText.setText(gv.getStatus());
00068 statusText.setForeground(
00069 gv.isStatusError()?
00070 Color.RED:
00071 Color.BLACK
00072 );
00073 getStatusBar().invalidate();
00074 }
00075 });
00076 getJContentPane().add(new GraphDisplay(gview));
00077 this.pack();
00078 this.setExtendedState(JFrame.MAXIMIZED_BOTH);
00079 }
00080
00086 private void initialize() {
00087 this.setSize(1024, 768);
00088 this.setJMenuBar(getJJMenuBar());
00089 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00090 this.setMinimumSize(new Dimension(480, 320));
00091 this.setContentPane(getJContentPane());
00092 this.setTitle(Config.APP_NAME);
00093 }
00094
00100 private JPanel getJContentPane() {
00101 if (jContentPane == null) {
00102 jContentPane = new JPanel();
00103 jContentPane.setLayout(new BorderLayout());
00104 jContentPane.add(getStatusBar(), BorderLayout.SOUTH);
00105 }
00106 return jContentPane;
00107 }
00108
00114 private JMenuBar getJJMenuBar() {
00115 if (jJMenuBar == null) {
00116 jJMenuBar = new JMenuBar();
00117 jJMenuBar.add(getVisMenu());
00118 jJMenuBar.add(getHelpMenu());
00119 }
00120 return jJMenuBar;
00121 }
00122
00128 private JMenu getVisMenu() {
00129 if (visMenu == null) {
00130 visMenu = new JMenu();
00131 visMenu.setText("Visualization");
00132 visMenu.setMnemonic(KeyEvent.VK_V);
00133 visMenu.add(getMenuItemNewVis());
00134 visMenu.addSeparator();
00135 visMenu.add(getMenuItemZootmToFit());
00136 visMenu.add(getMenuItemStatistics());
00137 }
00138 return visMenu;
00139 }
00140
00146 private JMenuItem getMenuItemZootmToFit() {
00147 if (menuItemZootmToFit == null) {
00148 menuItemZootmToFit = new JMenuItem();
00149 menuItemZootmToFit.setText("Zoom to fit");
00150 menuItemZootmToFit.setMnemonic(KeyEvent.VK_Z);
00151 menuItemZootmToFit.addActionListener(new java.awt.event.ActionListener() {
00152 public void actionPerformed(java.awt.event.ActionEvent e) {
00153 gview.zoomToFit();
00154 }
00155 });
00156 }
00157 return menuItemZootmToFit;
00158 }
00159
00165 private JMenuItem getMenuItemNewVis() {
00166 final Component obj = this;
00167 if (menuItemNewVis == null) {
00168 menuItemNewVis = new JMenuItem();
00169 menuItemNewVis.setText("New visualization");
00170 menuItemNewVis.setMnemonic(KeyEvent.VK_N);
00171 menuItemNewVis.addActionListener(new java.awt.event.ActionListener() {
00172 public void actionPerformed(java.awt.event.ActionEvent e) {
00173 if (0!=gview.getNodeCount() &&
00174 JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(
00175 obj,
00176 "This will erase current visualization.",
00177 Config.APP_NAME,
00178 JOptionPane.OK_CANCEL_OPTION))
00179 return;
00180 String urlString = JOptionPane.showInputDialog(
00181 obj,
00182 "Type URL to look for:",
00183
00184
00185
00186 Config.DEF_LOOKUP_FOR);
00187 if (null==urlString)
00188 return;
00189 try {
00190 URL url = new URL(urlString);
00191 gview.clear();
00192 gview.lookup(url);
00193 } catch (MalformedURLException e1) {
00194 JOptionPane.showMessageDialog(
00195 obj,
00196 "Invalid URL",
00197 Config.APP_NAME,
00198 JOptionPane.ERROR_MESSAGE);
00199 e1.printStackTrace();
00200 }
00201 }
00202 });
00203 }
00204 return menuItemNewVis;
00205 }
00206
00212 private JToolBar getStatusBar() {
00213 if (statusBar == null) {
00214 GridLayout gridLayout = new GridLayout();
00215 gridLayout.setRows(1);
00216 statusText = new JLabel();
00217 statusText.setText(" ");
00218 statusText.setFont(new Font("Dialog", Font.BOLD, 18));
00219 statusBar = new JToolBar();
00220 statusBar.setLayout(gridLayout);
00221 statusBar.setFloatable(false);
00222 statusBar.add(statusText);
00223 }
00224 return statusBar;
00225 }
00226
00232 private JMenuItem getMenuItemStatistics() {
00233 final Component obj = this;
00234 if (menuItemStatistics == null) {
00235 menuItemStatistics = new JMenuItem();
00236 menuItemStatistics.setText("Statistics");
00237 menuItemStatistics.setMnemonic(KeyEvent.VK_S);
00238 menuItemStatistics.addActionListener(new java.awt.event.ActionListener() {
00239 public void actionPerformed(java.awt.event.ActionEvent e) {
00240 JPanel panel = new JPanel(new GridLayout(3,0));
00241 panel.add(createGlobalStats());
00242 panel.add(createViewStats());
00243 panel.add(createLookupStats());
00244 JOptionPane pane = new JOptionPane(panel);
00245 JDialog dialog = pane.createDialog(obj, Config.APP_NAME + " - statistics");
00246 dialog.setVisible(true);
00247 }
00248 private JPanel createGlobalStats() {
00249 final int FIELD_CNT = 3;
00250 String names[] = new String[FIELD_CNT];
00251 String values[] = new String[FIELD_CNT];
00252 Statistics stats = gview.getStorage().getStatistics();
00253 names[0] = "Hosts"; values[0] = String.valueOf(stats.getHosts());
00254 names[1] = "URLs"; values[1] = String.valueOf(stats.getUrls());
00255 names[2] = "Relations"; values[2] = String.valueOf(stats.getRels());
00256 return createBox(names, values, "Downloaded data statistics");
00257 }
00258 private JPanel createViewStats() {
00259 final int FIELD_CNT = 3;
00260 String names[] = new String[FIELD_CNT];
00261 String values[] = new String[FIELD_CNT];
00262 names[0] = "Nodes"; values[0] = String.valueOf(gview.getNodeCount());
00263 names[1] = "Edges"; values[1] = String.valueOf(gview.getEdgeCount());
00264 names[2] = null; values[2] = " ";
00265 return createBox(names, values, "View statistics");
00266 }
00267 private JPanel createLookupStats() {
00268 final int FIELD_CNT = 3;
00269 String names[] = new String[FIELD_CNT];
00270 String values[] = new String[FIELD_CNT];
00271
00272 names[0] = "Pending"; values[0] = String.valueOf(LookupWorker.getActiveCount());
00273 names[1] = "Finished"; values[1] = String.valueOf(LookupWorker.getSuccessCount());
00274 names[2] = "Failed"; values[2] = String.valueOf(LookupWorker.getFailedCount());
00275 return createBox(names, values, "Lookup statistics");
00276 }
00277 private JPanel createBox(String[] names, String[] values, String title) {
00278 final int CNT = Math.min(names.length,values.length);
00279 JPanel panel = new JPanel(new GridLayout(CNT,2));
00280
00281 panel.setBorder(BorderFactory.createTitledBorder(null, title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
00282 for(int i=0; i<CNT; i++) {
00283 String nameText = names[i];
00284 if (null==nameText)
00285 nameText = "";
00286 else
00287 nameText += ":";
00288 JLabel name = new JLabel(nameText);
00289 panel.add(name);
00290 JLabel value = new JLabel(values[i]);
00291 value.setHorizontalAlignment(JLabel.RIGHT);
00292 panel.add(value);
00293 }
00294 return panel;
00295 }
00296 });
00297 }
00298 return menuItemStatistics;
00299 }
00300
00306 private JMenu getHelpMenu() {
00307 if (helpMenu == null) {
00308 helpMenu = new JMenu();
00309 helpMenu.setText("Help");
00310 helpMenu.add(getMenuItemHelp());
00311 helpMenu.setMnemonic(KeyEvent.VK_H);
00312 }
00313 return helpMenu;
00314 }
00315
00321 private JMenuItem getMenuItemHelp() {
00322 final Component obj = this;
00323 if (menuItemHelp == null) {
00324 menuItemHelp = new JMenuItem();
00325 menuItemHelp.setText("Quick help");
00326 menuItemHelp.setMnemonic(KeyEvent.VK_H);
00327 menuItemHelp.addActionListener(new java.awt.event.ActionListener() {
00328 public void actionPerformed(java.awt.event.ActionEvent e) {
00329 JPanel panel = new JPanel(new GridLayout(2,0));
00330 panel.add(createNodeHelp());
00331 panel.add(createVisHelp());
00332 JOptionPane pane = new JOptionPane(panel);
00333 JDialog dialog = pane.createDialog(obj, Config.APP_NAME + " - quick help");
00334 dialog.setModal(false);
00335 dialog.setVisible(true);
00336 }
00337 private JPanel createNodeHelp() {
00338 final int FIELD_CNT = 3;
00339 String names[] = new String[FIELD_CNT];
00340 String values[] = new String[FIELD_CNT];
00341 names[0] = "Arrange visualiztion"; values[0] = "mouse left click on node";
00342 names[1] = "Move node"; values[1] = "drag&drop (mouse left button)";
00343 names[2] = "Node options"; values[2] = "mouse right click on node";
00344 return createBox(names, values, "Node oprations");
00345 }
00346 private JPanel createVisHelp() {
00347 final int FIELD_CNT = 3;
00348 String names[] = new String[FIELD_CNT];
00349 String values[] = new String[FIELD_CNT];
00350 names[0] = "Move visualization"; values[0] = "drag&drop (mouse left button not over node)";
00351 names[1] = "Zoom"; values[1] = "mouse wheel";
00352 names[2] = "Zoom to fit"; values[2] = "mouse right click (not over node)";
00353 return createBox(names, values, "Visualization operations");
00354 }
00355 private JPanel createBox(String[] names, String[] values, String title) {
00356 final int CNT = Math.min(names.length,values.length);
00357 JPanel panel = new JPanel(new GridLayout(CNT,2));
00358 panel.setBorder(BorderFactory.createTitledBorder(null, title, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
00359 for(int i=0; i<CNT; i++) {
00360 String nameText = names[i];
00361 panel.add(new JLabel(nameText));
00362 panel.add(new JLabel(values[i]));
00363 }
00364 return panel;
00365 }
00366 });
00367 }
00368 return menuItemHelp;
00369 }
00370
00375 public static void main(String[] args) {
00376 try {
00377 GraphView gv = new GraphView();
00378 Main mainFrame = new Main(gv);
00379 mainFrame.setVisible(true);
00380 gv.lookup(new URL(Config.DEF_LOOKUP_FOR));
00381 } catch (Exception e) {
00382
00383 e.printStackTrace();
00384 }
00385 }
00386 }