1   package main;
2   
3   import intellego.Intellego;
4   import util.*;
5   import interfaces.*;
6   import real.*;
7   import simworldobjects.*;
8   import main.*;
9   import NetBeansResources.*;
10  import javax.swing.plaf.basic.BasicBorders.*;
11  
12  import java.awt.*;
13  import java.lang.*;
14  import java.awt.event.*;
15  import java.awt.event.ItemListener.*;
16  import javax.swing.*;
17  import javax.swing.event.*;
18  import java.io.*;
19  import javax.imageio.*;
20  import java.awt.image.*;
21  
22  import javax.swing.JTree;
23  import javax.swing.tree.DefaultMutableTreeNode;
24  import javax.swing.event.TreeSelectionListener;
25  import javax.swing.event.TreeSelectionEvent;
26  import javax.swing.tree.TreeSelectionModel;
27  import javax.swing.tree.DefaultTreeCellRenderer;
28  import javax.swing.ImageIcon;
29  import javax.swing.ToolTipManager;
30  import java.net.URL;
31  import java.io.IOException;
32  import javax.swing.JEditorPane;
33  import javax.swing.JScrollPane;
34  import javax.swing.JSplitPane;
35  import javax.swing.JFrame;
36  import java.awt.*;
37  import java.awt.event.*;
38  import javax.swing.plaf.basic.*;
39  import java.util.*;
40  
41  /** Provides a dialog for the user to view help files in html format.
42   * (A kind of browser for the files)
43   * @author Simon Zienkiewicz
44   */ 
45  public class HelpDialog extends JFrame implements HyperlinkListener, ActionListener,TreeSelectionListener{
46      
47      private JEditorPane htmlPane;
48      private URL helpURL;
49      private JButton back; 
50      private JButton forward;
51      private Stack backStack = new Stack();
52      private Stack forwardStack = new Stack();
53      private final JTree tree;
54      private LinkedList hyperlinkList = new LinkedList();
55      
56      /** Creates an instance of HelpDialog() */    
57      public HelpDialog(){
58          
59          setTitle("Intellego 2.0 Help");
60          setSize(920,620);
61          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
62          setLocation((screenSize.width/2)-460,(screenSize.height/2)-310);
63          
64          //set the frame boarder
65          this.setDefaultLookAndFeelDecorated(false);
66          this.setUndecorated(true);
67          this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
68                  
69          setVisible(true);
70          this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
71          //this.setResizable(false);
72          
73           //load icon image from a file
74          try{ 
75                  BufferedImage simIcon= ImageIO.read(new File("images\\lego.png"));
76                  this.setIconImage(simIcon);
77          }
78          catch(Exception e){}
79          
80          HelpInfo one = new HelpInfo("Intellego 2.0 Help","intellegoHelp.htm");
81          this.hyperlinkList.add(one);
82          DefaultMutableTreeNode top = new DefaultMutableTreeNode(one);
83          createNodes(top);
84  
85          //create forward and backwards buttons
86          forward = new JButton(new ImageIcon("images\\forward2.gif"));
87          back = new JButton(new ImageIcon("images\\back2.gif"));
88          forward.addActionListener(this);
89          back.addActionListener(this);
90          forward.setToolTipText("go forwards");
91          back.setToolTipText("go backwards");
92          forward.setMargin(new Insets(1,1,1,1));
93          back.setMargin(new Insets(1,1,1,1));
94           
95          forward.setBackground(Color.yellow);
96          forward.setForeground(Color.darkGray);
97          back.setBackground(Color.yellow);
98          back.setForeground(Color.darkGray);
99          forward.setEnabled(false);
100         back.setEnabled(false);
101         
102         
103         //Create a tree that allows one selection at a time.
104         
105         tree = new JTree(top);
106         tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
107         tree.setBackground(Color.darkGray);
108         tree.setForeground(Color.yellow);
109         tree.setCellRenderer(new MyRenderer());
110         tree.addTreeSelectionListener(this);
111         JScrollPane treePane = new JScrollPane(tree);
112                 
113         //Create the scroll pane and add the tree to it. 
114         JPanel treePanel = new JPanel();
115         treePanel.setBackground(Color.darkGray);
116         treePanel.setBorder(BasicBorders.getTextFieldBorder());
117         
118         treePanel.setLayout(new AbsoluteLayout());
119         
120         JTextField title = new JTextField(" Help Options: ");
121         title.setEditable(false);
122         title.setBackground(Color.darkGray);
123         title.setForeground(Color.yellow);
124         
125         JTextField backL = new JTextField("  Back");
126         JTextField forwardL = new JTextField("  Forward");
127         backL.setBackground(Color.darkGray);
128         backL.setForeground(Color.yellow);
129         backL.setEditable(false);
130         forwardL.setBackground(Color.darkGray);
131         forwardL.setForeground(Color.yellow);
132         forwardL.setEditable(false);
133         
134         treePanel.add(title,new AbsoluteConstraints(15,20,180,20));
135         treePanel.add(back,new AbsoluteConstraints(30,60,70,60));
136         treePanel.add(forward,new AbsoluteConstraints(105,60,70,60));
137         
138         treePanel.add(backL,new AbsoluteConstraints(30,120,70,20));
139         treePanel.add(forwardL,new AbsoluteConstraints(105,120,70,20));
140         
141         treePanel.add(treePane, new AbsoluteConstraints(15,160,300,400));//180,250));
142         JScrollPane treeView = new JScrollPane(treePanel);
143         treeView.setMinimumSize(new Dimension(330,300));
144         treeView.setMaximumSize(new Dimension(360,300));
145         tree.setBorder(BasicBorders.getTextFieldBorder());
146                 
147          //Create the HTML viewing pane.
148         htmlPane = new JEditorPane();
149         htmlPane.setEditable(false);
150         htmlPane.addHyperlinkListener(this);
151         JScrollPane htmlView = new JScrollPane(htmlPane);
152         htmlView.setMinimumSize(new Dimension(300,300));
153         
154          //Add the scroll panes to a split pane.
155         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,treeView,htmlView);
156         splitPane.setMinimumSize(new Dimension(520, 300));
157         splitPane.setDividerLocation(330);
158         splitPane.setOneTouchExpandable(true);
159        
160         //initialize it
161         HelpInfo helpFile = new HelpInfo("none","gettingStarted.html");
162         displayURL(helpFile);//.getURL());
163         
164         
165         //Add the split pane to this frame.
166         getContentPane().add(splitPane, BorderLayout.CENTER); 
167         
168         tree.setSelectionPath(tree.getNextMatch("Getting Started",1,javax.swing.text.Position.Bias.Forward));
169         tree.expandPath(tree.getNextMatch("Getting Started",1,javax.swing.text.Position.Bias.Forward));
170                 
171         tree.expandPath(tree.getNextMatch("Detailed Help",1,javax.swing.text.Position.Bias.Forward));
172                         
173         this.show();
174         
175     }
176          
177     /** Creates nodes, headings which will contain subheadings.
178      * @param top the top node
179      */    
180     private void createNodes(DefaultMutableTreeNode top) {
181         
182         DefaultMutableTreeNode gettingStarted = null;
183         DefaultMutableTreeNode detailedHelp = null;
184 
185         //GETTING STARTED
186         gettingStarted = new DefaultMutableTreeNode(new HelpInfo("Getting Started","gettingStarted.html"));
187         top.add(gettingStarted);
188         
189         DefaultMutableTreeNode codeEditor = null;
190         DefaultMutableTreeNode simulator = null;
191         HelpInfo one = null;
192         
193         //getting started: GETTING STARTED
194         one = new HelpInfo("The Code Editor","codeEditor.htm");
195         this.hyperlinkList.add(one);
196         codeEditor = new DefaultMutableTreeNode(one);
197         gettingStarted.add(codeEditor);
198         
199             //GETTING STARTED->the CODE EDITOR
200             one = new HelpInfo("Creating New Controller File","creatingController.htm");
201             this.hyperlinkList.add(one);
202             codeEditor.add(new DefaultMutableTreeNode(one));
203 
204             one = new HelpInfo("Editing Existing Controller File","editingController.htm");
205             this.hyperlinkList.add(one);
206             codeEditor.add(new DefaultMutableTreeNode(one));
207 
208             one = new HelpInfo("Uploading Controller File","uploadingController.htm");
209             this.hyperlinkList.add(one);
210             codeEditor.add(new DefaultMutableTreeNode(one));
211 
212             one = new HelpInfo("Uploading Firmware","uploadFirmware.htm");
213             this.hyperlinkList.add(one);
214             codeEditor.add(new DefaultMutableTreeNode(one));
215         
216         //getting started: SIMULATOR
217         one = new HelpInfo("The Simulator","simulator.htm");
218         this.hyperlinkList.add(one);
219         simulator = new DefaultMutableTreeNode(one);
220         gettingStarted.add(simulator);
221         
222             //GETTING STARTED->the SIMULATOR
223             one = new HelpInfo("Creating New SimWorld File","createSimWorld.htm");
224             this.hyperlinkList.add(one);
225             simulator.add(new DefaultMutableTreeNode(one));
226             
227             one = new HelpInfo("Running Simulation","runningSimulation.htm");
228             this.hyperlinkList.add(one);
229             simulator.add(new DefaultMutableTreeNode(one));
230         
231         //detailed help
232         detailedHelp = new DefaultMutableTreeNode(new HelpInfo("Detailed Help","detailedHelp.htm"));
233         top.add(detailedHelp);
234         
235         //important notices
236         detailedHelp.add(new DefaultMutableTreeNode(new HelpInfo("Important Notices", "important.html")));
237         
238         //SIMULATOR TOOLS
239         DefaultMutableTreeNode tools = null;
240         DefaultMutableTreeNode createSimWorldObjects = null;
241         DefaultMutableTreeNode wallO = null;
242         DefaultMutableTreeNode groundO = null;
243         DefaultMutableTreeNode theSimRobot = null;
244         DefaultMutableTreeNode lightProperties = null;
245         DefaultMutableTreeNode theGrid = null;
246                 
247         one = new HelpInfo("Simulator Tools", "tools.html");
248         this.hyperlinkList.add(one);
249         tools = new DefaultMutableTreeNode(one);
250         detailedHelp.add(tools);
251         
252             //CREATING SIM WORLD OBJECTS
253             one = new HelpInfo("Creating SimWorld Objects", "createSimWorldObjects.htm");
254             this.hyperlinkList.add(one);
255             createSimWorldObjects = new DefaultMutableTreeNode(one);
256             tools.add(createSimWorldObjects);
257                 
258                 //WALL OBJECTS
259                 one = new HelpInfo("Wall Objects", "wallObject.htm");
260                 this.hyperlinkList.add(one);
261                 wallO = new DefaultMutableTreeNode(one);
262                 createSimWorldObjects.add(wallO);
263                 
264                     //Creating/Moving Wall Objects
265                     one = new HelpInfo("Creating/Moving Wall Objects","creatingWallObjects.htm");
266                     this.hyperlinkList.add(one);
267                     wallO.add(new DefaultMutableTreeNode(one));
268 
269                     //Editing Wall Objects
270                     one = new HelpInfo("Editing Wall Objects","editingObjects.htm");
271                     this.hyperlinkList.add(one);
272                     wallO.add(new DefaultMutableTreeNode(one));
273                 
274             
275                 //GROUND/INTERSECTION OBJECTS
276                 one = new HelpInfo("Ground/Intersection Objects", "groundObject.htm");
277                 this.hyperlinkList.add(one);
278                 groundO = new DefaultMutableTreeNode(one);
279                 createSimWorldObjects.add(groundO);
280                 
281                     //Creating/Moving Ground Objects
282                     one = new HelpInfo("Creating/Moving Ground Objects","creatingGroundObjects.htm");
283                     this.hyperlinkList.add(one);
284                     groundO.add(new DefaultMutableTreeNode(one));
285 
286                     //Editing Ground/Intersection Objects
287                     one = new HelpInfo("Editing Ground/Intersection Objects","editingObjects.htm");
288                     this.hyperlinkList.add(one);
289                     groundO.add(new DefaultMutableTreeNode(one));
290                     
291                     //Assigning Colours to Ground Objects
292                     one = new HelpInfo("Assigning Colours to Ground Objects","colourGroundObject.htm");
293                     this.hyperlinkList.add(one);
294                     groundO.add(new DefaultMutableTreeNode(one));
295             
296             //LIGHT PROPERTIES
297             one = new HelpInfo("Light Properties", "lightProperties.htm");
298             this.hyperlinkList.add(one);
299             lightProperties = new DefaultMutableTreeNode(one);
300             tools.add(lightProperties);
301         
302                 //Opening LightSensor Prop Dialog
303                 one = new HelpInfo("Opening LightSensor Prop Dialog","openlightProperties.htm");
304                 this.hyperlinkList.add(one);
305                 lightProperties.add(new DefaultMutableTreeNode(one));
306 
307                 //Mapping Colours to Numbers
308                 one = new HelpInfo("Mapping Colours to Numbers","maplightProperties.htm");
309                 this.hyperlinkList.add(one);
310                 lightProperties.add(new DefaultMutableTreeNode(one));
311                 
312                 //Saving/Opening LightSensor Colour Values
313                 one = new HelpInfo("Saving/Opening LightSensor Colour Values","opensaveLight.htm");
314                 this.hyperlinkList.add(one);
315                 lightProperties.add(new DefaultMutableTreeNode(one));
316                 
317                 
318             //THE SIMULATED ROBOT
319             one = new HelpInfo("The Simulated Robot", "robot.htm");
320             this.hyperlinkList.add(one);
321             theSimRobot = new DefaultMutableTreeNode(one);
322             tools.add(theSimRobot);
323         
324                 //Initializing the Robot
325                 one = new HelpInfo("Initializing the Robot","initRobot.htm");
326                 this.hyperlinkList.add(one);
327                 theSimRobot.add(new DefaultMutableTreeNode(one));
328 
329                 //Moving the Robot in the SimWorld
330                 one = new HelpInfo("Moving the Robot in the SimWorld","moveRobot.htm");
331                 this.hyperlinkList.add(one);
332                 theSimRobot.add(new DefaultMutableTreeNode(one));
333                 
334                 //Rotating the Robot in the SimWorld
335                 one = new HelpInfo("Rotating the Robot in the SimWorld","rototateRobot.htm");
336                 this.hyperlinkList.add(one);
337                 theSimRobot.add(new DefaultMutableTreeNode(one));
338            
339             //THE GRID
340             one = new HelpInfo("Using the Grid", "grid.htm");
341             this.hyperlinkList.add(one);
342             theGrid = new DefaultMutableTreeNode(one);
343             tools.add(theGrid);
344         
345                 //Initializing the Robot
346                 one = new HelpInfo("Using the Grid Setup Editor","openGrid.htm");
347                 this.hyperlinkList.add(one);
348                 theGrid.add(new DefaultMutableTreeNode(one));
349 
350                 //Moving the Robot in the SimWorld
351                 one = new HelpInfo("Assinging Color to the Grid","colourGrid.htm");
352                 this.hyperlinkList.add(one);
353                 theGrid.add(new DefaultMutableTreeNode(one));
354               
355             //ASSIGNING COLOURS IN SIM WORLD
356             one = new HelpInfo("Assinging Colours in SimWorld","assignColours.html");
357             this.hyperlinkList.add(one);
358             tools.add(new DefaultMutableTreeNode(one));
359              
360             //USING UNDO/REDO
361             one = new HelpInfo("Using Undo/Redo","undoRedo.htm"); 
362             this.hyperlinkList.add(one);
363             tools.add(new DefaultMutableTreeNode(one));
364             
365             //USING THE LOCK TOOL
366             one = new HelpInfo("Using the Lock Tool","lockFunction.htm");
367             this.hyperlinkList.add(one);
368             tools.add(new DefaultMutableTreeNode(one));
369 
370          //CONTROLLER FILE SPECIFICATIONS
371         one = new HelpInfo("Controller File Specifications", "controllerSpecs.html");
372         this.hyperlinkList.add(one);
373         DefaultMutableTreeNode specs = new DefaultMutableTreeNode(one);
374         detailedHelp.add(specs);
375 
376             //The Controller Interface
377             one = new HelpInfo("The Controller Interface","controllerInterface.htm");
378             this.hyperlinkList.add(one);
379             specs.add(new DefaultMutableTreeNode(one));
380              
381             //AbstractRobot Functions
382             one = new HelpInfo("AbstractRobot Functions","abstractRobotFunctions.htm"); 
383             this.hyperlinkList.add(one);
384             specs.add(new DefaultMutableTreeNode(one));
385             
386             //Sample Controller File: ZincRocker
387             one = new HelpInfo("Sample Controller File: ZincRocker","zincRocker.htm");
388             this.hyperlinkList.add(one);
389             specs.add(new DefaultMutableTreeNode(one));
390         
391         
392         //shortcut/hotkeys
393         detailedHelp.add(new DefaultMutableTreeNode(new HelpInfo("Shortcuts/Hotkeys", "shortcut.html")));
394         
395         //known bugs
396         detailedHelp.add(new DefaultMutableTreeNode(new HelpInfo("Known Bugs/Errors", "knownBugs.htm")));
397         
398     }
399     
400     /** Display the HTML file
401      * @param url the path and name of the HTLM file wanting display
402      */    
403     private void displayURL(HelpDialog.HelpInfo url)
404     {
405         try {
406             
407             try
408             {
409                 if(!(url.getURL()).equals(((HelpDialog.HelpInfo)this.backStack.peek()).getURL())) this.backStack.push(url);
410             }
411                 
412             catch(Exception e2)
413             {
414                 this.backStack.push(url);
415             }
416             
417             if(this.backStack.size() < 2) this.back.setEnabled(false);
418             else this.back.setEnabled(true);
419             htmlPane.setPage(url.getURL());
420 
421         }
422         
423         catch (IOException e) 
424         {
425             System.err.println("Attempted to read a bad URL: " + url);
426         }
427         
428     }
429     
430     /** Displays the HTLM once a hyperlink is clicked within the page.
431      * @param e the triggered hyperlink event
432      */    
433     public void hyperlinkUpdate(HyperlinkEvent e) {
434         if(e.getEventType().toString().equals("ACTIVATED")){
435             
436             try
437             {
438                 HelpInfo a = null;
439                 
440                 for(int i=0;i<hyperlinkList.size();i++){
441                     
442                     a = (HelpInfo)this.hyperlinkList.get(i);
443                     String b = (a.getURL()).substring((a.getURL()).indexOf("\\")+1);
444                     String c = e.getDescription();
445 
446                     if(b.equals(c))
447                     {
448                         break;
449                     }
450                     
451                     else
452                     {
453                         if((i+1) == hyperlinkList.size()){
454                             a = new HelpInfo("none",e.getDescription());
455                         }
456                         //do nothing
457                     }
458                 }
459                                 
460                 this.forwardStack.clear();
461                 this.forward.setEnabled(false);
462                 displayURL(a);
463             }
464             catch(Exception e2){
465                 //do nothing
466             }
467         }
468     
469     }
470     
471     /** Performs the 'back' and 'forward' button commands, resulting from the passed
472      * event.
473      * @param e the event occuring from either the push of the 'back' or 'forward' buttons
474      */    
475     public void actionPerformed(ActionEvent e) {
476         
477         if(e.getSource() == back){
478             if(this.backStack.size() >= 2){
479                 this.forwardStack.push(this.backStack.pop());
480                 displayURL((HelpDialog.HelpInfo)this.backStack.peek());
481                 this.forward.setEnabled(true);
482                 if(this.backStack.size() ==1)tree.setSelectionPath(tree.getNextMatch("Getting Started",1,javax.swing.text.Position.Bias.Forward));
483             }
484             else{
485                 this.back.setEnabled(false);                
486             }
487         }
488         else if(e.getSource() == forward){
489                 
490             if(this.forwardStack.size() >= 1){
491                 Object b = this.forwardStack.pop();
492                 this.backStack.push(b);
493                 displayURL((HelpDialog.HelpInfo)b);
494                 if(forwardStack.isEmpty())this.forward.setEnabled(false);   
495             }    
496         }
497     }
498     
499     /** ...
500      * @param e tree selection event
501      */    
502     public void valueChanged(TreeSelectionEvent e) {
503         DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
504         
505         if (node == null) return;
506 
507         Object nodeInfo = node.getUserObject();
508        
509         HelpInfo helpFile = (HelpInfo)nodeInfo;
510         displayURL(helpFile);
511         
512     }
513     
514     /** Class used to get url information pertaining to nodes. */    
515     private class HelpInfo{
516         
517         /** name of the help file */        
518         private String helpName;
519         /** url of the help file. */        
520         public String helpURL=null;
521         /** the super path of the directory where the help files are stored */        
522         public String path = "file:HelpFiles\\";
523         
524         /** Creates a HelpInfo object
525          * @param name the title of the help file
526          * @param file the file name of the help file (*.html)
527          */        
528         public HelpInfo(String name, String file) {
529             
530             helpName = name;
531             
532             try 
533             {
534                 helpURL = path+file;
535             }
536             
537             catch (Exception e)
538             {
539                 helpURL = null;
540             }
541         }
542         
543         /** Gets the complete path+file name of the helpfile.
544          * @return the complete path+file name of the helpfile
545          */        
546         public String getURL(){
547             return helpURL;
548         }
549         
550         /** Gets the title of the help file.
551          * @return the title of the help file
552          */        
553         public String toString() {
554             return helpName;
555         }
556     }
557      
558     //enables color adjusments on the tree cells
559     /** Enables color adjusments on the tree cells */    
560     private class MyRenderer extends DefaultTreeCellRenderer {
561         ImageIcon tutorialIcon;
562 
563         /** Creates a new instance of MyRenderer() object */        
564         public MyRenderer() {
565             
566         }
567 
568         /** */        
569         public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {
570 
571             super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row,hasFocus);
572             this.setForeground(Color.yellow);
573             this.setBackgroundNonSelectionColor(Color.darkGray);
574             this.setBackgroundSelectionColor(Color.darkGray.darker());
575             return this;
576         }
577      }
578       
579 }