Package org.jampa.gui.views

Source Code of org.jampa.gui.views.HelpView

/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*/

package org.jampa.gui.views;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.jampa.Activator;
import org.jampa.gui.help.Help;
import org.jampa.gui.translations.Messages;

public class HelpView extends ViewPart {
 
  public static String ID = "Jampa.HelpView"; //$NON-NLS-1$

  private Composite _parent;
 
  private Browser _browser;
 
  private Action _backAction;
  private Action _homeAction;
  private Action _forwardAction;
 
  private List<String> _historyList;
  private int _historyIndex = -1;
 
  public HelpView() {   
    _historyList = new ArrayList<String>();
  }
 
  private void buildActions() {
    _backAction = new Action(Messages.getString("HelpView.BackAction")) { //$NON-NLS-1$
      public void run() {
        _historyIndex--;
        setLocation(_historyList.get(_historyIndex), false);
      }
    };
    _backAction.setImageDescriptor(Activator.getImageDescriptor("/icons/help_back_16.png")); //$NON-NLS-1$
   
    _homeAction = new Action(Messages.getString("HelpView.HomeAction")) { //$NON-NLS-1$
      public void run() {
        setLocationWithForwardReset("index.html"); //$NON-NLS-1$
      }
    };
    _homeAction.setImageDescriptor(Activator.getImageDescriptor("/icons/help_home_16.png")); //$NON-NLS-1$
   
    _forwardAction = new Action(Messages.getString("HelpView.ForwardAction")) { //$NON-NLS-1$
      public void run() {
        _historyIndex++;
        setLocation(_historyList.get(_historyIndex), false);
      }
    };
    _forwardAction.setImageDescriptor(Activator.getImageDescriptor("/icons/help_forward_16.png")); //$NON-NLS-1$
   
    // Toolbar   
    IToolBarManager rootMenuManager = getViewSite().getActionBars().getToolBarManager();
    rootMenuManager.removeAll();
   
    rootMenuManager.add(_backAction);
    rootMenuManager.add(_homeAction);
    rootMenuManager.add(_forwardAction);
  }
 
  private void updateActionState() {
    _backAction.setEnabled(_historyIndex > 0);
    _forwardAction.setEnabled(_historyIndex < _historyList.size() - 1);
  }
 
  private void degradeDisplay() {
    this.setContentDescription(Messages.getString("HelpView.ErrorNoBrowser")); //$NON-NLS-1$
    _backAction.setEnabled(false);
    _homeAction.setEnabled(false);
    _forwardAction.setEnabled(false);
  }
 
  private void initializeBrowser() {
    _browser = new Browser(_parent, SWT.NONE);          
    setLocation("index.html"); //$NON-NLS-1$
   
    _browser.addLocationListener(new LocationListener() {
      public void changed(LocationEvent event) {
        updateActionState();
      }
      public void changing(LocationEvent event) {
        if (!event.location.equals("about:blank")) { //$NON-NLS-1$
          // Cancel default link processing.
          event.doit = false;
         
          if ((event.location.startsWith("about:")) ||
              (event.location.startsWith("file:///"))) {
            String url;         
            // Remove some stuff from event.location.
            if (Util.isWindows()) {
              url = event.location.replace("about:", ""); //$NON-NLS-1$ //$NON-NLS-2$
            } else {
              url = event.location.replace("file:///", ""); //$NON-NLS-1$ //$NON-NLS-2$
            }         

            setLocationWithForwardReset(url);
          } else {
            Program.launch(event.location);
          }
        }                         
      }
    });
  }
 
  private void setLocationWithForwardReset(String location) {
    for (int i = _historyList.size() - 1; i > _historyIndex; i--) {
      _historyList.remove(i);
    }
   
    setLocation(location, true);
   
    updateActionState();   
  }
 
  private void setLocation(String location, boolean addToHistory) {
    if (addToHistory) {
      _historyIndex++;
      _historyList.add(location);
    }
    _browser.setText(Help.getHtml(location));
    updateActionState();
  }
 
  public void setLocation(String location) {
    setLocation(location, true);
  }

  @Override
  public void createPartControl(Composite parent) {
    _parent = parent;
    setPartName(Messages.getString("HelpView.Title")); //$NON-NLS-1$

    buildActions();
   
    try {
      initializeBrowser();
     
      //degradeDisplay();
     
    } catch (SWTError e) {
      degradeDisplay();
    }
  }

  @Override
  public void setFocus() {
    // TODO Auto-generated method stub
  }
 
  public void dispose() {
    _browser.dispose();
    super.dispose();
  }

}
TOP

Related Classes of org.jampa.gui.views.HelpView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.